Я создал нарезку окон в качестве входных данных для обучения, и я попытался изменить единицы LSTM, Batch, и это ничего не помогает. Кто-нибудь знает, в чем проблема моего кода? пожалуйста помоги. Спасибо.
Это мой код:
#make window size
def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
series = tf.expand_dims(series, axis=-1)
ds = tf.data.Dataset.from_tensor_slices(series)
ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
ds = ds.flat_map(lambda w: w.batch(window_size + 1))
#ds = ds.shuffle(shuffle_buffer)
ds = ds.map(lambda w: (w[:-1], w[1:]))
return ds.batch(batch_size).prefetch(1)
#model forecasting
def model_forecast(model, series, window_size):
ds = tf.data.Dataset.from_tensor_slices(series)
ds = ds.window(window_size, shift=1, drop_remainder=True)
ds = ds.flat_map(lambda w: w.batch(window_size))
ds = ds.batch(16).prefetch(1)
forecast = model.predict(ds)
return forecast
Это мой код LSTM
tf.keras.backend.clear_session()
tf.random.set_seed(51)
np.random.seed(51)
window_size = 10
batch_size = 5
train_set = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)
print(train_set)
print(x_train.shape)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv1D(filters=8, kernel_size=5,
strides=1, padding="causal",
activation="relu",
input_shape=[None, 1]),
tf.keras.layers.LSTM(20, return_sequences=True),
tf.keras.layers.LSTM(20, return_sequences=True),
tf.keras.layers.LSTM(10, return_sequences=True),
tf.keras.layers.LSTM(5, return_sequences=True),
tf.keras.layers.LSTM(4, return_sequences=True),
tf.keras.layers.Dense(20, activation="relu"),
tf.keras.layers.Dense(5, activation="relu"),
tf.keras.layers.Dense(1),
tf.keras.layers.Lambda(lambda x: x * 400)
])
lr_schedule = tf.keras.callbacks.LearningRateScheduler(
lambda epoch: 1e-8 * 10**(epoch / 20))
optimizer = tf.keras.optimizers.SGD(lr=1e-8, momentum=0.9)
model.compile(loss=tf.keras.losses.Huber(),
optimizer=optimizer,
metrics=["mae"])
history = model.fit(train_set, epochs=100, callbacks=[lr_schedule])
#prediction
rnn_forecast = model_forecast(model, series[..., np.newaxis], window_size)
rnn_forecast = rnn_forecast[split_time - window_size:-1, -1, 0]
print(rnn_forecast)
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867
1287.0867 1287.0867 1287.0867 1287.0867 1287.0867 1287.0867