Implementation of initialization from scratch to understand the process:
- Forward pass computation
- Weight initialization
- Training loop integration
import numpy as np
class SimpleInitializedNetwork:
def __init__(self, layer_sizes, method='xavier'):
self.weights = []
self.biases = []
for i in range(len(layer_sizes) - 1):
if method == 'xavier':
std = np.sqrt(2.0 / (layer_sizes[i] + layer_sizes[i+1]))
W = np.random.randn(layer_sizes[i], layer_sizes[i+1]) * std
elif method == 'he':
std = np.sqrt(2.0 / layer_sizes[i])
W = np.random.randn(layer_sizes[i], layer_sizes[i+1]) * std
else:
W = np.random.randn(layer_sizes[i], layer_sizes[i+1]) * 0.01
self.weights.append(W)
self.biases.append(np.zeros((1, layer_sizes[i+1])))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, X):
activation = X
for W, b in zip(self.weights, self.biases):
z = np.dot(activation, W) + b
activation = self.sigmoid(z)
return activation
# Example usage
if __name__ == "__main__":
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
nn = SimpleInitializedNetwork([2, 4, 1], method='xavier')
output = nn.forward(X)
print(output)