<jupyter_start><jupyter_text>Work below is based on Chapter 7 of [LSTM Networks with Python](https://machinelearningmastery.com/lstms-with-python/).  Some changes have been made to variable names<jupyter_code># generate damped sine wave in [0,1]
def generate_sequence(length, period, decay):
    return [0.5 + 0.5 * sin(2 * pi * i / period) * exp(-decay * i) for i in range(length)]
seq = generate_sequence(102, 20, 0.05)
f = plot_line(y=seq, figsize=(20, 5))
# generate input and output pairs of damped sine waves
def generate_examples(length, samples, test_length):
    X, y = list(), list()
    for _ in range(samples):
        p = np.random.randint(10, 40)
        d = np.random.uniform(0.01, 0.05)
        sequence = generate_sequence(length + test_length, p, d)
        X.append(sequence[:-test_length])
        y.append(sequence[-test_length:])

    X = np.array(X).reshape(samples, length, 1)
    y = np.array(y).reshape(samples, test_length)
    return X, y
x, y = generate_examples(length=20, samples=5, test_length=5)
print(x.shape)
print(y.shape)
[plot_line(y=x[n, :, 0], figsize=(20, 1)) for n in range(x.shape[0])]<jupyter_output><empty_output><jupyter_text>We are building a many-to-one sequence problem - feeding in the sequence and then making a single prediction.  This prediction is for n time steps (where n = output)<jupyter_code>input_length = 15
test_length = 20

model = forecast.make_model(
    model_id='lstm',
    input_length=input_length,
    layers=(20, 20),
    output_nodes=test_length,
    lr=0.001,
    batch_size=64,

)<jupyter_output>making lstm
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 15, 20)            1760      
_________________________________________________________________
lstm_2 (LSTM)                (None, 20)                3280      
_________________________________________________________________
dense_1 (Dense)              (None, 20)                420       
=================================================================
Total params: 5,460
Trainable params: 5,460
Non-trainable params: 0
_________________________________________________________________
None<jupyter_text>Training<jupyter_code>X, y = generate_examples(input_length, 100000, test_length)
hist = model.fit(X, y, epochs=1)<jupyter_output>Epoch 1/1
100000/100000 [==============================] - 39s 386us/step - loss: 0.0774<jupyter_text>Predictions<jupyter_code>def make_prediction():
    X, y = generate_examples(input_length, 1, test_length)
    preds = model.predict(X)

    df = pd.DataFrame(index=np.arange(len(y[0])))
    df.loc[:, 'actuals'] = y[0]
    df.loc[:, 'preds'] = preds[0]

    return plot_time_series(df, list(df.columns), same_plot=True)


for _ in range(5):
    make_prediction()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16395663956639567, 'fraction_numerical': 0.04065040650406504, 'mean_word_length': 4.171628721541156, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '307495', 'license': 'MIT', 'n_tokens_mistral': 1006, 'n_tokens_neox': 899, 'n_words': 218, 'path': 'notebooks/sine_lstm.ipynb', 'repo_name': 'ADGEfficiency/forecast', 'size': '2952'}