<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import tensorflow.keras as keras
import matplotlib.pyplot as plt


def generate_time_series(batch_size, n_steps):
    freq1, freq2, offset1, offset2, = np.random.rand(4, batch_size, 1)
    time = np.linspace(0, 1, n_steps)
    series = 0.5 * np.sin((time - offset1) * (freq1 * 10 + 10))
    series += 0.2 * np.sin((time - offset2) * (freq2 * 20 + 20))
    series += 0.1 * (np.random.rand(batch_size, n_steps) - 0.5)
    return series[..., np.newaxis].astype(np.float32)
n_steps = 50
n_samples = 10000
series = generate_time_series(batch_size=n_samples, n_steps=n_steps + 10)
x_train, y_train_single = series[:7000, :n_steps], series[:7000, -1]
x_valid, y_valid_single = series[7000:9000, :n_steps], series[7000:9000, -1]
x_test, y_test_single = series[9000:, :n_steps], series[9000:, -1]

Y = np.empty((10000, n_steps, 10))
for step_ahead in range(1, 10+1):
    Y[:, :, step_ahead-1] = series[:, step_ahead:(step_ahead + n_steps), 0]
y_train = Y[:7000]
y_valid = Y[7000:9000]
y_test = Y[9000:]
# Every sample is a serie of 50 values (x) with 1 prediction (y) at the end.
plt.plot(x_train[0])
plt.show()
print(x_train[0].shape)
print(y_train[0].shape)
# y_train contains the next 10 values of the series at any perticular point.
assert y_train[0].shape[1] == 10
plt.plot(y_train[0])
model = keras.models.Sequential([
    keras.layers.SimpleRNN(20, return_sequences=True, input_shape=[None, 1]),
    keras.layers.SimpleRNN(20, return_sequences=True),
    keras.layers.TimeDistributed(keras.layers.Dense(1)),
])
def last_time_step_mse(Y_true, Y_pred):
    return keras.metrics.mean_squared_error(Y_true[:, -1], Y_pred[:, -1])


optimizer = keras.optimizers.Adam(learning_rate=1e-4)
model.compile(loss="mse",
              optimizer=optimizer, metrics=[last_time_step_mse])
model.fit(
    x=x_train,
    y=y_train,
    validation_data=(x_valid, y_valid),
    epochs=20)
[loss, accuracy] = model.test_on_batch(x=x_test, y=y_test)
print("loss=" + str(loss) + " accuracy=" + str(accuracy))
f_x = np.expand_dims(x_test[0], axis=0)  # batch of 1 dimension
f_y = y_test[0]
f_pred = model.predict(f_x)
print(np.linalg.norm((f_pred - f_y)*(f_pred - f_y)))
print(f_pred.shape)
print(f_y[:,9].shape)
plt.figure(1)
plt.plot(f_pred[0,:,0])
plt.plot(f_y[:,9])
plt.show()
x_test.shape
model.predict(x_test)[0:10].shape
model.layers[0].weights[0].shape # input_matrix
model.layers[0].weights[1].shape # recurrent matrix
model.layers[1].weights[0].shape
model.layers[1].weights[1].shape
len(model.layers[2].weights)
print(model.layers[2].weights[0].shape) # weights on each input
print(model.layers[2].weights[1].shape) # bias<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1664195700518903, 'fraction_numerical': 0.05670867309117865, 'mean_word_length': 5.320843091334894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '196438', 'license': 'MIT', 'n_tokens_mistral': 1172, 'n_tokens_neox': 1090, 'n_words': 205, 'path': 'HML/chapter15/exampleDeepRNNTimeDistributed.ipynb', 'repo_name': 'Zilleplus/MachineLearning', 'size': '2698'}