ValueError:连续图层_37的输入0与该图层不兼容:预期ndim = 3,找到的ndim = 2。收到完整的形状:[无,15]

时间:2020-09-04 19:28:47

标签: python tensorflow keras deep-learning lstm

我已经做了所有我知道的尝试。 同样,input_dim = 15的所有组合。如果有人可以帮助我?

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

(233941,15)

(233941,1)

(100261,15)

(100261,)

我已经使用input_dim =(233941,15)和input_dim =(233941,1)进行了测试。但是我仍然找不到问题。 我的问题可以在数据集划分中吗?

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dense, Dropout, LSTM
model = Sequential()
model.add(LSTM(100, input_dim=15, return_sequences=True))
model.add(Dropout(0.3))

model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))
#3 camada
model.add(LSTM(50, return_sequences = True))
model.add(Dropout(0.3))

model.add(LSTM(units = 50))
model.add(Dropout(0.3))

model.add(Dense(1, activation='sigmoid'))

# Compile model
model.compile(optimizer = 'adam', loss = 'mean_squared_error',
                  metrics = ['mean_absolute_error'])
# Fit the model
model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))
Epoch 1/100
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-2e6c4d489e38> in <module>()
     21                   metrics = ['mean_absolute_error'])
     22 # Fit the model
---> 23 model.fit(x_train,y_train,epochs=100, validation_data=(x_test,y_test))

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:180 assert_input_compatibility
        str(x.shape.as_list()))

    ValueError: Input 0 of layer sequential_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 15]

1 个答案:

答案 0 :(得分:1)

正如Marco Certliani在评论中提到的那样,您需要正确设置RNN的输入格式,因为提到的错误是3维的。

这里是输入张量应该是什么样的表示: enter image description here

这意味着您的3D张量将​​具有(batch_size,time_step,input_dimension)的形状。