Jeff Liu
Jeff Liu

Reputation: 79

Tensorflow: ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None

Here's the example codes:

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.

However, when I trained my model, I got this error:

Tensorflow: ValueError: Input size (depth of inputs) must be accessible via shape inference, but saw value None.

I assume it's the variable maxlength that causes the problem. Do I need to write my own LSTM for my model or is there a way to fix this?

Upvotes: 1

Views: 2956

Answers (1)

Seppo Enarvi
Seppo Enarvi

Reputation: 3663

The last dimension of inputs has to be known already when compiling the graph. You can check this by printing its static shape:

print(inputs.get_shape())

What you probably see is that the last dimension is ? meaning that TensorFlow was unable to infer it. You said that the shape of the inputs is always [batch_size, ?, hidden_dim]. However, TensorFlow is not necessarily able to infer hidden_dim. The level of intelligence that TensorFlow has for infering the output shape of different operations varies between TensorFlow versions.

You can fix the problem by explicitly telling the dimensionality of the inputs to TensorFlow. Before calling tf.nn.dynamic_rnn(), set the shape of the inputs usinginputs.set_shape(shape). It works like inputs = tf.reshape(inputs, shape), but it only sets the static shape. If some dimensions of shape are None, they won't be altered:

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

Upvotes: 2

Related Questions