TensorFlow Model Fit

Anika Tabassum Era Nov 21, 2022
TensorFlow Model Fit

The model.fit() method offered by the TensorFlow library saves time in the machine learning journey. Usually, when building a specific model, we need to set the training function, which requires a lot of constraints and coding hassle.

To mitigate these issues for the basic models, the model.fit() can be greatly handy.

On the contrary, when model.fit() deals with the training session, the model.evaluate() extracts the expected output data from any random input based on the trained model. Both methods play an inevitable role in the case of decision-making and value generation.

In the following section, we will examine how to implement the model.fit() method for the Sequential() model from Keras. The demonstration is with basic concepts, and the live code demo is also presented in this thread. Let’s begin!

Use TensorFlow model.fit()

We need a valid dataset to initiate the training and testing of a model. In our case, we will generate a dataset with an independent set of values and their dependent values.

Later, we will show a scatter plot to see if our values can plot as the equation was set for them. The code for this drive is as follows.

# Necessary Imports
from tensorflow.keras import layers
import matplotlib.pyplot as plt
from tensorflow import keras
import tensorflow as tf
import numpy as np
import os

# Generating random data
x = np.random.uniform(0.0, 1.0, (500))
y = 0.3 + 0.5 * x + np.random.normal(0.0, 0.3, len(x))

plt.scatter(x, y)

Output:

Use Modelfit in TensorFlow 1

We will initiate the model and the Dense layer for it. The Dense layer defined the input and expected output dimensions.

And also, we will set the loss function and optimizer for our model. The following code represents the words.

model = (
    keras.Sequential()
)  # Sequential for 1D data correlating (as we only have x[independent] and y[dependent])
model.add(tf.keras.layers.Dense(1, input_shape=(1,)))
model.compile(loss="mse", optimizer="rmsprop")

Use TensorFlow model.fit() and model.evaluate()

We will now examine the model.fit() and model.evaluate() to see if the training was and also match the last epochs loss function from the model.fit() with the evaluated methods loss function. And if it matches, we can say that the model was properly trained, and our system can now identify any value that falls under these constraints.

Here, in training, we set the batch_size (sample) to 100 from 500 data. And in the test, we gave a batch size of 500.

The system could have gained knowledge on those 100 samples with a greater epoch number.

print("train: ", model.fit(x, y, epochs=600, batch_size=100))

# The evaluate() method - gets the loss statistics
print("test: ", model.evaluate(x, y, batch_size=500))

Output:

Use Modelfit in Tensorflow 2

Use modelfit in TensorFlow 3

Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook