LSTM keras-值错误如何解析输入维

时间:2018-10-23 03:55:49

标签: python neural-network keras lstm dimensions

我提供了一种训练数据的功能,该功能可以选择正或负的随机文件。这是一个二进制分类问题。

    model=Sequential()
InputBatch = np.expand_dims(InputBatch, 0)
print(InputBatch.shape)
model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)

InputBatch变量的形状为(1,6,30)

例如我的输入数据是

[[   nan  1520.  1295.    nan  8396.  9322. 12715.    nan  5172.  7232.
  11266.    nan 11266.  2757.  4416. 12020. 12111.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  3045. 11480.   900.  5842. 11496.  4463.    nan 11956.   900.
  10400.  8022.  2504. 12106.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  9307. 12003.  2879.  6398.  9372.  4614.  5222.    nan    nan
   2879. 10364.  6923.  4709.  4860. 11871.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  6689.  2818. 12003.  6480.    nan     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  3395.  1087. 11904.  7232.  8840. 10115.  4494. 11516.  7441.
   8535. 12106.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]
 [   nan  1287.   420.  4070. 11087.  7410. 12186.  2387. 12111.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.
      0.     0.     0.     0.     0.     0.     0.     0.     0.     0.]]

我将数据的形状设置为(6,30)

我收到价值错误

ValueError: Error when checking input: expected lstm_16_input to have 3 dimensions, but got array with shape (1,6, 30)

正在收到三维输入,我不知道如何以及为什么

1 个答案:

答案 0 :(得分:2)

LSTM输入必须位于numpys reshape()中。而且您的数据似乎是二维的。您可以使用array.reshape(6,1,30)函数将数据转换为3D。

例如,如果您使用1个时间步长,则必须将其重塑为array.reshape(1,6,30);如果您使用6个时间步长,则必须重塑 model=Sequential() InputBatch = np.expand_dims(InputBatch, 0) print(InputBatch.shape) model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True)) model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy']) model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)

有关重塑LSTM输入的更多信息,请检查此site

[[更新]] 您的代码有很多问题

    a=np.array([ 
             [0,1520,1295,0,8396,9322,12715,0,5172,7232,11266,0,11266,2757,4416,12020,12111,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,3045,11480,900,5842,11496,4463,0,11956,900,10400,8022,2504,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,9307,12003,2879,6398,9372,4614,5222,0,0,2879,10364,6923,4709,4860,11871,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,6689,2818,12003,6480,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,3395,1087,11904,7232,8840,10115,4494,11516,7441,8535,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
             [0,1287,420,4070,11087,7410,12186,2387,12111,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
           ]
          )

PositiveOrNegativeLabel=np.array([[1]])
PositiveOrNegativeLabel=PositiveOrNegativeLabel.reshape(1,-1)
PositiveOrNegativeLabel.shape
InputBatch =InputBatch.reshape(1,6,30)
InputBatch.shape
model=Sequential()
model.add(LSTM(1,input_shape=(6,30)))
model.add(Dense(1,activation="sigmoid"))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=1,verbose=1)

当您将数据转换为(1,6,30)时,您基本上是在说您只有一个样本(只有1个),batch_size为6但您只有1个样本,您只有一个样本,但是您在做验证拆分。由于您只有一个X值,所以它将只有一个Y(PositiveOrNegativeLabel),因此我只建议了一个值,即1。

我已经运行了您的程序,所做的更改与您在问题中显示的代码和数据几乎没有变化(我将NA更改为0):

Object.prototype.isString = function() { return false; };
String.prototype.isString = function() { return true; };

var isString = function(a) {
  return (a !== null) && (a !== undefined) && a.isString();
};
相关问题