{"id":637,"date":"2021-09-02T00:41:10","date_gmt":"2021-09-02T00:41:10","guid":{"rendered":"https:\/\/es-andreabianchini.it\/andrewsblog\/?p=637"},"modified":"2021-09-02T04:50:46","modified_gmt":"2021-09-02T04:50:46","slug":"rete-neuronale","status":"publish","type":"post","link":"https:\/\/es-andreabianchini.it\/andrewsblog\/?p=637","title":{"rendered":"Rete Neuronale"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import numpy as np\n\nclass NeuralNetwork():\n    \n    def __init__(self):\n        # seeding for random number generation\n        np.random.seed(1)\n        \n        #converting weights to a 3 by 1 matrix with values from -1 to 1 and mean of 0\n        self.synaptic_weights = 2 * np.random.random((3, 1)) - 1\n\n    def sigmoid(self, x):\n        #applying the sigmoid function\n        return 1 \/ (1 + np.exp(-x))\n\n    def sigmoid_derivative(self, x):\n        #computing derivative to the Sigmoid function\n        return x * (1 - x)\n\n    def train(self, training_inputs, training_outputs, training_iterations):\n        \n        #training the model to make accurate predictions while adjusting weights continually\n        for iteration in range(training_iterations):\n            #siphon the training data via  the neuron\n            output = self.think(training_inputs)\n\n            #computing error rate for back-propagation\n            error = training_outputs - output\n            \n            #performing weight adjustments\n            adjustments = np.dot(training_inputs.T, error * self.sigmoid_derivative(output))\n\n            self.synaptic_weights += adjustments\n\n    def think(self, inputs):\n        #passing the inputs via the neuron to get output   \n        #converting values to floats\n        \n        inputs = inputs.astype(float)\n        output = self.sigmoid(np.dot(inputs, self.synaptic_weights))\n        return output\n\n\nif __name__ == \"__main__\":\n\n    #initializing the neuron class\n    neural_network = NeuralNetwork()\n\n    print(\"Beginning Randomly Generated Weights: \")\n    print(neural_network.synaptic_weights)\n\n    #training data consisting of 4 examples--3 input values and 1 output\n    training_inputs = np.array(&#91;&#91;0,0,1],\n                                &#91;1,1,1],\n                                &#91;1,0,1],\n                                &#91;0,1,1]])\n\n    training_outputs = np.array(&#91;&#91;0,1,1,0]]).T\n\n    #training taking place\n    neural_network.train(training_inputs, training_outputs, 15000)\n\n    print(\"Ending Weights After Training: \")\n    print(neural_network.synaptic_weights)\n\n    user_input_one = str(input(\"User Input One: \"))\n    user_input_two = str(input(\"User Input Two: \"))\n    user_input_three = str(input(\"User Input Three: \"))\n    \n    print(\"Considering New Situation: \", user_input_one, user_input_two, user_input_three)\n    print(\"New Output data: \")\n    print(neural_network.think(np.array(&#91;user_input_one, user_input_two, user_input_three])))\n    print(\"Wow, we did it!\")\n<\/code><\/pre>\n\n\n\n<p>by <strong>&nbsp;<a href=\"https:\/\/www.linkedin.com\/in\/garbade\">Dr. Michael J. Garbade<\/a><\/strong>  <\/p>\n\n\n\n<p><a href=\"https:\/\/www.kdnuggets.com\/2018\/10\/simple-neural-network-python.html\">https:\/\/www.kdnuggets.com\/2018\/10\/simple-neural-network-python.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>by &nbsp;Dr. Michael J. Garbade https:\/\/www.kdnuggets.com\/2018\/10\/simple-neural-network-python.html<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,12,7],"tags":[],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-intelligenza-artificiale","category-python","category-stem"],"_links":{"self":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=637"}],"version-history":[{"count":3,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":640,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions\/640"}],"wp:attachment":[{"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/es-andreabianchini.it\/andrewsblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}