recurrent neural network ValueError: Found array with dim 3. Estimator expected <= 2
recurrent neural network ValueError: Found array with dim 3. Estimator expected <= 2
我正在运行使用以下代码构建 LSTM、GRU 和 bilstm 模型
# Create BiLSTM model
def create_model_bilstm(units):
model = Sequential()
model.add(Bidirectional(LSTM(units = units,
return_sequences=True),
input_shape=(X_train.shape[1], X_train.shape[2])))
#model.add(Bidirectional(LSTM(units = units)))
model.add(Dense(1))
#Compile model
model.compile(loss='mse', optimizer='adam')
return model
# Create LSTM or GRU model
def create_model(units, m):
model = Sequential()
model.add(m (units = units, return_sequences = True,
input_shape = [X_train.shape[1], X_train.shape[2]]))
model.add(Dropout(0.1))
#model.add(m (units = units))
#model.add(Dropout(0.2))
model.add(Dense(units = 1))
#Compile model
model.compile(loss='mse', optimizer='adam')
return model
# BiLSTM
model_bilstm = create_model_bilstm(20)
# GRU and LSTM
model_gru = create_model(50, GRU)
model_lstm = create_model(20, LSTM)
# Fit BiLSTM, LSTM and GRU
def fit_model(model):
early_stop = EarlyStopping(monitor = 'val_loss',
patience = 100)
history = model.fit(X_train, y_train, epochs = 700,
validation_split = 0.2, batch_size = 32,
shuffle = False, callbacks = [early_stop])
return history
history_bilstm = fit_model(model_bilstm)
history_lstm = fit_model(model_lstm)
history_gru = fit_model(model_gru)
这一切 运行 都很顺利,并打印出了我的损失图表。但是当涉及到预测时,我 运行 以下代码
# Make prediction
def prediction(model):
prediction = model.predict(X_test)
prediction = scaler_y.inverse_transform(prediction)
return prediction
prediction_bilstm = prediction(model_bilstm)
prediction_lstm = prediction(model_lstm)
prediction_gru = prediction(model_gru)
我收到以下错误
ValueError Traceback (most recent call last)
<ipython-input-387-9d45f01ae2a2> in <module>
5 return prediction
6
----> 7 prediction_bilstm = prediction(model_bilstm)
8 prediction_lstm = prediction(model_lstm)
9 prediction_gru = prediction(model_gru)
<ipython-input-387-9d45f01ae2a2> in prediction(model)
2 def prediction(model):
3 prediction = model.predict(X_test)
----> 4 prediction = scaler_y.inverse_transform(prediction)
5 return prediction
...
ValueError: Found array with dim 3. Estimator expected <= 2.
根据我读过的其他帖子,我假设这与我的 X_test 形状有关,所以我尝试将其重塑为 2d,但出现另一个错误,告诉我“预期 bidirectional_3_input有 3 个维度,但在第 7 行再次得到形状为 (62, 36)" 的数组。
我做错了什么,我该如何解决?
数据说明:
因此,我尝试使用地下水位(34 个特征)、降水量和温度作为输入来预测排放率(目标变量),这给了我总共 36 个特征。我的数据是每月分辨率。我在我的测试中使用了 63 个观察值(预测 5 年),其余的用于我的训练。
你做错了什么? 假设你的输入数据有 X_train.shape = [d0,d1,d2]
的形状,然后在设置你的 BiLSTM-model 之后
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Bidirectional,LSTM,Dense
model = tf.keras.Sequential()
model.add(
tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(
units = 10,
return_sequences=True),
input_shape=(d1, d2)
)
)
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
我们可以通过
检查您的模型所期望的输入和output-shapes
>>model.input.shape
TensorShape([None, d1, d2])
>>model.output.shape
TensorShape([None, d1, 1])
因此您的模型需要形状 (n_batch,d1,d2)
的输入,其中 n_batch
是数据的批量大小,return 是形状 (n_batch,d1,1)
,因此 3d-张量.
现在,如果您为模型提供 3d 张量,model.prediction
方法将成功 return 3d 张量,但是 sklearn.preprocessing.StandardScaler.inverse_transform
仅适用于 2d 数据,这就是为什么它说
ValueError: Found array with dim 3. Estimator expected <= 2.
另一方面,如果您首先将数据重塑为 2d,那么 model.prediction
会报错,因为它被设置为期望 3d 张量。
如何修复它?如需有关如何修复代码的进一步帮助,您需要向我们提供有关您期望的更详细信息 您的模型要做的事情,尤其是 output-shape 您希望 BiLSTM-model 具有的功能。我假设您实际上希望每个样本的 BiLSTM-model 到 return 一个标量,因此额外的 Flatten
层可能会起作用:
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Bidirectional,LSTM,Dense,Flatten
model = tf.keras.Sequential()
model.add(
tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(
units = 10,
return_sequences=True),
input_shape=(d1, d2)
)
)
model.add(Flatten()) #<-- additional flatten-layer
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
我正在运行使用以下代码构建 LSTM、GRU 和 bilstm 模型
# Create BiLSTM model
def create_model_bilstm(units):
model = Sequential()
model.add(Bidirectional(LSTM(units = units,
return_sequences=True),
input_shape=(X_train.shape[1], X_train.shape[2])))
#model.add(Bidirectional(LSTM(units = units)))
model.add(Dense(1))
#Compile model
model.compile(loss='mse', optimizer='adam')
return model
# Create LSTM or GRU model
def create_model(units, m):
model = Sequential()
model.add(m (units = units, return_sequences = True,
input_shape = [X_train.shape[1], X_train.shape[2]]))
model.add(Dropout(0.1))
#model.add(m (units = units))
#model.add(Dropout(0.2))
model.add(Dense(units = 1))
#Compile model
model.compile(loss='mse', optimizer='adam')
return model
# BiLSTM
model_bilstm = create_model_bilstm(20)
# GRU and LSTM
model_gru = create_model(50, GRU)
model_lstm = create_model(20, LSTM)
# Fit BiLSTM, LSTM and GRU
def fit_model(model):
early_stop = EarlyStopping(monitor = 'val_loss',
patience = 100)
history = model.fit(X_train, y_train, epochs = 700,
validation_split = 0.2, batch_size = 32,
shuffle = False, callbacks = [early_stop])
return history
history_bilstm = fit_model(model_bilstm)
history_lstm = fit_model(model_lstm)
history_gru = fit_model(model_gru)
这一切 运行 都很顺利,并打印出了我的损失图表。但是当涉及到预测时,我 运行 以下代码
# Make prediction
def prediction(model):
prediction = model.predict(X_test)
prediction = scaler_y.inverse_transform(prediction)
return prediction
prediction_bilstm = prediction(model_bilstm)
prediction_lstm = prediction(model_lstm)
prediction_gru = prediction(model_gru)
我收到以下错误
ValueError Traceback (most recent call last)
<ipython-input-387-9d45f01ae2a2> in <module>
5 return prediction
6
----> 7 prediction_bilstm = prediction(model_bilstm)
8 prediction_lstm = prediction(model_lstm)
9 prediction_gru = prediction(model_gru)
<ipython-input-387-9d45f01ae2a2> in prediction(model)
2 def prediction(model):
3 prediction = model.predict(X_test)
----> 4 prediction = scaler_y.inverse_transform(prediction)
5 return prediction
...
ValueError: Found array with dim 3. Estimator expected <= 2.
根据我读过的其他帖子,我假设这与我的 X_test 形状有关,所以我尝试将其重塑为 2d,但出现另一个错误,告诉我“预期 bidirectional_3_input有 3 个维度,但在第 7 行再次得到形状为 (62, 36)" 的数组。
我做错了什么,我该如何解决?
数据说明: 因此,我尝试使用地下水位(34 个特征)、降水量和温度作为输入来预测排放率(目标变量),这给了我总共 36 个特征。我的数据是每月分辨率。我在我的测试中使用了 63 个观察值(预测 5 年),其余的用于我的训练。
你做错了什么? 假设你的输入数据有 X_train.shape = [d0,d1,d2]
的形状,然后在设置你的 BiLSTM-model 之后
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Bidirectional,LSTM,Dense
model = tf.keras.Sequential()
model.add(
tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(
units = 10,
return_sequences=True),
input_shape=(d1, d2)
)
)
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
我们可以通过
检查您的模型所期望的输入和output-shapes>>model.input.shape
TensorShape([None, d1, d2])
>>model.output.shape
TensorShape([None, d1, 1])
因此您的模型需要形状 (n_batch,d1,d2)
的输入,其中 n_batch
是数据的批量大小,return 是形状 (n_batch,d1,1)
,因此 3d-张量.
现在,如果您为模型提供 3d 张量,model.prediction
方法将成功 return 3d 张量,但是 sklearn.preprocessing.StandardScaler.inverse_transform
仅适用于 2d 数据,这就是为什么它说
ValueError: Found array with dim 3. Estimator expected <= 2.
另一方面,如果您首先将数据重塑为 2d,那么 model.prediction
会报错,因为它被设置为期望 3d 张量。
如何修复它?如需有关如何修复代码的进一步帮助,您需要向我们提供有关您期望的更详细信息 您的模型要做的事情,尤其是 output-shape 您希望 BiLSTM-model 具有的功能。我假设您实际上希望每个样本的 BiLSTM-model 到 return 一个标量,因此额外的 Flatten
层可能会起作用:
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Bidirectional,LSTM,Dense,Flatten
model = tf.keras.Sequential()
model.add(
tf.keras.layers.Bidirectional(
tf.keras.layers.LSTM(
units = 10,
return_sequences=True),
input_shape=(d1, d2)
)
)
model.add(Flatten()) #<-- additional flatten-layer
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')