首先,您的数据重塑是错误的。 N.B.:我刚刚注意到,您将 X1、Y1 误认为 X2、Y2。
打印(X1.shape)->(56,3)打印(Y1.shape)->(56,3)
您是否尝试将它们重塑为尺寸 (-1,30,1) 或 (-1,3,1)。序列中没有 32 个。 X1 和 Y1 都具有相同的维度 (56,3),所以这是一个 seq2seq 问题。但是,您使用的是 1 个单位的 FC 层,就像回归方式一样,这会给您带来错误,因为您的输出具有更多维度。
此外,还有其他错误,例如 X 未定义,但您仍然使用它。不幸的是,您的代码已完全损坏。
案例 #1,我假设数据有 56 个样本,每个样本有 3 个值(时间维度)。
我已经添加了一个最小的代码库来开始使用。
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# load
X1 = np.load('X1.npy')
Y1 = np.load('Y1.npy')
X2 = np.load('X2.npy')
Y2 = np.load('Y2.npy')
print(X1.shape)
print(Y1.shape)
# Reshape data
X = X1.reshape(-1,3,1)
Y = Y1.reshape(-1,3)
# LSTM
model = tf.keras.models.Sequential()
# layre #1
model.add(tf.keras.layers.LSTM(units=3, return_sequences=True, input_shape=(3,1), activation = 'relu'))
model.add(tf.keras.layers.Dropout(0.4))
#layer #2
model.add(tf.keras.layers.LSTM(units=3, return_sequences=False))
model.add(tf.keras.layers.Dropout(0.12))
# layer #3
model.summary()
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_9 (LSTM) (None, 3, 3) 60
_________________________________________________________________
dropout_8 (Dropout) (None, 3, 3) 0
_________________________________________________________________
lstm_10 (LSTM) (None, 3) 84
_________________________________________________________________
dropout_9 (Dropout) (None, 3) 0
=================================================================
Total params: 144
Trainable params: 144
Non-trainable params: 0
__________________________
model.compile(optimizer='adam', loss='mse')
model.fit(X,Y, epochs=2, batch_size=20, verbose=1)
pred = model.predict(X)
对于 X2,Y2:
X = X1.reshape(-1,32,1)
Y = Y1.reshape(-1,32)
# LSTM
model = tf.keras.models.Sequential()
# layre #1
model.add(tf.keras.layers.LSTM(units=32, return_sequences=True, input_shape=(3,1), activation = 'relu'))
model.add(tf.keras.layers.Dropout(0.4))
#layer #2
model.add(tf.keras.layers.LSTM(units=32, return_sequences=False))
model.add(tf.keras.layers.Dropout(0.12))
model.compile(optimizer='adam', loss='mse')
model.fit(X,Y, epochs=2, batch_size=20, verbose=1)
pred = model.predict(X)
提高性能的一些想法:使用 min-max 归一化,在最后一层使用 sigmoid 进行激活。添加更多的dropout和recurrent_dropout。但是,不要在最后一层之后使用 dropout,它会打乱你的预测。
这是一个更新的代码:
X = X1.reshape(-1,3,1)
Y = Y1.reshape(-1,3)
X = (X-np.min(X))/(np.max(X) - np.min(X))
Y = (Y-np.min(Y))/(np.max(Y) - np.min(Y))
# LSTM
model = tf.keras.models.Sequential()
# layre #1
model.add(tf.keras.layers.LSTM(units=8, return_sequences=True, input_shape=(3,1), activation = 'relu', recurrent_dropout = 0.2))
model.add(tf.keras.layers.Dropout(0.4))
model.add(tf.keras.layers.LSTM(units=8, return_sequences=True, input_shape=(3,1), activation = 'relu', recurrent_dropout = 0.2))
model.add(tf.keras.layers.Dropout(0.4))
#layer #2
model.add(tf.keras.layers.LSTM(units=3, return_sequences=False, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='mse')
hist = model.fit(X,Y, epochs=50, batch_size=20, verbose=1)
import matplotlib.pyplot as plt
plt.plot(hist.history['loss'])
plt.show()
案例 #2,您只有一个样本,具有 56 个时间维度和 3 个过滤器。这根本不是 LSTM 的情况。您需要提供有关输入数据格式的更多详细信息。
案例#3,你有更多的数据和代码,这里没有添加。请添加这些 sn-ps,以便我们提供帮助。