- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将两个张量混合在一起。 scatter_nd
非常适合这种情况,我编写了以下函数来完成我的任务。它基本上只是将 2 个 scatter_nds 广告放在一起。
def tf_munge(t, i, r, j, axis=0):
#insert tensor t at indices i and tensor r at indices j on axis `axis`.
#requires: i.shape[0] == t.shape[axis] && j.shape[0] == r.shape[axis] && t.shape[k] == r.shape[k] ∀k != axis
i = tf.expand_dims(i, -1)
j = tf.expand_dims(j, -1)
rank_indices = tf.range(tf.rank(t))
roller = tf.roll(rank_indices, -axis, 0)
rolled_t = tf.transpose(t, roller)
rolled_r = tf.transpose(r, roller)
scatter_shape = tf.concat((tf.shape(i)[0:1] + tf.shape(j)[0:1], tf.shape(rolled_t)[1:]), axis=0)
scattered = tf.scatter_nd(i, rolled_t, scatter_shape) + tf.scatter_nd(j, rolled_r, scatter_shape)
return tf.transpose(scattered, tf.roll(rank_indices, axis, 0))
一般来说,它按预期工作。但是,只要 r
和 t
沿某个轴均为空,它就会失败。我有两个代码“路径”,具体取决于 bool 值,其中我分割张量并根据该 bool 值是真还是假执行不同的操作。有时,该 bool 值对于 0 行为 false。在这种情况下,我最终会对一个空张量做一些事情。其中之一就是这种尝试分散。该错误实际上引用了输出形状(上述代码中的 scatter_shape ),声称:
ValueError: Indices and updates specified for empty output shape for 'ScatterNd_4' (op: 'ScatterNd')
with input shapes: [3,1], [3,0,2], [3] and with input tensors computed as partial shapes: input[2] = [5,0,2].
请注意,空轴与我散射的轴不同。这是一个工作示例:
foo = tf.ones((3,1,2))
bar = tf.ones((2,1,2))*2
i = tf.constant([1,3,4])
j = tf.constant([0,2])
tf_munge(foo,i,bar,j,axis=0)
#Output: <tf.Tensor 'transpose_13:0' shape=(5, 1, 2) dtype=float32>
这是一个失败的例子:
foo = tf.ones((3,0,2))
bar = tf.ones((2,0,2))*2
tf_munge(foo,i,bar,j,axis=0)
#Output: The error above
这里的预期输出显然是形状为(5,0,2)
的空张量。
我考虑过对输入的形状使用条件,但是tf.cond
executes both pathways 。当我有一个带有 scatter_nd
的空张量时,如何处理这种情况?
最佳答案
您可以使用 tf.gather
更简单地做到这一点以适用于所有情况的方式:
import tensorflow as tf
def tf_munge(t, i, r, j, axis=0):
tr = tf.concat([t, r], axis=axis)
idx = tf.argsort(tf.concat([i, j], axis=0))
return tf.gather(tr, idx, axis=axis)
with tf.Graph().as_default(), tf.Session() as sess:
foo = tf.ones((3, 1, 2))
bar = tf.ones((2, 1, 2)) * 2
i = tf.constant([1, 3, 4])
j = tf.constant([0, 2])
out = tf_munge(foo, i, bar, j, axis=0)
print(sess.run(out))
# [[[2. 2.]]
#
# [[1. 1.]]
#
# [[2. 2.]]
#
# [[1. 1.]]
#
# [[1. 1.]]]
foo2 = tf.ones((3, 0, 2))
bar2 = tf.ones((2, 0, 2)) * 2
out2 = tf_munge(foo2, i, bar2, j, axis=0)
print(sess.run(out2))
# []
关于python - 带有空张量的tensorflow scatter_nd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58612735/
我正在尝试将两个张量混合在一起。 scatter_nd 非常适合这种情况,我编写了以下函数来完成我的任务。它基本上只是将 2 个 scatter_nds 广告放在一起。 def tf_munge(t,
我正在尝试使用 scatter_nd TensorFlow 中的函数可对矩阵行中的元素进行重新排序。例如,假设我有代码: indices = tf.constant([[1],[0]]) update
在tensorflow中,如何将动态形状应用于scatter_nd? 当我使用具有动态形状的输入张量时,出现以下错误: ValueError: Cannot convert a partially k
我试图只优化变量的一部分。我找到了 this看似有用的答案。 但是我的变量是一个图像,我只想更改它的一部分,所以我试图将代码扩展到更多维度。这似乎工作正常: import tensorflow as
假设我有一个 (None, 2)-shape 张量 indices 和 (None,)-shape 张量 values。这些实际行号和值将在运行时确定。 我想设置一个 4x5 张量 t,索引的每个元素
我在将 tf.scatter_nd_add() 应用于二维张量时遇到困难。该文档有点不清楚,没有包含稀疏更新的示例,但仅包含完整切片更新的示例。 我的情况如下: 更新 - 形状为 [None, 6]
我读过 tf.scatter_nd documentation并运行 1D 和 3D 张量的示例代码......现在我正在尝试为 2D 张量执行此操作。我想“交错”两个张量的列。对于一维张量,可以通过
我正在尝试在 tensorflow 中编写一个仅传播每个特征图的前 k 个值的操作。 例子: k=1,输入大小是[batch_size, x, y, channels] 假设它是[1,2,2,3] 输
给定形状为[batch_size, sequence_len]的indices,形状为[batch_size, sequence_len, sampled_size]的updates >,to_sha
我是一名优秀的程序员,十分优秀!