了解张量流切片操作

时间:2018-09-18 13:18:16

标签: python tensorflow

我对以下代码感到困惑:

import tensorflow as tf
import numpy as np
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import dtypes

'''
Randomly crop a tensor, then return the crop position
'''
def random_crop(value, size, seed=None, name=None):
    with ops.name_scope(name, "random_crop", [value, size]) as name:
        value = ops.convert_to_tensor(value, name="value")
        size = ops.convert_to_tensor(size, dtype=dtypes.int32, name="size")
        shape = array_ops.shape(value)
        check = control_flow_ops.Assert(
                math_ops.reduce_all(shape >= size),
                ["Need value.shape >= size, got ", shape, size],
                summarize=1000)
        shape = control_flow_ops.with_dependencies([check], shape)
        limit = shape - size + 1
        begin = tf.random_uniform(
                array_ops.shape(shape),
                dtype=size.dtype,
                maxval=size.dtype.max,
                seed=seed) % limit
        return tf.slice(value, begin=begin, size=size, name=name), begin

sess = tf.InteractiveSession()
size = [10]
a = tf.constant(np.arange(0, 100, 1))

print (a.eval())

a_crop, begin = random_crop(a, size = size, seed = 0)
print ("offset: {}".format(begin.eval()))
print ("a_crop: {}".format(a_crop.eval()))

a_slice = tf.slice(a, begin=begin, size=size)
print ("a_slice: {}".format(a_slice.eval()))

assert (tf.reduce_all(tf.equal(a_crop, a_slice)).eval() == True)
sess.close()

输出:

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 96 97 98 99]
offset: [46]
a_crop: [89 90 91 92 93 94 95 96 97 98]
a_slice: [27 28 29 30 31 32 33 34 35 36]

有两个tf.slice选项:

(1)。在函数random_crop中调用,例如tf.slice(value, begin=begin, size=size, name=name)

(2)。称为a_slice = tf.slice(a, begin=begin, size=size)

这两个values操作的参数(beginsizeslice)是相同的。

但是,为什么打印值a_cropa_slice不同而tf.reduce_all(tf.equal(a_crop, a_slice)).eval()为True?

谢谢

EDIT1 谢谢@ xdurch0,我现在明白第一个问题。 Tensorflow random_uniform似乎是一个随机生成器。

import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()
size = [10]
np_begin = np.random.randint(0, 50, size=1)
tf_begin = tf.random_uniform(shape = [1], minval=0, maxval=50, dtype=tf.int32, seed = 0)
a = tf.constant(np.arange(0, 100, 1))

a_slice = tf.slice(a, np_begin, size = size)
print ("a_slice: {}".format(a_slice.eval()))
a_slice = tf.slice(a, np_begin, size = size)
print ("a_slice: {}".format(a_slice.eval()))

a_slice = tf.slice(a, tf_begin, size = size)
print ("a_slice: {}".format(a_slice.eval()))
a_slice = tf.slice(a, tf_begin, size = size)
print ("a_slice: {}".format(a_slice.eval()))

sess.close()

输出

a_slice: [42 43 44 45 46 47 48 49 50 51]
a_slice: [42 43 44 45 46 47 48 49 50 51]
a_slice: [41 42 43 44 45 46 47 48 49 50]
a_slice: [29 30 31 32 33 34 35 36 37 38]

1 个答案:

答案 0 :(得分:1)

这里令人困惑的是,tf.random_uniform(就像TensorFlow中的每个随机操作一样)在每次评估调用(每次调用.eval()或通常每次调用{ {1}})。因此,如果您评估tf.Session.run会得到一件事,如果您评估a_crop则会得到另一件事,但是如果您评估a_slice则会得到tf.reduce_all(tf.equal(a_crop, a_slice)),因为一切都在在单个评估步骤中计算得出的值,因此仅产生一个随机值,并且它确定Truea_crop的值。另一个例子是,如果运行a_slice,将得到张量相等的张量。再次,仅产生一个随机值。更一般而言,如果您调用带有多个张量的tf.stack([a_crop, a_slice]).eval()进行求值,则该调用中的所有计算将使用相同的随机值。

请注意,如果您实际上需要在计算中保留一个随机值,以供以后的计算使用,最简单的方法是仅使用tf.Session.run以及其他任何需要的计算来检索,以便稍后通过tf.Session.run进行反馈;或者您可以有一个feed_dict并在其中存储随机值。更高级的可能性是使用partial_run,这是一种实验性API,可让您评估计算图的一部分并在以后继续评估,同时保持相同的状态(即,相同的随机值,等等)。

相关问题