简体   繁体   English

在张量流中使用 LSTM 层时出现 ValueError 问题

[英]ValueError issue when using LSTM layer in tensorflow

I am trying to build up a very simple LSTM structure using padding and masking to learn how to train time series data.我正在尝试使用填充和掩码构建一个非常简单的 LSTM 结构来学习如何训练时间序列数据。 Suppose I have two people's blood value information per month.假设我每个月有两个人的血值信息。 For the first person, I only have seven months of data, wheares, for the next person, I only have four months of data.对于第一个人,我只有七个月的数据,wheares,对于第二个人,我只有四个月的数据。 Lastly, I select -100 as my padding value to complete the sequences.最后,我选择 -100 作为填充值来完成序列。

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.utils import Sequence
from keras.layers import LSTM, Dense, Masking

#each column represent a different blood value. 
xPad =  np.array([[
  [  0.4654949,    0.06225133],
  [ -0.48630088,   0.97063685],
  [ -0.23714237,  1.07598604],
  [ -0.94519772,   0.76515959],
  [ -0.81456729,   1.05963647],
  [  0.60236851,   1.26799774],
  [  1.89095161,   1.02534057]],

 [[ -1.76505643,   0.61171791],
  [  2.00335928,  -0.02941931],
  [ -1.58293956,  -0.02671103],
  [  1.57166957,  -0.39450184],
  [-100,         -100        ],
  [-100,         -100        ],
  [-100,         -100        ]]])

Suppose I would like to estimate whether the person will be low weight, medium weight, and high weight.假设我想估计这个人是低体重、中等体重还是高体重。 That's why I also have this information for each month per person as shown below.这就是为什么我也有每个人每个月的这些信息,如下所示。 For the second person, I fill the missing months with zero vector.对于第二个人,我用零向量填充缺失的月份。 For instance, [0,0,1] implies that the person within that spesific month is high weight.例如, [0,0,1]表示该特定月份内的人体重较高。

y = np.array([
    [[0,0,1],[0,0,1],[0,0,1],[0,0,1],[0,1,0],[0,1,0],[0,1,0]],
    [[1,0,0],[1,0,0],[1,0,0],[0,1,0],[0,0,0],[0,0,0],[0,0,0]]
    ])

Then, I create my network using tensorflow.然后,我使用 tensorflow 创建我的网络。

special_value= -100
seq_len = 7
dim = 2

model = Sequential()
model.add(Masking(mask_value=special_value, input_shape=(seq_len, dim)))
model.add(LSTM(5))
model.add(Dense(3))
model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001))
model.fit(xPad, y, epochs=10, batch_size=2)

I was wondering what I am doing wrong here.我想知道我在这里做错了什么。 I would appreciate any help.我将不胜感激任何帮助。

Dimensions must be equal, but are 7 and 2 for '{{node binary_crossentropy/mul}} = Mul[T=DT_FLOAT](binary_crossentropy/Cast, binary_crossentropy/Log)' with input shapes: [2,7,3], [2,3]

The short answer is you can make your problem go away by changing简短的回答是你可以通过改变来解决你的问题

model.add(LSTM(5))

to,至,

model.add(LSTM(5, return_sequences=True))

Here's an expansion on the answer.这是答案的扩展。

You got xPad of shape [2, 7, 2] and y of shape [2, 7, 3] .你得到了xPad的形状[2, 7, 2]y的形状[2, 7, 3] So it seems you are trying to predict y for every time step of xPad .因此,您似乎正在尝试为xPad每个时间步预测y Then what the error says is,然后错误说的是,

It has an LSTM that outputs only the last state of it, but your y is for all of the time steps (which are incompatible).它有一个 LSTM,只输出它的最后一个状态,但你的y是所有时间步长(不兼容)。 return_sequences=True outputs the states for all of the time steps, which makes your y compatible with the output of the model. return_sequences=True输出所有时间步的状态,这使您的y与模型的输出兼容。

This answer shows what return_sequences=True does. 这个答案显示了return_sequences=True的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:使用 RandomizedSearchCV 的 Tensorflow LSTM 中的形状不兼容 - ValueError: Shapes are incompatible in Tensorflow LSTM using RandomizedSearchCV 使用Tensorflow的LSTM网络中的Shape ValueError - Shape ValueError in LSTM network using Tensorflow 使用Tensorflow调用copy.deepcopy(network)时出现“ ValueError:未知层:…” - “ValueError: Unknown layer: … ” when calling copy.deepcopy(network) using Tensorflow 使用 Tensorflow 格式化具有可变时间步长的 LSTM 层的输入 - Formatting inputs for LSTM layer with variable timestep using Tensorflow 使用 Conv1D 层时出现 ValueError - ValueError when using Conv1D layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer LSTM 层之后的自定义注意力层在 Keras 中给出 ValueError - Custom attention layer after LSTM layer gives ValueError in Keras 在 Keras 中实现 LSTM。 ValueError:层顺序与层不兼容 - Implementing LSTM in Keras. ValueError: layer sequential is incompatible with the layer 如何在Tensorflow中获得LSTM的密集层输出? - How to get dense layer output of LSTM in Tensorflow? Tensorflow:了解LSTM模型的层结构 - Tensorflow: Understanding the layer structure of LSTM model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM