Created
December 2, 2019 15:54
-
-
Save aydinnyunus/f1d96d070c368bfd9fb7e0431d56ef2d to your computer and use it in GitHub Desktop.
Basic Neural Networks on Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import exp,array,random,dot | |
class NeuralNetwork(): | |
def __init__(self): | |
random.seed(1) | |
self.synaptic_weights = 2* random.random((3,1))-1 | |
def __sigmoid(self,x): | |
return 1/(1+exp(-x)) | |
def __sigmoid_derivative(self,x): | |
return x*(1-x) | |
def train(self,trainin_set_inputs,trainin_set_outputs,number_of_training_iterations): | |
for iteration in xrange(number_of_training_iterations): | |
output = self.think(trainin_set_inputs) | |
error = trainin_set_outputs - output | |
adjustment = dot(trainin_set_inputs.T,error * self.__sigmoid_derivative(output)) | |
self.synaptic_weights += adjustment | |
def think(self,inputs): | |
return self.__sigmoid(dot(inputs,self.synaptic_weights)) | |
if __name__ == '__main__': | |
neural_network = NeuralNetwork() | |
print 'Random starting synaptic weights:' | |
print neural_network.synaptic_weights | |
trainin_set_inputs = array([[0,0,1],[1,1,1],[1,0,0],[0,1,1],[0,1,0]]) | |
trainin_set_outputs = array([[0,1,1,0,1]]).T | |
neural_network.train(trainin_set_inputs,trainin_set_outputs,10000) | |
print 'New synaptic weights after training :' | |
print neural_network.synaptic_weights | |
print 'Considering new situtation [1,0,0] --> ?' | |
print neural_network.think(array([1,0,0])) | |
print 'Considering new situtation [0,0,0] --> ?' | |
print neural_network.think(array([0,0,0])) | |
print 'Considering new situtation [1,1,0] --> ?' | |
print neural_network.think(array([1,1,0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment