ccs355 Lab Manual
ccs355 Lab Manual
Aim
To implement a Simple Vector Addition using TensorFlow in Python.
Algorithm
Algorithm
Step 1 - Import the library
Step 2 – Initialize two vectors as x any y using constant function
Step 3 – Add both the vectors using add function
Step 4 – Display the output
Program
import numpy as np
import tensorflow as tf
x = tf.constant([1, 2, 3, 4, 5])
y = tf.constant([1, 2, 3, 4, 5])
z=tf.add(x, y)
print(z)
Output
tf.Tensor([ 2 4 6 8 10], shape=(5,), dtype=int32)
1. Implement a regression model in Keras.
Aim
To implement a simple regression model using Keras in Python.
Algorithm
Step 1 - Import the library
Step 2 - Loading the Dataset
Step 3 - Creating Regression Model
Step 4 - Compiling the model
Step 5 - Fitting the model
Step 6 - Evaluating the model
Step 7 - Predicting the output
Program
from keras import models
from keras.layers import Dense, Dropout
from keras.utils import to_categorical
from keras.datasets import mnist
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
# Load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], 28*28))
X_train = X_train.astype('float32') / 255
X_test = X_test.reshape((X_test.shape[0],28*28))
X_test = X_test.astype('float32') / 255
y_train = to_categorical(y_train,10)
y_test = to_categorical(y_test,10)
# Compile model
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train model
model.fit(X_train, y_train,
batch_size=128,
epochs=2,
verbose=1,
validation_data=(X_test, y_test))
model.summary()
#Evaluating model
score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
#Predicting
y_pred = model.predict(X_test)
print()
print(y_pred)
Output
Epoch 1/2
469/469 [==============================] - 3s 6ms/step - loss: 9.5282 - accuracy: 0.1621 -
val_loss: 11.0893 - val_accuracy: 0.2014
Epoch 2/2
469/469 [==============================] - 3s 6ms/step - loss: 9.7238 - accuracy: 0.1655 -
val_loss: 7.7625 - val_accuracy: 0.2367
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 512) 401920
=================================================================
Total params: 535,818
Trainable params: 535,818
Non-trainable params: 0
_________________________________________________________________
Test loss: 7.762475967407227
Test accuracy: 0.23669999837875366
313/313 [==============================] - 0s 917us/step
Aim
To implement a multi-layer perceptron using TensorFlow in Python.
Algorithm
Step 1: Import the necessary libraries.
Step 2: Download the dataset.
TensorFlow allows us to read the MNIST dataset and we can load it directly in the program as a
train and test dataset.
Step 3: Now we will convert the pixels into floating-point values.
Step 4: Understand the structure of the dataset
Step 5: Visualize the data.
Step 6: Form the Input, hidden, and output layers.
Step 7: Compile the model.
Step 8: Fit the model.
Step 9: Find Accuracy of the model.
Program
# importing modules
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Activation
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# dense layer 1
Dense(256, activation='sigmoid'),
# dense layer 2
Dense(128, activation='sigmoid'),
# output layer
Dense(10, activation='sigmoid'),
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10,
batch_size=2000,
validation_split=0.2)
Epoch 1/10
24/24 [==============================] - 1s 14ms/step - loss: 2.0874 - accuracy: 0.3941 -
val_loss: 1.7334 - val_accuracy: 0.6237
Epoch 2/10
24/24 [==============================] - 0s 11ms/step - loss: 1.4023 - accuracy: 0.7329 -
val_loss: 1.0575 - val_accuracy: 0.8115
Epoch 3/10
24/24 [==============================] - 0s 11ms/step - loss: 0.8895 - accuracy: 0.8204 -
val_loss: 0.6979 - val_accuracy: 0.8616
Epoch 4/10
24/24 [==============================] - 0s 11ms/step - loss: 0.6294 - accuracy: 0.8618 -
val_loss: 0.5178 - val_accuracy: 0.8853
Epoch 5/10
24/24 [==============================] - 0s 11ms/step - loss: 0.4909 - accuracy: 0.8842 -
val_loss: 0.4190 - val_accuracy: 0.8982
Epoch 6/10
24/24 [==============================] - 0s 12ms/step - loss: 0.4119 - accuracy: 0.8965 -
val_loss: 0.3614 - val_accuracy: 0.9068
Epoch 7/10
24/24 [==============================] - 0s 12ms/step - loss: 0.3638 - accuracy: 0.9047 -
val_loss: 0.3260 - val_accuracy: 0.9130
Epoch 8/10
24/24 [==============================] - 0s 12ms/step - loss: 0.3310 - accuracy: 0.9106 -
val_loss: 0.3002 - val_accuracy: 0.9193
Epoch 9/10
24/24 [==============================] - 0s 12ms/step - loss: 0.3070 - accuracy: 0.9155 -
val_loss: 0.2815 - val_accuracy: 0.9242
Epoch 10/10
24/24 [==============================] - 0s 11ms/step - loss: 0.2878 - accuracy: 0.9195 -
val_loss: 0.2668 - val_accuracy: 0.9253
test loss, test acc: [0.27316993474960327, 0.9235000014305115]
3. B. Implement a single-layer perceptron in TensorFlow Environment.
Aim
To implement a single-layer perceptron using TensorFlow in Python.
Algorithm
Step1: Import necessary libraries
Step 2: Now load the dataset using “Keras” from the imported version of tensor flow.
Step 3: Now display the shape and image of the single image in the dataset. The image size contains
a 28*28 matrix and length of the training set is 60,000 and the testing set is 10,000.
Step 4: Now normalize the dataset in order to compute the calculations in a fast and accurate
manner.
Step 5: Building a neural network with single-layer perception. Here we can observe as the model is
a single-layer perceptron that only contains one input layer and one output layer there is no
presence of the hidden layers.
Step 6: Output the accuracy of the model on the testing data.
Program
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
(x_train, y_train),\
(x_test, y_test) = keras.datasets.mnist.load_data()
len(x_train)
len(x_test)
x_train[0].shape
plt.matshow(x_train[0])
# Normalizing the dataset
x_train = x_train/255
x_test = x_test/255
Output:
Epoch 1/5
1875/1875 [==============================] - 1s 679us/step - loss: 0.4709 - accuracy:
0.8760
Epoch 2/5
1875/1875 [==============================] - 1s 712us/step - loss: 0.3035 - accuracy:
0.9156
Epoch 3/5
1875/1875 [==============================] - 1s 635us/step - loss: 0.2834 - accuracy:
0.9210
Epoch 4/5
1875/1875 [==============================] - 1s 635us/step - loss: 0.2732 - accuracy:
0.9241
Epoch 5/5
1875/1875 [==============================] - 1s 616us/step - loss: 0.2666 - accuracy:
0.9256
313/313 [==============================] - 1s 661us/step - loss: 0.2677 - accuracy: 0.9246
[0.26773443818092346, 0.9246000051498413]
3. Implement a Feed-Forward Network in TensorFlow/Keras.
Aim
To implement a Feed-Forward Network using TensorFlow in Python.
Algorithm
Step1: Import necessary libraries
Step 2: Now preparing training data (inputs-outputs).
Step 3: Preparing neural network parameters (weights and bias) using TensorFlow Variables
Step 4: Preparing inputs of the activation function
Step 5: Calculating the prediction error
Step 6: Minimizing the prediction error using gradient descent optimizer
Program
import tensorflow as tf
import numpy as np
# Preparing training data (inputs-outputs)
training_inputs_data = [[255, 0, 0],
[248, 80, 68],
[0, 0, 255],
[67, 15, 210]]
training_outputs_data = [[10], [20], [30], [40]]
# Preparing neural network parameters (weights and bias) using TensorFlow Variables
weights = tf.Variable(initial_value=[[0.1], [0.2], [0.3]], dtype=tf.float32)
bias = tf.Variable(initial_value=[[1]], dtype=tf.float32)
learning_rate = 0.05
optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate)
trainable_variables = [weights, bias] # List of variables to optimize
#train_op = optimizer.minimize(prediction_error, var_list=trainable_variables)
#train_op =
tensorflow.train.GradientDescentOptimizer(learning_rate=0.05).minimize(prediction_error)
Output
Expected Scores: [[1.]
[1.]
[1.]
[1.]]
4. Implement an Image Classifier using CNN in TensorFlow/Keras.
Aim
To implement an Image Classifier using CNN in TensorFlow using Python.
Algorithm
Step1: Import necessary libraries
Step 2: Download and prepare the CIFAR10 dataset
Step 3: Normalize pixel values to be between 0 and 1
Step 4: Create the convolutional base and Add Dense layers on top
Step 5: Compile and train the model
Step 6: Evaluate the model
Program
#Import TensorFlow
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
#Download and prepare the CIFAR10 dataset
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i])
# The CIFAR labels happen to be arrays,
# which is why you need the extra index
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
#Create the convolutional base
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.summary()
#Add Dense layers on top
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.summary()
#Compile and train the model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
=================================================================
Total params: 56,320
Trainable params: 56,320
Non-trainable params: 0
_________________________________________________________________
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
1563/1563 [==============================] - 22s 13ms/step - loss: 1.5517 - accuracy:
0.4339 - val_loss: 1.2641 - val_accuracy: 0.5459
Epoch 2/10
1563/1563 [==============================] - 20s 13ms/step - loss: 1.1461 - accuracy:
0.5942 - val_loss: 1.0613 - val_accuracy: 0.6225
Epoch 3/10
1563/1563 [==============================] - 19s 12ms/step - loss: 0.9905 - accuracy:
0.6495 - val_loss: 1.0187 - val_accuracy: 0.6456
Epoch 4/10
1563/1563 [==============================] - 19s 12ms/step - loss: 0.8999 - accuracy:
0.6842 - val_loss: 0.9230 - val_accuracy: 0.6756
Epoch 5/10
1563/1563 [==============================] - 20s 13ms/step - loss: 0.8289 - accuracy:
0.7104 - val_loss: 0.9123 - val_accuracy: 0.6847
Epoch 6/10
1563/1563 [==============================] - 21s 13ms/step - loss: 0.7659 - accuracy:
0.7314 - val_loss: 0.8839 - val_accuracy: 0.6920
Epoch 7/10
1563/1563 [==============================] - 21s 13ms/step - loss: 0.7181 - accuracy:
0.7495 - val_loss: 0.8938 - val_accuracy: 0.6907
Epoch 8/10
1563/1563 [==============================] - 21s 13ms/step - loss: 0.6725 - accuracy:
0.7638 - val_loss: 0.9034 - val_accuracy: 0.6961
Epoch 9/10
1563/1563 [==============================] - 21s 13ms/step - loss: 0.6320 - accuracy:
0.7776 - val_loss: 0.9145 - val_accuracy: 0.6988
Epoch 10/10
1563/1563 [==============================] - 21s 14ms/step - loss: 0.5945 - accuracy:
0.7924 - val_loss: 0.8990 - val_accuracy: 0.7111
---------------------------------------------------------------------------
313/313 - 1s - loss: 0.8990 - accuracy: 0.7111 - 1s/epoch - 4ms/step
0.7110999822616577
9. Perform Sentiment Analysis using RNN
Aim
To implement an Image Classifier using CNN in TensorFlow using Python.
Algorithm
Step1: Import necessary libraries
Step 2: Getting reviews with words that come under 5000 most occurring words in the entire corpus
of textual review data
Step 3: Getting all the words from word_index dictionary, again printing the review
Step 4: Get the minimum and the maximum length of reviews
Step 5: Keeping a fixed length of all reviews to max 400 words and fixing every word's embedding
size to be 32
Step 6: Creating a RNN model
Step 7: printing model summary
Step 8: Compiling model
Step 9: Training the model
Step 10: Printing model score on test data
Program
from tensorflow.keras.layers import SimpleRNN, LSTM, GRU, Bidirectional, Dense, Embedding
from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
import numpy as np
print(x_train[0])
Model: "Simple_RNN"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 400, 32) 160000
=================================================================
Total params: 180,737
Trainable params: 180,737
Non-trainable params: 0
_________________________________________________________________
None
Epoch 1/5
390/390 [==============================] - 82s 206ms/step - loss: 0.6776 - accuracy:
0.5587 - val_loss: 0.6771 - val_accuracy: 0.6094
Epoch 2/5
390/390 [==============================] - 82s 209ms/step - loss: 0.6412 - accuracy:
0.6250 - val_loss: 0.6056 - val_accuracy: 0.6406
Epoch 3/5
390/390 [==============================] - 80s 206ms/step - loss: 0.5382 - accuracy:
0.7320 - val_loss: 0.5407 - val_accuracy: 0.7500
Epoch 4/5
390/390 [==============================] - 81s 207ms/step - loss: 0.5320 - accuracy:
0.7370 - val_loss: 0.6075 - val_accuracy: 0.6719
Epoch 5/5
390/390 [==============================] - 83s 213ms/step - loss: 0.4920 - accuracy:
0.7670 - val_loss: 0.6157 - val_accuracy: 0.6250