@IsaacB1/

Neural Net 2

Python

More advanced

fork
loading
Files
  • main.py
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
import numpy as np #adds math

class NeuralNetwork(): #neural net class

#init
  def __init__(self):
    np.random.seed(1) #seeding the random

    self.synaptic_weights = 2 * np.random.random((3,1)) -1 # initializing synaptic weights as a 3 by 1 matrix

  def sigmoid(self, x): #defining sigmoid
    return 1/ (1 + np.exp(-x))
  
  def sigmoid_derivative(self, x): #defining sigmoid'
    return x * (1-x)
  
  def train(self, training_inputs, training_outputs, training_iterations): # defining train function
    
    for iteration in range(training_iterations): #. main loop

      output = self.think(training_inputs) #def outputs
      error = training_outputs - output #defining error margin
      adjustments = np.dot(training_inputs.T, error * self.sigmoid_derivative(output)) #defining adj
      self.synaptic_weights += adjustments #. adjusting weights
  

  def think(self, inputs): #def think

    inputs = inputs.astype(float) # changing input type to float
    output = self.sigmoid(np.dot(inputs, self.synaptic_weights)) #defining output

    return output # returning output


if __name__ == "__main__":


  #initialize new Neural Network
    neural_network = NeuralNetwork()

    print("Random synaptic weights: ")
    print(neural_network.synaptic_weights)

    training_inputs = np.array([[0,0,1],
                            [1,1,1],
                            [1,0,1],
                            [0,1,1]
                            ])

    training_outputs = np.array([[0,1,1,1],]).T 

    neural_network.train(training_inputs, training_outputs, 900000)

    print("Synaptic weights after training: ")
    print(neural_network.synaptic_weights)

    A = str(input("input 1: "))
    B = str(input("input 2: "))
    C = str(input("input 3: "))

    print ("New situation: input data = ", A,B,C)
    print("Output data: ")
    print(neural_network.think(np.array([A,B,C])))