简体   繁体   中英

What does TensorFlow shape (?,) mean?

I'm getting the shape of a TensofFlow tensor as:

(?,)

This answer says that the ? means that the dimension is not fixed in the graph and it can vary between run calls.

What does the ? mean in conjuction with the trailing comma?

Documentation chapter and verse would be appreciated. I find syntax very difficult to google.

The comma means that the dimension is represented as a 1-elem tuple instead an int.

Each tensor, when created, is by default a n-dim:

import tensorflow as tf
t = tf.constant([1, 1, 1])
s = tf.constant([[1, 1, 1],[2,2,2]])

print("0) ", tf.shape(t))
print("1) ", tf.shape(s))

0)  Tensor("Shape_28:0", shape=(1,), dtype=int32)
1)  Tensor("Shape_29:0", shape=(2,), dtype=int32)

However, you can reshape it to get a more "whole" shape (ie n X m / n X m X r... dim):

print("2) ", tf.reshape(t, [3,1]))
print("3) ", tf.reshape(s, [2,3]))

2)  Tensor("Reshape_12:0", shape=(3, 1), dtype=int32)
3)  Tensor("Reshape_13:0", shape=(2, 3), dtype=int32)

Use tf.shape(tensor)[0] to get a scalar tensor with the variable dimension. It is useful if you then need to reshape. tensor.get_shape().as_list()[0] generates None for shapes with (?, ... ). This is usually the position for the batch size when training models.

Reference: TF Issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM