【发布时间】: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