我对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具有形状(无,4)但是具有形状的数组(29,1)
很确定我在输入和输出维度的某个地方出错了。
有什么想法吗?
答案 0 :(得分:0)
首先,您需要更改最终输出图层的尺寸:
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/