# Building a NEURAL NET from SCRATCH

What is a neural network?

Neural networks are one of the main tools used in machine learning. As neural suggests, they are brain-inspired systems which are intended to replicate the way that we humans learn. NNs consist of input and output layers, as well as a hidden layer consisting of units that transform the input. Photo by timJ on Unsplash

They are excellent tools for finding patterns which are far too complex or numerous for a human programmer to extract and teach the machine to recognize.

Steps for this mini-tutorial:

• Define independent and dependent variables.

# Step #0: Libraries.

`import numpy as np`

# Step #1: Variables.

`# Step #1: Variables.## trainnig data independent variable (x)training_set = np.array([[0,1,0],  # 3 features, 7 entries                        [0,0,1],                        [1,0,0],                        [1,1,0],                        [1,1,1],                        [0,1,1],                        [0,1,0]])labels = np.array([[1,                    0,                    0,                    1,                    1,                    0,                    1]])labelsarray([[1, 0, 0, 1, 1, 0, 1]])# reshaping our dependent variablelabels = labels.reshape(7,1)labelsarray([,       ,       ,       ,       ,       ,       ])`

Our input set contains seven records. Similarly, we also created a labels set that contains corresponding labels for each record in the input set. The labels are the values that we want our ANN to predict.

## Step #2: Hyper-paramaters

Here Random:

• Seed helps get the same values upon recursive execution
`#hyperparametersnp.random.seed(42)weights = np.random.rand(3,1)bias = np.random.rand(1)lr = 0.05`

## Step #3: Activation function.

The sigmoid function returns 0.5 when the input is 0. It returns a value close to 1 if the input is a large positive number. In the case of negative input, the sigmoid function outputs a value close to zero.

Quick tip: Scroll down on our page to find a dedicated post on ‘Sigmoid Function’ , the pros and cons!

`# methodsdef sigmoid(x):    return 1/(1+np.exp(-x))def sigmoid_derivative(x):    return sigmoid(x)*(1-sigmoid(x))`

## Step #4: Training.

In the context of machine learning, an epoch is one complete pass through the training data. A deep neural network has to be trained for multiple epochs.

`#training our modelfor epoch in range(30000):    inputs= training_set    XW = np.dot(inputs, weights)+bias    z = sigmoid(XW)    error = z -labels    print(error.sum())    dcost = error    dpred = sigmoid_derivative(z)    z_del = dcost * dpred    inputs = training_set.T    weights = weights - lr*np.dot(inputs, z_del)    for num in z_del:        bias = bias - lr*numinputs = training_set1.88062167156198121.85256408993252371.82431073408998961.79588476864383751.76730990818274131.73861033531865551.709810615159491.68093560685695741.65201037291821251.6230600870028531.59410994094986161.56518505179153731.53631036951310971.50751058630732481.47881004805314481.45023266871700731.42180184833463971.39354039518198361.36547045268638371.33761343156510741.30998994760873581.282619765453504...1.25552174861064851.22871381594380141.2022129047087811.17603494019522951.15019481193759351.12470635639512161.09958234593777471.0748344839180011.05047340555787531.02650868433747111.00294884353388140.97980137253100170.9570727474980540.93476845601953740.91289302525063020.89145005316943730.87044224250053960.84987143689230370.8297386589428620.81004414968590430.7907874091667520.77196723776107980.75358177791242280.73562855598976450.71810452399250730.70100610085647240.68432921314092330.66806933490252180.65222152658735730.63678047279640910.62174051880292520.60709570572190660.59283980425223560.57896634693074260.56546865885475450.55233988684531120.53957302703733620.52716095089561240.51509642966651840.50337215728516770.49198077176595880.480914875111654650.47016705178208060.45972988576840570.44959597632291140.43975795239716040.43020848584374660.42094030343831080.411946197779426760.40321903712430360.394751774218118250.38653745417426190.37856922146187420.370840326055865370.363344128803182560.35607410605745040.3490238536323310.342187090122026970.33555765963538420.32912953398795120.322896814394313840.316853732700876240.31099465219719380.30531406804186890.299806607336982060.29446702888303820.28929022264445590.28427120895374570.279405137480705830.274687285991208950.27011305891849070.26567798576822920.2613777193771970.257208034043803440.25316482354747170.249244099072506660.245441987050852740.2417547269370134`

## Coding a feed-forward neural network:

`#feed forwardXW = np.dot(inputs, weights)+biasz = sigmoid(XW)#errorerror = z - labelsprint(error.sum())#determining slopeslope = inputs * dcost * dpreddcost = errordpred = sigmoid_derivative(z)z_del = dcost * dpredinputs = training_set.Tweights = weights-lr*np.dot(inputs, z_del)for num in z_del:    bias = bias - lr*num0.238178668927295`

## Step #5: Outcomes.

In the first case the output (result) is closer to 0, so will be classified as 0. Second one has the value closer to 1 , so will be classified as 1.

`# predicting outcomessingle_pt = np.array([1,0,0])result = sigmoid(np.dot(single_pt, weights) + bias)print(result)[0.46808888]single_pt = np.array([0,1,0])result = sigmoid(np.dot(single_pt, weights) + bias)print(result)[0.70869886]`

I hope you like it.

No matter what books or blogs or courses or videos one learns from, when it comes to implementation everything can look like “Outside the Curriculum”.

The best way to learn is by doing! The best way to learn is by teaching what you have learned!

## Never give up!

See you on Linkedin!

# Other references

Master in Data Science. Passionate about learning new skills. Former branch risk analyst. https://www.linkedin.com/in/oscar-rojo-martin/. www.oscarrojo.es

## More from Oscar Rojo

Master in Data Science. Passionate about learning new skills. Former branch risk analyst. https://www.linkedin.com/in/oscar-rojo-martin/. www.oscarrojo.es