DEEP LEARNING LAB PROGRAMS
PROGRAM-1
Design and implement a neural based network for generating word embedding
for words in a document corpus.
# Word2Vec using Gensim
from [Link] import Word2Vec
# Sample corpus
sentences = [["deep", "learning", "is", "fun"],
["neural", "networks", "are", "powerful"],
["word", "embeddings", "capture", "semantics"]]
# Train Word2Vec model
model = Word2Vec(sentences, vector_size=50, window=3, min_count=1,
workers=4)
# Get embedding for a word
print("Vector for 'neural':\n", [Link]['neural'])
print("Most similar to 'learning':", [Link].most_similar('learning'))
OUTPUT:
Vector for 'neural':
[ 0.00855287 0.00015212 -0.01916856 -0.01933109 -0.01229639 -0.00025714
0.00399483 0.01886394 0.0111687 -0.00858139 0.00055663 0.00992872
0.01539662 -0.00228845 0.00864684 -0.01162876 -0.00160838 0.0162001
-0.00472013 -0.01932691 0.01155852 -0.00785964 -0.00244575 0.01996103
-0.0045127 -0.00951413 -0.01065877 0.01396178 -0.01141774 0.00422733
-0.01051132 0.01224143 0.00871461 0.00521271 -0.00298217 -0.00549213
0.01798587 0.01043155 -0.00432504 -0.01894062 -0.0148521 -0.00212748
-0.00158989 -0.00512582 0.01936544 -0.00091704 0.01174752 -0.01489517
-0.00501215 -0.01109973]
Most similar to 'learning': [('fun', 0.2373521476984024), ('networks',
0.16944655776023865), ('embeddings', 0.12119622528553009), ('deep',
0.06516239047050476), ('semantics', -0.012591083534061909), ('word', -
0.023209022358059883), ('neural', -0.039671964943408966), ('are', -
0.08306728303432465), ('powerful', -0.0934436097741127), ('is', -
0.11219381541013718
PROGRAM-02
Write a program to demonstrate the working of a deep neural network for
classification task.
import tensorflow as tf
from [Link] import layers, models
from [Link] import load_digits
from sklearn.model_selection import train_test_split
from [Link] import LabelBinarizer
# Load dataset
digits = load_digits()
X = [Link]
y = LabelBinarizer().fit_transform([Link])
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Build DNN model
model = [Link]([
[Link](128, activation='relu', input_shape=(64,)),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
# Compile & Train
[Link](optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=10, batch_size=32, verbose=1)
# Evaluate
print("Accuracy:", [Link](X_test, y_test)[1])
OUTPUT:
Accuracy: 0.975000023841857
PROGRAM-03
Design and implement a Convolutional Neural Network (CNN) for classification
of image dataset.
import tensorflow as tf
from [Link] import datasets, layers, models
# Load CIFAR-10 dataset
(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
X_train, X_test = X_train/255.0, X_test/255.0
# Build CNN
model = [Link]([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)),
[Link](),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
# Compile & Train
[Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=5, validation_data=(X_test, y_test))
OUTPUT:
Epoch 1/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 8s 4ms/step - accuracy: 0.4665 - loss: 1.4825 -
val_accuracy: 0.5692 - val_loss: 1.2239
Epoch 2/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.6062 - loss: 1.1214 -
val_accuracy: 0.6256 - val_loss: 1.0780
Epoch 3/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 7s 4ms/step - accuracy: 0.6573 - loss: 0.9911 -
val_accuracy: 0.6662 - val_loss: 0.9674
Epoch 4/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 8s 5ms/step - accuracy: 0.6854 - loss: 0.9090 -
val_accuracy: 0.6711 - val_loss: 0.9510
Epoch 5/5
1563/1563 ━━━━━━━━━━━━━━━━━━━━ 8s 5ms/step - accuracy: 0.7109 - loss: 0.8384 -
val_accuracy: 0.6806 - val_loss: 0.9302
<[Link] at 0x1f4566906e0
PROGRAM-04
Build and demonstrate an autoencoder network using neural layers for data
compression on image dataset.
import tensorflow as tf
from [Link] import layers, models, datasets
# Load MNIST
(x_train, _), (x_test, _) = [Link].load_data()
x_train, x_test = x_train/255.0, x_test/255.0
x_train = x_train.reshape((len(x_train), 28*28))
x_test = x_test.reshape((len(x_test), 28*28))
# Build Autoencoder
input_dim = 784
encoding_dim = 64
input_layer = [Link](shape=(input_dim,))
encoded = [Link](encoding_dim, activation='relu')(input_layer)
decoded = [Link](input_dim, activation='sigmoid')(encoded)
autoencoder = [Link](input_layer, decoded)
[Link](optimizer='adam', loss='mse')
# Train
[Link](x_train, x_train, epochs=10, batch_size=256, shuffle=True,
validation_data=(x_test, x_test))
OUTPUT:
Epoch 1/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 3s 7ms/step - loss: 0.0594 - val_loss: 0.0310
Epoch 2/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - loss: 0.0256 - val_loss: 0.0206
Epoch 3/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 6ms/step - loss: 0.0181 - val_loss: 0.0151
Epoch 4/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0136 - val_loss: 0.0117
Epoch 5/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0107 - val_loss: 0.0093
Epoch 6/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0087 - val_loss: 0.0077
Epoch 7/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0073 - val_loss: 0.0066
Epoch 8/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0064 - val_loss: 0.0059
Epoch 9/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0058 - val_loss: 0.0054
Epoch 10/10
235/235 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.0053 - val_loss: 0.0050
<[Link] at 0x1e88d8eb6e0>
PROGRAM-05
Design and implement a deep learning network for classification of textual
documents.
import numpy as np
from [Link] import layers,
models
# Sample data - integers for embedding
input
X = [Link]([
[0, 0, 0, 0, 0, 0, 1, 2, 3, 4],
[0, 0, 0, 0, 0, 0, 5, 6, 7, 8],
[0, 0, 0, 0, 0, 0, 9, 10, 11, 12],
[0, 0, 0, 0, 0, 0, 0, 1, 13, 14]
],
dtype='int32')
labels = [Link]([0, 1, 0, 1],
dtype='float32')
model = [Link]([
[Link](1000, 32,
input_length=10),
[Link](),
[Link](10, activation='relu'),
[Link](1, activation='sigmoid')
])
[Link](optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
[Link](X, labels, epochs=10,
verbose=1)
OUTPUT:
Epoch 1/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 604ms/step - accuracy: 0.5000 - loss: 0.6866
Epoch 2/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step - accuracy: 0.5000 - loss: 0.6807
Epoch 3/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step - accuracy: 0.5000 - loss: 0.6768
Epoch 4/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step - accuracy: 0.5000 - loss: 0.6732
Epoch 5/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step - accuracy: 0.5000 - loss: 0.6689
Epoch 6/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step - accuracy: 0.5000 - loss: 0.6643
Epoch 7/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 35ms/step - accuracy: 0.7500 - loss: 0.6597
Epoch 8/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step - accuracy: 0.7500 - loss: 0.6553
Epoch 9/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 37ms/step - accuracy: 0.7500 - loss: 0.6506
Epoch 10/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 34ms/step - accuracy: 0.7500 - loss: 0.6459
<[Link] at 0x21808ac4c80>
PROGRAM-06
Design and implement a deep learning network for forecasting time series data.
import numpy as np
import tensorflow as tf
from [Link] import layers, models
# Sample sine wave dataset
t = [Link](0, 100, 1000)
data = [Link](t)
# Prepare sequences
def create_sequences(data, seq_length=20):
X, y = [], []
for i in range(len(data)-seq_length):
[Link](data[i:i+seq_length])
[Link](data[i+seq_length])
return [Link](X), [Link](y)
X, y = create_sequences(data)
X = [Link](([Link][0], [Link][1], 1))
# RNN model
model = [Link]([
[Link](50, activation='tanh', input_shape=(20,1)),
[Link](1)
])
[Link](optimizer='adam', loss='mse')
[Link](X, y, epochs=10, batch_size=32)
[Link](data[i+seq_length]) return
[Link](X), [Link](y)
X, y = create_sequences(data)
X = [Link](([Link][0], [Link][1], 1))
# RNN model
model = [Link]([
[Link](50, activation='tanh', input_shape=(20,1)),
[Link](1)
])
[Link](optimizer='adam', loss='mse') [Link](X, y, epochs=10,
batch_size=32)
OUTPUT:
31/31 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - loss: 0.1946
Epoch 2/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.0055
Epoch 3/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 8.9386e-04
Epoch 4/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 6.0025e-04
Epoch 5/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 4.7499e-04
Epoch 6/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 3.7981e-04
Epoch 7/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 2.9688e-04
Epoch 8/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 2.2571e-04
Epoch 9/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 1.8218e-04
Epoch 10/10
31/31 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 1.4676e-04
<[Link] at 0x17cb3429250>
PROGRAM-07
Write a program to enable pre-train models to classify a given image dataset.
import tensorflow as tf
from [Link] import MobileNetV2
from [Link] import image
import numpy as np
# Load pre-trained model
model = MobileNetV2(weights='imagenet')
# Load an image
img = image.load_img("[Link]", target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = [Link].mobilenet_v2.preprocess_input(x)
# Predict
preds = [Link](x)
decoded = [Link].mobilenet_v2.decode_predictions(preds,
top=3)[0]
print("Predictions:", decoded)
OUTPUT:
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 576ms/step
Predictions: [('n02099601', 'golden_retriever', 0.81162447), ('n02099712',
'Labrador_retriever', 0.028772647), ('n02108551', 'Tibetan_mastiff', 0.027160227)
PROGRAM-08
Write a program to read a dataset of text reviews. Classify the reviews as
positive or negative.
import tensorflow as tf
from [Link] import layers, models, datasets
# Load IMDB dataset
(X_train, y_train), (X_test, y_test) = [Link].load_data(num_words=10000)
# Pad sequences
X_train = [Link].pad_sequences(X_train, maxlen=200)
X_test = [Link].pad_sequences(X_test, maxlen=200)
# Build model
model = [Link]([
[Link](10000, 64, input_length=200),
[Link](64),
[Link](1, activation='sigmoid')
])
[Link](optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=3, batch_size=64,
validation_data=(X_test, y_test))
OUTPUT:
Epoch 1/3
391/391 ━━━━━━━━━━━━━━━━━━━━ 32s 80ms/step - accuracy: 0.8028 - loss: 0.4237 -
val_accuracy: 0.8630 - val_loss: 0.3513
Epoch 2/3
391/391 ━━━━━━━━━━━━━━━━━━━━ 27s 69ms/step - accuracy: 0.8972 - loss: 0.2617 -
val_accuracy: 0.8516 - val_loss: 0.3411
Epoch 3/3
391/391 ━━━━━━━━━━━━━━━━━━━━ 26s 67ms/step - accuracy: 0.9283 - loss: 0.1897 -
val_accuracy: 0.8647 - val_loss: 0.3197
<[Link] at 0x1b114db16a0>