-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneuralnet.py
62 lines (49 loc) · 2.4 KB
/
neuralnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size, learning_rate):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.learning_rate = learning_rate
# Initialize weights and biases with random values
self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
self.bias_hidden = np.zeros((1, self.hidden_size))
self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)
self.bias_output = np.zeros((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
self.hidden_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
self.hidden_output = self.sigmoid(self.hidden_input)
self.output_input = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
self.output = self.sigmoid(self.output_input)
return self.output
def backward(self, X, y, output):
error_output = y - output
delta_output = error_output * self.sigmoid_derivative(output)
error_hidden = delta_output.dot(self.weights_hidden_output.T)
delta_hidden = error_hidden * self.sigmoid_derivative(self.hidden_output)
self.weights_hidden_output += self.hidden_output.T.dot(delta_output) * self.learning_rate
self.bias_output += np.sum(delta_output, axis=0, keepdims=True) * self.learning_rate
self.weights_input_hidden += X.T.dot(delta_hidden) * self.learning_rate
self.bias_hidden += np.sum(delta_hidden, axis=0, keepdims=True) * self.learning_rate
def train(self, X, y, epochs):
for epoch in range(epochs):
output = self.forward(X)
self.backward(X, y, output)
loss = np.mean(np.square(y - output))
if epoch % 1000 == 0:
print(f"Epoch {epoch}, Loss: {loss:.4f}")
# Sample data for XOR problem
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# Create a neural network
nn = NeuralNetwork(input_size=2, hidden_size=4, output_size=1, learning_rate=0.1)
# Train the neural network
nn.train(X, y, epochs=10000)
# Test the trained neural network
test_output = nn.forward(X)
print("Predicted Output:")
print(test_output)