【发布时间】:2021-01-13 06:23:30
【问题描述】:
您好,我无法为我的 LTSM 模型找到正确的输入形状。我一直在努力寻找合适的形状,但无法理解需要什么。
我认为问题在于 ytest 和 ytrain 形状。为什么和xtrain和xtest的形状不一样?
xtrain (80304, 37)
xtest (39538, 37)
ytrain (80304,)
ytest (39538,)
Epoch 1/3
2510/2510 [==============================] - 34s 13ms/step - loss: nan
Epoch 2/3
2510/2510 [==============================] - 32s 13ms/step - loss: nan
Epoch 3/3
2510/2510 [==============================] - 33s 13ms/step - loss: nan
Model: "sequential_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_10 (LSTM) (None, 4) 96
_________________________________________________________________
dense_9 (Dense) (None, 1) 5
=================================================================
Total params: 101
Trainable params: 101
Non-trainable params: 0
当我尝试拟合这个模型时:
print('tf version', tf.version.VERSION)
train_size = int(len(oral_ds) * 0.67)
print(train_size)
test_size = len(oral_ds) - train_size
print(test_size)
train = oral_ds[:train_size]
test = oral_ds[80319:119881]
print(len(train), len(test))
X_train = train.drop(columns=['PRICE','WEEK_END_DATE','Optimized rev','Original rev'])
y_train = train.PRICE
X_test = test.drop(columns=['PRICE','WEEK_END_DATE','Optimized rev','Original rev'])
y_test = test.PRICE
print('xtrain',np.shape(X_train))
print('xtest',np.shape(X_test))
print('ytrain',np.shape(y_train))
print('ytest',np.shape(y_test))
X_train=X_train.values.reshape(X_train.shape[0],X_train.shape[1],1)
#y_train=y_train.values.reshape(y_train.shape[0],y_train.shape[1],1)
X_test=X_test.values.reshape(X_test.shape[0],X_test.shape[1],1)
#y_test=y_test.values.reshape(y_test.shape[0],y_test.shape[1],1)
#print('reshaped xtrain',np.shape(X_train))
#print('reshaped xtest',np.shape(X_test))
#print('reshaped ytrain',np.shape(y_train))
#print('reshaped ytest',np.shape(y_test))
single_step_model = tf.keras.models.Sequential()
single_step_model.add(tf.keras.layers.LSTM(4,
input_shape=(37,1)))
single_step_model.add(tf.keras.layers.Dense(units = 1))
single_step_model.compile(optimizer = 'adam', loss = 'mean_squared_error')
BATCH_SIZE=32
train_data = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = train_data.cache().shuffle(10000).batch(BATCH_SIZE)
valid_data = tf.data.Dataset.from_tensor_slices((X_test, y_test))
history = single_step_model.fit(train_data, epochs=3)
single_step_model.summary()
我已尝试实施其他帖子中的解决方案,例如:
- https://datascience.stackexchange.com/questions/39334/recurrent-neural-network-lstm-dimensions-error
- ValueError: Input 0 of layer cu_dnnlstm is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 175]
但这些都不起作用。
有什么指导吗?
【问题讨论】:
-
点击此链接解决您的问题。这里解释的很清楚。 stackoverflow.com/a/54416792/12598386
-
谢谢 Tamang,我已经尝试过了,但它似乎不起作用。我已更新我的代码以显示相同的错误消息
标签: python tensorflow machine-learning keras lstm