预计将看到1个数组,但获得了以下4个数组的列表

时间:2019-02-05 17:12:33

标签: python arrays pandas keras lstm

我需要一些帮助来适应已编译的Keras模型。特别是我在输入数据的维度方面遇到问题。

X_train =
    [[[1, 16], [1, 19], [1, 17]],
     [[1, 19], [1, 17], [0, 17]],
     [[1, 17], [0, 17], [1, 17]],
     [[0, 17], [1, 17], [1, 18]]]

y_train =
    0  1  0
    1  0  0
    1  0  0
    0  0  1

输入数据具有以下维度:

print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', np.array(X_train).shape )
print('X_test shape:', np.array(X_test).shape )
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)

> 4 train sequences
> 1 test sequences
> X_train shape: (4, 3, 2)
> X_test shape: (1, 3, 2)
> y_train shape: (4, 3)
> y_test shape: (1, 3)

X的形状应解释如下:

  • 样品。样本数量。
  • 时间步伐。一个时间步是样本中的一个观察点。
  • 功能。一种功能是每次观察一次。

(样本-> 4,时间戳-> 3,功能-> 2)

这是Keras模型:

model = Sequential()

model.add(LSTM(
                units=32, # number of units returned by LSTM
                return_sequences=True, 
                input_shape=(timestamps,nb_features),
                dropout=0.2, 
                recurrent_dropout=0.2
              )
         )

model.add(TimeDistributed(Dense(1)))

model.add(Dropout(0.2))

model.add(Flatten())

# model.add(TimeDistributedDense(1))
model.add(Dense(units=nb_classes,
               activation='softmax'))

# Define a performance metric
#sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy",
              metrics = ['accuracy'],
              optimizer='adadelta')

模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_2 (LSTM)                (None, 3, 32)             4480      
_________________________________________________________________
time_distributed_2 (TimeDist (None, 3, 1)              33        
_________________________________________________________________
dropout_2 (Dropout)          (None, 3, 1)              0         
_________________________________________________________________
flatten_2 (Flatten)          (None, 3)                 0         
_________________________________________________________________
dense_4 (Dense)              (None, 3)                 12        
=================================================================
Total params: 4,525
Trainable params: 4,525
Non-trainable params: 0
_________________________________________________________________

在此图中,您可以看到输入为(None, 3, 2),这意味着X_train的(4,3,2)应该可以,但由于某种原因,它不是。 / p>

enter image description here

执行此代码时,它会引发错误:

history = model.fit(X_train, y_train, validation_split=0.25, epochs=500, batch_size=2, shuffle=True, verbose=0)

错误:

  

ValueError:检查模型输入时出错:Numpy数组的列表   您传递给模型的信息不是模型期望的大小。   预计将看到1个数组,但得到以下4个列表   数组:[array([[[1,16],          [1,19],          [[17]]),数组([[1,19],          [1,17],          [[0,17]]),array([[1,17],          [0,17],          [1,17]]),数组([[0,17],          [1,17],         ...

0 个答案:

没有答案