import numpy as np
import keras
from keras.models import Sequential
from keras.layers import LSTM, Dense
# initial data (first~ fifth bandwidth)
bw = [10, 15, 20, 18, 22]
# LSTM model settings
model = Sequential()
model.add(LSTM(units=50, activation='relu', input_shape=(5, 1)))
model.add(Dense(units=1))
model.compile(optimizer='adam', loss='mean_squared_error')
# Preprocess function
def prepare_data(data):
X, y = [], []
for i in range(len(data) - 5):
X.append(data[i:i + 5])
y.append(data[i + 5])
return np.array(X), np.array(y)
# train & predict
for _ in range(10):
X_train, y_train = prepare_data(bw)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
model.fit(X_train, y_train, epochs=50, batch_size=1, verbose=0)
next_bw = model.predict(np.array([bw[-5:]])) # Use the most recent 5 data as input
next_bw = next_bw[0][0]
# Add predicted values to bw list and delete oldest value
bw.append(next_bw)
bw.pop(0)
print("predicted data:", next_bw)
print("updated bw list:", bw)
in <module>
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
IndexError: tuple index out of range
When you run it, the above error occurs.
We assumed that the bandwidth of the first 5 sections was stored in the 'bw' list. At this time, I tried to write code that repeats the process of learning the values in the list, predicting the next bandwidth, and updating the bw list.
but i faced error when numpy reshape