【问题标题】:What is RNN LSTM input shape supposed to be?RNN LSTM 输入形状应该是什么?
【发布时间】:2018-04-21 21:29:13
【问题描述】:

我对 LSTM 输入大小的工作原理感到困惑。

我有一个场景,我试图根据时间序列中的体重和身高来预测一个人的体重。

我似乎无法弄清楚我在尺寸方面哪里出错了:

from datetime import datetime
import numpy as np
import pandas as pd
import math
from keras.layers import Dense, Activation, LSTM, Input, concatenate
from keras.models import Model

def create_dataset(dataset, window_length=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-window_length-1):
        # print("dataX from {} to {}".format(i, i+window_length))
        # print("dataY from {}".format(i+window_length))
        dataX.append(dataset[i:(i+window_length)])
        dataY.append(dataset[(i + window_length):])


    return np.array(dataX), np.array(dataY)

def buildModel(dataLength, labelLength):

    weight = Input(shape=(dataLength, 1), name="weight")
    height = Input(shape=(dataLength, 1), name="height")

    weightLayers = LSTM(64, return_sequences=False)(weight)
    heightLayers = LSTM(64, return_sequences=False)(height)

    output = concatenate([ weightLayers, heightLayers ])

    output = Dense(labelLength, activation="linear", name="weightedAverage_output")(output)

    model = Model(
        inputs=[weight, height],
        outputs=[output]
        )

    model.compile(optimizer="rmsprop", loss="mse")

    return model

bogus = {
    "weight": range(100,200),
    "height": range(150,250)
}

dataset = pd.DataFrame(bogus)



train_size      = int(len(dataset) * 0.90)
test_size       = len(dataset) - train_size
train, test     = dataset[:train_size], dataset[-test_size:]

# print("*" * 30)
# print(train.head())
# print(train.tail())
# print("==> {}".format(len(train)))
# print("*" * 30)
# print(test.head())
# print(test.tail())
# print("==> {}".format(len(test)))
# input(">")

height_train = np.array(train["height"].values.tolist()).reshape((-1, 1)).astype('float32')
weight_train = np.array(train["weight"].values.tolist()).reshape((-1, 1)).astype('float32')

height_test = np.array(test["height"].values.tolist()).reshape((-1, 1)).astype('float32')
weight_test = np.array(test["weight"].values.tolist()).reshape((-1, 1)).astype('float32')

x_train_height, y_train_height  = create_dataset(height_train, 60)
x_train_weight, y_train_weight  = create_dataset(weight_train, 60)

x_test_height, y_test_height  = create_dataset(height_test, 60)
x_test_weight, y_test_weight  = create_dataset(weight_test, 60)



model = buildModel(60,4)
model.fit(
    [ 
        x_train_weight,
        x_train_height,
    ],

    [ 
        y_train_weight
    ],

    validation_data=(
        [
            x_test_weight,
            x_test_height,
        ],
        [
            y_test_weight
        ],
    ),

    epochs=1,
    batch_size=3000,
    callbacks=[
        # board.createTensorboardConfig("log/graph"),
    ]
)

我收到此错误:

ValueError: 检查目标时出错:预期 weightedAverage_output 的形状为 (None, 4) 但得到的数组的形状为 (29, 1)

很确定我在输入和输出维度的某个地方出错了。

有什么想法吗?

【问题讨论】:

  • 你能不能做一个 model.summary() 并在这里发布?
  • 所以我可以看到,你需要在连接权重层和高度层后重塑你的输出变量。
  • 你能告诉我怎么做吗?
  • 你能把这个 'output = Dense(labelLength, activation="linear", name="weightedAverage_output")(output)' 改成 'output = Dense(1, activation="linear", name ="weightedAverage_output")(output) ' 让我知道这是否有效?

标签: python machine-learning time-series lstm rnn


【解决方案1】:

首先您需要更改最终输出层的尺寸:

output = Dense(1, activation="linear", name="weightedAverage_output")(output)

其次,您需要更改输入维度以包含时间步长:

timesteps = 1
weight = Input(shape=(dataLength,timesteps,1), name="weight")
height = Input(shape=(dataLength,timesteps,1), name="height")

时间步长可以在 (0,inf) 之间。

您可以参考这篇文章以进一步了解时间步:https://machinelearningmastery.com/use-timesteps-lstm-networks-time-series-forecasting/

【讨论】:

  • ValueError: Input 0 is in compatible with layer lstm_1: expected ndim=3, found ndim=4
  • 时间步长到底是什么意思?你的意思是你想要考虑的滞后? (即自回归部分)还是您指的是滞后样本序列意义上的时间步长? NONE,NONE 表达了他们的意思,大多数人使用错误的术语
猜你喜欢
  • 2020-11-27
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多