Implementing Neural Network using TensorFlow in Python


Neural Network is a widely used concept in the field of Artificial Intelligence and is based on the structure of the human brain. A neural network works in layers, the simplest one being a sequential model where the input of the current layer is the output of the previous layer.

To create, train and test a neural network model, we can use a deep learning framework like Tensorflow in Python. Every neural network model is based on a few simple steps like taking data, making predictions, comparing predictions and finally, changing them to go closer to the target.

Algorithm

  • Step 1 − Import Python’s numpy and pandas libraries and load the csv file as a data frame.

  • Step 2 − Import tensorflow and split the data into training and validation sets.

  • Step 3 − Preprocess and scale the data of both the sets.

  • Step 4 − Separate the data of both the sets into different features.

  • Step 5 − Use the Keras module to form a linear neural network model and set adam and mae parameters for it. Note that adam works as an optimizer whereas mae works in predictions.

  • Step 6 − Train the dataset for as many epochs as you want. Here we are taking only 10 epochs.

  • Step 7 − Now use the predict function to test our model. Here we are passing the first 4 rows as input to generate the result.

  • Step 8 − Compare the result with the target value. If the predicted values are close enough to the actual ones, then we can conclude that the model works fine.

Example

In this example, we will take a diabetes dataset from kaggle and implement a neural network for it. Although we can create a multilayered neural network model as well, here we will limit ourselves to a linear network for the sake of simplicity.

#step 1 - import numpy and pandas and load the dataset
import numpy as np
import pandas as pd
df = pd.read_csv('dataset.csv')
df.head()

#step 2 - import tensorflow and split the data into training and validation sets 
import tensorflow as tf
train_df = df.sample(frac=0.85, random_state=4)
val_df = df.drop(train_df.index)

#step 3 - scale and preprocess the data
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

#step 4 - separate the data into features
X_train = train_df.drop('gravity',axis=1)
X_val = val_df.drop('gravity',axis=1)
y_train = train_df['gravity']
y_val = val_df['gravity']
input_shape = [X_train.shape[1]]
input_shape

#step 5 - make a linear neural network model and add parameters
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1,input_shape=input_shape)])
model.summary()
model.compile(optimizer='adam',
              loss='mae') 

#step 6 - train the dataset for epochs
losses = model.fit(X_train, y_train,
 
            validation_data=(X_val, y_val),
            batch_size=256,
            epochs=10,  
 
         )

#step 7 - use the predict function to test the model 
model.predict(X_val.iloc[0:4, :])

#step 8 - compare the result with the target value 
y_val.iloc[0:4]

We split the data into training and validation sets and preprocess the data by calculating the maximum and minimum values in the training set and we normalise the data between 0 and 1. We further separate the data into features and define an input shape based on the number of features.

We then create a linear neural network model and add a layer with 1, which represents output, with the previously defined input shape. We compile and train the model and obtain predictions from the trained model for the first 4 rows of data. Then we compare the predicted values with the actual values to assess the performance.

Output

Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_1 (Dense)             (None, 1)                 8         
                                                                 
=================================================================
Total params: 8
Trainable params: 8
Non-trainable params: 0

_________________________________________________________________
Epoch 1/10
1/1 [==============================] - 1s 944ms/step - loss: 0.8678 - val_loss: 1.0600
Epoch 2/10
1/1 [==============================] - 0s 71ms/step - loss: 0.8639 - val_loss: 1.0556
Epoch 3/10
1/1 [==============================] - 0s 63ms/step - loss: 0.8600 - val_loss: 1.0511
Epoch 4/10
1/1 [==============================] - 0s 66ms/step - loss: 0.8561 - val_loss: 1.0467
Epoch 5/10
1/1 [==============================] - 0s 65ms/step - loss: 0.8522 - val_loss: 1.0422
Epoch 6/10
1/1 [==============================] - 0s 66ms/step - loss: 0.8483 - val_loss: 1.0378
Epoch 7/10
1/1 [==============================] - 0s 74ms/step - loss: 0.8444 - val_loss: 1.0334
Epoch 8/10
1/1 [==============================] - 0s 71ms/step - loss: 0.8405 - val_loss: 1.0289
Epoch 9/10
1/1 [==============================] - 0s 74ms/step - loss: 0.8366 - val_loss: 1.0245
Epoch 10/10
1/1 [==============================] - 0s 68ms/step - loss: 0.8327 - val_loss: 1.0200
1/1 [==============================] - 0s 84ms/step

1     0.413793
9     0.551724
36    0.103448
44    0.517241
Name: gravity, dtype: float64

Conclusion

Neural networks are known to process huge amounts of data fairly easily along with providing a high computational power and parallel processing capability. However, implementing a neural network still remains a bit expensive as compared to the traditional methods. Nonetheless, they find great usage in AI based applications like image recognition, face recognition or stock market predictions to name a few.

Updated on: 07-Aug-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements