Untitled
unknown
plain_text
8 months ago
2.8 kB
8
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 return self.z2 def backward(self, X, y, learning_rate=0.01): # Forward propagation predictions = self.forward(X) # Backpropagation N = len(X) K = self.output_size for i in range(N): delta3 = predictions[i] - y[i] dW2 = np.outer(self.a1[i], delta3) db2 = delta3 delta2 = np.dot(delta3, self.W2.T) * (self.a1[i] * (1 - self.a1[i])) dW1 = np.outer(X[i], delta2) db1 = delta2 # 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 custom_cost_function(self, X, y): # Compute custom cost function predictions = self.forward(X) N = len(X) K = self.output_size cost = 0.0 for i in range(N): for k in range(K): cost += 0.5 * (predictions[i][k] - y[i][k]) ** 2 return cost def accuracy(self, X, y): # Make predictions predictions = np.argmax(self.forward(X), axis=1) # Compare predictions with true labels correct = np.sum(predictions == y) # Calculate accuracy accuracy = correct / len(y) return accuracy # Example usage: # Create an instance of the NeuralNetwork class input_size = 3 hidden_size = 4 output_size = 2 model = NeuralNetwork(input_size, hidden_size, output_size) # Example data X = np.random.randn(5, input_size) # 5 samples, 3 features y = np.random.randint(0, 2, size=(5, output_size)) # 5 samples, 2 output values # Training loop num_epochs = 1000 learning_rate = 0.01 for epoch in range(num_epochs): # Forward pass and backward pass model.backward(X, y, learning_rate) # Print the loss if epoch % 100 == 0: loss = model.custom_cost_function(X, y) print(f'Epoch {epoch}, Loss: {loss}') accuracy = model.accuracy(X, y) print(f'Test Accuracy: {accuracy}')
Leave a Comment