尝试从Keras运行顺序模型时发生ValueError

时间:2020-08-10 18:17:23

标签: python tensorflow machine-learning keras valueerror

我正在尝试使用Keras构建NARX NN。我仍然不确定在LSTM神经元中使用参数 return_sequence = True ,但是,在我能确认之前,我需要使代码正常工作。当我尝试运行它时,出现以下消息:

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

请参阅下面的代码。运行 model.fit 命令时会引发错误。我的数据数据具有40101个时间步长x 6个特征(3个外部输入,3个系统响应)的形状。

import numpy as np
import pandas as pd
from sklearn.model_selection import TimeSeriesSplit
import tensorflow as tf
from tensorflow.keras import initializers

# --- main
data = pd.read_excel('example.xlsx',usecols=['wave','wind','current','X','Y','RZ'])
data.plot(subplots=True, figsize=[15,10])

x_data = np.array(data.loc[:,['wave','wind','current']])
y_data = np.array(data.loc[:,['X','Y','RZ']])

timeSeriesCrossValidation = TimeSeriesSplit(n_splits=5)
for train, validation in timeSeriesCrossValidation.split(x_data, y_data):  

    # create model
    model = tf.keras.models.Sequential()
    
    # input layer
    model.add(tf.keras.layers.LSTM(units=50,
                                   input_shape=(40101,3),
                                   dropout=0.01,
                                   recurrent_dropout=0.2,
                                   kernel_initializer=initializers.RandomNormal(mean=0,stddev=.5),
                                   bias_initializer=initializers.Zeros(),
                                   return_sequences = True))
    
    # 1st hidden layer
    model.add(tf.keras.layers.LSTM(units=50,
                                   dropout=0.01,
                                   recurrent_dropout=0.2,
                                   kernel_initializer=initializers.RandomNormal(mean=0,stddev=.5),
                                   bias_initializer=initializers.Zeros(),
                                   return_sequences = True))
    
    # 2nd hidder layer
    model.add(tf.keras.layers.LSTM(units=50,
                                   dropout=0.01,
                                   recurrent_dropout=0.2,
                                   kernel_initializer=initializers.RandomNormal(mean=0,stddev=.5),
                                   bias_initializer=initializers.Zeros(),
                                   return_sequences = False))
    
    # output layer
    model.add(tf.keras.layers.Dense(3))
    
    model.compile(loss='mse',optimizer='nadam',metrics=['accuracy'])

    model.fit(x_data[train], y_data[train],
              verbose=2,
              batch_size=None,
              epochs=10,
              validation_data=(x_data[validation], y_data[validation])
              #callbacks=early_stop
              )
    
    prediction = model.predict(x_data[validation])
    y_validation = y_data[validation]

1 个答案:

答案 0 :(得分:1)

LSTM层需要在3维上输入:

(n_samples, time_steps, features)

您以以下格式传递了数据:

(n_samples, features)

由于您没有创建时间步的功能,因此最简单的解决方案是将输入更改为形状:

(40101, 1, 3)

伪造数据:

x_data = np.random.rand(40101, 1, 3)
y_data = np.random.rand(40101, 3)

此外,您不应在Keras层的input_shape参数中传递样本数。只需使用此:

input_shape=(1, 3)

这是经过纠正的代码(带有伪造的数据):

import numpy as np
from sklearn.model_selection import TimeSeriesSplit
import tensorflow as tf
from tensorflow.keras import initializers
from tensorflow.keras.layers import *

x_data = np.random.rand(40101, 1, 3)
y_data = np.random.rand(40101, 3)

timeSeriesCrossValidation = TimeSeriesSplit(n_splits=5)
for train, validation in timeSeriesCrossValidation.split(x_data, y_data):
    # create model
    model = tf.keras.models.Sequential()

    # input layer
    model.add(LSTM(units=5,
                   input_shape=(1, 3),
                   dropout=0.01,
                   recurrent_dropout=0.2,
                   kernel_initializer=initializers.RandomNormal(mean=0, stddev=.5),
                   bias_initializer=initializers.Zeros(),
                   return_sequences=True))

    # 1st hidden layer
    model.add(LSTM(units=5,
                   dropout=0.01,
                   recurrent_dropout=0.2,
                   kernel_initializer=initializers.RandomNormal(mean=0, stddev=.5),
                   bias_initializer=initializers.Zeros(),
                   return_sequences=True))

    # 2nd hidder layer
    model.add(LSTM(units=50,
                   dropout=0.01,
                   recurrent_dropout=0.2,
                   kernel_initializer=initializers.RandomNormal(mean=0, stddev=.5),
                   bias_initializer=initializers.Zeros(),
                   return_sequences=False))

    # output layer
    model.add(tf.keras.layers.Dense(3))

    model.compile(loss='mse', optimizer='nadam', metrics=['accuracy'])

    model.fit(x_data[train], y_data[train],
              verbose=2,
              batch_size=None,
              epochs=1,
              validation_data=(x_data[validation], y_data[validation])
              # callbacks=early_stop
              )

    prediction = model.predict(x_data[validation])
    y_validation = y_data[validation]

如果您希望函数创建时间步,请使用以下方法:

def multivariate_data(dataset, target, start_index, end_index, history_size,
                      target_size, step, single_step=False):
  data = []
  labels = []

  start_index = start_index + history_size
  if end_index is None:
    end_index = len(dataset) - target_size

  for i in range(start_index, end_index):
    indices = range(i-history_size, i, step)
    data.append(dataset[indices])

    if single_step:
      labels.append(target[i+target_size])
    else:
      labels.append(target[i:i+target_size])

  return np.array(data), np.array(labels)

它将为您提供正确的形状,例如:

multivariate_data(dataset=np.random.rand(40101, 3), 
                  target=np.random.rand(40101, 3), 
                  0, len(x_data), 5, 0, 1, True)[0].shape
(40096, 5, 3)

您丢失了5个数据点,因为在开始时您无法回顾过去的5个步骤。