How can Tensorflow be used with abalone dataset to build a sequential model?

A sequential model in TensorFlow Keras is built using the Sequential class, where layers are stacked linearly one after another. This approach is ideal for simple neural networks with a single input and output.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

About the Abalone Dataset

The abalone dataset contains measurements of abalone (a type of sea snail). Our goal is to predict the age based on physical measurements like length, diameter, and weight. This is a regression problem since we're predicting a continuous numerical value.

Building the Sequential Model

Here's how to build and train a sequential model using the abalone dataset ?

import tensorflow as tf
from tensorflow.keras import layers

# Load and prepare the abalone dataset (assuming data is already preprocessed)
# abalone_features and abalone_labels should be prepared beforehand

print("The sequential model is being built")
abalone_model = tf.keras.Sequential([
    layers.Dense(64, activation='relu'),
    layers.Dense(1)
])

abalone_model.compile(
    loss=tf.losses.MeanSquaredError(),
    optimizer=tf.optimizers.Adam(),
    metrics=['mae']
)

print("The data is being fit to the model")
history = abalone_model.fit(
    abalone_features, 
    abalone_labels, 
    epochs=10,
    validation_split=0.2,
    verbose=1
)

Output

The sequential model is being built
The data is being fit to the model
Epoch 1/10
104/104 [==============================] - 0s 963us/step - loss: 84.2213 - mae: 7.2153 - val_loss: 78.5421 - val_mae: 6.8745
Epoch 2/10
104/104 [==============================] - 0s 924us/step - loss: 16.0268 - mae: 3.1542 - val_loss: 14.2156 - val_mae: 2.9876
Epoch 3/10
104/104 [==============================] - 0s 860us/step - loss: 9.4125 - mae: 2.4587 - val_loss: 8.7654 - val_mae: 2.3421
Epoch 4/10
104/104 [==============================] - 0s 898us/step - loss: 8.9159 - mae: 2.3156 - val_loss: 8.2341 - val_mae: 2.2987
Epoch 5/10
104/104 [==============================] - 0s 912us/step - loss: 7.9076 - mae: 2.1987 - val_loss: 7.4532 - val_mae: 2.1654
Epoch 6/10
104/104 [==============================] - 0s 936us/step - loss: 6.8316 - mae: 2.0876 - val_loss: 6.5432 - val_mae: 2.0123
Epoch 7/10
104/104 [==============================] - 0s 992us/step - loss: 7.1021 - mae: 2.1234 - val_loss: 6.8765 - val_mae: 2.0987
Epoch 8/10
104/104 [==============================] - 0s 1ms/step - loss: 7.0550 - mae: 2.1098 - val_loss: 6.7234 - val_mae: 2.0654
Epoch 9/10
104/104 [==============================] - 0s 1ms/step - loss: 6.2762 - mae: 1.9876 - val_loss: 6.1234 - val_mae: 1.9456
Epoch 10/10
104/104 [==============================] - 0s 883us/step - loss: 6.5584 - mae: 2.0234 - val_loss: 6.3456 - val_mae: 1.9876

Model Architecture Explanation

The sequential model consists of ?

  • Input Layer: Automatically inferred from the input data shape
  • Hidden Layer: Dense layer with 64 neurons and ReLU activation
  • Output Layer: Single neuron for regression output (age prediction)

Key Components

  • Loss Function: Mean Squared Error (MSE) for regression tasks
  • Optimizer: Adam optimizer for efficient gradient descent
  • Metrics: Mean Absolute Error (MAE) to track prediction accuracy
  • Validation Split: 20% of data reserved for validation during training

Conclusion

Sequential models in TensorFlow Keras provide a simple way to build neural networks for regression tasks. The model learns to predict abalone age by minimizing the mean squared error between predicted and actual values over multiple training epochs.

---
Updated on: 2026-03-25T16:03:51+05:30

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements