-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1_main.py
129 lines (101 loc) · 3.48 KB
/
v1_main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import numpy as np
import decimal
from pprint import pprint
class ArtificialNeuralNetwork:
"""
A class to represent an Artificial Neural Network.
Attributes
----------
synaptic_weights : ndarray
The weights of the synapses in the neural network.
"""
def __init__(self) -> None:
"""
Initializes the ArtificialNeuralNetwork object with random synaptic weights.
"""
self.synaptic_weights = 2 * np.random.random((4, 1)) - 1
def sigmoid(self, x) -> float:
"""
Maps a value from 0 to 1 using the sigmoid function.
Parameters
----------
x : float
The input value.
Returns
-------
float
The sigmoid of the input value.
"""
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x) -> float:
"""
Computes the derivative of the sigmoid function.
Parameters
----------
x : float
The input value.
Returns
-------
float
The derivative of the sigmoid of the input value.
"""
return x * (1-x)
def think(self, inputs) -> float:
"""
Computes the dot product of the inputs and synaptic weights, and applies the sigmoid function.
Parameters
----------
inputs : ndarray
The input values.
Returns
-------
float
The output of the neural network.
"""
inputs = inputs.astype(float)
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output
def train(self, training_inputs: np.ndarray, training_outputs: np.ndarray, number_of_iterations: int) -> None:
"""
Trains the neural network using the given training inputs and outputs.
It calculates the error margin by comparing the output to the expected output and adjusts the synapses based on this error margin (back propagation).
Parameters
----------
training_inputs : ndarray
The inputs that the model will train with.
training_outputs : ndarray
The expected outputs of the model.
number_of_iterations : int
The number of training iterations to run.
Returns
-------
None
"""
for _ in range(number_of_iterations):
output = self.think(training_inputs)
error = training_outputs - output
adjustments = np.dot(training_inputs.T, error *
self.sigmoid_derivative(output))
self.synaptic_weights += adjustments
if __name__ == '__main__':
ann = ArtificialNeuralNetwork()
print("Weights Pre-Training")
pprint(ann.synaptic_weights)
training_inputs = np.array([[1, 0, 0, 1],
[0, 1, 1, 0],
[1, 0, 1, 0],
[0, 0, 0, 1]])
training_outputs = np.array([[1],
[0],
[1],
[0]])
ann.train(training_inputs, training_outputs, 10000)
print('\n')
print("Weights post-Training")
pprint(ann.synaptic_weights)
_input = np.array([1, 0, 1, 0])
output = ann.think(_input)
result = decimal.Decimal(output[0]).quantize(
decimal.Decimal('1'), rounding=decimal.ROUND_HALF_UP)
print('\n')
pprint(f"The answer for {_input} is: {result}")