DEV Community

Kevin Ng'ang'a
Kevin Ng'ang'a

Posted on

Neural Networks, its Inspiration and Various Components.

Introduction

In this week, I got introduced to Deep learning, and one key takeaway was on neural networks. I discovered that a neural network is a way of getting a computer to learn patterns from data the whole concept is loosely inspired by how the human brain works. For instancem in your brain, neurons receive signals, decide whether those signals are strong enough to matter, and pass a signal onward if they are. A neural network copies this idea in a much simpler mathematical form where a "neuron" takes in some numbers, combines them, and passes the result forward, layer by layer, until the network produces a final answer.

In this article we will walk through how one of these networks actually gets built and trained in code, using real screenshots from a working example.

1. Building the Architecture

This is the network's structure, built with tf.keras.Sequential. "Sequential" just means the data flows through the layers one after another, in a straight line, and with no branching.

  • The input layer does not do any calculating. It just tells the network the shape of the data coming in, based on how many columns X_train_processed has.
  • The three hidden layers are where the actual learning happens. Each one is Dense, meaning every neuron in that layer is connected to every neuron in the layer before it. They use "relu" as their activation function, which is a simple rule that says if a neuron's combined signal is positive, pass it on as is and if it's negative, pass on zero instead. This small rule is what lets the network learn patterns that are not just straight lines.
  • The output layer has 3 neurons with a "softmax" activation, which means the network is choosing between 3 possible classes. It turns the network's raw output into probabilities that add up to 1, so the class with the highest probability becomes the prediction.

2. Compiling the Model

Before a network can learn anything, it needs to be compiled. Compiling tells the network how it should learn and how it should judge its own performance.

  • optimizer = "adam" sets the method the network uses to adjust itself after each mistake. Adam is a popular default because it adapts how big each adjustment is as training goes on, rather than using one fixed step size the whole time.
  • loss = "sparse_categorical_crossentropy" is the function that measures how wrong a prediction was. This particular loss function is built for problems like this one, where there are several classes to choose from and the correct answer is given as a plain number (like 0, 1, or 2) rather than one-hot encoded.
  • metrics = ["accuracy"] doesn't affect learning at all and it is just a number reported after each epoch so you can see how the network is doing in a way that is easy to read.

3. Deciding When to Stop Training

Neural networks do not automatically know when to stop learning and thus, if left alone, they can keep training long after they have stopped genuinely improving, and start memorizing the training data instead of learning general patterns. This callback protects against that.

EarlyStopping(monitor = "val_loss", patience = 5, restore_best_weights = True) watches the loss on the validation data (data the network does not train on, used only to check its progress). If that validation loss does not improve for 5 epochs in a row (patience = 5), training stops early. restore_best_weights = True makes sure that when training stops, the network keeps the version of itself from its best epoch, not just whatever it looked like when it finally stopped.

4. Training the Model

This is where everything comes together and the network actually starts learning.

model.fit(...) feeds the training data, X_train_processed and y_train_encoded, into the network. validation_data gives it a separate set to check its progress on after each epoch, without training on it. epochs = 200 sets the maximum number of full passes through the training data, though the EarlyStopping callback from the step before may cut this short. batch_size = 32 means the network looks at 32 examples at a time before updating itself, rather than updating after every single example or waiting until it has seen the entire dataset. Finally, callbacks = [callbacks] plugs in the early stopping rule, so it's actually applied during this run.

Conclusion

In conclusion, when these four pieces are put together, they form the full life cycle of a neural network which revolves around the axis of, the architecture deciding what the network looks like, compiling decides how it learns and how it is judged, the callback decides when it should stop, and fit is the step where all of that is actually put into motion. Each piece is simple enough on its own, but the network's real behavior comes from how these choices interact with each other during training.

Top comments (0)