Tracking tensor shape at graph creation time

In some cases, the tensor flow is apparently capable of checking the tensor values ​​during the creation of the graph, while in other cases this fails.

>>> shape = [constant([2])[0], 3]
>>> reshape([1,2,3,4,5,6], shape)
<tf.Tensor 'Reshape_13:0' shape=(2, 3) dtype=int32>
>>> zeros(shape)
<tf.Tensor 'zeros_2:0' shape=(?, 3) dtype=float32>

In the above example, reshape () can see that the tensor passed in the form of a form has a value of 2, and the result obtained has the form (2,3), but zeros () cannot and the static form (α 3). What is the reason for the difference?

My colleague published Definition of tensor forms during the creation of a graph in TensorFlow , based on the same main problem, but he asked a slightly different question on how to work better with tensor flow to solve these kinds of things, while my question is why this happens with tensor flow. This is mistake?

+4
source share
1 answer

TD DR:

  • tf.reshapecan output an output form, but tf.zeroscannot:
  • shapesupports integers (like static / defined ), as well as tensors (like dynamic / undefined ) for both functions.

Codes are more specific and understandable:

shape = [tf.constant([2])[0], tf.constant([3])[0]]
print(tf.reshape([1,2,3,4,5,6], shape))  
# Tensor("Reshape:0", shape=(?, ?), dtype=int32)
print(tf.zeros(shape))  
# Tensor("zeros:0", shape=(?, ?), dtype=float32)

and this:

shape = [tf.constant([5])[0], 3]
print tf.reshape([1,2,3,4,5,6], shape)  
# Tensor("Reshape:0", shape=(2, 3), dtype=int32)
# This will cause an InvalidArgumentError at running time!

When using Tensor(for example, tf.constant([2])[0]) as the quality shapeto create another Tensor(for example, tf.zeros(shape)), the form is always vague during the creation of the graph. However tf.reshape()different. It can output the exit form using the entry form and the given form (static part).

3 - ([6]); (2, 3) , . . tf.constant([5]), . ( , !)

+2

Source: https://habr.com/ru/post/1694306/


All Articles