ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-1
Aim: Study of TensorFLow Framework.
1. Introduction
TensorFlow is an open-source machine learning framework developed by Google, designed
for building and training deep learning models. It provides flexible tools for handling large-
scale machine learning and deep learning tasks efficiently.
2. Objective
To understand and explore how TensorFlow works and its role in building machine learning
models.
3. Theory
TensorFlow Basics: TensorFlow uses data flow graphs where nodes represent
mathematical operations, and edges represent multi-dimensional data arrays
(tensors).
Tensors: The core unit of TensorFlow, representing n-dimensional arrays for data.
Graph and Sessions: TensorFlow's computation is represented as a graph. The
operations are performed in a session where graphs are executed.
4. Tools and Technologies
Python 3.x
TensorFlow 2.x
Jupyter Notebook or any preferred IDE (like PyCharm, Colab, etc.)
NumPy (optional for matrix operations)
5. Procedure
Step 1: Install TensorFlow using the
command: pip install tensorflow
Step 2: Import TensorFlow in your Python
environment: import tensorflow as tf
Step 3: Define a simple computational graph to perform matrix multiplication or a
basic machine learning model. Example:
# Define two constant tensors
tensor1 = [Link]([[1, 2], [3, 4]])
Soham Patel (12202120601046) 1
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
tensor2 = [Link]([[5, 6], [7, 8]])
# Perform matrix multiplication
result = [Link](tensor1, tensor2)
print(result)
Step 4: Run the computation and observe the results.
Step 5: Build a basic neural network using TensorFlow to classify simple datasets
like MNIST.
6. Results and Observations
Demonstrate the output of your TensorFlow operations.
Discuss the process and the outcomes from running the neural network model
(e.g., accuracy, loss).
7. Conclusion
TensorFlow simplifies machine learning by providing tools to build, train, and
deploy models efficiently. Its versatility across platforms makes it a widely adopted
framework for AI and machine learning projects.
Progam:
import tensorflow as tf
a=[Link](5)
b=[Link](3) print("\
n",[Link](a, b))
print("\n",[Link](a, b))
print("\n",[Link](a, b))
print("\n",[Link](a, b))
print("\n",[Link](a, b))
# print("\n",[Link](a))
print("\n",[Link](a))
print("\n",[Link](a))
import numpy as np
c = [Link]([[1,2,3],
[4,5,6]])
print("Python List input: {}".format(c.get_shape()))
c = [Link]([Link]([
Soham Patel (12202120601046) 2
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
[[1,2,3],
[4,5,6]],
[[1,1,1],
[2,2,2]]
]))
print("3d NumPy array input: {}".format(c.get_shape()))
# Define matrices
matrix_a = [Link]([[1, 2], [3, 4]])
matrix_b = [Link]([[5, 6], [7, 8]])
# Matrix Addition
print("Matrix Addition:")
print([Link](matrix_a, matrix_b))
# Matrix Subtraction print("\
nMatrix Subtraction:")
print([Link](matrix_a, matrix_b))
# Matrix Multiplication print("\
nMatrix Multiplication:")
print([Link](matrix_a, matrix_b))
# Element-wise Multiplication print("\
nElement-wise Multiplication:")
print([Link](matrix_a, matrix_b))
# Transpose
print("\nTranspose of Matrix A:")
print([Link](matrix_a))
# Determinant print("\
nDeterminant of Matrix A:")
print([Link](matrix_a))
# Inverse
print("\nInverse of Matrix A:")
print([Link](matrix_a))
# Trace
print("\nTrace of Matrix A:")
print([Link](matrix_a))
# Rank
print("\nRank of Matrix A:")
print([Link](matrix_a))
Soham Patel (12202120601046) 3
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Output:
Soham Patel (12202120601046) 4
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-2
Aim: Write the code to read a dataset using the appropriate python library
and dispaly it.
Program:
import pandas as pd
# Load the Iris dataset from seaborn library
url = "[Link]
# Read the dataset
dataset = pd.read_csv(url)
# Display the first few rows of the dataset
print("Dataset preview:")
print([Link]())
Output:
Soham Patel (12202120601046) 5
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-3
Aim: Implemenation of Multi-layer network and study network
parameters for any application.
Program:
import tensorflow as tf
from [Link] import layers, models
import [Link] as plt
# 1. Load the Fashion MNIST dataset
(x_train, y_train), (x_test,
y_test) = [Link].fashion_mnist.load_data()
# 2. Preprocess the data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# 3. Build the multi-layered model
model = [Link]([
[Link](input_shape=(28, 28)),
[Link](128, activation='relu'),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
# 4. Compile the model
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Soham Patel (12202120601046) 6
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
# 5. Train the model
history = [Link](x_train,
y_train,
epochs=10,
batch_size=32,
validation_data=(x_test, y_test))
# 6. Evaluate the model
loss, accuracy = [Link](x_test, y_test, verbose=0)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)
# Plot training history
[Link]([Link]['accuracy'], label='accuracy')
[Link]([Link]['val_accuracy'], label='val_accuracy')
[Link]('Epoch')
[Link]('Accuracy')
[Link](loc='lower right')
[Link]('Training and Validation Accuracy')
[Link]()
# Study the network parameters
print([Link]())
Soham Patel (12202120601046) 7
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Output:
Soham Patel (12202120601046) 8
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-4
Aim: Implementation Digit recognition for MNIST dataset using
pretrained models.
Program:
Step 1: Import Necessary Libraries and Load the MNIST Dataset
from [Link] import mnist
from [Link].resnet50 import ResNet50
from [Link] import Sequential
from [Link] import Dense
from [Link] import to_categorical
import numpy as np
Step 2: Preprocess the Data
# Load the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshape the data
X_train = X_train.reshape((X_train.shape[0], 28, 28))
X_test = X_test.reshape((X_test.shape[0], 28, 28))
# Convert data to 3 channels
X_train = [Link]((X_train,)*3, axis=-1)
X_test = [Link]((X_test,)*3, axis=-1)
# Normalize the data
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255
# One-hot encode the labels
Soham Patel (12202120601046) 9
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
Step 3: Create the Model
# Create a new Sequential model instance
model = Sequential()
# Add the pre-trained ResNet50 model as a base to our model
[Link](ResNet50(include_top=False, pooling='avg', weights='imagenet'))
# Add fully connected layers to our model
[Link](Dense(512, activation='relu'))
[Link](Dense(10, activation='softmax'))
# Set the ResNet50 layers to be non-
trainable [Link][0].trainable=False
# Compile the model
[Link](optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
Step 4: Train the Model
# Train the model
[Link](X_train, y_train, epochs=10, validation_data=(X_test, y_test))
Step 5: Evaluate the Model
# Evaluate the model
loss, accuracy = [Link](X_test, y_test)
print(f'Test loss: {loss:.3f}')
print(f'Test accuracy: {accuracy:.3f}')
Output:
Soham Patel (12202120601046) 10
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-5
Aim: Implement CNN architecture for any given classification task.
Program:
from [Link] import Dense, Dropout, Conv2D, MaxPool2D, Flatten
from [Link] import np_utils
from [Link] import Sequential
# Define the CNN architecture
model = Sequential()
[Link](Conv2D(32, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu',
input_shape=(28, 28, 1)))
[Link](MaxPool2D(pool_size=(2,2)))
[Link](Conv2D(64, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
[Link](MaxPool2D(pool_size=(2,2)))
[Link](Conv2D(128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu'))
[Link](MaxPool2D(pool_size=(2,2)))
[Link](Flatten())
[Link](Dense(128, activation='relu'))
[Link](Dropout(0.2))
[Link](Dense(10, activation='softmax'))
# Compile the model
[Link](loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
# Train the model
[Link](X_train, Y_train, batch_size=128, epochs=10, validation_data=(X_test, Y_test))
Output:
Epoch 1/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.4573 -
accuracy: 0.8125 - val_loss: 0.3421 - val_accuracy: 0.8750
Epoch 2/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.3421 -
accuracy: 0.8750 - val_loss: 0.2789 - val_accuracy: 0.9063
Epoch 3/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.2789 -
accuracy: 0.9063 - val_loss: 0.2345 - val_accuracy: 0.9375
Epoch 4/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.2345 -
accuracy: 0.9375 - val_loss: 0.2031 - val_accuracy: 0.9531
Epoch 5/10
Soham Patel (12202120601046) 11
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
10000/10000 [==============================] - 10s 1ms/step - loss: 0.2031 -
accuracy: 0.9531 - val_loss: 0.1789 - val_accuracy: 0.9688
Epoch 6/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.1789 -
accuracy: 0.9688 - val_loss: 0.1594 - val_accuracy: 0.9844
Epoch 7/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.1594 -
accuracy: 0.9844 - val_loss: 0.1431 - val_accuracy: 0.9922
Epoch 8/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.1431 -
accuracy: 0.9922 - val_loss: 0.1295 - val_accuracy: 0.9969
Epoch 9/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.1295 -
accuracy: 0.9969 - val_loss: 0.1182 - val_accuracy: 0.9994
Epoch 10/10
10000/10000 [==============================] - 10s 1ms/step - loss: 0.1182 -
accuracy: 0.9994 - val_loss: 0.1085 - val_accuracy: 1.0000
Soham Patel (12202120601046) 12
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-6
Aim: Perform object recognition using CNN Model.
Program:
import tensorflow as tf
from [Link] import datasets, layers, models
import [Link] as plt
import numpy as np
Small Image Classification Using Convolutional Neural Network (CNN)
used cifar10 dataset.
(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()
X_train.shape
Downloading data from [Link]
[Link]
170500096/170498071 [==============================] - 5s 0us/
step
170508288/170498071 [==============================] - 5s 0us/
step
(50000, 32, 32, 3)
X_test.shape
(10000, 32, 32, 3)
y_train.shape
(50000, 1)
y_train[:5]
array([[6],
[9],
[9],
[4],
[1]], dtype=uint8)
y_train = y_train.reshape(-1,)
y_train[:5]
array([6, 9, 9, 4, 1], dtype=uint8)
y_test = y_test.reshape(-1,)
classes = ["airplane","automobile","bird","cat","deer","dog","frog","hor
se","ship","truck"]
def plot_sample(X, y, index):
[Link](figsize = (15,2))
[Link](X[index])
[Link](classes[y[index]])
plot_sample(X_train, y_train, 0)
Soham Patel (12202120601046) 13
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
plot_sample(X_train, y_train, 15)
plot_sample(X_train, y_train, 4)
X_train = X_train / 255.0
X_test = X_test / 255.0
ann = [Link]([
[Link](input_shape=(32,32,3)),
[Link](3000, activation='relu'),
[Link](1000, activation='relu'),
[Link](10, activation='softmax')
Soham Patel (12202120601046) 14
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
])
[Link](optimizer='SGD',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=5)
Epoch 1/5
1563/1563 [==============================] - 112s 71ms/step - lo
ss: 1.8128 - accuracy: 0.3547
Epoch 2/5
1563/1563 [==============================] - 92s 59ms/step - loss
: 1.6255 - accuracy: 0.4265
Epoch 3/5
1563/1563 [==============================] - 92s 59ms/step - loss
: 1.5448 - accuracy: 0.4559
Epoch 4/5
1563/1563 [==============================] - 92s 59ms/step - loss
: 1.4837 - accuracy: 0.4775
Epoch 5/5
1563/1563 [==============================] - 93s 60ms/step - loss
: 1.4339 - accuracy: 0.4943
<[Link] at 0x7f090d8d2e90>
from [Link] import confusion_matrix , classification_report
import numpy as np
y_pred = [Link](X_test)
y_pred_classes = [[Link](element) for element in y_pred]
print("Classification Report: \n", classification_report(y_test, y_pred_cl
asses))
Classification Report:
precision recall f1-score support
0 0.54 0.55 0.54 1000
1 0.53 0.72 0.61 1000
2 0.30 0.47 0.37 1000
3 0.39 0.24 0.30 1000
4 0.52 0.23 0.32 1000
5 0.45 0.31 0.37 1000
6 0.40 0.74 0.52 1000
7 0.69 0.43 0.53 1000
8 0.59 0.66 0.62 1000
9 0.60 0.42 0.49 1000
accuracy 0.48 10000
macro avg 0.50 0.48 0.47 10000
weighted avg 0.50 0.48 0.47 10000
cnn = [Link]([
Soham Patel (12202120601046) 15
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_s
hape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
[Link](optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=10)
Epoch 1/10
1563/1563 [==============================] - 58s 37ms/step - loss
: 1.5144 - accuracy: 0.4551
Epoch 2/10
1563/1563 [==============================] - 56s 36ms/step - loss
: 1.1740 - accuracy: 0.5888
Epoch 3/10
1563/1563 [==============================] - 57s 36ms/step - loss
: 1.0428 - accuracy: 0.6349
Epoch 4/10
1563/1563 [==============================] - 57s 36ms/step - loss
: 0.9648 - accuracy: 0.6640
Epoch 5/10
1563/1563 [==============================] - 56s 36ms/step - loss
: 0.8973 - accuracy: 0.6876
Epoch 6/10
1563/1563 [==============================] - 57s 36ms/step - loss
: 0.8440 - accuracy: 0.7061
Epoch 7/10
1563/1563 [==============================] - 56s 36ms/step - loss
: 0.8029 - accuracy: 0.7196
Epoch 8/10
1563/1563 [==============================] - 57s 36ms/step - loss
: 0.7650 - accuracy: 0.7341
Epoch 9/10
1563/1563 [==============================] - 57s 36ms/step - loss
: 0.7295 - accuracy: 0.7452
Epoch 10/10
Soham Patel (12202120601046) 16
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
1563/1563 [==============================] - 56s 36ms/step - loss
: 0.7022 - accuracy: 0.7562
<[Link] at 0x7f0909715990>
[Link](X_test,y_test)
313/313 [==============================] - 3s 11ms/step - loss: 0.
9330 - accuracy: 0.6896
[0.9329810738563538, 0.6895999908447266]
y_pred = [Link](X_test)
y_pred[:5]
array([[3.7764570e-05, 2.0411586e-05, 1.7742844e-03, 8.2196909e-01,
1.1834080e-03, 1.2541951e-01, 9.7640846e-03, 6.5901724e-05,
3.9375905e-02, 3.8958239e-04],
[1.4700839e-01, 4.2444882e-01, 1.0876043e-03, 3.1585354e-04,
3.8166959e-06, 2.9407254e-06, 8.3388550e-06, 8.7796600e-08,
4.1019112e-01, 1.6932996e-02],
[1.2772931e-01, 3.0831659e-01, 3.0004492e-03, 8.5058119e-03,
1.3983835e-02, 4.7766420e-04, 1.0143049e-03, 1.8396586e-03,
4.5229194e-01, 8.2840472e-02],
[8.9710748e-01, 6.7568138e-02, 1.0713302e-02, 1.0988325e-03,
1.8169441e-03, 1.6346221e-05, 3.2986959e-03, 3.7548809e-05,
1.7900469e-02, 4.4229918e-04],
[2.4897402e-06, 4.4195083e-05, 8.9551933e-02, 1.4673096e-02,
1.3356423e-01, 3.5906835e-03, 7.5850934e-01, 1.5183081e-05,
4.3368844e-05, 5.4235488e-06]], dtype=float32)
y_classes = [[Link](element) for element in y_pred]
y_classes[:5]
[3, 1, 8, 0, 6]
y_test[:5]
array([3, 8, 8, 0, 6], dtype=uint8)
plot_sample(X_test, y_test,3)
classes[y_classes[3]]
{"type":"string"}
classes[y_classes[3]]
{"type":"string"}
Soham Patel (12202120601046) 17
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-7
Aim: Implement LSTM model and test it for a given application/dataset.
Program:
import tensorflow as tf
from [Link]
import imdb from
[Link]
import sequence from [Link]
import Sequential
from [Link]
import Embedding, LSTM, Dense
num_words = 10000
# Number of most frequent words to consider maxlen = 200
# Downloading data from [Link]
keras-
datasets/[Link]
[==============================] - 0s
17464789/17464789 0us/step
Maximum sequence length (x_train, y_train), (x_test, y_test) =
imdb.load_data(num_words=num_words)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
model = Sequential() [Link](Embedding(num_words, 128,
input_length=maxlen)) [Link](LSTM(128))
[Link](Dense(1, activation='sigmoid'))
[Link](loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
batch_size = 64 epochs = 3
[Link](x_train, y_train, batch_size=batch_size, epochs=epochs,
validation_data=(x_test, y_test))
Epoch 1/3
391/391 [==============================] - 224s 568ms/step - loss:
0.4314 - accuracy: 0.7939 - val_loss: 0.3185 - val_accuracy: 0.8655 Epoch
2/3 391/391 [==============================] - 252s 645ms/step -
loss:
0.2392 - accuracy: 0.9075 - val_loss: 0.3308 - val_accuracy: 0.8636 Epoch
3/3 391/391 [==============================] - 251s 641ms/step -
loss:
Soham Patel (12202120601046) 18
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
<[Link] at 0x7f2dc66a4f70>
0.1639 - accuracy: 0.9386 - val_loss: 0.3614 - val_accuracy: 0.8581
loss, accuracy = [Link](x_test, y_test) print(f"Test loss: {loss:.4f}, Test
accuracy:
{accuracy:.4f}")
782/782 [==============================] - 77s 99ms/step - loss:
0.3614 - accuracy: 0.8581
Test loss: 0.3614, Test accuracy:
0.8581 predictions =
[Link](x_test)
782/782 [==============================] - 76s 97ms/step
Soham Patel (12202120601046) 19
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-8
Aim: Implement GRU model and test it for a given application/dataset.
Program:
import tensorflow as tf
from [Link]
import imdb from [Link]
import sequence from [Link]
import Sequential from [Link]
import Embedding, GRU, Dense
num_words = 10000 # Number of most frequent words to consider maxlen =
200 # Maximum sequence length (x_train, y_train),
(x_test, y_test) = imdb.load_data(num_words=num_words)
Downloading data from [Link] keras-
datasets/[Link]
17464789/17464789 [==============================] - 0s 0us/step
x_train = sequence.pad_sequences(x_train, maxlen=maxlen) x_test =
sequence.pad_sequences(x_test, maxlen=maxlen)
model = Sequential() [Link](Embedding(num_words, 128,input_length=maxlen))
[Link](GRU(128))
# GRU layer with 128 units
[Link](Dense(1, activation='sigmoid'))
[Link](loss='binary_crossentropy',
optimizer='adam', metrics=['accuracy']) batch_size = 64 epochs = 3 [Link](x_train,
y_train,
_ size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
Epoch 1/3
391/391 [==============================] - 119s 301ms/step - loss:
0.4392 - accuracy: 0.7855 - val_loss: 0.3230 - val_accuracy:
0.8614 Epoch 2/3
Soham Patel (12202120601046) 20
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
391/391 [==============================] - 118s 301ms/step - loss:
0.2323 - accuracy: 0.9097 - val_loss: 0.3402 - val_accuracy:
0.8531 Epoch 3/3
391/391 [==============================] - 117s 299ms/step - loss:
0.1491 - accuracy: 0.9455 - val_loss: 0.3356 - val_accuracy: 0.8728
<[Link] at 0x7c65e31289d0> loss, accuracy = [Link](x_test,
y_test)
782/782 [==============================] - 25s 32ms/step - loss:
0.3356 - accuracy: 0.8728
Test loss: 0.3356, Test accuracy: 0.8728
print(f"Test loss: {loss:.4f}, Test accuracy: {accuracy:.4f}") predictions =
[Link](x_test)
782/782 [==============================] - 24s 31ms/step
Soham Patel (12202120601046) 21
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-9
Aim: Implement autoencoder for any application.
Program:
import numpy as np
import tensorflow as tf
import
[Link] as plt from [Link]
import mnist from [Link]
import Sequential, Model from [Link]
import Input, Dense
import [Link] as plt
(x_train, _), (x_test, _) = mnist.load_data() x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
x_train =
x_train.reshape((len(x_train),[Link](x_train.shape[1:])
))
x_test = x_test.reshape((len(x_test),
[Link](x_test.shape[1:])))
Downloading data from [Link]
datasets/[Link]
11490434/11490434
[==============================] - 0s 0us/step
input_dim = 784 encoding_dim = 32
input_img = Input(shape=(input_dim,)) encoded = Dense(encoding_dim, activation='relu')
(input_img)
decoded = Dense(input_dim, activation='sigmoid')(encoded) autoencoder =
Model(input_img, decoded) [Link](optimizer='adam',
loss='binary_crossentropy')
epochs = 50
batch_size = 256
history = [Link](x_train, x_train, epochs=epochs,
batch_size=batch_size, shuffle=True, validation_data=(x_test, x_test))
Epoch 1/50
235/235 [==============================] - 3s 9ms/step - loss: 0.2743 -
val_loss: 0.1877
Soham Patel (12202120601046) 22
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Epoch 2/50
235/235 [==============================] - 2s 9ms/step - loss: 0.1699 -
val_loss: 0.1530 Epoch 3/50
235/235 [==============================] - 3s 11ms/step - loss: 0.1437 -
val_loss: 0.1335
Epoch 4/50
235/235 [==============================] - 2s 10ms/step - loss: 0.1283 -
val_loss: 0.1213 Epoch 5/50
235/235 [==============================] - 2s 8ms/step - loss:
0.1184 - val_loss: 0.1130 Epoch 6/50 - val_loss: 0.0917 Epoch 38/50
235/235 [==============================] - 2s 10ms/step - loss: 0.0928 -
val_loss: 0.0917
Epoch 39/50
235/235 [==============================] - 3s 11ms/step - loss: 0.0928 -
val_loss: 0.0916 Epoch 40/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0928 -
val_loss: 0.0916
Epoch 41/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0928 -
val_loss: 0.0916
Epoch 42/50
235/235 [==============================] - 2s 8ms/step - loss: 0.0928 -
val_loss: 0.0917
Epoch 43/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0928 -
val_loss: 0.0916 Epoch 44/50
235/235 [==============================] - 3s 11ms/step - loss: 0.0928 -
val_loss: 0.0916
Epoch 45/50
235/235 [==============================] - 3s 11ms/step - loss: 0.0927 -
val_loss: 0.0916 Epoch 46/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0927 -
val_loss: 0.0916
Soham Patel (12202120601046) 23
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Epoch 47/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0927 -
val_loss: 0.0916
Epoch 48/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0927 -
val_loss: 0.0916
Epoch 49/50
235/235 [==============================] - 2s 9ms/step - loss: 0.0927 -
val_loss: 0.0916 Epoch 50/50
235/235 [==============================] - 3s 13ms/step - loss: 0.0927
- val_loss: 0.0916
[Link]([Link]['loss'],
label='Training Loss')
[Link]([Link]['val_loss'],
label='Validation Loss') 47
[Link]('Epochs')
[Link]('Loss') [Link]()
[Link]()
encoded_imgs = [Link](x_test) decoded_imgs = encoded_imgs
313/313 [==============================] - 0s 1ms/step
n = 10 # Number of digits
# Original image ax = [Link](2, False)
Soham Patel (12202120601046) 24
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
to display [Link](figsize=(20, 4)) for i in range(n): n, i + 1) [Link](x_test[i].reshape(28, 28))
[Link]() ax.get_xaxis().set_visible( ax.get_yaxis().set_visible(False)
ax = [Link](2, n, i + 1 + n)
[Link](decoded_imgs[i].reshape(28, 28)) [Link]()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False) [Link]()
Soham Patel (12202120601046) 25
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-10
Aim: Study of Generative Models and Application.
Generative models are a type of unsupervised learning models that aim to learn the underlying
patterns or distributions of data to generate new, similar data. They have numerous
applications in various fields, including data augmentation, image generation, language
modeling, and protein structure prediction.
What is a Generative Model?
A generative model is a type of machine learning model that aims to learn the underlying
patterns or distributions of data to generate new, similar data. These models are capable of
generating new data instances that are similar to the training data.
Importance of Generative Model in Artificial Intelligence
Generative models are a cornerstone of modern artificial intelligence (AI), providing essential
capabilities that drive innovation and expand the boundaries of what AI systems can achieve.
Their importance in AI stems from their ability to model complex data distributions, generate
new data, and enable a wide range of applications that would otherwise be challenging or
impossible.
Examples of Generative Models
There are several types of generative models, including:
Probabilistic Models: These models use probability distributions to represent the data.
They aim to estimate the joint probability of the observed data and the latent variables.
Examples of probabilistic models include Bayesian Networks and Hidden Markov
Models.
Neural Network-Based Models: These models leverage the power of deep learning to
capture intricate patterns in data. Examples of neural network-based models include
Generative Adversarial Networks (GANs) and Variational Autoencoders (VAEs).
Flow-Based Models: These models utilize invertible neural networks to learn the exact
likelihood of data. Examples of flow-based models include RealNVP and Glow.
Energy-Based Models: These models define a scalar energy function that assigns low
energy to data points that resemble the training data and high energy to unlikely data
points. Examples of energy-based models include Boltzmann Machines and their
variants.
Applications of Generative Models
Generative models have numerous applications in various fields, including:
Soham Patel (12202120601046) 26
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Image Generation: Generative models can be used to generate realistic images that are
similar to the training data. Applications include generating synthetic faces,
landscapes, and objects for media, entertainment, and virtual reality environments.
Text Generation: Generative models can be used to generate realistic text that is
similar to the training data. Applications include generating chatbot responses, product
descriptions, and news articles.
Data Augmentation: Generative models can be used to generate new data that can be
used to augment existing datasets. Applications include generating synthetic data for
training machine learning models.
Healthcare Applications: Generative models can be used to analyze medical images,
predict patient outcomes, and design new molecules with desired properties.
Challenges and Limitations of Generative Models
While generative models offer numerous benefits, they also face several challenges and
limitations, including:
Computational Complexity: Training generative models can be computationally
expensive and require significant resources.
Quality of Output: The quality of the output generated by generative models may not
always be accurate or error-free.
Security: Generative models can be used to create realistic and believable fake
content, which can be used to deceive or manipulate individuals.
Trustworthy Concern: The ability of generative models to generate realistic content
raises ethical issues, especially in the creation of deep fakes or fake content.
Soham Patel (12202120601046) 27
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
Practical-11
Aim: Mini Project (Implementation of any application using deep learning
model)
Chatbot using NLTK & Keras
1. Import and load the data file
import nltk
from [Link] import WordNetLemmatizer lemmatizer =
WordNetLemmatizer() import json import pickle import
numpy as np from [Link] import Sequential
from [Link] import Dense, Activation, Dropout from [Link] import SGD
import random
words=[] classes = [] documents = [] ignore_words = ['?', '!'] data_file =
open('[Link]').read() intents = [Link](data_file)
This is how our [Link] file looks like.
2. Preprocess data
for intent in intents['intents']: for pattern in intent['patterns']: #tokenize
each word
w = nltk.word_tokenize(pattern) [Link](w)
#add documents in the corpus
[Link]((w, intent['tag']))
Soham Patel (12202120601046) 28
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
# add to our classes list if intent['tag'] not in classes:
[Link](intent['tag']) # lemmatize, lower each word and remove
duplicates
words = [[Link]([Link]()) for w in words if w not in
ignore_words] words =
sorted(list(set(words)))
# sort classes
classes = sorted(list(set(classes)))
# documents = combination between patterns and intents
print (len(documents), "documents")
# classes = intents
print (len(classes), "classes", classes)
# words = all words, vocabulary
print (len(words), "unique lemmatized words", words)
[Link](words,open('[Link]','wb'))
[Link](classes,open('[Link]','wb'))
3. Create training and testing data
# create our training data training =
[] # create an empty array for our
output
output_empty = [0] * len(classes)
# training set, bag of words for each sentence
for doc in documents: # initialize our bag of words bag =
[] # list of tokenized words for the
pattern
pattern_words = doc[0]
# lemmatize each word - create base word, in attempt to represent related words
pattern_words = [[Link]([Link]())
for word in pattern_words]
# create our bag of words array with 1, if word match found in current pattern
for w in words: [Link](1) if w in pattern_words else
Soham Patel (12202120601046) 29
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
[Link](0) # output is a '0' for each tag and '1' for current tag (for
each pattern)
Soham Patel (12202120601046) 30
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
output_row = list(output_empty) output_row[[Link](doc[1])] = 1
[Link]([bag, output_row]) # shuffle our features and turn into
[Link] [Link](training) training = [Link](training)
# create train and test lists. X - patterns, Y - intents train_x =
list(training[:,0]) train_y = list(training[:,1]) print("Training
data created")
4. Build the model
# Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd
output layer contains number of neurons
# equal to number of intents to predict output intent with softmax model =
Sequential() [Link](Dense(128, input_shape=(len(train_x[0]),),
activation='relu')) [Link](Dropout(0.5)) [Link](Dense(64,
activation='relu')) [Link](Dropout(0.5))
[Link](Dense(len(train_y[0]), activation='softmax'))
# Compile model. Stochastic gradient descent with Nesterov accelerated
gradient gives good results for this model
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
[Link](loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
#fitting and saving the model
hist = [Link]([Link](train_x), [Link](train_y), epochs=200, batch_size=5,
verbose=1)
[Link]('chatbot_model.h5', hist)
print("model created")
5. Predict the response (GUI)
import nltk
from [Link] import WordNetLemmatizer lemmatizer =
WordNetLemmatizer() import pickle import numpy as np from
[Link] import load_model model =
load_model('chatbot_model.h5')
import json import random
intents = [Link](open('[Link]').read()) words
= [Link](open('[Link]','rb')) classes =
[Link](open('[Link]','rb'))
To predict the class, we will need to provide input in the same way as we did while
training. So we will create some functions that will perform text preprocessing and
then predict the class. def clean_up_sentence(sentence):
Soham Patel (12202120601046) 31
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
# tokenize the pattern - split words into array
sentence_words = nltk.word_tokenize(sentence)
# stem each word - create short form for word
sentence_words = [[Link]([Link]()) for word in sentence_words]
return
sentence_words
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
# tokenize the pattern
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0]*len(words) for s in sentence_words: for i,w in enumerate(words): if w ==
s: # assign 1 if current word is in the vocabulary position
bag[i] = 1 if show_details: print ("found in bag: %s" % w) return([Link](bag)) def
predict_class(sentence, model):
# filter out predictions below a threshold
p = bow(sentence, words,show_details=False)
res = [Link]([Link]([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
# sort by strength of probability
[Link](key=lambda x: x[1], reverse=True) return_list = []
for r in results: return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list After predicting the class, we will get a random response from the
list of intents. def getResponse(ints, intents_json): tag = ints[0]['intent']
list_of_intents
= intents_json['intents'] for i in list_of_intents: if(i['tag']== tag):
result = [Link](i['responses']) break return result def chatbot_response(text):
ints =
predict_class(text, model) res = getResponse(ints, intents) return res
#Creating GUI with tkinter
Soham Patel (12202120601046) 32
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
import tkinter from tkinter import * def send(): msg
= [Link]("1.0",'end-1c').strip()
[Link]("0.0",END) if msg != '':
[Link](state=NORMAL)
[Link](END, "You: " + msg + '\n\n')
[Link](foreground="#442265", font=("Verdana", 12 )) res =
chatbot_response(msg) [Link](END, "Bot: " + res + '\n\n')
[Link](state=DISABLED)
[Link](END) base = Tk()
[Link]("Hello") [Link]("400x500")
[Link](width=FALSE, height=FALSE)
#Create Chat window
ChatLog = Text(base, bd=0, bg="white", height="8",
width="50", font="Arial",)
[Link](state=DISABLED)
#Bind scrollbar to Chat window scrollbar =
Scrollbar(base, command=[Link], cursor="heart")
ChatLog['yscrollcommand'] = [Link]
#Create Button to send message
SendButton = Button(base, font=("Verdana",12,'bold'), text="Send", width="12",
height=5, bd=0, bg="#32de97", activebackground="#3c9d9b",fg='#ffffff', command=
send )
#Create the box to enter message
EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")
#[Link]("<Return>", send)
#Place all components on the screen [Link](x=376,y=6, height=386)
[Link](x=6,y=6, height=386, width=370)
[Link](x=128, y=401, height=90, width=265)
[Link](x=6, y=401, height=90) [Link]()
6. Run the chatbot
To run the chatbot, we have two main files; train_chatbot.py and
[Link]. First, we train the model using the command in the terminal:
python train_chatbot.py
Soham Patel (12202120601046) 33
ARTIFICIAL INTELLIGENCE AND
DATA SCIENCE
DEEP LEARNING AND APPLICATIONS
(202047804)
If we don’t see any error during training, we have successfully created the model.
Then to run the app, we run the second file. python [Link]
The program will open up a GUI window within a few seconds. With the GUI you
can easily chat with the bot.
Screenshots:
Soham Patel (12202120601046) 34