From 2540d1a0866d3b73473585a3efd415c7f3104195 Mon Sep 17 00:00:00 2001 From: Karthik Udyawar <54676225+KarthikUdyawar@users.noreply.github.com> Date: Sun, 1 Sep 2019 19:54:11 +0530 Subject: [PATCH] New Updates and fixes + New Training inputs and outputs. + Constants like iterations and learning rate. + Improve sigmoid function. + And some miner changes. --- main-1.1.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 main-1.1.py diff --git a/main-1.1.py b/main-1.1.py new file mode 100644 index 0000000..a1012c1 --- /dev/null +++ b/main-1.1.py @@ -0,0 +1,62 @@ +import numpy as np + +class NeuralNetwork(): + def __init__(self): + np.random.seed(1) + + self.learning_rate = 10 + + self.synaptic_weights = 2 * np.random.standard_normal((2, 1)) - 1 + + def sigmoid(self, x, deriv = False): + if deriv == True: + return (1 / (1 + np.exp(-x))) * (1 - (1 / (1 + np.exp(-x)))) + return 1 / (1 + np.exp(-x)) + + def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations): + for iteration in range(number_of_training_iterations): + output = self.think(training_set_inputs) + + error = training_set_outputs - output + + adjustment = np.dot(training_set_inputs.T, error * self.sigmoid(output,deriv=True)) + + self.synaptic_weights += adjustment * self.learning_rate + + def think(self, inputs): + return self.sigmoid(np.dot(inputs, self.synaptic_weights)) + +if __name__ == "__main__": + + iterations = 10000 + + neural_network = NeuralNetwork() + + print ("Random starting synaptic weights: ") + print (neural_network.synaptic_weights) + + training_set_inputs = np.array(([[3,1.5], + [2,1], + [4,1.5], + [3,1], + [4,0.5], + [2,0.5], + [5.5,1], + [1,1]])) + + training_set_outputs = np.array([[1], + [0], + [1], + [0], + [1], + [0], + [1], + [0]]) + + neural_network.train(training_set_inputs, training_set_outputs, iterations) + + print ("New synaptic weights after training: ") + print (neural_network.synaptic_weights) + + print ("Considering new situation [4.5, 1] -> ?: ") + print (neural_network.think(np.array([[4.5, 1]])))