首页 > 解决方案 > 传入 keras 模型的数组维度

问题描述

我试图让 Keras 接受某些尺寸的输入,而有些东西是没有意义的。1)我改变了我的数组的形状,但 .shape 函数返回相同的东西,没有任何变化。2)我相信我的数据是正确的,模型不接受它。

完整代码(复制和粘贴)

import numpy as np
from keras.models import Sequential #, LSTM
from keras.layers.core import  Dense;
from keras.layers import LSTM
import tensorflow as tf    
price_1 = [0,0.2,0.6 , 0.87]
price_2 = [0.05,0.2,0.6 , 0.45]
price_3 = [0.25,0.62,0.9 , 0.22]
dates = [0.2, 0.4, 0.6, 0.8 , 1.0]
Y = np.array(dates)
X = np.hstack([np.array(price_1).reshape(- 1, 1), np.array(price_2).reshape(- 1, 1), np.array(price_3).reshape(- 1, 1)])
print(X.shape)  # (4, 3)     
X = X.reshape(X.shape[0], 1, X.shape[1])
print(X.shape)  # (4, 1, 3) looks good to me?

model = Sequential()
model.add(LSTM(10 , return_sequences = False , input_shape =(len(X) , 1 , len(X[0][0])) ))

# error here = ValueError: Input 0 of layer lstm is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 4, 1, 3)
# its looks right shape to me   4,1,3 looks right to me?

奇怪的是,我尝试更改我的数据,所以我有 4 行和 3 列而不是 3 行和 4 列,并且数组的形状完全相同......没有倒置。

备用数据:

price_1 = [0,0.2,0.6]
price_2 = [0.05,0.2,0.6 ]
price_3 = [0.25,0.62,0.9 ]
price_4 = [0.25,0.62,0.9 ]
X = np.hstack([np.array(price_1).reshape(- 1, 1), np.array(price_2).reshape(- 1, 1),np.array(price_3).reshape(- 1, 1) , np.array(price_4).reshape(- 1, 1)])

print(X.shape)  # (4, 3)    STILL?  HOW?   SURELY NOW ITS 3,4   NOT 4,3  !!!!!

当然,备用数据的形状应该与我的原始数据不同,不是吗?

标签: pythonnumpykeraslstmrecurrent-neural-network

解决方案


推荐阅读