Input 0 of layer “lstm_1“ is incompatible with the layer: expected ndim=3, found ndim=2. Full shape=

This is Chinese version
[中文版]

1 Where this problem took place?

While using TensorFlow framework.


2 Description

When I fit the LSTM model based on TensorFlow, I got an error report
Input 0 of layer "lstm_37" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (16, 7)

Model.fit(X_train, Y_train, batch_size = BATCH_SIZE, epochs = EPOCHS, shuffle = True, verbose = 1, validation_split = VALIDATION_SPLIT)

3 Why?

Because the shape of the input array is incompatible with the layer, which expected a 3D array, but we only have 2D.


4 Solutions

These are original words,

X_train = df_1.values

4.1 Solution 1

.reshape()

X_train = X_train.reshape(dimi_x,dim_y,dim_z)

.reshape could set the shape of the array directly and manually.

4.2 Solution 2

np.expand_dims()
numpy is for doing the math

import numpy as np   # import numpy module

X_train = np.expand_dims(X_train, axis=0)

This is a simple way to solve this problem.

4.3 Solution 3

Lambda(lambda _:K.expand_dims(_,axis=-1))()

import keras.backend as K
from keras.layers import Lambda

X_train = Lambda(lambda d_1:K.expand_dims(X_train,axis=-1))(X_train)

Warning: This method may create new error like below,
ValueError: Input 0 of layer "lstm_43" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 63360, 7, 1)
Because the shape of input array has 4 dimensions which is more than expected dimension number, now you could just try to delete
input_shape = X_train.shape

4.4 Solution 4

X_train = X_train[:, None]
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值