# Unified neural network in Python

NumPy | Python Methods and Functions

One neuron converts this input to some output. Depending on the given input and the weights assigned to each input, decide whether the neuron has fired or not. Suppose a neuron has 3 input connections and one output.

We will use

` # Python program to implement `
` # neural network of one neuron `

` `
` # import all required libraries `

` from ` ` numpy ` ` import ` ` exp, array, random, dot, tanh `

` # Class for creating neural `
` # network with one neuron `

` class ` ` NeuralNetwork (): `

` `

` ` ` def ` ` __ init __ ( ` ` self ` `): `

` `

` # Use seeds to make sure this is `

` # generate the same weights every time you run `

` random.seed (` ` 1 ` `) `

` # 3x1 Weight Matrix `

` self ` `. weight_matrix ` ` = ` ` 2 ` ` * ` ` random.random ((` ` 3 ` `, ` ` 1 ` `)) ` ` - ` ` 1 `

` # tanh as an activation function `

` def ` ` tanh (` ` self ` `, x): `

` return ` ` tanh (x) `

` # derivative of the Tan function. `

` # Needed for calculating gradients. `

` def ` ` tanh_derivative (` ` self ` `, x): `

` return ` ` 1.0 ` ` - ` ` tanh (x) ` ` * ` ` * ` ` 2 `

` # direct distribution `

` ` ` def ` ` forward_propagation (` ` self ` `, inputs): `

` return ` ` self ` `. tanh (dot (inputs, ` ` self ` `. weight_matrix)) `

` # train the neural network. `

` def ` ` train (` ` self ` `, train_inputs , train_outputs, `

` num_train_iterations): `

` # Number of iterations we want `

` # execute input for this set. `

` for ` ` iteration ` ` in ` ` range ` ` (num_train_iterations): `

` output ` ` = ` ` self ` `. forward_propagation (train_inputs) `

` `

` ` ` # Calculate an error in the output. `

` error ` ` = ` ` train_outputs ` ` - ` ` output `

` `

` # multiply the error by the input, then `

` # along the gradient of the tanh function for calculation `

` # adjustments must be made on the scales `

` adjustment ` ` = ` ` dot (train_inputs.T, error ` ` * `

` self ` `. tanh_derivative (output)) `

` `

` # Adjust the weight matrix `

` ` ` self ` `. weight_matrix ` ` + ` ` = ` ` adjustment `

` Code driver `

` if ` ` __ name__ ` ` = ` ` = ` ` "__ main__" ` `: `

` neural_network = NeuralNetwork () `

```   print ( ' Random weights at the start of training' ) print (neural_network.weight_matrix)   train_inputs = array ([[ 0 , 0 , 1 ], [ 1 , 1 , 1 ], [ 1 , 0 , 1 ], [ 0 , 1 , 1 ]]) train_outputs = array ([[ 0 , 1 , 1 , 0 ]]). T   neural_network.train (train_inputs, train_outputs, 10000 )      print ( 'New weights after training' )   print (neural_network.weight_matrix)    # Testing the neural network in a new situation. print ( " Testing network on new examples - & gt; " )   print (neural_network.forward_propagation (array ([ 1 , 0 , 0 ]))) Output: Random weights at the start of training [[-0.16595599] [0.44064899] [-0.99977125] ] New weights after training [[5.39428067] [0.19482422] [0.34317086]] Testing network on new examples - & gt; [0.99995873] (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` ```
``` ```
``` `````` Tutorials (adsbygoogle = window.adsbygoogle || []).push({}); Python OpenCV | cv2.putText () method numpy.arctan2 () in Python Python | os.path.realpath () method Python OpenCV | cv2.circle () method Python OpenCV | cv2.cvtColor () method Python - Move item to the end of the list time.perf_counter () function in Python Check if one list is a subset of another in Python Python | os.path.join () method Python | os.path.expanduser () method Replace negative value with zero in Numpy array Python | Convert nested dictionary list to Pandas dataframe TimeField — Django Models Python | os.path.isdir () method Python OpenCV cv2.line () method numpy.poly1d () in Python Numpy.sqrt () in Python Split nested list into two lists in Python Python program to convert camel case string to snake case Python | Pandas DataFrame.reset_index () Binning in Data Mining Python | Add suffix / prefix to strings in a list Dunn index Python Python - Print list vertically Books for developers Python Data Science Handbook 22/08/2021 CEH v11 Certified Ethical Hacker Study Guide 12/08/2021 Data Science from Scratch. First Principles with Python 23/09/2020 Python Workout: 50 TEN-MINUTE EXERCISES 23/09/2020 © 2022 Python.Engineering Best Python tutorials books for beginners and professionals Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python in Italiano Python auf Deutsch Python en Français Python en Español Türk dilinde Python Python: мануалы на русском ITIL v4 Computations Development Cryptography For dummies Machine Learning Big Data Loops Counters NumPy NLP PHP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```