0% found this document useful (0 votes)
4 views16 pages

Perceptron and ANN Implementation Guide

The document outlines the implementation of various neural network models including a single unit perceptron, multi-layer perceptron, deep feedforward ANN, and a CNN for image classification. It discusses the training process, accuracy results, and the limitations of the perceptron on non-linearly separable data. Additionally, it details the use of different activation functions and datasets like MNIST and CIFAR-10 for testing the models.

Uploaded by

Lucky
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)
4 views16 pages

Perceptron and ANN Implementation Guide

The document outlines the implementation of various neural network models including a single unit perceptron, multi-layer perceptron, deep feedforward ANN, and a CNN for image classification. It discusses the training process, accuracy results, and the limitations of the perceptron on non-linearly separable data. Additionally, it details the use of different activation functions and datasets like MNIST and CIFAR-10 for testing the models.

Uploaded by

Lucky
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

1.

Design a single unit perceptron for classification of a linearly separable binary dataset
[Link]) without using pre-defined models. Use the Perceptron() from sklearn.

Program
# Single unit perceptron import numpy as np
import pandas as pd
import seaborn as sns
import [Link] as plt
from sklearn.linear_model import Perceptron
df=pd.read_csv('/content/[Link]')
X = [Link][:,0:2]
y = [Link][:,-1]
p = Perceptron()
[Link](X,y)
print(p.coef_)
print(p.intercept_)
z=[Link](X,y)
print("accuracy score is",z)
from [Link] import plot_decision_regions
plot_decision_regions([Link], [Link], clf=p, legend=2)

OUTPUT:
[[11. 47.]]
[-72.]
accuracy score is 0.5
2. Identify the problem with single unit Perceptron. Classify using Or-, And-
and Xor-ed data and analysis the result.

Program
# Perceptron on Or-, And- and Xor-ed data
import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
or_data = [Link]()
and_data = [Link]()
xor_data = [Link]()

or_data['input1']=[1,1,0,0]
or_data['input2']=[1,0,1,0]
or_data['ouput']=[1,1,1,0]

and_data['input1']=[1,1,0,0]
and_data['input2']=[1,0,1,0]
and_data['ouput']=[1,0,0,0]

xor_data['input1']=[1,1,0,0]
xor_data['input2']=[1,0,1,0]
xor_data['ouput']=[0,1,1,0]

from sklearn.linear_model import Perceptron


clf1=Perceptron()
clf2=Perceptron()
clf3=Perceptron()
[Link](and_data.iloc[:,0:2].values,and_data.iloc[:,-1].values)
print(clf1.coef_)
print(clf1.intercept_)
x=[Link](-1,1,5)
y=-x+1
[Link](x,y)
#[Link](and_data['input1'],and_data['input2'],hue=and_data['ouput'],s=200)
[Link](or_data.iloc[:,0:2].values,or_data.iloc[:,-1].values)
print(clf2.coef_)
print(clf2.intercept_)
x1=[Link](-1,1,5)
y1=-x+0.5
[Link](x1,y1)
#[Link](or_data['input1'],or_data['input2'],hue=or_data['ouput'],s=200)
[Link](xor_data.iloc[:,0:2].values,xor_data.iloc[:,-1].values)
print(clf3.coef_)
print(clf3.intercept_)
plot_decision_regions(xor_data.iloc[:,0:2].values,xor_data.iloc[:,-1].values,
clf=clf3, legend=2)
OUTPUT:
[[2. 2.]]
[-2.]
[[2. 2.]]
[-1.]
[[0. 0.]]
3. Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the
same using appropriate data sets. Vary the activation functions used and compare the results.

Program:
from [Link] import Sequential
from [Link] import Dense, Activation
import numpy as np
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
X, y = datasets.load_iris( return_X_y = True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)
# Define the network model and its arguments.
# Set the number of neurons/nodes for each layer:
model = Sequential()
[Link](Dense(2, input_shape=(4,)))
[Link](Activation('sigmoid'))
[Link](Dense(1))
[Link](Activation('sigmoid'))
#sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
#[Link](loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# Compile the model and calculate its accuracy:
[Link](loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
#[Link](X_train, y_train, batch_size=32, epochs=3)
# Print a summary of the Keras model:
[Link]()
#[Link](X_train, y_train)
#[Link](X_train, y_train, batch_size=32, epochs=300)
[Link](X_train, y_train, epochs=5)
score = [Link](X_test, y_test)
print(score)

OUTPUT:
Model: "sequential_5"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ dense_10 (Dense) │ (None, 2) │ 10 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_10 (Activation) │ (None, 2) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_11 (Dense) │ (None, 1) │ 3 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_11 (Activation) │ (None, 1) │ 0 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 13 (52.00 B)
Trainable params: 13 (52.00 B)
Non-trainable params: 0 (0.00 B)
Epoch 1/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.2783 - loss: 0.8262
Epoch 2/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - accuracy: 0.3447 - loss: 0.8401
Epoch 3/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - accuracy: 0.2978 - loss: 0.7824
Epoch 4/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.3330 - loss: 0.8033
Epoch 5/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.2939 - loss: 0.8342
.
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 12ms/step - accuracy: 0.3375 - loss: 0.9378
[0.9902694225311279, 0.3499999940395355
4. Build a Deep Feed Forward ANN by implementing the Backpropagation algorithm and test
the same using appropriate data sets. Use the number of hidden layers >=4.

Program:
from [Link] import Sequential
from [Link] import Dense, Activation
import numpy as np
import pandas as pd
from sklearn import datasets
iris = datasets.load_iris()
X, y = datasets.load_iris(return_X_y = True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)
# Define the network model and its arguments.
# Set the number of neurons/nodes for each layer:
model = Sequential()
[Link](Dense(2, input_shape=(4,)))
[Link](Activation('sigmoid'))
[Link](Dense(1))
[Link](Activation('sigmoid'))
[Link](Dense(2, input_shape=(4,)))
[Link](Activation('sigmoid'))
[Link](Dense(1))
[Link](Activation('sigmoid'))
[Link](Dense(2, input_shape=(4,)))
[Link](Activation('sigmoid'))
[Link](Dense(1))
[Link](Activation('sigmoid'))

#sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)


#[Link](loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# Compile the model and calculate its accuracy:
[Link](loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
#[Link](X_train, y_train, batch_size=32, epochs=3)
# Print a summary of the Keras model:
[Link]()
#[Link](X_train, y_train)
#[Link](X_train, y_train, batch_size=32, epochs=300)
[Link](X_train, y_train, epochs=5)
score = [Link](X_test, y_test)
print(score)

OUTPUT:

Model: "sequential_6"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ dense_12 (Dense) │ (None, 2) │ 10 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_12 (Activation) │ (None, 2) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_13 (Dense) │ (None, 1) │ 3 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_13 (Activation) │ (None, 1) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_14 (Dense) │ (None, 2) │ 4 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_14 (Activation) │ (None, 2) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_15 (Dense) │ (None, 1) │ 3 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_15 (Activation) │ (None, 1) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_16 (Dense) │ (None, 2) │ 4 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_16 (Activation) │ (None, 2) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_17 (Dense) │ (None, 1) │ 3 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ activation_17 (Activation) │ (None, 1) │ 0 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 27 (108.00 B)
Trainable params: 27 (108.00 B)
Non-trainable params: 0 (0.00 B)
Epoch 1/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.3063 - loss: 0.7693
Epoch 2/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.3023 - loss: 0.7750
Epoch 3/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - accuracy: 0.3102 - loss: 0.7665
Epoch 4/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.2945 - loss: 0.7863
Epoch 5/5
3/3 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - accuracy: 0.2789 - loss: 0.7912
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - accuracy: 0.4118 - loss: 0.7190
[0.751515805721283, 0.38333332538604736]
5. Design and implement a CNN model (with 2 layers of convolutions) to classify multi category image
datasets. Record the accuracy corresponding to the number of epochs. Use the MNIST, CIFAR-10 datasets.

Program

import keras
from [Link] import mnist
from [Link] import Dense, Activation, Flatten, Conv2D,
MaxPooling2D from [Link] import Sequential
from [Link] import to_categorical
import numpy as np
import [Link] as plt
(train_X,train_Y), (test_X,test_Y) = mnist.load_data()
train_X = train_X.reshape(-1, 28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)
train_X.shape
train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X / 255
test_X = test_X / 255
train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)
model = Sequential()
[Link](Conv2D(64, (3,3), input_shape=(28, 28, 1)))
[Link](Activation('relu'))
[Link](MaxPooling2D(pool_size=(2,2)))
[Link](Conv2D(64, (3,3)))
[Link](Activation('relu'))
[Link](MaxPooling2D(pool_size=(2,2)))
[Link](Flatten())
[Link](Dense(64))
[Link](Dense(10))
[Link](Activation('softmax'))
[Link](loss=[Link].categorical_crossentropy,
optimizer=[Link](),metrics=['accuracy'])
[Link](train_X, train_Y_one_hot, batch_size=64, epochs=10)
test_loss, test_acc = [Link](test_X, test_Y_one_hot)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
predictions = [Link](test_X)
print([Link]([Link](predictions[0])))
[Link](test_X[0].reshape(28, 28), cmap = [Link])
[Link]()

OUTPUT:
Epoch 1/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 89s 93ms/step - accuracy: 0.9046 - loss: 0.3224
Epoch 2/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 140s 92ms/step - accuracy: 0.9849 - loss: 0.0506
Epoch 3/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 140s 90ms/step - accuracy: 0.9899 - loss: 0.0335
Epoch 4/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 141s 89ms/step - accuracy: 0.9905 - loss: 0.0279
Epoch 5/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 142s 89ms/step - accuracy: 0.9937 - loss: 0.0211
Epoch 6/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 143s 90ms/step - accuracy: 0.9943 - loss: 0.0174
Epoch 7/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 141s 89ms/step - accuracy: 0.9954 - loss: 0.0141
Epoch 8/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 82s 88ms/step - accuracy: 0.9965 - loss: 0.0110
Epoch 9/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 143s 89ms/step - accuracy: 0.9967 - loss: 0.0103
Epoch 10/10
938/938 ━━━━━━━━━━━━━━━━━━━━ 143s 90ms/step - accuracy: 0.9974 - loss: 0.0083
313/313 ━━━━━━━━━━━━━━━━━━━━ 4s 13ms/step - accuracy: 0.9884 - loss: 0.0515
Test loss 0.038335878401994705
Test accuracy 0.9909999966621399
313/313 ━━━━━━━━━━━━━━━━━━━━ 4s 13ms/step
7
[Link] and implement a CNN model (with 4+ layers of convolutions) to classify multi category image datasets.
Record the accuracy corresponding to the number of epochs. Use the Fashion MNIST datasets. Record the time
required to run the program, using CPU as well as using GPU in Colab.

Program-
import keras
from [Link] import fashion_mnist
from [Link] import Dense, Activation, Flatten, Conv2D,
MaxPooling2D from [Link] import Sequential
from [Link] import to_categorical
import numpy as np
import [Link] as plt

(train_X,train_Y), (test_X,test_Y) =

fashion_mnist.load_data() train_X = train_X.reshape(-1,

28,28, 1)
test_X = test_X.reshape(-1, 28,28, 1)

train_X = train_X.astype('float32')
test_X = test_X.astype('float32')
train_X = train_X / 255
test_X = test_X / 255

train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)

model = Sequential()

[Link](Conv2D(256, (3,3), input_shape=(28, 28, 1)))


[Link](Activation('relu'))
[Link](MaxPooling2D(pool_size=(2,2)))

[Link](Conv2D(128, (3,3)))
[Link](Activation('relu'))
[Link](MaxPooling2D(pool_size=(2,2)))

[Link](Conv2D(64, (3,3), input_shape=(28, 28, 1)))


[Link](Activation('relu'))
#[Link](MaxPooling2D(pool_size=(2,2)))

[Link](Conv2D(28, (3,3)))
[Link](Activation('relu'))
#[Link](MaxPooling2D(pool_size=(2,2)))

[Link](Flatten())
[Link](Dense(64))

[Link](Dense(10))
[Link](Activation('softmax'))
[Link](loss=[Link].categorical_crossentropy,
optimizer=[Link](),metrics=['accuracy'])

[Link](train_X, train_Y_one_hot, batch_size=64, epochs=5)

test_loss, test_acc = [Link](test_X, test_Y_one_hot)


print('Test loss', test_loss)
print('Test accuracy', test_acc)

predictions = [Link](test_X)
print([Link]([Link](predictions[0])))

[Link](test_X[0].reshape(28, 28), cmap = [Link])


[Link]()

OUTPUT:

Downloading data from [Link]


[Link]
29515/29515 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
Downloading data from [Link]
[Link]
26421880/26421880 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step
Downloading data from [Link]
[Link]
5148/5148 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
Downloading data from [Link]
[Link]
4422102/4422102 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step
/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py:107:
UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using
Sequential models, prefer using an `Input(shape)` object as the first layer in the model
instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
Epoch 1/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 484s 514ms/step - accuracy: 0.7166 - loss: 0.7675
Epoch 2/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 476s 487ms/step - accuracy: 0.8754 - loss: 0.3404
Epoch 3/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 504s 489ms/step - accuracy: 0.8944 - loss: 0.2883
Epoch 4/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 494s 481ms/step - accuracy: 0.9085 - loss: 0.2493
Epoch 5/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 502s 480ms/step - accuracy: 0.9168 - loss: 0.2297
313/313 ━━━━━━━━━━━━━━━━━━━━ 20s 62ms/step - accuracy: 0.9016 - loss: 0.2778
Test loss 0.270274817943573
Test accuracy 0.902999997138977313/313 ━━━━━━━━━━━━━━━━━━━━ 21s 67ms/step

9
7. Implement the standard LeNet CNN architecture model to classify multi category image dataset (MNIST) and
check the accuracy.

Program-
# LeNet

import tensorflow as tf
from tensorflow import keras
import numpy as np
(train_x, train_y), (test_x, test_y) = [Link].load_data()
train_x = train_x / 255.0
test_x = test_x / 255.0
train_x = tf.expand_dims(train_x, 3)
test_x = tf.expand_dims(test_x, 3)

val_x = train_x[:5000]
val_y = train_y[:5000]

lenet_5_model = [Link]([
[Link].Conv2D(6, kernel_size=5, strides=1, activation='tanh',
input_shape=train_x[0].shape, padding='same'), #C1
[Link].AveragePooling2D(), #S2
[Link].Conv2D(16, kernel_size=5, strides=1, activation='tanh',
padding='valid'), #C3
[Link].AveragePooling2D(), #S4
[Link].Conv2D(120, kernel_size=5, strides=1, activation='tanh',
padding='valid'), #C5
[Link](), #Flatten
[Link](84, activation='tanh'), #F6
[Link](10, activation='softmax') #Output layer
])

lenet_5_model.compile(optimizer='adam',
loss=[Link].sparse_categorical_crossentropy, metrics=['accuracy'])
lenet_5_model.fit(train_x, train_y, epochs=5, validation_data=(val_x, val_y))
lenet_5_model.evaluate(test_x, test_y)

OUTPUT:

Epoch 1/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 363s 192ms/step - accuracy: 0.8765 - loss: 0.4175 - val_accuracy:
0.8992 - val_loss: 0.3405
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 381s 192ms/step - accuracy: 0.8891 - loss: 0.3619 - val_accuracy:
0.9074 - val_loss: 0.3157
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 384s 193ms/step - accuracy: 0.8933 - loss: 0.3487 - val_accuracy:
0.9152 - val_loss: 0.2739
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 361s 192ms/step - accuracy: 0.9113 - loss: 0.2934 - val_accuracy:
0.9174 - val_loss: 0.2693
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 364s 194ms/step - accuracy: 0.9141 - loss: 0.2875 - val_accuracy:
0.9326 - val_loss: 0.2321
313/313 ━━━━━━━━━━━━━━━━━━━━ 16s 52ms/step - accuracy: 0.9127 - loss: 0.2881
[0.25613024830818176, 0.9229000210762024]
[Link] RNN for sentiment analysis on movie

reviews. Program-

# RNN sentiment analysis on movie reviews

from [Link] import imdb


from [Link] import Tokenizer
from [Link] import pad_sequences
from keras import Sequential
from [Link] import
Dense,SimpleRNN,Embedding,Flatten(X_train,y_train),(X_test
,y_test) = imdb.load_data() X_train =
pad_sequences(X_train,padding='post',maxlen=50) X_test =
pad_sequences(X_test,padding='post',maxlen=50)
X_train.shape
model = Sequential()
#[Link](Embedding(10000,
2))
[Link](SimpleRNN(32,input_shape=(50,1), return_sequences=False))
[Link](Dense(1, activation='sigmoid'))

[Link]()
[Link](optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
[Link](X_train, y_train,epochs=5,validation_data=(X_test,y_test))
test_loss, test_acc = [Link](X_test, y_test)
print('Test loss', test_loss)
print('Test accuracy', test_acc)

OUTPUT:
Model: "sequential_3"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ simple_rnn_1 (SimpleRNN) │ (None, 32) │ 1,088 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_5 (Dense) │ (None, 1) │ 33 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 1,121 (4.38 KB)
Trainable params: 1,121 (4.38 KB)
Non-trainable params: 0 (0.00 B)
Epoch 1/5
782/782 ━━━━━━━━━━━━━━━━━━━━ 13s 15ms/step - acc: 0.5060 - loss: 0.7268 - val_acc: 0.5026
- val_loss: 0.6949
Epoch 2/5
782/782 ━━━━━━━━━━━━━━━━━━━━ 12s 15ms/step - acc: 0.5120 - loss: 0.6930 - val_acc: 0.4997
- val_loss: 0.6943
Epoch 3/5
782/782 ━━━━━━━━━━━━━━━━━━━━ 20s 14ms/step - acc: 0.5078 - loss: 0.6925 - val_acc: 0.5080
- val_loss: 0.6935
Epoch 4/5
782/782 ━━━━━━━━━━━━━━━━━━━━ 10s 13ms/step - acc: 0.5105 - loss: 0.6926 - val_acc: 0.5072
- val_loss: 0.6932
Epoch 5/5
782/782 ━━━━━━━━━━━━━━━━━━━━ 22s 15ms/step - acc: 0.5106 - loss: 0.6925 - val_acc: 0.5010
- val_loss: 0.6947
782/782 ━━━━━━━━━━━━━━━━━━━━ 3s 4ms/step - acc: 0.5072 - loss: 0.6938
Test loss 0.6946738362312317
Test accuracy 0.5009999871253967
[Link] Bi-directional LSTM for sentiment analysis on movie reviews.

Program-
# Bi directional LSTM

import numpy as np
from [Link] import sequence
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Dense, Dropout, Embedding, LSTM, Bidirectional
from [Link] import imdb

n_unique_words = 10000 # cut texts after this number of words


maxlen = 200
batch_size = 128
(x_train, y_train),(x_test, y_test) = imdb.load_data(num_words=n_unique_words)
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)
y_train = [Link](y_train)
y_test = [Link](y_test)

model = Sequential()
[Link](Embedding(n_unique_words, 128,
input_length=maxlen)) [Link](Bidirectional(LSTM(64)))
[Link](Dropout(0.5))
[Link](Dense(1, activation='sigmoid'))
[Link](loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history=[Link](x_train, y_train, batch_size=batch_size, epochs=10,
validation_data=[x_test, y_test])
test_loss, test_acc = [Link](x_test, y_test)
print('Test loss', test_loss)
print('Test accuracy', test_acc)
print([Link]['loss'])
print([Link]['accuracy'])
from matplotlib import pyplot
[Link]([Link]['loss'])
[Link]([Link]['accuracy'])
[Link]('model loss vs accuracy')
[Link]('epoch')
[Link](['loss', 'accuracy'], loc='upper right')
[Link]()

OUTPUT:

Epoch 1/10
/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/[Link]: UserWarning:
Argument `input_length` is deprecated. Just remove it.
[Link](
196/196 ━━━━━━━━━━━━━━━━━━━━ 210s 1s/step - accuracy: 0.6789 - loss: 0.5679 - val_accuracy:
0.8626 - val_loss: 0.3282
Epoch 2/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 261s 1s/step - accuracy: 0.9014 - loss: 0.2527 - val_accuracy:
0.8695 - val_loss: 0.3081
Epoch 3/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 207s 1s/step - accuracy: 0.9245 - loss: 0.2057 - val_accuracy:
0.8598 - val_loss: 0.3507
Epoch 4/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 258s 1s/step - accuracy: 0.9405 - loss: 0.1620 - val_accuracy:
0.8680 - val_loss: 0.3414
Epoch 5/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 263s 1s/step - accuracy: 0.9616 - loss: 0.1102 - val_accuracy:
0.8659 - val_loss: 0.4334
Epoch 6/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 203s 1s/step - accuracy: 0.9694 - loss: 0.0928 - val_accuracy:
0.8518 - val_loss: 0.4732
Epoch 7/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 238s 1s/step - accuracy: 0.9743 - loss: 0.0820 - val_accuracy:
0.8586 - val_loss: 0.4679
Epoch 8/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 228s 1s/step - accuracy: 0.9806 - loss: 0.0590 - val_accuracy:
0.7561 - val_loss: 0.5712
Epoch 9/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 260s 1s/step - accuracy: 0.9010 - loss: 0.2421 - val_accuracy:
0.8538 - val_loss: 0.4951
Epoch 10/10
196/196 ━━━━━━━━━━━━━━━━━━━━ 265s 1s/step - accuracy: 0.9793 - loss: 0.0620 - val_accuracy:
0.8598 - val_loss: 0.5510
782/782 ━━━━━━━━━━━━━━━━━━━━ 53s 68ms/step - accuracy: 0.8614 - loss: 0.5558
Test loss 0.5510081052780151
Test accuracy 0.8598399758338928
[0.4591962397098541, 0.2561950087547302, 0.21162627637386322, 0.15591321885585785,
0.11620788276195526, 0.10155797749757767, 0.08795470744371414, 0.06875146925449371,
0.1556294709444046, 0.07134319096803665]
[0.7749199867248535, 0.8991199731826782, 0.9213200211524963, 0.942520022392273,
0.9589999914169312, 0.9653599858283997, 0.9707199931144714, 0.9758800268173218,

You might also like