keras 输入层 (Nnoe, 2) 与 LSTM 但没有工作

keras Input layer (Nnoe, 2) with LSTM but didn't work

我尝试创建示例,它们是 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个小时解题,还是不行

任何帮助。我很感激。

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, .....)

可能有帮助的答案: