How to create Regression Model in TensorFlow

This article explains how to create Regression Model in TensorFlow using tf.keras.Sequential.

Import required libraries


import pandas as pd
import numpy as np

import tensorflow as tf
from tensorflow.keras import layers



Download abalone dataset into Pandas DataFrame


abalone_train = pd.read_csv(
    "https://storage.googleapis.com/download.tensorflow.org/data/abalone_train.csv",
    names=["Length", "Diameter", "Height", "Whole weight", "Shucked weight",
           "Viscera weight", "Shell weight", "Age"])


Separate Features and Labels for training


abalone_features = abalone_train.copy()
abalone_labels = abalone_features.pop('Age')

Create Feature array with numpy


abalone_features = np.array(abalone_features)

Create a Regression model to predict the age


abalone_model = tf.keras.Sequential([
  layers.Dense(64),
  layers.Dense(1)
])

abalone_model.compile(loss = tf.losses.MeanSquaredError(),
                      optimizer = tf.optimizers.Adam())

Train the regression model


abalone_model.fit(abalone_features, abalone_labels, epochs=10)

Categories: TensorFlow