Professional Documents
Culture Documents
PRACTICAL JOURNAL OF
DEEP LEARNING
SUBJECT GUIDE
ANAND UPADHYAY
SUBMITTED BY
SHONA KANOJIYA
ROLL NO: 178
1
CERTIFICATE OF APPROVAL
Commerce”.
It is further certified that the student has completed all the required
_______________________
HEAD OF DEPARTMENT
_______________________ _______________________
PROFESSOR INCHARGE EXTERNAL EXAMINER
2
INDEX
3
Practical 01
Aim: Write a program to read and pre-process the data.
csv_file=df.to_csv(header=None)
print(csv_file)
4
3. Plot bar graph, pie chart, line diagram and scatter plot.
plt.bar(df["Width"],df['Height'])
plt.show()
plt.scatter(df["Width"],df['Height'])
plt.show()
plt.plot(df["Width"],df['Height'])
plt.show()
5
plt.pie(df['Height'])
plt.show()
import numpy as np
plt.bar(df["Width"], 0.4, label = 'Width')
plt.bar(df["Height"], 0.4, label = 'Height')
plt.legend()
plt.show()
6
low = [x * 0.01 for x in range(0, 10)]
low
7
Practical 02
Aim: Write a program to demonstrate various operation related to tensor, matrix and vector.
Source Code & Output:
1. Tensor operation.
import tensorflow as tf
import numpy as np
a=tf.constant([[1,2,3],[4,5,6],[7,8,9]])
a
b=tf.constant([[3,2,3],[4,5,6],[1,8,2]])
b
c=tf.add(a,b)
c
print(tf.matmul(a,b))
print(tf.reduce_max(a))
print(tf.reduce_max(b))
print(tf.math.argmax(b))
a=tf.ones([3,3])
a
print(tf.nn.softmax(a))
8
a=tf.zeros([3,3])
a
import tensorflow as tf
a=tf.range(3, 19, 3)
a
2. Matrix operation.
import numpy as np
mx1 = np.array([[5, 10], [15, 20]])
mx2 = np.array([[25, 30], [35, 40]])
9
print("Matrix1 =\n",mx1)
print("\nMatrix2 =\n",mx2)
print ("\nAddition of two matrices: ")
print (np.add(mx1,mx2))
print ("\nSubtraction of two matrices: ")
print (np.subtract(mx1,mx2))
print ("\nMatrix Division: ")
print (np.divide(mx1,mx2))
print ("\nMultiplication of two matrices: ")
print (np.multiply(mx1,mx2))
print ("\nThe summation of elements=")
print (np.sum(mx1))
print ("\nThe Transpose =")
print (mx1.T)
print("\ninner:", np.inner(mx1, mx2))
print("dot:", np.dot(mx1, mx2))
det = np.linalg.det(mx1)
print("\nDeterminant:", np.round(det))
rank = np.linalg.matrix_rank(mx1)
print("\nRank:", rank)
print("\nTrace:", mx1.trace())
print("Trace:", sum(mx1.diagonal()))
3. Vector operation.
import numpy as np
list1 = [10,20,30,40,50]
list2 = [11,12,13,14,15]
10
vtr1 = np.array(list1)
vtr2= np.array(list2)
print("vector1:")
print(vtr1)
print("vector 2:")
print(vtr2)
print("Addition of two vectors: ",vtr1+vtr2)
print("Subtraction of two vectors: ",vtr1-vtr2)
print("Multiplication of two vectors: ",vtr1*vtr2)
print("Division of two vectors: ",vtr1/vtr2)
print("Dot product of two vectors: ",vtr1.dot(vtr2) )
11
Practical 03
Aim: Write a program to implement multi perceptron using TensorFlow.
Source Code & Output:
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
gray_scale = 255
x_train /= gray_scale
x_test /= gray_scale
print("Feature matrix:", x_train.shape)
print("Target matrix:", x_test.shape)
12
model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(256,activation="sigmoid"))
model.add(keras.layers.Dense(128,activation="sigmoid"))
model.add(keras.layers.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)
13
Practical 04
Aim: Write a program to implement concept of convolution operation.
Source Code:
from keras.layers import Convolution2D, MaxPooling2D, Activation
from keras.models import Sequential
from numpy import asarray
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('/content/download.jpg',cv2.IMREAD_GRAYSCALE)
plt.imshow(img, cmap = 'gray')
plt.show()
img.shape
img_batch = img.reshape(1,img.shape[0], img.shape[1],1)
img_batch.shape
img_batch.shape[1:]
model1 = Sequential()
model1.add(Convolution2D(1,(15,15) , padding = 'valid', input_shape=img_batch.shape[1:]))
#random weight initilaization
model1.summary()
conv_img = model1.predict(img_batch)
conv_img.shape
conv_img_show = conv_img.reshape(conv_img.shape[1], conv_img.shape[2])
print(conv_img_show)
plt.imshow(conv_img_show, cmap = 'gray')
plt.show()
model2 = Sequential()
model2.add(Convolution2D(1, (15,15) , padding = 'valid', input_shape=img_batch.shape[1:])
14
) # random weight initilaization
model2.add(Activation('relu'))
conv_img = model2.predict(img_batch)
conv_img_show = conv_img.reshape(conv_img.shape[1], conv_img.shape[2])
plt.imshow(conv_img_show, cmap = 'gray')
plt.show()
model4 = Sequential()
model4.add(Convolution2D(1, (15,15) , padding = 'valid', input_shape=img_batch.shape[1:])
) # random weight initilaization model4.add(Activation('relu'))
model4.add(MaxPooling2D(pool_size=(2,2)))
model4.summary()
conv_img = model4.predict(img_batch)
conv_img_show = conv_img.reshape(conv_img.shape[1], conv_img.shape[2])
plt.imshow(conv_img_show, cmap = 'gray')
plt.show()
Output:
15
16
Practical 05
Aim: Write a program to implement convolution with various layers.
Source Code & Output:
import tensorflow as tf
from tensorflow import keras
tf.__version__
fashion_mnist=keras.datasets.fashion_mnist
print(fashion_mnist)
(x_train_full,y_train_full),(x_test,y_test)=fashion_mnist.load_data();
#x_train_full.shape
x_test.shape
x_validate,x_train=x_train_full[:5000]/255.0,x_train_full[5000:]/255.0
y_validate,y_train=y_train_full[:5000],y_train_full[5000:]
x_validate.shape
class_name=["T-
shirt/Top","Trouser","pullover","Dress","coat","sandal","shirt","sneaker","bag","Ankleboot"]
model=keras.models.Sequential([
keras.layers.Conv2D(64,7,activation="relu",padding="same",input_shape=[28,28,1]),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(128,3,activation="relu",padding="same"),
keras.layers.Conv2D(128,3,activation="relu",padding="same"),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(256,3,activation="relu",padding="same"),
keras.layers.Conv2D(256,3,activation="relu",padding="same"),
keras.layers.MaxPooling2D(2),
17
keras.layers.Flatten(),
keras.layers.Dense(128,activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(64,activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10,activation="softmax"),
])
model.summary()
model.layers[0]
hidden1=model.layers[1]
18
hidden1.get_weights()
model.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
history=model.fit(x_train,y_train,epochs=20,steps_per_epoch=5,validation_data=(x_validate,y_
validate))
19
Practical 06
Aim: Write a program to display and plot various performance related parameter for CNN.
Source Code & Output:
import pandas as pd
import matplotlib.pyplot as plt
plt.figure(figsize=(8,5))
plt.plot(pd.DataFrame(history.history))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
pd.DataFrame(history.history).plot(figsize=(8,5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show()
20
21
Practical 07
Aim: Write a program to implement RNN.
Source Code & Output:
import keras
model=keras.Sequential()
model.add(Dropout(0.2))
model.add(LSTM(128))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.compile( loss='sparse_categorical_crossentropy',optimizer='Adam',
metrics=['accuracy] )
22
Practical 08
Aim: Write a program to show role of different optimization algorithm for underfitting and
overfitting.
Source Code & Output:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
def true_fun(X):
return np.cos(1.5 * np.pi * X)
np.random.seed(0)
n_samples = 30
degrees = [1, 4, 15]
X = np.sort(np.random.rand(n_samples))
y = true_fun(X) + np.random.randn(n_samples) * 0.1
plt.figure(figsize=(14, 5))
for i in range(len(degrees)):
ax = plt.subplot(1, len(degrees), i + 1)
plt.setp(ax, xticks=(), yticks=())
polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False)
linear_regression = LinearRegression()
pipeline = Pipeline(
[
("polynomial_features", polynomial_features),
("linear_regression", linear_regression),
23
]
)
pipeline.fit(X[:, np.newaxis], y)
# Evaluate the models using crossvalidation
scores = cross_val_score(
pipeline, X[:, np.newaxis], y, scoring="neg_mean_squared_error", cv=10
)
X_test = np.linspace(0, 1, 100)
plt.plot(X_test, pipeline.predict(X_test[:, np.newaxis]), label="Model")
plt.plot(X_test, true_fun(X_test), label="True function")
plt.scatter(X, y, edgecolor="b", s=20, label="Samples")
plt.xlabel("x")
plt.ylabel("y")
plt.xlim((0, 1))
plt.ylim((-2, 2))
plt.legend(loc="best")
plt.title(
"Degree {}\nMSE = {:.2e}(+/- {:.2e})".format(
degrees[i], -scores.mean(), scores.std()
)
)
plt.show()
24
25
Practical 09
Aim: Write a program to implement deep neural network and save trained model for future use.
Source Code & Output:
Save Model:
model=keras.models.Sequential([
keras.layers.Conv2D(64,7,activation="relu",padding="same",input_shape=[28,28,1]),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(128,3,activation="relu",padding="same"),
keras.layers.Conv2D(128,3,activation="relu",padding="same"),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(256,3,activation="relu",padding="same"),
keras.layers.Conv2D(256,3,activation="relu",padding="same"),
keras.layers.MaxPooling2D(2),
keras.layers.Flatten(),
keras.layers.Dense(128,activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(64,activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(10,activation="softmax"),
])
model.save('/content/drive/MyDrive/modelCNN.h')
from google.colab import drive
drive.mount('/content/drive')
Loading the data:
import tensorflow as tf
tf.__version__
26
mod=tf.keras.models.load_model('/content/drive/MyDrive/model1.h')
mod.summary()
fashion_mnist=keras.datasets.fashion_mnist
print(fashion_mnist)
(x_train_full,y_train_full),(x_test,y_test)=fashion_mnist.load_data();
27
res=mod.evaluate(x_test,y_test,batch_size=64)
28
Practical 10
Aim: Write a program to implement deep neural network with various pretrained model.
Source Code & Output:
VGG Model:
import tensorflow as tf
fashion_mnist=keras.datasets.fashion_mnist
print(fashion_mnist)
(x_train_full,y_train_full),(x_test,y_test)=fashion_mnist.load_data();
base_model = VGG16(weights='imagenet')
model_vgg.summary()
29
model_vgg.compile(loss="sparse_categorical_crossentropy",
optimizer="sgd",
metrics=["accuracy"])
history=model_vgg.fit(x_train,y_train,epochs=20,validation_data=(x_validate,y_validate))
30
31