gpt4 book ai didi

python - RNN LSTM 输入形状应该是什么?

转载 作者:行者123 更新时间:2023-11-30 09:18:55 25 4
gpt4 key购买 nike

我对 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: Error when checking target: expected weightedAverage_output to have shape (None, 4) but got array with shape (29, 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/

关于python - RNN LSTM 输入形状应该是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com