检查目标时出错:预期 time_distributed_5 有 3 个维度,但得到了形状为 (14724, 1) 的数组 [英] Error when checking target: expected time_distributed_5 to have 3 dimensions, but got array with shape (14724, 1)

查看:17
本文介绍了检查目标时出错:预期 time_distributed_5 有 3 个维度,但得到了形状为 (14724, 1) 的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试建立单输出回归模型,但最后一层似乎有问题

Trying to build a single output regression model, but there seems to be problem in the last layer

inputs = Input(shape=(48, 1))
lstm = CuDNNLSTM(256,return_sequences=True)(inputs)
lstm = Dropout(dropouts[0])(lstm)

#aux_input
auxiliary_inputs = Input(shape=(48, 7))
auxiliary_outputs = TimeDistributed(Dense(4))(auxiliary_inputs)
auxiliary_outputs = TimeDistributed(Dense(7))(auxiliary_outputs)

#concatenate
output = keras.layers.concatenate([lstm, auxiliary_outputs])

output = TimeDistributed(Dense(64, activation='linear'))(output)
output = TimeDistributed(Dense(64, activation='linear'))(output)
output = TimeDistributed(Dense(1, activation='linear'))(output)

model = Model(inputs=[inputs, auxiliary_inputs], outputs=[output])

我是 keras 的新手...我收到以下错误

I am new to keras... I am getting the following error

ValueError: 检查目标时出错:预期 time_distributed_5 有 3 个维度,但得到形状为 (14724, 1) 的数组

ValueError: Error when checking target: expected time_distributed_5 to have 3 dimensions, but got array with shape (14724, 1)

推荐答案

好吧伙计们,我想我找到了解决办法根据 - https://keras.io/layers/wrappers/ 它说我们正在申请每个时间步的密集层(在我的情况下,我有 48 个时间步).因此,我最后一层的输出将是(batch_size、timesteps、dimensions),如下所示:

Okay guys, think I found a fix According to - https://keras.io/layers/wrappers/ it says that we are applying dense layer to each timestep (in my case I have 48 timesteps). So, the output of my final layer would be (batch_size, timesteps, dimensions) for below:

output = TimeDistributed(Dense(1, activation='linear'))(output)

将是 (?,48,1) 因此尺寸不匹配.但是,如果我想将其转换为单个回归输出,我们将不得不展平最终的 TimeDistributed 层

will be (?,48,1) hence the dimensions mismatch. However, If I want to convert this to single regression output we will have to flatten the final TimeDistributed layer

所以我添加了以下几行来修复它:

so I added the following lines to fix it:

output = Flatten()(output)
output = (Dense(1, activation='linear'))(output)

所以现在时间分布层平坦化为 49 个输入(看起来包括偏置输入)到最终密集层为单个输出.

so now the timedistributed layer flattens to 49 inputs(looks like a bias input is included) to the final dense layer into a single output.

好的,代码工作正常,我得到了正确的结果(模型学习).我唯一的疑问是,将 TimeDistributed 层展平为简单的密集层以获得如上所述的结果在数学上是否可行?

Okay, the code works fine and I am getting proper results(the model learns). My only doubt is if it is mathematically okay to flatten TimeDistributed layer to simple dense layer to get my result like stated above?

这篇关于检查目标时出错:预期 time_distributed_5 有 3 个维度,但得到了形状为 (14724, 1) 的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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