Tensorflow:ValueError:输入大小(输入深度)必须通过形状推断进行访问,但saw值无

Tensorflow:ValueError:输入大小(输入深度)必须通过形状推断进行访问,但saw值无,tensorflow,Tensorflow,以下是示例代码: class Model: def __init__(self, config): inputs = self.get_inputs() outputs, _ = tf.nn.dynamic_rnn( cell=tf.contrib.rnn.BasicLSTMCell(config.hidden_dim, state_is_tuple=True), inputs=inputs, dtype=

以下是示例代码:

class Model:

def __init__(self, config):
    inputs = self.get_inputs()
    outputs, _ = tf.nn.dynamic_rnn(
            cell=tf.contrib.rnn.BasicLSTMCell(config.hidden_dim, state_is_tuple=True),
            inputs=inputs,
            dtype=tf.float32)

def get_inputs(self):
    # do something to archive the inputs, 
    # which are not just the word embeddings,
    # rather, they are the outputs of another
    # model. The shape is (batch_size, ?, hidden_dim),
    # ? means the maxlength for each batch depends
    # on the data.
然而,当我训练我的模型时,我得到了以下错误:

Tensorflow:ValueError:Input size(输入深度)必须可以通过形状推断访问,但saw值为None。


我假设是变量maxlength导致了这个问题。我需要为我的模型编写自己的LSTM吗?还是有办法解决这个问题?

编译图表时必须知道
输入的最后一个维度。您可以通过打印其静态形状来检查这一点:

print(inputs.get_shape())
您可能看到的是,最后一个维度是
,这意味着TensorFlow无法推断它。您说过输入的形状总是
[批次大小,隐藏尺寸]
。然而,TensorFlow不一定能够推断出隐藏的维度。TensorFlow用于推断不同操作的输出形状的智能水平因TensorFlow版本而异

您可以通过显式地告诉TensorFlow输入的维度来解决这个问题。在调用
tf.nn.dynamic\u rnn()
之前,请使用
inputs.set\u shape(shape)
设置输入的形状。它的工作原理类似于
inputs=tf.reforme(inputs,shape)
,但它只设置静态形状。如果
形状
的某些尺寸为
,则它们不会被更改:

inputs.set_shape([None, None, hidden_dim])

maxlength不应导致此问题。你确定你输入的最后一个维度的大小定义得很好吗?我知道这个主题很长时间都不活跃,只是为未来的谷歌用户发布的。当我尝试将CNN与bi LSTM叠加时,我遇到了类似的问题。在打印张量形状后,很明显CNN
output
以某种方式“忘记”了它的形状信息。(它只打印[None,None,None],而实际上它知道维度)因此,在将输出输入LSTM层之前,我使用
output.set_shape()
@emrekgn设置“形状信息”,非常感谢您的评论。它为我解决了一个类似的问题,我相信它将在未来继续帮助许多其他人。也许把它作为一个答案?