python - 了解 tensorflow 切片操作

标签 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()是真的吗?

谢谢

编辑1 谢谢@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]

最佳答案

这里令人困惑的是,tf.random_uniform(就像 TensorFlow 中的每个随机操作一样)在每次评估调用(每次调用 .eval() 或者,一般来说,每次调用 tf.Session.run)。因此,如果您评估 a_crop ,您会得到一件事,如果您评估 a_slice ,您会得到另一件事,但如果您评估 tf.reduce_all(tf.equal( a_crop, a_slice)) 得到 True,因为所有内容都是在单个评估步骤中计算的,因此仅生成一个随机值,并且它确定 a_crop< 的值a_slice。另一个例子是这样的,如果你运行 tf.stack([a_crop, a_slice]).eval() ,你将得到一个具有相等行数的张量;同样,只产生了一个随机值。更一般地说,如果您使用多个张量调用 tf.Session.run 进行评估,则该调用中的所有计算都将使用相同的随机值。

顺便说一句,如果您确实需要在计算中保留一个随机值以供以后计算,最简单的方法就是使用 tf.Session.run 检索 if以及任何其他所需的计算,稍后通过 feed_dict 反馈;或者您可以有一个 tf.Variable 并在其中存储随机值。更高级的可能性是使用 partial_run ,一个实验性 API,允许您评估计算图的一部分并在稍后继续评估它,同时保持相同的状态(即相同的随机值等)。

关于python - 了解 tensorflow 切片操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52387537/

相关文章:

python - 在字典中使用字符串作为键总是更快吗?

numpy - 从 txt 文件计算平均值、标准差的有效方法

python - 有没有办法让 BaseRequestHandler 类有状态?

python - 如何在 z3 的 Python API 中实现位向量数组

python - Dataset.from_tensors 和 Dataset.from_tensor_slices 有什么区别?

python - Tensorflow 2.0 : Can I change the settings on a Tf. data.Dataset——特别是 `repeat()` 功能?

python - Pytorch CNN 损失没有变化,

python - 如何在 TensorFlow 1.13 中检查 TFRecord 文件的结构?

tensorflow - keras Conv2d值错误: Negative dimension size

tensorflow - 如何从子目录加载tensorflow中的数据