Untitled
unknown
plain_text
9 months ago
2.8 kB
6
Indexable
import numpy as np
# Sigmoid activation function and its derivative
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# Neural Network class
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# Initialize weights and biases
self.weights_input_hidden = np.random.uniform(-1, 1, (input_size, hidden_size))
self.weights_hidden_output = np.random.uniform(-1, 1, (hidden_size, output_size))
self.bias_hidden = np.random.uniform(-1, 1, (1, hidden_size))
self.bias_output = np.random.uniform(-1, 1, (1, output_size))
def forward_propagation(self, X):
# Compute hidden layer activation
self.hidden_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
self.hidden_output = sigmoid(self.hidden_input)
# Compute output layer activation
self.output_input = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
self.output_output = sigmoid(self.output_input)
return self.output_output
def back_propagation(self, X, y, learning_rate):
# Error in output
output_error = y - self.output_output
output_delta = output_error * sigmoid_derivative(self.output_output)
# Error in hidden layer
hidden_error = np.dot(output_delta, self.weights_hidden_output.T)
hidden_delta = hidden_error * sigmoid_derivative(self.hidden_output)
# Update weights and biases
self.weights_hidden_output += np.dot(self.hidden_output.T, output_delta) * learning_rate
self.bias_output += np.sum(output_delta, axis=0, keepdims=True) * learning_rate
self.weights_input_hidden += np.dot(X.T, hidden_delta) * learning_rate
self.bias_hidden += np.sum(hidden_delta, axis=0, keepdims=True) * learning_rate
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
# Forward and backward propagation
self.forward_propagation(X)
self.back_propagation(X, y, learning_rate)
# Print loss at every 100 epochs
if (epoch + 1) % 100 == 0:
loss = np.mean(np.square(y - self.output_output))
print(f"Epoch {epoch + 1}/{epochs}, Loss: {loss}")
# Example: XOR dataset
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# Network configuration
input_size = 2
hidden_size = 4
output_size = 1
# Create and train the neural network
nn = NeuralNetwork(input_size, hidden_size, output_size)
nn.train(X, y, epochs=10000, learning_rate=0.1)
# Test the network
print("\nTesting the network:")
for i in range(len(X)):
output = nn.forward_propagation(X[i].reshape(1, -1))
print(f"Input: {X[i]}, Predicted Output: {output}, Actual Output: {y[i]}")Editor is loading...
Leave a Comment