在张量上使用while_loop在tensorflow中创建掩码 [英] using while_loop over the tensor for creating a mask in tensorflow

查看:192
本文介绍了在张量上使用while_loop在tensorflow中创建掩码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个在张量上迭代的蒙版. 我有以下代码:

I want to create a mask with iterating over the tensor. I have this code:

import tensorflow as tf

out = tf.Variable(tf.zeros_like(alp, dtype=tf.int32))

rows_tf = tf.constant (
[[1, 2, 5],
 [1, 2, 5],
 [1, 2, 5],
 [1, 4, 6],
 [1, 4, 6],
 [2, 3, 6],
 [2, 3, 6],
 [2, 4, 7]])

columns_tf = tf.constant(
[[1],
 [2],
 [3],
 [2],
 [3],
 [2],
 [3],
 [2]])

我想遍历rows_tf和相应的columns_tf以在out上创建掩码.

I want to iterate through rows_tf and accordingly columns_tf to create a mask over the out.

例如,它将掩盖out张量等于1[1,1] [2,1] and [5,1]处的索引.

for example, it will mask the index at [1,1] [2,1] and [5,1] in the out tensor equals 1.

将被设置为1,以此类推,对于总共8行

for the second row in rows_tf indexes at [1,2] [2,2] [5,2] in the out tensor will be set to 1 and so on for the total 8 rows

到目前为止,尽管它无法成功运行,但我已经做到了:

So far I have done this, though it does not run successfully:

body = lambda k, i: (tf.add(out[rows_tf[i][k]][columns_tf[i][i]], 1)) # find the corresponding element in out tensor and add 1 to it (0+1=1)
k = 0
n2, m2 = rows_tf.shape
for i in tf.range(0,n2): # loop through rows in rows_tf    
    cond = lambda k, _: tf.less(k, m2) #this check to go over the columns in rows_tf
    tf.while_loop(cond, body, (k, i))

它会引发此错误:

TypeError: Cannot iterate over a scalar tensor. 
in this while cond(*loop_vars):

我已经浏览了几个链接,即此处,以确保Im遵循指令,但无法解决此问题.

I have gone through several links namely here to make sure Im following the instruction, but could not fix this one.

感谢您的帮助

推荐答案

您可以使用 tf.scatter_nd 像这样:

You can do that without a loop using tf.scatter_nd like this:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    out = tf.zeros([10, 4], dtype=tf.int32)
    rows_tf = tf.constant(
        [[1, 2, 5],
         [1, 2, 5],
         [1, 2, 5],
         [1, 4, 6],
         [1, 4, 6],
         [2, 3, 6],
         [2, 3, 6],
         [2, 4, 7]], dtype=tf.int32)
    columns_tf = tf.constant(
        [[1],
         [2],
         [3],
         [2],
         [3],
         [2],
         [3],
         [2]], dtype=tf.int32)
    # Broadcast columns
    columns_bc = tf.broadcast_to(columns_tf, tf.shape(rows_tf))
    # Scatter values to indices
    scatter_idx = tf.stack([rows_tf, columns_bc], axis=-1)
    mask = tf.scatter_nd(scatter_idx, tf.ones_like(rows_tf, dtype=tf.bool), tf.shape(out))
    print(sess.run(mask))

输出:

[[False False False False]
 [False  True  True  True]
 [False  True  True  True]
 [False False  True  True]
 [False False  True  True]
 [False  True  True  True]
 [False False  True  True]
 [False False  True False]
 [False False False False]
 [False False False False]]

或者,您也可以只使用布尔操作来做到这一点:

Alternatively, you could also do this using boolean operations only:

import tensorflow as tf

with tf.Graph().as_default(), tf.Session() as sess:
    out = tf.zeros([10, 4], dtype=tf.int32)
    rows_tf = tf.constant(
        [[1, 2, 5],
         [1, 2, 5],
         [1, 2, 5],
         [1, 4, 6],
         [1, 4, 6],
         [2, 3, 6],
         [2, 3, 6],
         [2, 4, 7]], dtype=tf.int32)
    columns_tf = tf.constant(
        [[1],
         [2],
         [3],
         [2],
         [3],
         [2],
         [3],
         [2]], dtype=tf.int32)
    # Compare indices
    row_eq = tf.equal(tf.range(out.shape[0])[:, tf.newaxis],
                      rows_tf[..., np.newaxis, np.newaxis])
    col_eq = tf.equal(tf.range(out.shape[1])[tf.newaxis, :],
                      columns_tf[..., np.newaxis, np.newaxis])
    # Aggregate
    mask = tf.reduce_any(row_eq & col_eq, axis=[0, 1])
    print(sess.run(mask))
    # Same as before

但是原则上这会占用更多内存.

However this would in principle take more memory.

这篇关于在张量上使用while_loop在tensorflow中创建掩码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆