0% found this document useful (0 votes)
54 views42 pages

Deep Learning Experiments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views42 pages

Deep Learning Experiments

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

EXPERIMRNT NO:- 01

Aim:- Implementing feedforward neural networks with keras and tensorflow.


Program:
# import the necessary packages

from sklearn.preprocessing import LabelBinarizer


from sklearn.metrics import classification_report
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
from tensorflow.keras import backend as K
import matplotlib.pyplot as plt
import numpy as np
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
help="path to the output loss/accuracy plot")
args = vars(ap.parse_args())

# grab the MNIST dataset (if this is your first time using
this # dataset then the 11MB download may take a
minute) print("[INFO] accessing MNIST...")
((trainX, trainY), (testX, testY)) = mnist.load_data()
# each image in the MNIST dataset is represented as a 28x28x1

# image, but in order to apply a standard neural network we must


# first "flatten" the image to be simple list of 28x28=784 pixels
trainX = trainX.reshape((trainX.shape[0], 28 * 28 * 1))
testX = testX.reshape((testX.shape[0], 28 * 28 * 1))
# scale data to the range of [0, 1]
trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0
# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
0: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]

2: [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
3: [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
4: [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
5: [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
6: [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]

7: [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
8: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
9: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
# define the 784-256-128-10 architecture using Keras
model = Sequential()
model.add(Dense(256, input_shape=(784,), activation="sigmoid"))
model.add(Dense(128, activation="sigmoid"))
model.add(Dense(10, activation="softmax"))

# train the model using SGD


print("[INFO] training network...")
sgd = SGD(0.01)
model.compile(loss="categorical_crossentropy", optimizer=sgd,
metrics=["accuracy"])
H = model.fit(trainX, trainY, validation_data=(testX, testY),
epochs=100, batch_size=128)

# evaluate the network


print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size=128)
print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1),
target_names=[str(x) for x in lb.classes_]))
# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, 100), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, 100), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.savefig(args["output"])
OUTPUT:-
$ python keras_mnist.py --output output/keras_mnist.png
[INFO] loading MNIST (full) dataset...
[INFO] training network...
Train on 52500 samples, validate on 17500
samples Epoch 1/100
1s - loss: 2.2997 - acc: 0.1088 - val_loss: 2.2918 - val_acc: 0.1145
Epoch 2/100
1s - loss: 2.2866 - acc: 0.1133 - val_loss: 2.2796 - val_acc: 0.1233
Epoch 3/100
1s - loss: 2.2721 - acc: 0.1437 - val_loss: 2.2620 - val_acc: 0.1962
...

Epoch 98/100
1s - loss: 0.2811 - acc: 0.9199 - val_loss: 0.2857 - val_acc: 0.9153
Epoch 99/100
1s - loss: 0.2802 - acc: 0.9201 - val_loss: 0.2862 - val_acc: 0.9148
Epoch 100/100
1s - loss: 0.2792 - acc: 0.9204 - val_loss: 0.2844 - val_acc: 0.9160
[INFO] evaluating network...
precision recall f1-score support
0.0 0.94 0.96 0.95 1726
1.0 0.95 0.97 0.96 2004

2.0 0.91 0.89 0.90 1747


3.0 0.91 0.88 0.89 1828
4.0 0.91 0.93 0.92 1686
5.0 0.89 0.86 0.88 1581
6.0 0.92 0.96 0.94 1700

7.0 0.92 0.94 0.93 1814


8.0 0.88 0.88 0.88 1679
9.0 0.90 0.88 0.89 1735
avg / total 0.92 0.92 0.92 17500

EXPERIMENT NO:-02
Aim:- Applying the convolutional Neural Network on computer vision problem.
Program:-
import pandas as
pd import numpy as
np import cPickle
#Define a function to load each batch as dictionary:
def unpickle(file):
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
#Make dictionaries by calling the above function:
batch1 = unpickle('data/data_batch_1')
batch2 = unpickle('data/data_batch_2')
batch3 = unpickle('data/data_batch_3')
batch4 = unpickle('data/data_batch_4')
batch5 = unpickle('data/data_batch_5')
batch_test = unpickle('data/test_batch')

#Define a function to convert this dictionary into dataframe with image pixel array and labels:
def get_dataframe(batch):
df = pd.DataFrame(batch['data'])
df['image'] = df.as_matrix().tolist()
df.drop(range(3072),axis=1,inplace=True)
df['label'] = batch['labels']
return df
#Define train and test files:
train =
pd.concat([get_dataframe(batch1),get_dataframe(batch2),get_dataframe(batch3),get_dataframe(batch4),g
et_dataframe(batch5)],ignore_index=True)
test = get_dataframe(batch_test)

# We can verify this data by looking at the head and shape of data as
follow: print train.head()
OUTPUT:-

print
train.shape, test.shape
OUTPUT:-

import graphlab as gl
gltrain =
gl.SFrame(train) gltest =
gl.SFrame(test)
model = gl.neuralnet_classifier.create(gltrain, target='label', validation_set=None)

OUTPUT:-
model.evaluate(gltest)
OUTPUT:

#Convert pixels to graphlab image format


gltrain['glimage'] = gl.SArray(gltrain['image']).pixel_array_to_image(32, 32, 3, allow_rounding = True)
gltest['glimage'] = gl.SArray(gltest['image']).pixel_array_to_image(32, 32, 3, allow_rounding = True)

#Remove the original column


gltrain.remove_column('image')
gltest.remove_column('image')
gltrain.head()

OUTPUT:-
#Convert into 256x256 size
gltrain['image'] =
gl.image_analysis.resize(gltrain['glimage'], 256, 256, 3)
gltest['image'] = gl.image_analysis.resize(gltest['glimage'], 256, 256, 3)
#Remove old column:
gltrain.remove_column('glimage')
gltest.remove_column('glimage')
gltrain.head()
OUTPUT:-

#Load the pre-trained model:


pretrained_model = gl.load_model('http://s3.amazonaws.com/GraphLab-
Datasets/deeplearning/imagenet_model_iter45')
gltrain['features'] = pretrained_model.extract_features(gltrain)
gltest['features'] = pretrained_model.extract_features(gltest)
gltrain.head()

simple_classifier =

graphlab.classifier.create(gltrain, features = ['features'], target = 'label')


The various outputs are:-

1. Boosted Trees Classifier


2. Random Forest Classifier

3. Decision Tree Classifier

4. Logistic Regression Classifier


The final model selection is based on a validation set with 5% of the data. The
results are:
OUTPUT:-

simple_classifier.evaluate(gltest)
OUTPUT:-
EXPERIMENT NO:-03
AIM:- Image Classification on MNIST dataset.(CNN model with Fully connected
layer.)
Program:

1. Import packages:

import tensorflow as tf

from tensorflow import

keras import numpy as np

import matplotlib.pyplot as plt

from tensorflow.keras.preprocessing.image import load_img, img_to_array

import cv2

np.random.seed(42) # This allows us to reproduce the results from our script

from keras.models import Sequential

from keras.layers import Dense,

Activation from keras.optimizers import

Adam, SGD

from keras.utils import np_utils # help us to transform our data later

2. Load dataset:-

This is a dataset of 60,000 28x28 grayscale images of the 10 digits, along with a test set of 10,000 images.

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()

#Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-#datasets/mnist.npz

#11493376/11490434 [==============================] - 0s 0us/step

#Checkout input dataset

print('Total no of Images: ',X_train.shape[0])

print('Size of Image:', X_train.shape[1:])

print('Total no of labels:', y_train.shape)


output:

total no of Images: 60000

Size of Image: (28, 28)

Total no of labels:

(60000,)

#look input data

plt.imshow(X_train[0], cmap = plt.get_cmap('gray')) # cmap - convert image into grascale

print('Label:', y_train[0])

Lable 5

3. Prepare input data

X_train = X_train.reshape((X_train.shape[0],-1))X_test = X_test.reshape((X_test.shape[0], -1))

X_train = X_train.astype('float32')

X_test = X_test.astype('float32')

print(X_train.shape, X_test.shape)

output:

(60000, 784) (10000, 784)

X_train = X_train/255

X_test = X_test/255

# print(X_train[0])

X_train.shape

Output:

(60000, 784)
# One-hot encoding

y_train = np_utils.to_categorical(y_train)

y_test = np_utils.to_categorical(y_test)

print(y_train.shape)

output:

(60000, 10)

In [8]:

num_classes = y_test.shape[1]

num_pixels = 784

In [9]:

# define baseline model

def baseline_model():

# create model

model = Sequential()

model.add(Dense(256, input_dim=num_pixels, activation='relu'))

model.add(Dense(64, activation='relu'))

model.add(Dense(num_classes, activation='softmax'))

return model

In [10]:

# build the model

model = baseline_model()

model.summary()

out:

Model: "sequential"

Layer (type) Output Shape Param #

=================================================================

dense (Dense) (None, 256) 200960

dense_1 (Dense) (None, 64) 16448


dense_2 (Dense) (None, 10) 650

=================================================================

Total params: 218,058

Trainable params: 218,058

Non-trainable params: 0

In [11]:

opt = SGD(lr = 0.001)

model.compile(loss='categorical_crossentropy', optimizer= opt, metrics=['accuracy'])

5. Train Model

In [12]:

model.fit(X_train, y_train, epochs=5, batch_size=32, verbose=1)

out:

Epoch 1/5

1875/1875 [==============================] - 4s 2ms/step - loss: 1.6834 - accuracy: 0.5609

Epoch 2/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.8661 - accuracy: 0.7995

Epoch 3/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.5972 - accuracy: 0.8497

Epoch 4/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.4890 - accuracy: 0.8709

Epoch 5/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.4314 - accuracy: 0.8838

<tensorflow.python.keras.callbacks.History at 0x7f0d68b03c10>

6. Test Model

In [13]:
scores = model.evaluate(X_test, y_test, verbose=1)
print("Error: %.2f%%" % (100-scores[1]*100))
313/313 [==============================] - 0s 1ms/step - loss: 0.3892 - accuracy: 0.8957

Error: 10.43%

Predict In [14]:
img_width, img_height = 28, 28

In [15]:

ii = cv2.imread("../input/mnistpredict/3.png")

gray_image = cv2.cvtColor(ii, cv2.COLOR_BGR2GRAY)

# print(gray_image)

plt.imshow(gray_image,cmap='Greys')

plt.show()

# gray_image.shape

x = np.expand_dims(gray_image, axis=0)

x = x.reshape((1, -1))

In [16]:

preds = model.predict_classes(x)

prob = model.predict_proba(x)

print('Predicted value is ',preds[0])

print('Probability across all numbers :', prob)

Predicted value is 3

Probability across all numbers : [[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]


Experiment – 4
Aim: Applying the autoencoder algorithms for encoding the real world data.

import matplotlib.pyplot

as plt import numpy as

np

import pandas as
pd import
tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score,

recall_score from sklearn.model_selection import train_test_split

from keras import layers,


losses from keras.datasets
import mnist from
keras.models import Model

(x_train, _), (x_test, _) = mnist.load_data()

x_train =
x_train.astype('float32') / 255.
x_test = x_test.astype('float32') /
255.

print("Shape of the training data:",


x_train.shape) print("Shape of the testing
data:", x_test.shape)

# Definition of the Autoencoder model as a subclass of the TensorFlow Model class

class SimpleAutoencoder(Model):
def init (self,latent_dimensions ,
data_shape):
super(SimpleAutoencoder, self). init
() self.latent_dimensions =
latent_dimensions self.data_shape =
data_shape

# Encoder architecture using a Sequential


model self.encoder = tf.keras.Sequential([
layers.Flatten(),

layers.Dense(latent_dimensions, activation='relu'),
])

# Decoder architecture using another

Sequential model self.decoder =

tf.keras.Sequential([

layers.Dense(tf.math.reduce_prod(data_shape), activation='sigmoid'),
layers.Reshape(data_shape)
])

def call(self, input_data):


encoded_data =
self.encoder(input_data) decoded_data
= self.decoder(encoded_data) return
decoded_data

input_data_shape = x_test.shape[1:]

# Specifying the dimensionality of the


latent space latent_dimensions = 64

# Creating an instance of the SimpleAutoencoder model


simple_autoencoder = SimpleAutoencoder(latent_dimensions, input_data_shape)

simple_autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())

simple_autoencoder.fit(x_train, x_train,
epochs=1,
shuffle=Tru
e,
validation_data=(x_test, x_test))
encoded_imgs = simple_autoencoder.encoder(x_test).numpy()
decoded_imgs =
simple_autoencoder.decoder(encoded_imgs).numpy()

n=6

plt.figure(figsize=(8,
4)) for i in range(n):
# display original
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test
[i])
plt.title("original"
) plt.gray()

# display reconstruction
ax = plt.subplot(2, n, i + 1
+ n)
plt.imshow(decoded_imgs
[i])
plt.title("reconstructed")
plt.gray()

plt.show()
Shape of the training data: (60000, 28, 28)
Shape of the testing data: (10000, 28,
28) Epoch 1/10
1875/1875 - 6ms/step - loss: 0.0243- val_loss:
[==============================] 12s
0.0091
Epoch 2/10
1875/1875 - 9ms/step - loss: 0.0069- val_loss:
[==============================] 16s
0.0054
Epoch 3/10
1875/1875 - 8ms/step - loss: 0.0051- val_loss:
[==============================] 15s
0.0046
Epoch 4/10
1875/1875 - 8s 5ms/step - loss: 0.0045 - val_loss:
[==============================]
0.0043
Epoch 5/10
1875/1875 - 8s 4ms/step - loss: 0.0043 - val_loss:
[==============================]
0.0041
Epoch 6/10
1875/1875 - 9s 5ms/step - loss: 0.0042 - val_loss:
[==============================]
0.0041
Epoch 7/10
1875/1875 - 7s 4ms/step - loss: 0.0041 - val_loss:
[==============================]
0.0040
EXPERIMENT NO:– 5
Aim: Applying Generative Adversial Networks for image generation And unsupervised
tasks.
Program:

import torch

import torch.nn as nn

import torch.optim as optim

import torchvision

from torchvision import datasets, transforms

import matplotlib.pyplot as plt

import numpy as np

# Set device

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Define a basic transform

transform =

transforms.Compose([ transfor

ms.ToTensor(),

transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

])

train_dataset = datasets.CIFAR10(root='./data',\

train=True, download=True, transform=transform)

dataloader = torch.utils.data.DataLoader(train_dataset, \

batch_size=32, shuffle=True)

# Hyperparameters

latent_dim = 100

lr = 0.0002

beta1 = 0.5

beta2 = 0.999

num_epochs = 10

# Define the generator

class Generator(nn.Module):
def init (self, latent_dim):

super(Generator, self). init ()

self.model = nn.Sequential(

nn.Linear(latent_dim, 128 * 8 * 8),

nn.ReLU(),

nn.Unflatten(1, (128, 8, 8)),

nn.Upsample(scale_factor=2),

nn.Conv2d(128, 128, kernel_size=3, padding=1),

nn.BatchNorm2d(128, momentum=0.78),

nn.ReLU(),

nn.Upsample(scale_factor=2),

nn.Conv2d(128, 64, kernel_size=3, padding=1),

nn.BatchNorm2d(64, momentum=0.78),

nn.ReLU(),

nn.Conv2d(64, 3, kernel_size=3, padding=1),

nn.Tanh()

def forward(self, z):

img = self.model(z)

return img

# Define the discriminator

class Discriminator(nn.Module):

def init (self):

super(Discriminator, self). init ()

self.model = nn.Sequential(

nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),

nn.LeakyReLU(0.2),

nn.Dropout(0.25),

nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),

nn.ZeroPad2d((0, 1, 0, 1)),
nn.BatchNorm2d(64, momentum=0.82),

nn.LeakyReLU(0.25),

nn.Dropout(0.25),

nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),

nn.BatchNorm2d(128, momentum=0.82),

nn.LeakyReLU(0.2),

nn.Dropout(0.25),

nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),

nn.BatchNorm2d(256, momentum=0.8),

nn.LeakyReLU(0.25),

nn.Dropout(0.25),

nn.Flatten(),

nn.Linear(256 * 5 * 5, 1),

nn.Sigmoid()

def forward(self, img):

validity =

self.model(img) return

validity

# Define the generator and discriminator

# Initialize generator and discriminator

generator = Generator(latent_dim).to(device)

discriminator = Discriminator().to(device)

# Loss function

adversarial_loss = nn.BCELoss()

# Optimizers

optimizer_G = optim.Adam(generator.parameters()\

, lr=lr, betas=(beta1, beta2))

optimizer_D = optim.Adam(discriminator.parameters()\

, lr=lr, betas=(beta1, beta2))

# Training loop

for epoch in range(num_epochs):


for i, batch in enumerate(dataloader):

# Convert list to tensor

real_images =

batch[0].to(device) # Adversarial

ground truths

valid = torch.ones(real_images.size(0), 1,

device=device) fake = torch.zeros(real_images.size(0), 1,

device=device) # Configure input

real_images = real_images.to(device)

# Train Discriminator

optimizer_D.zero_grad()

# Sample noise as generator input

z = torch.randn(real_images.size(0), latent_dim, device=device)

# Generate a batch of images

fake_images = generator(z)

# Measure discriminator's ability

# to classify real and fake images

real_loss = adversarial_loss(discriminator\

(real_images), valid)

fake_loss = adversarial_loss(discriminator\

(fake_images.detach()), fake)

d_loss = (real_loss + fake_loss) / 2

# Backward pass and optimize

d_loss.backward()

optimizer_D.step()

# Train Generator

optimizer_G.zero_grad()
# Generate a batch of

images gen_images =

generator(z)

# Adversarial loss

g_loss = adversarial_loss(discriminator(gen_images), valid)

# Backward pass and optimize

g_loss.backward()

optimizer_G.step()

# Progress Monitoring

if (i + 1) % 100 == 0:

print(

f"Epoch [{epoch+1}/{num_epochs}]\

Batch {i+1}/{len(dataloader)} "

f"Discriminator Loss: {d_loss.item():.4f} "

f"Generator Loss: {g_loss.item():.4f}"

# Save generated images for every epoch

if (epoch + 1) % 10 == 0:

with torch.no_grad():

z = torch.randn(16, latent_dim, device=device)

generated = generator(z).detach().cpu()

grid = torchvision.utils.make_grid(generated,\

nrow=4, normalize=True)

plt.imshow(np.transpose(grid, (1, 2, 0)))

plt.axis("off")

plt.show()

Output:

Epoch [10/10] Batch 1300/1563 Discriminator Loss: 0.4473 Generator Loss:


0.9555 Epoch [10/10] Batch 1400/1563 Discriminator Loss: 0.6643 Generator Loss:
1.0215 Epoch [10/10] Batch 1500/1563 Discriminator Loss: 0.4720 Generator Loss:
2.5027
EXPERIMENT:-06
AIM: Write a python program to built Recurrent Neural Network.
Program:
(number_of_records x length_of_sequence x types_of_sequences)

(number_of_records x types_of_sequences) #where types_of_sequences is 1)

import math

sin_wave = np.array([math.sin(x) for x in np.arange(200)])

plt.plot(sin_wave[:50])

X_val = []

Y_val = []

for i in range(num_records - 50, num_records):

X_val.append(sin_wave[i:i+seq_len])

Y_val.append(sin_wave[i+seq_len])

X_val = np.array(X_val)

X_val = np.expand_dims(X_val, axis=2)

Y_val = np.array(Y_val)

Y_val = np.expand_dims(Y_val, axis=1)

Step 1: Create the Architecture for our RNN model


learning_rate = 0.0001

nepoch = 25

T = 50 # length of sequence

hidden_dim = 100

output_dim = 1

bptt_truncate = 5

min_clip_value = -10

max_clip_value = 10

#define the weights of the network:


U = np.random.uniform(0, 1, (hidden_dim, T))

W = np.random.uniform(0, 1, (hidden_dim, hidden_dim))

V = np.random.uniform(0, 1, (output_dim, hidden_dim))


#define the activation function, sigmoid, to be used in the hidden layer:
def sigmoid(x):

return 1 / (1 + np.exp(-x))

Step 2: Train the Model


Step 2.1: Check the loss on training data
for epoch in range(nepoch):

# check loss on train

loss = 0.0

# do a forward pass to get

prediction for i in range(Y.shape[0]):

x, y = X[i], Y[i] # get input, output values of each record

prev_s = np.zeros((hidden_dim, 1)) # here, prev-s is the value of the previous activation of hidden layer; which
is initialized as all zeroes

for t in range(T):

new_input = np.zeros(x.shape) # we then do a forward pass for every timestep in the sequence

new_input[t] = x[t] # for this, we define a single input for that timestep

mulu = np.dot(U, new_input)

mulw = np.dot(W, prev_s)

add = mulw + mulu

s = sigmoid(add)

mulv = np.dot(V,

s) prev_s = s

# calculate error

loss_per_record = (y - mulv)**2 / 2

loss += loss_per_record

loss = loss / float(y.shape[0])

Step 2.2: Check the loss on validation data


# check loss on val

val_loss = 0.0

for i in range(Y_val.shape[0]):
x, y = X_val[i], Y_val[i]

prev_s = np.zeros((hidden_dim, 1))

for t in range(T):

new_input = np.zeros(x.shape)

new_input[t] = x[t]

mulu = np.dot(U, new_input)

mulw = np.dot(W, prev_s)

add = mulw + mulu

s = sigmoid(add)

mulv = np.dot(V,

s) prev_s = s

loss_per_record = (y - mulv)**2 / 2

val_loss += loss_per_record

val_loss = val_loss / float(y.shape[0])

print('Epoch: ', epoch + 1, ', Loss: ', loss, ', Val Loss: ', val_loss)

You should get the below output:


Epoch: 1 , Loss: [[101185.61756671]] , Val Loss: [[50591.0340148]]
...

...

Step 2.3: Start actual training


Step 2.3.1: Forward Pass
# train model

for i in

range(Y.shape[0]): x, y

= X[i], Y[i]

layers = []

prev_s = np.zeros((hidden_dim, 1))

dU = np.zeros(U.shape)

dV = np.zeros(V.shape)

dW =
np.zeros(W.shape)
dU_t = np.zeros(U.shape)

dV_t = np.zeros(V.shape)

dW_t =

np.zeros(W.shape)

dU_i = np.zeros(U.shape)

dW_i =

np.zeros(W.shape)

# forward pass

for t in

range(T):

new_input = np.zeros(x.shape)

new_input[t] = x[t]

mulu = np.dot(U, new_input)

Step 2.3.2 : Backpropagate Error


# derivative of pred

dmulv = (mulv - y)

# backward pass

for t in

range(T):

dV_t = np.dot(dmulv, np.transpose(layers[t]['s']))

dsv = np.dot(np.transpose(V), dmulv)

ds = dsv

dadd = add * (1 - add) * ds

dmulw = dadd * np.ones_like(mulw)

dprev_s = np.dot(np.transpose(W), dmulw)

for i in range(t-1, max(-1, t-bptt_truncate-1), -1):

ds = dsv + dprev_s

dadd = add * (1 - add) * ds

dmulw = dadd * np.ones_like(mulw)


dmulu = dadd * np.ones_like(mulu)

dW_i = np.dot(W, layers[t]['prev_s'])


dprev_s = np.dot(np.transpose(W), dmulw)

new_input = np.zeros(x.shape)

new_input[t] = x[t]

dU_i = np.dot(U, new_input)

dx = np.dot(np.transpose(U), dmulu)

dU_t += dU_i

dW_t += dW_i

dV += dV_t

dU += dU_t

dW += dW_t

Step 2.3.3 : Update weights


if dU.max() > max_clip_value:

dU[dU > max_clip_value] = max_clip_value

if dV.max() > max_clip_value:

dV[dV > max_clip_value] = max_clip_value

if dW.max() > max_clip_value:

dW[dW > max_clip_value] = max_clip_value

if dU.min() < min_clip_value:

dU[dU < min_clip_value] = min_clip_value

if dV.min() < min_clip_value:

dV[dV < min_clip_value] = min_clip_value

if dW.min() < min_clip_value:

dW[dW < min_clip_value] = min_clip_value

# update

U -= learning_rate * dU

V -= learning_rate * dV

W -= learning_rate * dW
On training the above model, we get this output:
Epoch: 1 , Loss: [[101185.61756671]] , Val Loss: [[50591.0340148]]
Epoch: 2 , Loss: [[61205.46869629]] , Val Loss: [[30601.34535365]]
Epoch: 3 , Loss: [[31225.3198258]] , Val Loss: [[15611.65669247]]
Epoch: 4 , Loss: [[11245.17049551]] , Val Loss: [[5621.96780111]]
Epoch: 5 , Loss: [[1264.5157739]] , Val Loss: [[632.02563908]]
Epoch: 6 , Loss: [[20.15654115]] , Val Loss: [[10.05477285]]
Epoch: 7 , Loss: [[17.13622839]] , Val Loss: [[8.55190426]]
Epoch: 8 , Loss: [[17.38870495]] , Val Loss: [[8.68196484]]
Epoch: 9 , Loss: [[17.181681]] , Val Loss: [[8.57837827]]
Epoch: 10 , Loss: [[17.31275313]] , Val Loss: [[8.64199652]]
Epoch: 11 , Loss: [[17.12960034]] , Val Loss: [[8.54768294]]
Epoch: 12 , Loss: [[17.09020065]] , Val Loss: [[8.52993502]]
Epoch: 13 , Loss: [[17.17370113]] , Val Loss: [[8.57517454]]
Epoch: 14 , Loss: [[17.04906914]] , Val Loss: [[8.50658127]]
Epoch: 15 , Loss: [[16.96420184]] , Val Loss: [[8.46794248]]
Epoch: 16 , Loss: [[17.017519]] , Val Loss: [[8.49241316]]
Epoch: 17 , Loss: [[16.94199493]] , Val Loss: [[8.45748739]]
Epoch: 18 , Loss: [[16.99796892]] , Val Loss: [[8.48242177]]
Epoch: 19 , Loss: [[17.24817035]] , Val Loss: [[8.6126231]]
Epoch: 20 , Loss: [[17.00844599]] , Val Loss: [[8.48682234]]
Epoch: 21 , Loss: [[17.03943262]] , Val Loss: [[8.50437328]]
Epoch: 22 , Loss: [[17.01417255]] , Val Loss: [[8.49409597]]
Epoch: 23 , Loss: [[17.20918888]] , Val Loss: [[8.5854792]]

Step 3: Get predictions


preds = []
for i in
range(Y.shape[0]): x, y
= X[i], Y[i]
prev_s = np.zeros((hidden_dim, 1))
# Forward pass
for t in range(T):
mulu = np.dot(U, x)
mulw = np.dot(W,
prev_s) add = mulw +
mulu
s = sigmoid(add)
mulv = np.dot(V,
s) prev_s = s
preds.append(mulv)
preds =
np.array(preds)

Plotting these predictions alongside the actual values:


plt.plot(preds[:, 0, 0], 'g')

plt.plot(Y[:, 0], 'r')

plt.show()

preds = []

for i in

range(Y_val.shape[0]): x, y

= X_val[i], Y_val[i]

prev_s = np.zeros((hidden_dim, 1))

# For each time step...

for t in range(T):

mulu = np.dot(U,

x)
mulw = np.dot(W,

prev_s) add = mulw +

mulu

s = sigmoid(add)
mulv = np.dot(V,

s) prev_s = s

preds.append(mulv)

preds = np.array(preds)

plt.plot(preds[:, 0, 0], 'g')

plt.plot(Y_val[:, 0], 'r')

plt.show()

The RMSE score on the validation data is respectable as well:


from sklearn.metrics import mean_squared_error

math.sqrt(mean_squared_error(Y_val[:, 0] * max_val, preds[:, 0, 0] * max_val))

OUTPUT:
0.127191931509431
EXPERIMENT NO:-07

AIM: Build and train a Long Short-Term Memory (LSTM) model using
TensorFlow and Keras.
Program:

# Import necessary libraries

import numpy as np

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import LSTM, Dense

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

# Generate synthetic sequential data

def generate_data(seq_length, num_samples):

X = np.random.rand(num_samples, seq_length, 1) # Random sequences

y = X.sum(axis=1) # Sum of sequences as target

return X, y

# Parameters

sequence_length = 10

num_samples = 1000

# Data preparation
X, y = generate_data(sequence_length, num_samples)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build the LSTM model

model = Sequential([

LSTM(50, activation='relu', input_shape=(sequence_length, 1)),

Dense(1)

])

# Compile the model

model.compile(optimizer='adam', loss='mse')

# Train the model


history = model.fit(X_train, y_train, epochs=20, batch_size=32, validation_split=0.2)

# Evaluate the model

loss = model.evaluate(X_test, y_test)

print(f"Test Loss: {loss}")

# Predict on test data

predictions = model.predict(X_test[:5])

print("Predictions:", predictions.flatten())

print("Actual:", y_test[:5])

# Plot training and validation loss

plt.plot(history.history['loss'], label='Training Loss')

plt.plot(history.history['val_loss'], label='Validation Loss')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.title('Training and Validation Loss')

plt.legend()

plt.show()

Output:

Training Progress:

Example:

arduino

Copy code

Epoch 1/20

25/25 [==============================] - 1s 10ms/step - loss: 0.2100 - val_loss: 0.0421

...

Epoch 20/20

25/25 [==============================] - 0s 7ms/step - loss: 0.0052 - val_loss: 0.0045

Model Evaluation:

yaml

Copy code

Test Loss: 0.0043

Sample Predictions:
makefile

Copy code

Predictions: [3.58 5.21 4.87 6.32 3.98]

Actual: [3.60 5.22 4.85 6.30 4.00]

Loss Plot:

A graph showing the decreasing trend of training and validation loss over epochs.

You might also like