我有两个张量,我必须迭代第一个,只取另一个张量中的元素。在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()
对它们求值和迭代,并使用操作符in
检查t2
的元素是否在t1
中,但没有起作用。有张量流的函数可以做到吗?你知道吗
编辑
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
这就是你想要的吗?我只用了这两个测试用例。你知道吗
相关问题 更多 >
编程相关推荐