1
votes

I'm trying to build a graph with variable batch size, variable reshape and variable weight shape. I'm using tensorflow 1.3.0.

Using the code below, tf.get_variable throws a TypeError: int() argument must be a string or a number, not 'Tensor'. pool2 is defined somewhere else in the code.

# declare placeholder for variable batch size
images_ph = tf.placeholder(tf.float32, shape=[None, 64, 64, 1])
# code for 2 layers of convolution, normalization and max pooling
# reshape to perform, one matrix multiply
reshape = tf.reshape(pool2, [tf.shape(images_ph)[0], -1])
dim = tf.shape(reshape)[1]
var = tf.get_variable('name', [dim, 384], validate_shape=False)

I have also tried to substitute dim to have the 'correct' type as follows:

dim = reshape.get_shape()[1]

dim is equal to None and throws a ValueError: Shape of a new variable (local3/xpto) must be fully defined, but instead was (?, 384).

3
It looks like pool2 isn't defined. - Kamran
pool2 is not defined in the code-snippet but is defined somewhere else in the code - rafaelvalle

3 Answers

1
votes

The second argument to get_variable takes an integer or string, but as the error say, you've given it a tensor [dim, 384].

See: https://www.tensorflow.org/api_docs/python/tf/get_variable

0
votes

tf.shape returns a tensor, so dim in [dim, 384] is a Tensor; here is an int needed.

Try dim = reshape.get_shape().as_list()[1] # output is of int type

0
votes

Thanks everyone for trying to help.

Given that I'm flattening the output of pool2 to perform just one matmul, the solution is to explicitly compute the length of the second dimension of the reshape. This is computed on lines 1 and 2, where the [1:] offset on the first line comes from the variable batch size.

I could not use reshape with all dimensions being unspecified.

pool2_shapes = pool2.get_shape().as_list()[1:]
pool2_features_length = reduce(lambda x, y: x*y, pool2_shapes)
reshape = tf.reshape(pool2, [tf.shape(images)[0], pool2_features_length])