Build, Compile, and Fit Models in TensorFlow Part I
Build and Compile in TensorFlow
TensorFlow is a powerful tool for building machine learning models.
It’s like a big box of building blocks that you can use to make computer programs that can learn by themselves. It is a free and open-source software library for machine learning and artificial intelligence. It can be used across a range of tasks but has a particular focus on training and inference of deep neural networks. It may be used to your true advantage as a machine learning engineer, as a machine learning beginner, using it will be a real magic trick in your hands this is why I'd want to discuss this incredible framework in my first three pieces of this series.
Background
A Google Brain Team-developed open-source machine learning library used for internal Google research and production. It is utilized for deep learning applications, large-scale machine learning, and numerical computation. As a beginner to a machine learning engineer, you will discover many ideas in this series of practical articles.
In this article, let's talk about general concepts of building, compiling and fitting models :
Building a Model in TensorFlow
Building a model in TensorFlow is similar to building a tower out of blocks like in the game of Jenga (the word "Jenga" means "to build" in Swahili one of my original languages): you have to choose how many blocks you want to use, what shape they should have, and how to stack them on top of one another this first step is what Machine Learning Engineers call defining the architecture of the model: this comprises the number of layers, the kind of layers (such as dense or convolutional layers), the input, hidden, and output layers, as well as any additional procedures like regularization or activation functions (for a tutorial here ).
This can be done using the Keras API, which is included with TensorFlow and provides a user-friendly interface for building models. I know actually in your head, you ask yourself how Machine Learning Engineers make all those decisions when they are defining the architecture of the model.
Here is the solution: The type of problem being handled, the properties of the data, and the intended performance of the model are all important considerations for machine learning engineers when choosing the architecture of a model. Here are some tips to help you comprehend the decisions machine learning engineers make:
Start with a straightforward architecture: It's frequently a good idea to start with a basic architecture when creating a new model and to progressively add complexity as necessary. This enables you to swiftly test and improve your ideas.
Play around with various architectures: When it comes to model architecture, there is no universally applicable answer. It's crucial to test out many architectures to see which one solves your problem the best.
Rely on prior knowledge You can use this information to inform your model architecture decisions if you have prior knowledge about the issue you're trying to address or the data you're using.
Stay current with the field of study: Machine learning is a field that is constantly developing, with new methods and structures being created all the time. Making educated decisions about your model design can be facilitated by staying current with the most recent research.
To demonstrate how these concepts might be used in practice, let's look at an example :
############## Code ##################
# importation of libraries already installed
import tensorflow as tf
from tensorflow import keras
# Define the model architecture
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
############## Code ##################
In this example, we’re building a simple neural network model using TensorFlow’s Keras API. The first layer is a Flatten layer that takes an input image with shape (28, 28) and flattens it into a 1D array. The second layer is a Dense layer with 128 neurons and uses the ReLU activation function. The final layer is another Dense layer with 10 neurons.
The number of layers and neurons in this example were chosen based on prior knowledge and experimentation. For example, 128 neurons in the hidden layer might have been found to provide good performance on similar problems in the past. Similarly, using the ReLU activation function is a common choice because it has been shown to work well in practice, you need more explanations and information please check here.
Once we’ve built our model, we need to compile it. This means telling TensorFlow how we want it to learn. Because the key here is to make our program learn so it will be intelligent for next challenges.
Compiling a Model in TensorFlow
Let's get back to our Jenga block tower-building game. Building a model in TensorFlow is similar to completing a Jenga block tower. TensorFlow model compilation is similar to adding the finishing touches to a Jenga block construction. Imagine that the different sorts of blocks used to construct the tower's layers correspond to the various model layers. When building the model, we check that the tower is stable and that all the bricks are neatly stacked.
In machine learning terms, compiling a model means specifying how we want the model to learn from the data. We achieve this by selecting elements such as the loss function, which informs us of the model's performance, and the optimizer, which assists us in adjusting the blocks to increase the tower's stability. Once more, the numerous types of blocks that make up each layer of the tower symbolize the various model levels. When building the model, we check that the tower is stable and that all the bricks are neatly stacked.
The loss function in machine learning acts as a yardstick to indicate how well our model is performing. It gauges how well our model's predictions match up with the correct solutions. Machine learning engineers select the best-performing loss function for their task from a wide variety that is available (see a small selection here).
The optimizer functions as a shovel to help us improve our model. To improve the forecasts, it modifies the model's parameters. Here are some instances of the various optimizers available, which are similar to loss functions.
Machine learning engineers use their knowledge and experience to pick the best loss function and optimizer for their problem.
Here’s an example of compiling a model in TensorFlow:
############## Code ##################
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
############## Code ##################
In this example, we’re telling TensorFlow that we want to use categorical_crossentropy as our loss function and adam as our optimizer. We’re also saying that we want to keep track of how accurate our model is by including accuracy in our list of metrics.
Compiling a model in TensorFlow enables us to ensure that everything is set up properly so that our model can learn from the data, much like how we must ensure that all the Jenga bricks are appropriately placed.
After creating our model, we can begin training it with the help of our data. Fitting the model is the term for this. It tries to learn from that when we give it some information and specify the correct responses.
Let's talk about it in the next article and some bonus. We are coming back as soon as possible, if you appreciate this one, please comment, like and share it.
Remember, building machine learning models is like building a tower of blocks. It takes time and practice, but it can be lots of fun too! 😊
Arthur Kaza spartanwk@gmail.com @#PeaceAndLove @Copyright_by_Kaz’Art / @ArthurStarks