Definition of tensor forms at the time of creating a graph in TensorFlow

I am trying to write a piece of reusable code that reads the shape of one tensor and then uses the resulting object to determine the shape of the other tensors. I have the choice to read the dynamic form of tensor c tf.shape(tensor)or the static form of tensor c tensor.get_shape(). An example of a toy looks like this (with two different strategies):

def my_function_strategy_1(x, y):
    x_shape = tf.shape(x)
    a = tf.reshape(y, x_shape)
    b = tf.zeros(x_shape)
    num_x_values = x_shape[0]
    c = tf.reshape(y, [num_x_values, 4])
    d = tf.zeros([num_x_values, 4])
    return a, b, c, d

def my_function_strategy_2(x, y):
    x_shape = x.get_shape()
    a = tf.reshape(y, x_shape)
    b = tf.zeros(x_shape)
    num_x_values = x_shape[0]
    c = tf.reshape(y, [num_x_values, 4])
    d = tf.zeros([num_x_values, 4])
    return a, b, c, d

I want to use this piece of code on different graphs. Sometimes the shape of the input tensors will be known, and sometimes they will be unknown:

graph_A = tf.Graph()
with graph_A.as_default():
    x = tf.placeholder(tf.float32, [2, 4])
    y = tf.placeholder(tf.float32, [8])
    a, b, c, d = my_function(x, y)

with graph_B.as_default():
    x = tf.placeholder(tf.float32)
    y = tf.placeholder(tf.float32)
    a, b, c, d = my_function(x, y)

, : (A) ( graph_A), , TensorFlow ( ..) (B) ( graph_B), , TensorFlow , .

strategy_1 . (B), (A), TensorFlow . , a, b c , d ( d ). , a.get_shape(), b.get_shape() ..

, strategy_2 (A) , (B), TensorFlow () , () .

(A), (B) ? / strategy_1 , ?

+2
1

, , , " ":

def my_get_shape(tensor):
    if tensor.shape.ndims is None:
        # Fully dynamic
        return tf.shape(tensor)
    if tensor.shape.is_fully_defined():
        # Fully static
        return tensor.shape
    # Partially static
    dyn_shape = tf.shape(tensor)
    shape = []
    for i, d in enumerate(tensor.shape):
        shape.append(d.value if d.value is not None else dyn_shape[i])
    return shape

def my_function(x, y):
    x_shape = my_get_shape(x)  # Or just tf.shape(x)! - see edit
    a = tf.reshape(y, x_shape)
    b = tf.zeros(x_shape)
    num_x_values = x_shape[0]
    c = tf.reshape(y, [num_x_values, 4])
    d = tf.zeros([num_x_values, 4])
    return a, b, c, d

# Fully static
with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [2, 4])
    y = tf.placeholder(tf.float32, [8])
    a, b, c, d = my_function(x, y)
print('a:', a.shape, ', b:', b.shape, ', c:', c.shape, ', d:', d.shape)
# a: (2, 4) , b: (2, 4) , c: (2, 4) , d: (2, 4)

# Fully dynamic
with tf.Graph().as_default():
    x = tf.placeholder(tf.float32)
    y = tf.placeholder(tf.float32)
    a, b, c, d = my_function(x, y)
print('a:', a.shape, ', b:', b.shape, ', c:', c.shape, ', d:', d.shape)
# a: <unknown> , b: <unknown> , c: (?, 4) , d: (?, 4)

# Partially static
with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [None, 4])
    y = tf.placeholder(tf.float32)
    a, b, c, d = my_function(x, y)
print('a:', a.shape, ', b:', b.shape, ', c:', c.shape, ', d:', d.shape)
# a: (?, 4) , b: (?, 4) , c: (?, 4) , d: (?, 4)

EDIT:

, my_get_shape tf.shape . , tf.shape ( , ), undefined.

, . , , , . -, TensorFlow ++ (, Python , ), " ". , , tensorflow/core/ops/array_ops.cc), , .SetShapeFn , , InferenceContext, . , , , , , , tf.shape, tf.fill ( tf.ones) . - , Python, ( , ) call_cpp_shape_fn:

from tensorflow.python.framework.common_shapes import call_cpp_shape_fn
with tf.Graph().as_default():
    print(call_cpp_shape_fn(tf.reshape(tf.placeholder(tf.float32), tf.fill([2], 3)).op))
    # Shows this:
    # {
    #   'shapes': [dim { size: 3 } dim { size: 3 }],
    #   'handle_data': [None],
    #   'inputs_needed': b'\x12\x01\x01'
    # }
    print(call_cpp_shape_fn(tf.reshape(tf.placeholder(tf.float32), (2 * tf.fill([2], 3))).op))
    # Shows this:
    # {
    #   'shapes': [dim { size: -1 } dim { size: -1 }],
    #   'handle_data': [None],
    #   'inputs_needed': b'\x12\x01\x01'
    # }

, , tf.fill([2], 3) , TensorFlow , 2 * tf.fill([2], 3) [6, 6], , , , , .

, , - , ops , / . , , , tf.shape, undefined.

+1

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


All Articles