- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对以下代码感到困惑:
import tensorflow as tfimport numpy as npfrom tensorflow.python.framework import opsfrom tensorflow.python.ops import array_opsfrom tensorflow.python.ops import control_flow_opsfrom tensorflow.python.ops import math_opsfrom 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), beginsess = 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
的参数( begin
、 size
和 slice
)操作是一样的。
但是,为什么打印值 a_crop
和a_slice
不同并且 tf.reduce_all(tf.equal(a_crop, a_slice)).eval()
是真的吗?
谢谢
编辑1谢谢@xdurch0,我现在明白第一个问题了。 tensorflow random_uniform
看起来像一个随机生成器。
import tensorflow as tfimport numpy as npsess = 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/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!