Vertopal.com DLWP Chapter6
Vertopal.com DLWP Chapter6
Output y_t:
[[2.14]]
import numpy as np
#ReLU activation
def relu(x):
return np.maximum(0,x)
#sequence of 3 input vectors, each with 2 features(shape:3 x 2 x 1)
sequence=[
np.array([[1], [2]]),
np.array([[0], [1]]),
np.array([[3], [1]])
]
#intial hidden state(2x1)
h_t=np.zeros((2,1))
#weights and biases
w_xh=np.array([[0.1, 0.2],
[0.3, 0.4]])
w_hh=np.array([[0.5, 0.6],
[0.7, 0.8]])
w_hy=np.array([[0.9, 1.0]])
b_h=np.array([[0.1],
[0.2]])
b_y=np.array([[0.3]])
#process each step
print("RNN time step outputs")
for t, x_t in enumerate(sequence,1):
#hidden state update
h_t=relu(np.dot(w_xh, x_t)+np.dot(w_hh, h_t)+b_h)
#output calculation
y_t=np.dot(w_hy, h_t)+b_y
print(f"Time step {t}:")
print("Input x_t:\n", x_t.flatten())
print("Hidden state h_t:\n",h_t.flatten())
print("Output y_t:\n", y_t,y_t.flatten())
{' ': 0, 'E': 1, 'I': 2, 'R': 3, 'T': 4, 'c': 5, 'e': 6, 'h': 7, 'l':
8, 'm': 9, 'o': 10, 't': 11, 'w': 12}
{0: ' ', 1: 'E', 2: 'I', 3: 'R', 4: 'T', 5: 'c', 6: 'e', 7: 'h', 8:
'l', 9: 'm', 10: 'o', 11: 't', 12: 'w'}
[[ 7 6 8]
[ 6 8 8]
[ 8 8 10]
[ 8 10 0]
[10 0 12]
[ 0 12 6]
[12 6 8]
[ 6 8 5]
[ 8 5 10]
[ 5 10 9]
[10 9 6]
[ 9 6 0]
[ 6 0 11]
[ 0 11 10]
[11 10 0]
[10 0 2]
[ 0 2 4]
[ 2 4 1]]
[ 8 10 0 12 6 8 5 10 9 6 0 11 10 0 2 4 1 3]
/usr/local/lib/python3.11/dist-packages/keras/src/layers/rnn/
rnn.py:200: 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)
#Back Propagation
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN,Dense
X = np.array([[1.0],[2.0]]) #Shape: (1,2,1)
y_true = np.array([0.5]) #Shape: (1,1)
model = Sequential([
SimpleRNN(units=2, activation='tanh',
input_shape=(2,1),return_sequences=False),
Dense(1) #Output layer #2.Define a simple RNN model (many-to-
one)
])
loss_fn=tf.keras.losses.MeanSquaredError() #3.Forward and backward
pass using Gra
optimizer = tf.keras.optimizers.Adam(learning_rate=0.1)
with tf.GradientTape() as tape:
y_pred = model(X, training=True)
loss = loss_fn(y_true, y_pred)
gradients = tape.gradient(loss, model.trainable_variables)
#4.Compute Gradient
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
#5.Apply gradient
for var, grad in zip(model.trainable_variables, gradients):
print(f'Variable: {var.name}') #6.Show gradient value
print("Gradient:\n",grad.numpy())
print("\nPredicted:", y_pred.numpy())
print("Loss:", loss.numpy())
/usr/local/lib/python3.11/dist-packages/keras/src/layers/rnn/
rnn.py:200: 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)
Variable: kernel
Gradient:
[[-0.98720163 -0.13503356]]
Variable: recurrent_kernel
Gradient:
[[0. 0.]
[0. 0.]]
Variable: bias
Gradient:
[-0.5919188 -0.10538915]
Variable: kernel
Gradient:
[[ 0.02860823]
[-0.45368445]]
Variable: bias
Gradient:
[0.51464826]
Predicted: [[0.6705861 ]
[0.84406215]]
Loss: 0.07373919