We’re going to introduce in this chapter what is a Neuron.
The purpose of a neuron is that of taking an input and in return give us an output, with also the capacity that the neuron can learn from its mistakes. So the neuron can check if the output is wrong, and adjust itself to be more precise the next time.

In this chapter we’re going to learn 4 basic concepts of the neuron:

  1. Input
  2. Weights
  3. Activation Function
  4. Output

1. The input

The input is the image (or the images) that we give to the Neuron. As we learned in the previous lessons, we can’t just use the images right away as they are but first we have to do some preprocessing of the images.

Preprocessing the image means transform it in a format that the computer can understand.

This one is the original image as we see it.

Original image (vertical line)

This one below is the same image, flattened, after the preprocessing.

Vertical flattened

This below is the input written in python.
On line 3 we load the image, on line 4 we divide the pixels per 255 so that we get the values of 0 if the pixel is black and 1 if the pixel is white.
Finally we flatten the image to have it all in one row.

# 1. Input 
# 1.1 Image preprocessing 
img = cv2.imread("images/vertical.png", cv2.IMREAD_GRAYSCALE) 
img = img / 255 img_flattened = img.flatten()

2. The Weights

The weights are float numbers with the value from 0.0 to 1.
The array containing the weights has the same size of the image. So we’re using an image of 3×3 pixels, so the weights are 9 numbers.
We will start now generating weights with value of 0.5 each, then we will understand how to use them and why they’re so important on the next lesson.

We then multiply the weights with the image flattened.

# 2. Weights
wheights = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
weighted_sum = sum(img_flattened * wheights)

3. The Activation function

The activation function is the operation that classifies the object. The activation function will return a value between 0 and 1.
At the moment we’re working with “Vertical” and “Horizontal” images, and I decided to associate the vertical image with value 0 and the horizontal image with the value 1.

We will understand the importance of the activation function in the next lesson.

# 3. Activation function 
result = sigmoid(weighted_sum)

4. The Output

The output is the final classification of the image that we passed to the neuron.
If the activation function returned a value closer to 0, then our output will be 0, so “vertical”, otherwise 1 so “horizontal”.

In the case of our image we got a value of 0.81, so it si saying that our image is “Horizontal”, while in reality is vertical. This happens because the neuron is still really dumb and it has not been trained.

# 4. Output
# Vertical 0
if result < 0.5:
    print("Vertical")
# Horizontal 1
elif result > 0.5:
    print("Horizontal")