Skip to content

Instantly share code, notes, and snippets.

@bradlipovsky
Created October 24, 2023 23:23
Show Gist options
  • Save bradlipovsky/537c9cb8b169cd16ce00651cbce779ad to your computer and use it in GitHub Desktop.
Save bradlipovsky/537c9cb8b169cd16ce00651cbce779ad to your computer and use it in GitHub Desktop.
Minimal Example LSTM in Keras
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from sklearn.model_selection import train_test_split
# Generate example data (you should replace this with your data)
# Here, we create three input time series and one target time series.
# You should load your own data accordingly.
n_samples = 1000
n_timestamps = 50
n_features = 3 # Number of input time series
n_target_features = 1 # Number of target time series
# Generate random input data (you should replace this with your data)
X = np.random.rand(n_samples, n_timestamps, n_features)
# Generate random target data (you should replace this with your data)
y = np.random.rand(n_samples, n_timestamps, n_target_features)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Define the LSTM model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_timestamps, n_features), return_sequences=True))
model.add(Dense(n_target_features))
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train the model
model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test))
# Evaluate the model on test data
loss = model.evaluate(X_test, y_test)
print(f'Test Loss: {loss}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment