Implementing Neural Networks Using TensorFlow (original) (raw)

Last Updated : 29 May, 2026

Deep learning is a branch of machine learning that uses neural networks with multiple layers to learn complex patterns from data. These networks automatically extract features and make intelligent predictions, enabling important applications across various domains.

Implementation

Step 1: Install TensorFlow

Install TensorFlow to set up the environment for building and training deep learning models.

pip install -U tensorflow

Step 2: Download and Read the Dataset

The Red Wine Quality dataset is used for this classification task. The dataset is loaded into a Pandas DataFrame for preprocessing and model training.

Python `

import numpy as np import pandas as pd

df = pd.read_csv('winequality-red.csv',sep=';')

df.head()

`

**Output

data

Step 3: Data Preparation and Train-Test Splitting

The dataset is divided into training and validation sets for model training and evaluation.

import tensorflow as tf

train_df = df.sample(frac=0.75, random_state=4)

val_df = df.drop(train_df.index)

`

Step 4: Feature Scaling and Data Separation

Neural networks perform better when input features are scaled to a similar range. The dataset is normalized and then separated into input features and target values.

Python `

max_val = train_df.max(axis= 0) min_val = train_df.min(axis= 0)

range = max_val - min_val train_df = (train_df - min_val)/(range)

val_df = (val_df- min_val)/range X_train = train_df.drop('quality',axis=1) X_val = val_df.drop('quality',axis=1) y_train = train_df['quality'] y_val = val_df['quality']

input_shape = [X_train.shape[1]]

input_shape

`

**Output:

[11]

Step 5: Create the Neural Network Model

Keras is used to build a neural network using the Sequential model.

model = tf.keras.Sequential([

tf.keras.layers.Dense(units=64, activation='relu',
                      input_shape=input_shape),
tf.keras.layers.Dense(units=64, activation='relu'),
tf.keras.layers.Dense(units=1)

]) model.summary()

`

**Output:

Step 6: Compile the Model

The model is compiled by configuring the optimizer, loss function and evaluation metrics.

model.compile(optimizer='adam', loss='mae')

`

Step 7: Train the Neural Network Model

The model is trained using the fit() method with training and validation datasets.

losses = model.fit(X_train, y_train,

               validation_data=(X_val, y_val),
               batch_size=256, 
               epochs=15, 

)

`

**Output:

Step 8: Predictions and Evaluate Accuracy

After training, the model predicts wine quality using the predict() method.

Python `

model.predict(X_val.iloc[0:3, :])

`

**Output:

array([[0.40581337],

[0.5295989 ],

[0.3883106 ]], dtype=float32)

Step 9: Compare Predictions with Actual Values

Python `

y_val.iloc[0:3]

`

**Output:

0 0.4

9 0.4

12 0.4

Name: quality, dtype: float64

Step 10: Visualize Training and Validation Loss

Training and validation loss curves help analyze model performance and identify overfitting.

Python `

loss_df = pd.DataFrame(losses.history) loss_df.loc[:,['loss','val_loss']].plot()

`

**Output:

download

output

You can download the code from here.

While training, the training loss usually decreases continuously, but an increasing validation loss indicates overfitting. Techniques like Early Stopping can automatically stop training at the optimal epoch.