您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – Keras LSTM输入维度设置

5b51 2022/1/14 8:23:09 python 字数 2176 阅读 598 来源 www.jb51.cc/python

我试图用keras训练LSTM模型,但我觉得我在这里弄错了. 我收到了错误 ValueError: Error when checking input: expected lstm_17_input to have 3 dimensions, but got array with shape (10000, 0, 20) 而我的代码看起来像 model = Sequential() model.a

概述

我收到了错误

ValueError: Error when checking input: expected lstm_17_input to have
3 dimensions,but got array with shape (10000,20)

而我的代码看起来像

model = Sequential()
model.add(LSTM(256,activation="relu",dropout=0.25,recurrent_dropout=0.25,input_shape=(None,20,64)))
model.add(Dense(1,activation="sigmoid"))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X_train,y_train,batch_size=batch_size,epochs=10)

其中X_train的形状为(10000,20),前几个数据点类似

array([[ 0,...,40,9],[ 0,33,51],54,50],...

y_train的形状为(10000,),它是二进制(0/1)标签数组.

有人能指出我错在哪里吗?

首先,LSTM与Keras中的所有图层一样,接受两个参数:input_shape和batch_input_shape.不同之处在于,input_shape不包含批量大小,而batch_input_shape是包含批量大小的完整输入形状.

因此,规范input_shape =(None,64)告诉keras期望一个4维输入,这不是你想要的.正确的只是(20,).

但那还不是全部. LSTM层是一个循环层,因此它需要一个三维输入(batch_size,timesteps,input_dim).这就是为什么正确的规范是input_shape =(20,1)或batch_input_shape =(10000,1).此外,您的训练阵列也应该重新塑造,以表示每步有20个时间步和1个输入功能.

因此,解决方案:

X_train = np.expand_dims(X_train,2)  # makes it (10000,1)
...
model = Sequential()
model.add(LSTM(...,input_shape=(20,1)))

总结

以上是编程之家为你收集整理的python – Keras LSTM输入维度设置全部内容,希望文章能够帮你解决python – Keras LSTM输入维度设置所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶