Decoding AI: Dive Deep Into Neural Networks and Create Your Own from Scratch!by@rusanov

# Decoding AI: Dive Deep Into Neural Networks and Create Your Own from Scratch!

March 27th, 2024

In our fast-paced world, machine learning and artificial intelligence are infiltrating every corner of our daily lives. But what is AI really all about? What is behind this magic? How can such a complex phenomenon be explained so simply? Join us on an amazing journey where we will immerse you in the world of artificial intelligence with clear and accessible examples!

In our fast-paced world, machine learning and artificial intelligence are infiltrating every corner of our daily lives. But what is AI really all about? What is behind this magic? How can such a complex phenomenon be explained so simply? Join us on an amazing journey where we will immerse you in the world of artificial intelligence with clear and accessible examples!

A neuron is the basic element of any neural network. To understand how a neuron works, let's take a closer look at it.

1. Input data are the values that the neuron takes in. This data can be anything, such as image pixels or feature values.

2. The weighting factor is the number by which the input data is multiplied. The weights determine how strongly the neuron responds to the input data.

3. Output value is the result of multiplying the input data by the weighting factor.

Each layer of an artificial neural network contains neurons, but they perform different functions. In the input layer, neurons simply transmit input data without additional processing. In the hidden layer, neurons actively process information by applying weighting coefficients and activation functions. And in the output layer, the neuron generates a final result based on the processed information from the previous layer.

### How Does a Neuron Make Predictions?

To make a prediction, the neuron simply multiplies the input data by its weighting factor:

prediction=input×weight prediction=input×weight

Sample code:

``````def neural_network(input, weight):
prediction = input * weight
return prediction
``````

In the code above, I've given you a peek behind the scenes of a simple neural network. This is a basic model with a single input. Here `weight` is our tuning tool. Multiplying it by the input, we get the prediction. But it's actually much more interesting than that!

## The World Is Multifaceted: When One Characteristic Isn't Enough

Imagine determining whether you're looking at a cat or a dog based on more than just their weight. After all, each animal has its own unique coat color, height and many other characteristics. What if your neural network could analyze all this mosaic of information at once and make accurate predictions? Each input neuron is like a detective examining different clues, and together they come to the right conclusion!

Let's use the example of defining an animal. Imagine we have an animal shelter, and we often have to determine whether an arriving pet is a cat or a dog based on various characteristics.

We will use the following data for each animal:

• `paw_length` - the length of the animal's paw.
• `sound_freq` - the frequency of sounds produced (meowing or barking).
• `fan_count` - the number of people (volunteers) who want to care for this animal.

Based on the experience of previous shelter professionals, we have initial coefficients to help us make predictions:

``````weights = [0.4, -0.2, 0.1]
``````

These coefficients were obtained by analyzing previous data:

• `0.1` for paw length: Paw length was found to have little effect on animal identification.
• `0.2` for frequency of sounds: The frequency of sounds produced appeared to be a more important characteristic.
• `0` for number of volunteers: This parameter has no effect on determining who a pet is, but it may be useful for other tasks, such as predicting how long an animal will stay at the shelter.

We can now use these weights to determine who is in front of us:

``````paw_lengths = [1.5, 7.5, 9.0, 7.0]
sound_freqs = [1.3, 0.85, 0.5, 0.9]
fan_counts = [1.1, 1.2, 1.4, 1.3]

input_data = [paw_lengths[0], sound_freqs[0], fan_counts[0]]
``````

Based on the provided data, our neural network will try to make a prediction. If we need to improve the accuracy of the prediction, we can adjust the weights based on additional data and feedback.

This graph shows the distribution of the characteristics of each animal. You can easily compare how much each characteristic makes up for each animal.

Let's write our code!

``````def w_sum(a, b):
assert (len(a) == len(b))
output = 0
for i in range(len(a)):
output += (a[i] * b[i])
return output
``````

w_sum: This is our data blending cookbook! Think of it as a chef carefully combining ingredients to create the perfect dish. We take two lists, multiply their values element by element, and then add them up. This process is like creating art from simple colors!

``````def neural_network(input, weights):
prediction = w_sum(input, weights)
return prediction
``````

neural_network: This is our cheat sheet for the data world! All you need to do is give it the information and weights and it will "decipher" the answer for you. Is it a cat or a dog? Like a true detective, this feature will solve this mystery.

``````input_data = [paw_length[0], sound_freq[0], enthusiasm[0]]
prediction = neural_network(input_data, weights)

if prediction > 0.5:
print("It's probably a dog!")
else:
print("It's probably a cat!")
``````

We chose a threshold value of 0.5 (or any other number) because this value optimally separated cats and dogs based on our test data. If the prediction is above this threshold, we consider that we have a dog in front of us, otherwise, we consider it a cat.

### Final Python Code

``````import numpy as np

# Function for calculating the weighted sum of inputs
def w_sum(a, b):
assert len(a) == len(b)
output = 0
for i in range(len(a)):
output += (a[i] * b[i])
return output

# Activation function (here we use the identity function for simplicity)
def activation(x):
return x

# Neural network function
def neural_network(input, weights):
pred = w_sum(input, weights)
return activation(pred)

# Input data (foot length, sound frequency, number of fans)
input_data = [1.5, 0.85, 1.2]

# Starting weights
weights = [0.1, 0.2, 0]

# Making a prediction
prediction = neural_network(input_data, weights)
print("Initial prediction:", prediction)

# Target (let 0 be a cat, 1 be a dog)
target = 1

# Learning curve
learning_rate = 0.01

# The learning process
for iteration in range(100):
prediction = neural_network(input_data, weights)
error = (prediction - target) ** 2
print("Error:", error, "Prediction:", prediction)

for i in range(len(weights)):
direction_and_amount = (prediction - target) * input_data[i]
weights[i] -= direction_and_amount * learning_rate

# Final prediction after training
prediction = neural_network(input_data, weights)
print("Final prediction after training:", prediction)
``````

In this code, we start with an initial prediction based on the input data and initial weights. We then proceed to a learning process where we gradually adjust the weights based on the error between the current prediction and the target value. The goal of training is to minimize the error. Once the training is complete, the final prediction is output.

This example demonstrates the basic principles of a neural network, including the training process and the adjustment of weights. It is intended for training purposes and to illustrate the concepts presented in the article.

### Conclusion: The Art of the Possible

The journey into the world of artificial intelligence and machine learning is not just about learning about technology; it is about exploring how we can push the boundaries of what is possible. We started with the basics - neurons and how they work, going through the creation of simple neural networks that can tell the difference between a cat and a dog using just a few characteristics. And while our examples were simplistic, they serve as a powerful demonstration of what artificial intelligence can do.

Standing on the cusp of a new era, we see that AI and machine learning are no longer just tools for processing data or automating tasks. They are a new way of interacting with the world and understanding the complexities of nature and even our own minds. They are tools that help us solve problems we couldn't even dream of before.

Each of us can be part of this amazing journey. Regardless of your background in programming or math, it's easier than ever to start learning AI today. Plenty of resources, courses, and communities are ready to support your interest and help you grow. Our simple examples and basic models are just a starting point from which you can begin your journey into the world of artificial intelligence.

Remember, every great discovery begins with a first step. Your research and experimentation can lead to the development of new technologies that help solve global problems, improve lives, and open new horizons for all of humanity. Artificial intelligence isn't just the technology of the future; it's shaping our future right now. And each of us can be part of this amazing process.

Together, we can create a world where machines and humans work side by side, pushing the boundaries of our understanding and abilities.

So, get inspired, experiment, and don't be afraid to explore the unknown. After all, it is in the unknown that lies the future we create together. Let's go, to new discoveries!

L O A D I N G
. . . comments & more!