Implementation of basic matrix operations commonly used in deep learning.
import numpy as np
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# Initialize weights and biases
self.W1 = np.random.randn(input_size, hidden_size) * 0.01
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size) * 0.01
self.b2 = np.zeros((1, output_size))
def sigmoid(self, Z):
return 1 / (1 + np.exp(-Z))
def forward_propagation(self, X):
# First layer
Z1 = np.dot(X, self.W1) + self.b1 # Matrix multiplication and addition
A1 = self.sigmoid(Z1) # Element-wise operation
# Second layer
Z2 = np.dot(A1, self.W2) + self.b2 # Matrix multiplication and addition
A2 = self.sigmoid(Z2) # Element-wise operation
return A1, A2
def compute_gradients(self, X, Y, A1, A2):
m = X.shape[0]
# Backpropagation
dZ2 = A2 - Y
dW2 = (1/m) * np.dot(A1.T, dZ2) # Matrix multiplication with transpose
db2 = (1/m) * np.sum(dZ2, axis=0, keepdims=True)
dZ1 = np.dot(dZ2, self.W2.T) * (A1 * (1 - A1))
dW1 = (1/m) * np.dot(X.T, dZ1) # Matrix multiplication with transpose
db1 = (1/m) * np.sum(dZ1, axis=0, keepdims=True)
return dW1, db1, dW2, db2
# Example usage
if __name__ == "__main__":
# Create sample data
X = np.random.randn(5, 3) # 5 samples, 3 features
Y = np.random.randint(0, 2, (5, 1)) # Binary classification
# Initialize network
nn = SimpleNeuralNetwork(input_size=3, hidden_size=4, output_size=1)
# Forward pass
A1, A2 = nn.forward_propagation(X)
print("Output shape:", A2.shape)
# Compute gradients
dW1, db1, dW2, db2 = nn.compute_gradients(X, Y, A1, A2)
print("Weight gradients shapes:", dW1.shape, dW2.shape)