修改i-th下一个张量子值,每次值1出现在张量中
我有两个张量相同的张量:
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b = [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1]
张量 a 有三个区域,这些区域由连续值删除:区域1 IS [1,2,3,4,5]
,区域2是[10,11,12,13]
,区域3是[20,21,22,23,24,24,25,26,27,28]
。
对于每个区域,我要应用以下逻辑:如果 b 的值之一为1,则以下 i 值设置为0。如果它们已经为0,它们继续为0。在更改 i 值之后,直到另一个值B 为1。在这种情况下,下一个 i < /em>值被迫为0 ...
一些示例:
# i = 1
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1]
# i = 2
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]
# i = 4
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]
不确定如果这会有所帮助,但是我能够通过这样做将区域分为细分市场:
a_shifted = tf.roll(a - 1, shift=-1, axis=0)
a_shifted_segs = tf.math.cumsum(tf.cast(a_shifted != a, dtype=tf.int64), exclusive=True)
# a_shifted_segs =
= [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]
您知道有效地做到这一点吗?
I have two tensors with the same size:
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b = [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1]
Tensor a has three regions which are demarked by consecutive values: region 1 is [1,2,3,4,5]
, region 2 is [10,11,12,13]
and region 3 is [20, 21, 22, 23, 24, 25, 26, 27, 28]
.
For each of those regions, I want to apply the following logic: if one of the values of b is 1, then the following i values are set to 0. If they are already 0, they continue as 0. After i values are changed, nothing happens until another value of b is 1. In that case, the next i values are forced to 0...
Some examples:
# i = 1
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1]
# i = 2
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1]
# i = 4
a = [1, 2, 3, 4, 5, 10, 11, 12, 13, 20, 21, 22, 23, 24, 25, 26, 27, 28]
b_new = [0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]
Not sure if this would help, but I was able to separate the regions into segments by doing:
a_shifted = tf.roll(a - 1, shift=-1, axis=0)
a_shifted_segs = tf.math.cumsum(tf.cast(a_shifted != a, dtype=tf.int64), exclusive=True)
# a_shifted_segs =
= [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]
Do you know any way of doing this efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,您有一个基于
tf.scan
的TensorFlow解决方案。我知道条件有点复杂,如果您有建议如何简化,我就会提出建议。但是,如果您知道如何阅读条件,则应很清楚代码的作用。在这里,变量
i
告诉我们,对于数组中的每个位置,b
值必须用0
覆盖多少b
。示例:
此答案由Lambda赞助。
Here you have a tensorflow solution, based on
tf.scan
. I know the conditionals are a bit complicated, if you have suggestions how to simplify, I'm open for suggestions. However, if you know how to read the conditionals, it should be quite clear what the code does.Here, the variable
i
tells us, for each position in the array, how many moreb
values have to overwritten with0
.Examples:
This answer is sponsored by lambda.
这是纯
tensorflow
方法,它将在急切执行
和图形
模式:usage:usage:usage:usage:
Here is a pure
Tensorflow
approach, which will work inEager Execution
andGraph
mode:Usage:
如果我很好地了解,则需要在列表
a
定义的每个部分中,以保持第一个1
您在b
中遇到i
b
中的本节中的元素,如果有1
,请再次检查其余元素并应用相同的逻辑(零算出下一个<代码> i 元素)。然后移至下一节等。如果我很好地理解了一种实施方法,就是这样:for:
结果将是:
If I understand well, you want in every section defined from list
a
, to keep the first1
you meet inb
then zero out the nexti
elements in this section inb
and check again the rest elements if there is an1
and apply the same logic (zero out the nexti
elements). then move to the next section etc. If I understand well a way to implement it is like this:for:
the results would be :