0% found this document useful (0 votes)
11 views11 pages

Experiment 3 (A, B, C) (RNN) (Recuurent) (IMDB) )

RNN
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)
11 views11 pages

Experiment 3 (A, B, C) (RNN) (Recuurent) (IMDB) )

RNN
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/ 11

BASIC RNN IMPLIMENTATION

PROGRAM:

import numpy as np

import tensorflow as tf

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import SimpleRNN,Dense

# Generate Same Sample Data

# For This Example ,We Use A Simple Sine Wave

def generate_data(seq_length,num_samples):

x=[]

y=[]

for _ in range(num_samples):

start=np.random.rand()

step=np.random.rand()/10

sequence=[start+step*i for i in range(seq_length)]

x.append(sequence[:-1])

y.append(sequence[-1])

return np.array(x),np.array(y)

# Parameters

seq_length=10

num_samples=1000

# Generate Data

x,y=generate_data(seq_length,num_samples)

# Reshape Data for RNN Input

x=x.reshape((num_samples,seq_length-1,1))
# Build The RNN Model

model=Sequential([

SimpleRNN(50,activation='relu',input_shape=(seq_length-1,1)),

Dense(1)

])

# Compile The Model

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

# Train The Model

model.fit(x,y,epochs=20,batch_size=32)

# Make Predictions

test_input=np.array([generate_data(seq_length,1)[0][0]])

test_input=test_input.reshape((1,seq_length-1,1))

predicted_output=model.predict(test_input)

print("Test Input:",test_input.flatten())

print("Predicted Output:",predicted_output.flatten())
OUTPUT:

Epoch 1/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 1s 3ms/step - loss: 0.8896

Epoch 2/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.0122

Epoch 3/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.0012

Epoch 4/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 1.3424e-04

Epoch 5/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 2.0603e-05

Epoch 6/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 6.6726e-06

Epoch 7/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 4.3858e-06

Epoch 8/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 3.5385e-06

Epoch 9/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 5.2133e-06

Epoch 10/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 3.7234e-06

Epoch 11/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 3.8447e-06

Epoch 12/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 3.0746e-06

Epoch 13/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 2.8017e-06

Epoch 14/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 1.7573e-06

Epoch 15/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 2.8714e-06

Epoch 16/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 1.8284e-06

Epoch 17/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 1.5298e-06

Epoch 18/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 4.5050e-06

Epoch 19/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 2.8589e-06

Epoch 20/20

32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 1.2845e-06

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 131ms/step

Test Input: [0.55662999 0.60282893 0.64902788 0.69522682 0.74142577 0.78762471

0.83382366 0.8800226 0.92622155]

Predicted Output: [0.97289306]

RECURRENT LAYER IN KERAS


PROGRAM:

import numpy as np

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import LSTM,Dense

# Generate Same Sample Data

# For This Example ,We Use A Simple Sine Wave

def generate_data(seq_length,num_samples):

x=[]

y=[]

for _ in range (num_samples):

start=np.random.rand()

step=np.random.rand()/10

sequence=[start+step*i for i in range(seq_length)]

x.append(sequence[:-1])

y.append(sequence[-1])

return np.array(x),np.array(y)

# Parameters

seq_length=10

num_samples=1000

# Generate Data

x,y=generate_data(seq_length,num_samples)

# Reshape Data For RNN Input

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

# Build A Model
model=Sequential()

model.add(LSTM(50,input_shape=(seq_length-1,1)))

model.add(Dense(1))

# Compile The Model

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

# Print The Model Summary

model.summary()

# Train The Model

model.fit(x,y,epochs=20,batch_size=32)

# Make Predictions

test_input=np.array([generate_data(seq_length,1)[0][0]])

test_input=test_input.reshape((1,seq_length-1,1))

predicted_output=model.predict(test_input)

print("Test Input:",test_input.flatten())

print("Predicted Output:",predicted_output.flatten())

OUTPUT:
/usr/local/lib/python3.10/dist-packages/keras/src/layers/rnn/rnn.py:204:
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__(**kwargs)
Model: "sequential_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━
━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━
━━━━┩
│ lstm_1 (LSTM) │ (None, 50) │
10,400 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ dense_1 (Dense) │ (None, 1) │
51 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────
────┘
Total params: 10,451 (40.82 KB)
Trainable params: 10,451 (40.82 KB)
Non-trainable params: 0 (0.00 B)
Epoch 1/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - loss: 0.5613
Epoch 2/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 1s 8ms/step - loss: 0.0242
Epoch 3/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0170
Epoch 4/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0161
Epoch 5/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.0145
Epoch 6/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0139
Epoch 7/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0128
Epoch 8/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0117
Epoch 9/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0094
Epoch 10/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 8ms/step - loss: 0.0082
Epoch 11/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step - loss: 0.0068
Epoch 12/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.0046
Epoch 13/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.0021
Epoch 14/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 0.0011
Epoch 15/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - loss: 9.1042e-04
Epoch 16/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - loss: 6.9418e-04
Epoch 17/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 6.3219e-04
Epoch 18/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 5.4120e-04
Epoch 19/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 6ms/step - loss: 4.8169e-04
Epoch 20/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step - loss: 4.0162e-04
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 155ms/step
Test Input: [0.6400427 0.73388067 0.82771863 0.9215566 1.01539457
1.10923253
1.2030705 1.29690847 1.39074644]
Predicted Output: [1.4982712]
IMDB DATA FOR MOVIE REVIEW CLASSIFICATION PROBLEM

PROGRAM:

import tensorflow as tf

from tensorflow.keras.datasets import imdb

from tensorflow.keras.preprocessing.sequence import pad_sequences

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Embedding, LSTM, Dense

# Parameters

vocab_size = 1000

maxlen = 500

embedding_dim = 32

# Load the IMDB dataset

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)

# Pad sequences to ensure uniform input size

x_train = pad_sequences(x_train, maxlen=maxlen)

x_test = pad_sequences(x_test, maxlen=maxlen)

# Build the model

model = Sequential()

model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim))

model.add(LSTM(100))

model.add(Dense(1, activation='sigmoid'))

# Compile the model

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Print the model summary


model.summary()

# Train the model

model.fit(x_train, y_train, epochs=3, batch_size=64, validation_split=0.2)

# Evaluate the model

loss, accuracy = model.evaluate(x_test, y_test)

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

print(f"Test Accuracy: {accuracy}")


OUTPUT:
Model: "sequential_1"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━
━━━━┓
┃ Layer (type) ┃ Output Shape ┃
Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━
━━━━┩
│ embedding_1 (Embedding) │ (None,500,32) │ 32000

├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ lstm_1 (LSTM) │ (None,100 │ 53200

├──────────────────────────────────────┼─────────────────────────────┼─────────────
────┤
│ dense_1 (Dense) │ (None,1) │ 101

└──────────────────────────────────────┴─────────────────────────────┴─────────────
────┘
Total params: 85,301 (0.00 B)
Trainable params: 85,301 (0.00 B)
Non-trainable params: 0 (0.00 B)
Epoch 1/3
313/313 ━━━━━━━━━━━━━━━━━━━━ 248s 783ms/step - accuracy: 0.6252 - loss:
0.6422 - val_accuracy: 0.8196 - val_loss: 0.4121
Epoch 2/3
313/313 ━━━━━━━━━━━━━━━━━━━━ 241s 771ms/step - accuracy: 0.8010 - loss:
0.4427 - val_accuracy: 0.8154 - val_loss: 0.4214
Epoch 3/3
313/313 ━━━━━━━━━━━━━━━━━━━━ 263s 776ms/step - accuracy: 0.8237 - loss:
0.4010 - val_accuracy: 0.7276 - val_loss: 0.5377
782/782 ━━━━━━━━━━━━━━━━━━━━ 88s 112ms/step - accuracy: 0.7225 - loss:
0.5353
Test Loss: 0.5290795564651489
Test Accuracy: 0.7300400137901306

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy