10.02.2021 Views

Neural Networks from Scratch in Python by Harrison Kinsley, Daniel Kukie a

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2 - Coding Our First Neurons - Neural Networks from Scratch in Python

size as well. The result is a vector calculated as a sum of the consecutive vector elements:

21

A Single Neuron with NumPy

Let’s code the solution, for a single neuron to start, using the dot product and the addition of the

vectors with NumPy. This makes the code much simpler to read and write (and faster to run):

import ​numpy ​as ​np

inputs ​= ​[​1.0​, ​2.0​, ​3.0​, ​2.5​]

weights ​= ​[​0.2​, ​0.8​, ​-​0.5​, ​1.0​]

bias ​= ​2.0

outputs ​= ​np.dot(weights, inputs) ​+ ​bias

print​(outputs)

>>>

4.8

Fig 2.07:​ Visualizing the math of the dot product of inputs and weights for a single neuron.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!