Tensorflow:ValueError:输入大小(输入深度)必须可通过形状推理访问,但锯值无

时间:2017-05-04 14:08:07

标签: 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=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 (depth of inputs) must be accessible via shape inference, but saw value None.

我认为导致问题的是变量maxlength。我是否需要为自己的模型编写自己的LSTM,或者有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

编译图形时,必须已经知道inputs的最后一个维度。您可以通过打印其静态形状进行检查:

print(inputs.get_shape())

您可能会看到,最后一个维度是?,这意味着TensorFlow无法进行推断。您说输入的形状始终为[batch_size, ?, hidden_dim]。但是,TensorFlow不一定能够推断hidden_dim。 TensorFlow用来推断不同操作的输出形状的智能级别在TensorFlow版本之间有所不同。

您可以通过明确告知TensorFlow输入的维数来解决问题。调用tf.nn.dynamic_rnn()之前,请使用inputs.set_shape(shape)设置输入的形状。它的工作方式类似于inputs = tf.reshape(inputs, shape),但仅设置静态形状。如果shape的某些尺寸为None,则它们不会被更改:

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