How can model be fit to data with Auto MPG dataset using TensorFlow?

TensorFlow is a machine learning framework provided by Google. It is an open−source framework used with Python to implement algorithms, deep learning applications, and much more for both research and production purposes.

The tensorflow package can be installed on Windows using the below command ?

pip install tensorflow

The Auto MPG dataset contains fuel efficiency data of 1970s and 1980s automobiles, including attributes like weight, horsepower, and displacement. Our goal is to predict the fuel efficiency of specific vehicles using regression.

Fitting the Model to Training Data

Once we have prepared our model and data, we can train the model using the fit() method. Here's how to fit a horsepower−based model to the Auto MPG dataset ?

import pandas as pd
import tensorflow as tf
from tensorflow import keras

# Assume we have already loaded and preprocessed the Auto MPG dataset
# and created train_features, train_labels, and hrspwr_model

print("The training data is being fit to the model")
history = hrspwr_model.fit(
    train_features['Horsepower'], train_labels,
    epochs=150,
    verbose=0,
    validation_split=0.3
)

# Convert training history to DataFrame for analysis
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
print("Last 5 training epochs:")
print(hist.tail())

The output shows the training progress over the final epochs ?

The training data is being fit to the model
Last 5 training epochs:
     loss  val_loss  epoch
145  6.34     6.89    145
146  6.31     6.85    146
147  6.28     6.82    147
148  6.25     6.79    148
149  6.23     6.77    149

Key Parameters Explained

Parameter Description Value Used
epochs Number of training iterations 150
verbose Controls training output display 0 (silent)
validation_split Fraction of data for validation 0.3 (30%)

Understanding the Training Process

  • The fit() function trains the model on the horsepower feature to predict fuel efficiency

  • Training occurs over 150 epochs, with each epoch processing the entire training dataset

  • 30% of training data is reserved for validation to monitor overfitting

  • The history object captures training metrics like loss and validation loss

  • Converting history to a DataFrame allows easy analysis of training progress

Monitoring Training Progress

The history object contains valuable information about model performance during training. You can visualize the training progress ?

import matplotlib.pyplot as plt

# Plot training and validation loss
plt.figure(figsize=(10, 6))
plt.plot(hist['epoch'], hist['loss'], label='Training Loss')
plt.plot(hist['epoch'], hist['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Model Training History')
plt.legend()
plt.grid(True)
plt.show()

Conclusion

The fit() method trains your TensorFlow model on the Auto MPG dataset using specified epochs and validation split. Monitoring the training history helps ensure your model learns effectively without overfitting to the training data.

Updated on: 2026-03-25T15:38:36+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements