- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个张量,我必须迭代第一个张量以仅获取另一个张量内的元素。 t2
中只有一个元素也在 t1
内部。这是一个例子
t1 = tf.where(values > 0) # I get some indices example [6, 0], [3, 0]
t2 = tf.where(values2 > 0) # I get [4, 0], [3, 0]
t3 = .... # [3, 0]
我尝试使用 .eval()
评估和迭代它们,并使用以下命令检查 t2
的元素是否在 t1
中运算符in
,但不起作用。 TensorFlow 中是否有一个函数可以做到这一点?
编辑
for index in xrange(max_indices):
indices = tf.where(tf.equal(values, (index + 1))).eval() # indices: [[1 0]\n [4 0]\n [9 0]]
cent_indices = tf.where(centers > 0).eval() # cent_indices: [[6 0]\n [9 0]]
indices_list.append(indices)
for cent in cent_indices:
if cent in indices:
centers_list.append(cent)
break
第一次迭代cent
的值为[6 0]
,但它进入if
条件。
回答
for index in xrange(max_indices):
indices = tf.where(tf.equal(values, (index + 1))).eval()
cent_indices = tf.where(centers > 0).eval()
indices_list.append(indices)
for cent in cent_indices:
# batch_item is an iterator from an outer loop
if values[batch_item, cent[0]].eval() == (index + 1):
centers_list.append(tf.constant(cent))
break
该解决方案与我的任务相关,但如果您正在寻找一维张量的解决方案,我建议您查看tf.sets.set_intersection
最佳答案
这就是你想要的吗?我只使用了这两个测试用例。
x = tf.constant([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 1]])
y = tf.constant([[1, 2, 3, 4, 3, 6], [1, 2, 3, 4, 5, 1]])
# x = tf.constant([[1, 2], [4, 5], [7, 7]])
# y = tf.constant([[7, 7], [3, 5]])
def match(xiterations, yiterations, yvalues, xvalues ):
for i in range(xiterations):
for j in range(yiterations):
if (np.array_equal(yvalues[j], xvalues[i])):
print( yvalues[j])
with tf.Session() as sess:
xindex = tf.where( x > 4 )
yindex = tf.where( y > 4 )
xvalues = xindex.eval()
yvalues = yindex.eval()
xiterations = tf.shape(xvalues)[0].eval()
yiterations = tf.shape(yvalues)[0].eval()
print(tf.shape(xvalues)[0].eval())
print(tf.shape(yvalues)[0].eval())
if tf.shape(xvalues)[0].eval() >= tf.shape(yvalues)[0].eval():
match( xiterations, yiterations, yvalues, xvalues)
else:
match( yiterations, xiterations, xvalues, yvalues)
关于python - 取一个张量的一个元素,该元素也在另一个张量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52850196/
对于 Pytorch 张量 A : A = tensor([1,0,0], [0,0,0]) 有什么方法可以检查数字 1 是否是张量的元素 A ? 比如是否有一个返回 True
我知道 tf.where 将返回 True 值的位置,以便我可以使用结果的 shape[0] 来获取 True 的数量。 但是,当我尝试使用它时,维度是未知的(这是有道理的,因为它需要在运行时计算)。
我有一个形状为 (None,1024) 的张量 A,我想将它重复 49 次以生成一个形状为 (None, 7,7,1024)。每个 (,1024) 向量都是张量 A 的副本。 我该怎么做? 我试过了
我有两个张量T1(N * D维度)和T2(M * D维度)(M小于N)。 T2 行保证位于 T1 中。对于 T2 中的每一行,有没有办法找到该行匹配的 T1 索引?我能够使用急切执行来解决问题。 im
我是一名优秀的程序员,十分优秀!