Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
2.4 kB
2
Indexable
Never
import numpy as np

class NeuralNetwork:
    def __init__(self, input_size, hidden_size, output_size):
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size
        
        # Initialize weights and biases
        self.W1 = np.random.randn(self.input_size, self.hidden_size)
        self.b1 = np.zeros((1, self.hidden_size))
        self.W2 = np.random.randn(self.hidden_size, self.output_size)
        self.b2 = np.zeros((1, self.output_size))

    def forward(self, X):
        # Forward propagation
        self.z1 = np.dot(X, self.W1) + self.b1
        self.a1 = self.sigmoid(self.z1)
        self.z2 = np.dot(self.a1, self.W2) + self.b2
        self.probs = self.softmax(self.z2)
        return self.probs

    def backward(self, X, y, learning_rate=0.01):
        # Backpropagation
        batch_size = len(X)
        delta3 = self.probs - y
        dW2 = np.dot(self.a1.T, delta3)
        db2 = np.sum(delta3, axis=0, keepdims=True)
        delta2 = np.dot(delta3, self.W2.T) * (self.a1 * (1 - self.a1))
        dW1 = np.dot(X.T, delta2)
        db1 = np.sum(delta2, axis=0)
        
        # Update weights and biases
        self.W1 -= learning_rate * dW1
        self.b1 -= learning_rate * db1
        self.W2 -= learning_rate * dW2
        self.b2 -= learning_rate * db2

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def softmax(self, z):
        exp_scores = np.exp(z - np.max(z, axis=1, keepdims=True))
        return exp_scores / np.sum(exp_scores, axis=1, keepdims=True)

    def squared_error(self, X, y):
        # Compute squared error
        predictions = self.forward(X)
        sq_error = np.mean((predictions - y)**2)
        return sq_error

# Generate some random data for testing
np.random.seed(0)
X = np.random.randn(300, 10)  # 300 samples, 10 features
y = np.random.randint(0, 2, (300, 2))  # 2 classes (binary labels, one-hot encoded)

# Initialize and train the neural network
input_size = 10
hidden_size = 5
output_size = 2  # Two neurons for binary classification
model = NeuralNetwork(input_size, hidden_size, output_size)

# Training loop
num_epochs = 1000
for epoch in range(num_epochs):
    # Forward pass and backward pass
    model.backward(X, y, learning_rate=0.01)
    
    # Print the loss
    if epoch % 100 == 0:
        loss = model.squared_error(X, y)
        print(f'Epoch {epoch}, Loss: {loss}')
Leave a Comment