keras使用LSTM输入图层(Nnoe,2)但不起作用

时间:2017-10-03 09:48:14

标签: tensorflow deep-learning keras lstm rnn

我尝试创建样本,即X_train和y_train。

两个样本都与我的真实数据格式类似。

代码是我使用的。

以下是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import time
import csv
import keras
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.layers.core import Masking
from keras.layers.wrappers import TimeDistributed
from openpyxl import load_workbook
from datetime import datetime

X_arryA = np.array([[1, 2],[3, 8],[9, 10],[6, 7]])
X_arryB = np.array([[1, 2],[3, 8]])
X_arryC = np.array([[1, 2],[3, 8],[9, 10],[6, 7],[9, 10],[6, 7]])
X_train = np.array([X_arryA,X_arryB,X_arryC])
y_arryA = np.array([1,5,3,4])
y_arryB = np.array([2,1])
y_arryC = np.array([6,7,4,2,3,1])
y_train = np.array([y_arryA,y_arryB,y_arryC])
model = Sequential()
layers = [2, 50, 100, 1]
model.add(LSTM(
    input_shape=(None, 2),
    output_dim=layers[1],
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    layers[2],
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(
    output_dim=layers[3]))
model.add(Activation("linear"))

start = time.time()
model.compile(loss="mse", optimizer="rmsprop")
#print "Compilation Time : ", time.time() - start
model.summary()
model.fit(X_train, y_train, batch_size=1, nb_epoch=1, validation_split=0.05)

我检查了model.summary()。

我认为结构没问题。

有些消息显示:

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\ipykernel_launcher.py:14: UserWarning: Update your `LSTM` call to the Keras 2 API: `LSTM(units=50, input_shape=(None, 2), return_sequences=True)`

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_77 (LSTM)               (None, None, 50)          10600     
_________________________________________________________________
dropout_65 (Dropout)         (None, None, 50)          0         
_________________________________________________________________
lstm_78 (LSTM)               (None, 100)               60400     
_________________________________________________________________
dropout_66 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_36 (Dense)             (None, 1)                 101       
_________________________________________________________________
activation_33 (Activation)   (None, 1)                 0         
=================================================================
Total params: 71,101
Trainable params: 71,101
Non-trainable params: 0
_________________________________________________________________
C:\Users\user\Anaconda3\envs\py35\lib\site-packages\ipykernel_launcher.py:23: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=1)`
C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\models.py:848: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  warnings.warn('The `nb_epoch` argument in `fit` '
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-509-c6f954bdb474> in <module>()
     28 #print "Compilation Time : ", time.time() - start
     29 model.summary()
---> 30 model.fit(X_train, y_train, batch_size=1, nb_epoch=1, validation_split=0.05)

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    865                               class_weight=class_weight,
    866                               sample_weight=sample_weight,
--> 867                               initial_epoch=initial_epoch)
    868 
    869     def evaluate(self, x, y, batch_size=32, verbose=1,

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1520             class_weight=class_weight,
   1521             check_batch_axis=False,
-> 1522             batch_size=batch_size)
   1523         # Prepare validation data.
   1524         do_validation = False

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
   1376                                     self._feed_input_shapes,
   1377                                     check_batch_axis=False,
-> 1378                                     exception_prefix='input')
   1379         y = _standardize_input_data(y, self._feed_output_names,
   1380                                     output_shapes,

C:\Users\user\Anaconda3\envs\py35\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    130                                  ' to have ' + str(len(shapes[i])) +
    131                                  ' dimensions, but got array with shape ' +
--> 132                                  str(array.shape))
    133             for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
    134                 if not j and not check_batch_axis:

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

我花了5个小时来解决这个问题,但它仍然无效。

任何帮助。我很感激。

1 个答案:

答案 0 :(得分:1)

LSTM图层只接受(numberOfSequences, numberOfSteps, featuresPerStep)

等形状

这些是错误消息中提到的预期的3个维度。 您需要正确准备数据以适应这些维度。

问题是numpy数组不能接受变量大小。它必须是一个定义明确的矩阵。

当你给numpy数组提供3个不同长度X_arry时,结果不可能适合numpy数组,然后它会生成一个数组数组。 (Keras无法处理这个问题,它需要单个阵列)。

使用可变长度,您必须使用虚拟值填充每个数组并添加masking图层,或者只是单独训练每个长度。

X_arryLen4 = np.asarray([[[1, 2],[3, 8],[9, 10],[6, 7]]])
X_arryLen2 = np.asarray([[[1, 2],[3, 8]]])
X_arryLen6 = np.asarray([[[1, 2],[3, 8],[9, 10],[6, 7],[9, 10],[6, 7]]])

model.fit(X_arryLen4, .....)
model.fit(X_arryLen2, .....)
model.fit(X_arryLen6, .....)

可能有帮助的答案: