Experiment 3 (A, B, C) (RNN) (Recuurent) (IMDB) )
Experiment 3 (A, B, C) (RNN) (Recuurent) (IMDB) )
PROGRAM:
import numpy as np
import tensorflow as tf
def generate_data(seq_length,num_samples):
x=[]
y=[]
for _ in range(num_samples):
start=np.random.rand()
step=np.random.rand()/10
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)
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)
])
model.compile(optimizer='adam',loss='mse')
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
Epoch 2/20
Epoch 3/20
Epoch 4/20
Epoch 5/20
Epoch 6/20
Epoch 7/20
Epoch 8/20
Epoch 9/20
Epoch 10/20
Epoch 11/20
Epoch 12/20
Epoch 13/20
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step - loss: 2.8017e-06
Epoch 14/20
Epoch 15/20
Epoch 16/20
Epoch 17/20
Epoch 18/20
Epoch 19/20
Epoch 20/20
import numpy as np
def generate_data(seq_length,num_samples):
x=[]
y=[]
start=np.random.rand()
step=np.random.rand()/10
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)
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))
model.compile(optimizer='adam',loss='mse')
model.summary()
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
# Parameters
vocab_size = 1000
maxlen = 500
embedding_dim = 32
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))