keras LSTM模型中的尺寸不匹配 [英] Dimensions not matching in keras LSTM model

查看:153
本文介绍了keras LSTM模型中的尺寸不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有keras的LSTM神经网络来预测时间序列组,但是在使模型与我想要的匹配时遇到了麻烦.我的数据的维度是:

I want to use an LSTM neural Network with keras to forecast groups of time series and I am having troubles in making the model match what I want. The dimensions of my data are:

输入张量:(data length, number of series to train, time steps to look back)

输出张量:(data length, number of series to forecast, time steps to look ahead)

注意:我想保持尺寸完全一样,否 换位.

Note: I want to keep the dimensions exactly like that, no transposition.

重现此问题的虚拟数据代码是:

A dummy data code that reproduces the problem is:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed, LSTM

epoch_number = 100
batch_size = 20
input_dim = 4
output_dim = 3
look_back = 24
look_ahead = 24
n = 100

trainX = np.random.rand(n, input_dim, look_back)
trainY = np.random.rand(n, output_dim, look_ahead)
print('test X:', trainX.shape)
print('test Y:', trainY.shape)

model = Sequential()

# Add the first LSTM layer (The intermediate layers need to pass the sequences to the next layer)
model.add(LSTM(10, batch_input_shape=(None, input_dim, look_back), return_sequences=True))

# add the first LSTM layer (the dimensions are only needed in the first layer)
model.add(LSTM(10, return_sequences=True))

# the TimeDistributed object allows a 3D output
model.add(TimeDistributed(Dense(look_ahead)))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model.fit(trainX, trainY, nb_epoch=epoch_number, batch_size=batch_size, verbose=1)

这条辫子:

异常:检查模型目标时出错:预期 timedistributed_1具有形状(无,4、24),但具有形状的数组 (100,3,24)

Exception: Error when checking model target: expected timedistributed_1 to have shape (None, 4, 24) but got array with shape (100, 3, 24)

问题似乎出在定义TimeDistributed层时.

The problem seems to be when defining the TimeDistributed layer.

如何定义TimeDistributed层,以便对其进行编译和训练?

How do I define the TimeDistributed layer so that it compiles and trains?

推荐答案

我认为问题是您期望在output_dim(!= input_dim). io/layers/wrappers/#timedistributed"rel =" nofollow> TimeDistributed ,但这是不可能的.此维度是它认为的时间维度:已保留.

I think the problem is that you expect output_dim (!= input_dim) at the output of TimeDistributed, while it's not possible. This dimension is what it considers as the time dimension: it is preserved.

输入的数据至少应为3D,索引的尺寸为一个 被认为是时间维度.

The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.

TimeDistributed的目的是将相同的图层应用于每个时间步骤.您只能以与开始时相同的时间步数结束.

The purpose of TimeDistributed is to apply the same layer to each time step. You can only end up with the same number of time steps as you started with.

如果您确实需要将此尺寸从4减小到3,我认为您需要在末尾添加另一层,或使用与TimeDistributed不同的东西.

If you really need to bring down this dimension from 4 to 3, I think you will need to either add another layer at the end, or use something different from TimeDistributed.

PS:发现此问题的一个提示是,在创建模型时从未使用过output_dim,它仅出现在验证数据中.虽然只是一种代码味道(此观察可能没有什么不对),但值得检查一下.

PS: one hint towards finding this issue was that output_dim is never used when creating the model, it only appears in the validation data. While it's only a code smell (there might not be anything wrong with this observation), it's something worth checking.

这篇关于keras LSTM模型中的尺寸不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆