LSTM模型(在Keras中),每个输入具有不同的行数

时间:2018-04-26 00:26:45

标签: python keras lstm rnn sequential

所以我试图在Keras中安装一个简单的LSTM模型。

我的数据如下:

Student ID, feature1,feature2,feature3, feature..21 time,labely
1,  some value , some value, some value,time1,..,  y1
1,  some value , some value, some value,time2,..,  y2
1,  some value , some value, some value,time3,..,  y1
1,  some value , some value, some value,time4,..,  y1
2,  some value , some value, some value,time1,..,  y1
2,  some value , some value, some value,time2,..,  y2
3,  some value , some value, some value,time1,..,  y1
3,  some value , some value, some value,time2,..,  y2 
3,  some value , some value, some value,time3,..,  y1

因此,对于每个学生,我可以看到不同数量的观察结果。我想构建一个LSTM,它学习观察序列以预测y标签。功能的数量是固定的= 21。

我是LSTM的新手,我从数据维度开始面临以下问题:

问题1:我的输入格式为:

X[0]:
[array([[  0.00000000e+00,   0.00000000e+00,   0.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   8.34490000e+04],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   8.34490000e+04],
       [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   8.34490000e+04],
       ...,
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   7.11650000e+04],
       [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   7.11650000e+04],
       [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00, ...,
          0.00000000e+00,   0.00000000e+00,   7.11650000e+04]], dtype=float32)]

我的输出格式是:

y[0],y[1]:
 [array([  1.,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,   0.,  nan,   0.,
        nan,  nan,  nan,  nan,  nan,   1.,  nan,  nan,   1.,   1.,  nan,
        nan,  nan,  nan,   1.,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan], dtype=float32)
 array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,   0.,  nan], dtype=float32)]]

np.shape输出给出了y(19,)和x(33,)。我无法将其重塑为3D阵列。

问题2:我的lstm在

https://codeshare.io/5zv4ok

我得到的错误是:

  

ValueError:检查输入时出错:预期lstm_1_input有3个维度,但得到的数组有形状(19,1)

我想知道如何继续我的模型。我也对如何设置时间步长参数感到困惑,因为它对每个学生都有所不同。

1 个答案:

答案 0 :(得分:0)

Keras需要一个多维数组才能将其转换为张量(在tensorflow或您正在使用的任何后端)。因此,除了batch_size之外,通常不可能使用不同的尺寸调用拟合。

  1. 您的问题有几种可能的解决方案:您可以填充 您的序列用零,以便每个样本具有相同的序列 len(最简单的解决方案)
  2. 您可以批量分拣样品,每批产品都有 特定序列len然后将其提供给您的网络
  3. 使用batch_size = 1
  4. 训练模型