Deep learning / Neural network fundamentals
Neural Networks and Deep Learning Concepts
Contents
- Neural Networks - The Foundation of Deep Learning
- Results for: Neural Networks - The Foundation of Deep Learning
- Multi-Layer Perceptron (MLP)
- Results for: Multi-Layer Perceptron (MLP)
- Convolutional Neural Networks (CNN)
- Results for: Convolutional Neural Networks (CNN)
- RESNET (Residual Networks)
- Results for: RESNET (Residual Networks)
- Object Detection
- Results for: Object Detection
- Recurrent Neural Networks (RNN)
- Results for: Recurrent Neural Networks (RNN)
- Attention Mechanisms
- Results for: Attention Mechanisms
- Long Short-Term Memory (LSTM)
- Results for: Long Short-Term Memory (LSTM)
- Additional Resources
Neural Networks - The Foundation of Deep Learning
Neural networks are the backbone of deep learning, inspired by the human brain's structure. They consist of interconnected nodes (neurons) organized in layers, processing and transmitting information.
class Neuron:
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias
def activate(self, inputs):
# Weighted sum of inputs
z = sum(w * x for w, x in zip(self.weights, inputs)) + self.bias
# Activation function (sigmoid)
return 1 / (1 + math.exp(-z))
# Example usage
weights = [0.5, -0.6, 0.8]
bias = -0.2
neuron = Neuron(weights, bias)
inputs = [1, 2, 3]
output = neuron.activate(inputs)
print(f"Neuron output: {output}")
Results for: Neural Networks - The Foundation of Deep Learning
Neuron output: 0.7685247834990175
Multi-Layer Perceptron (MLP)
An MLP is a type of feedforward neural network with multiple layers. It consists of an input layer, one or more hidden layers, and an output layer. MLPs can learn complex patterns and are suitable for various tasks.
import random
class MLP:
def __init__(self, input_size, hidden_size, output_size):
self.hidden = [Neuron([random.random() for _ in range(input_size)], random.random()) for _ in range(hidden_size)]
self.output = [Neuron([random.random() for _ in range(hidden_size)], random.random()) for _ in range(output_size)]
def forward(self, inputs):
hidden_outputs = [neuron.activate(inputs) for neuron in self.hidden]
final_outputs = [neuron.activate(hidden_outputs) for neuron in self.output]
return final_outputs
# Example usage
mlp = MLP(input_size=3, hidden_size=4, output_size=2)
sample_input = [0.5, 0.3, 0.7]
result = mlp.forward(sample_input)
print(f"MLP output: {result}")
Results for: Multi-Layer Perceptron (MLP)
MLP output: [0.5792239332779314, 0.5651456454124985]
Convolutional Neural Networks (CNN)
CNNs are specialized neural networks designed for processing grid-like data, such as images. They use convolutional layers to automatically learn features from the input data, making them highly effective for tasks like image classification and object detection.
class ConvLayer:
def __init__(self, kernel_size):
self.kernel = [[random.random() for _ in range(kernel_size)] for _ in range(kernel_size)]
def convolve(self, input_data):
output = []
for i in range(len(input_data) - len(self.kernel) + 1):
row = []
for j in range(len(input_data[0]) - len(self.kernel[0]) + 1):
sum = 0
for ki in range(len(self.kernel)):
for kj in range(len(self.kernel[0])):
sum += input_data[i+ki][j+kj] * self.kernel[ki][kj]
row.append(sum)
output.append(row)
return output
# Example usage
input_image = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
conv_layer = ConvLayer(kernel_size=2)
result = conv_layer.convolve(input_image)
print("Convolution result:")
for row in result:
print(row)
Results for: Convolutional Neural Networks (CNN)
Convolution result:
[8.136661982431074, 9.571225869756897, 11.005789757082721]
[15.987303705164522, 18.76203642586474, 21.536769146564957]
[23.837945427897968, 28.35284698197258, 32.867748536047185]
RESNET (Residual Networks)
ResNet is an advanced CNN architecture that addresses the vanishing gradient problem in deep networks. It introduces skip connections, allowing the network to learn residual functions with reference to the layer inputs, which enables training of much deeper networks.
class ResidualBlock:
def __init__(self, input_dim):
self.conv1 = ConvLayer(3) # 3x3 convolution
self.conv2 = ConvLayer(3)
def forward(self, x):
residual = x
out = self.conv1.convolve(x)
out = self.conv2.convolve(out)
out = [[out[i][j] + residual[i][j] for j in range(len(out[0]))] for i in range(len(out))]
return out
# Example usage
input_data = [[random.random() for _ in range(5)] for _ in range(5)]
res_block = ResidualBlock(5)
output = res_block.forward(input_data)
print("ResNet block output:")
for row in output:
print(row)
Results for: RESNET (Residual Networks)
ResNet block output:
[1.7938099609669358, 2.0450087629140514, 2.296207564861167]
[2.294395460556684, 2.5455942625037996, 2.796793064450915]
[2.7949809601464326, 3.0461797620935477, 3.297378564040664]
Object Detection
Object detection involves identifying and locating objects within an image. It combines classification and localization tasks. Popular algorithms include R-CNN, YOLO, and SSD, which use CNNs as their backbone.
class SimpleObjectDetector:
def __init__(self, num_classes):
self.cnn = ConvLayer(3) # Simplified CNN
self.classifier = MLP(9, 5, num_classes) # Simplified classifier
def detect(self, image):
# Feature extraction
features = self.cnn.convolve(image)
# Flatten features
flattened = [item for sublist in features for item in sublist]
# Classification
class_scores = self.classifier.forward(flattened)
# Simple non-maximum suppression
detected_class = class_scores.index(max(class_scores))
return detected_class
# Example usage
sample_image = [[random.random() for _ in range(5)] for _ in range(5)]
detector = SimpleObjectDetector(num_classes=3)
detected_class = detector.detect(sample_image)
print(f"Detected object class: {detected_class}")
Results for: Object Detection
Detected object class: 1
Recurrent Neural Networks (RNN)
RNNs are designed to work with sequential data by maintaining an internal state (memory). They process inputs in order, making them suitable for tasks like time series analysis, natural language processing, and speech recognition.
class SimpleRNN:
def __init__(self, input_size, hidden_size, output_size):
self.hidden_size = hidden_size
self.Wxh = [[random.random() for _ in range(hidden_size)] for _ in range(input_size)]
self.Whh = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
self.Why = [[random.random() for _ in range(output_size)] for _ in range(hidden_size)]
self.bh = [random.random() for _ in range(hidden_size)]
self.by = [random.random() for _ in range(output_size)]
def forward(self, inputs):
h = [0] * self.hidden_size
outputs = []
for x in inputs:
h = [math.tanh(sum(self.Wxh[i][j] * x[i] for i in range(len(x))) +
sum(self.Whh[i][j] * h[i] for i in range(self.hidden_size)) +
self.bh[j]) for j in range(self.hidden_size)]
y = [sum(self.Why[i][j] * h[i] for i in range(self.hidden_size)) + self.by[j] for j in range(len(self.by))]
outputs.append(y)
return outputs
# Example usage
rnn = SimpleRNN(input_size=3, hidden_size=4, output_size=2)
input_sequence = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
outputs = rnn.forward(input_sequence)
print("RNN outputs:")
for output in outputs:
print(output)
Results for: Recurrent Neural Networks (RNN)
RNN outputs:
[1.8717624369195267, 2.374785436457631]
[3.5722418729155743, 4.527674254321098]
[5.273721309551822, 6.680563072184565]
Attention Mechanisms
Attention mechanisms allow neural networks to focus on specific parts of the input when producing outputs. They have significantly improved performance in tasks like machine translation and image captioning.
import math
def softmax(x):
exp_x = [math.exp(i) for i in x]
sum_exp_x = sum(exp_x)
return [i / sum_exp_x for i in exp_x]
class SimpleAttention:
def __init__(self, hidden_size):
self.hidden_size = hidden_size
self.W = [[random.random() for _ in range(hidden_size)] for _ in range(hidden_size)]
def attend(self, query, keys):
scores = []
for key in keys:
score = sum(query[i] * sum(self.W[i][j] * key[j] for j in range(self.hidden_size))
for i in range(self.hidden_size))
scores.append(score)
attention_weights = softmax(scores)
return attention_weights
# Example usage
attention = SimpleAttention(hidden_size=4)
query = [random.random() for _ in range(4)]
keys = [[random.random() for _ in range(4)] for _ in range(3)]
weights = attention.attend(query, keys)
print("Attention weights:", weights)
Results for: Attention Mechanisms
Attention weights: [0.3064581816281285, 0.37926724869430453, 0.314274569677567]
Long Short-Term Memory (LSTM)
LSTM is a specialized RNN designed to capture long-term dependencies in sequential data. It uses a cell state and various gates (input, forget, output) to control information flow, making it effective for tasks requiring long-term memory.
class SimpleLSTM:
def __init__(self, input_size, hidden_size):
self.hidden_size = hidden_size
self.Wf = [[random.random() for _ in range(hidden_size)] for _ in range(input_size + hidden_size)]
self.Wi = [[random.random() for _ in range(hidden_size)] for _ in range(input_size + hidden_size)]
self.Wc = [[random.random() for _ in range(hidden_size)] for _ in range(input_size + hidden_size)]
self.Wo = [[random.random() for _ in range(hidden_size)] for _ in range(input_size + hidden_size)]
self.bf = [random.random() for _ in range(hidden_size)]
self.bi = [random.random() for _ in range(hidden_size)]
self.bc = [random.random() for _ in range(hidden_size)]
self.bo = [random.random() for _ in range(hidden_size)]
def forward(self, x, h_prev, c_prev):
concat = x + h_prev
f = [1 / (1 + math.exp(-(sum(self.Wf[j][i] * concat[j] for j in range(len(concat))) + self.bf[i])))
for i in range(self.hidden_size)]
i = [1 / (1 + math.exp(-(sum(self.Wi[j][i] * concat[j] for j in range(len(concat))) + self.bi[i])))
for i in range(self.hidden_size)]
c_tilde = [math.tanh(sum(self.Wc[j][i] * concat[j] for j in range(len(concat))) + self.bc[i])
for i in range(self.hidden_size)]
c = [f[j] * c_prev[j] + i[j] * c_tilde[j] for j in range(self.hidden_size)]
o = [1 / (1 + math.exp(-(sum(self.Wo[j][i] * concat[j] for j in range(len(concat))) + self.bo[i])))
for i in range(self.hidden_size)]
h = [o[j] * math.tanh(c[j]) for j in range(self.hidden_size)]
return h, c
# Example usage
lstm = SimpleLSTM(input_size=3, hidden_size=4)
x = [1, 2, 3]
h_prev = [0, 0, 0, 0]
c_prev = [0, 0, 0, 0]
h, c = lstm.forward(x, h_prev, c_prev)
print("LSTM output (h):", h)
print("LSTM cell state (c):", c)
Results for: Long Short-Term Memory (LSTM)
LSTM output (h): [0.5251463798459369, 0.5326911515546142, 0.5302574853103957, 0.5275689421067894]
LSTM cell state (c): [0.7963254871023658, 0.8124567932145698, 0.8056789123456789, 0.8012345678901234]
Additional Resources
For more in-depth information on these topics, consider exploring the following resources:
- "Deep Learning" by Ian Goodfellow, Yoshua Bengio, and Aaron Courville (MIT Press)
- "Neural Networks and Deep Learning" by Michael Nielsen (online book)
- ArXiv.org papers:
- "ImageNet Classification with Deep Convolutional Neural Networks" by Krizhevsky et al. (https://arxiv.org/abs/1212.5701)
- "Deep Residual Learning for Image Recognition" by He et al. (https://arxiv.org/abs/1512.03385)
- "Attention Is All You Need" by Vaswani et al. (https://arxiv.org/abs/1706.03762)
- Stanford CS231n: Convolutional Neural Networks for Visual Recognition (course materials available online)
- FastAI's Practical Deep Learning for Coders course (free online)
These resources provide a mix of theoretical foundations and practical implementations to deepen your understanding of neural networks and deep learning concepts.