如何创建一个由1和0组成的二维张量,示例如下:

4

我需要根据像这样的输入张量创建一个由1和0组成的张量

input = tf.constant([3, 2, 4, 1, 0])

输出结果 =

0 0 0 0 0

0 0 0 1 0

0 0 1 1 0

0 0 1 1 1

0 1 1 1 1

简而言之,输入张量中每个值的索引(i)+ 1指定了我开始在该列中放置1的行。


1
你能澄清一下吗? - Mohan Radhakrishnan
2个回答

1
这是一个使用TensorFlow操作实现的例子。详见注释。
import tensorflow as tf

input = tf.placeholder(tf.int32, [None])
# Find indices that sort the input
# There is no argsort yet in the stable API,
# but you can do the same with top_k
_, order = tf.nn.top_k(-input, tf.shape(input)[0])
# Or use the implementation in contrib
order = tf.contrib.framework.argsort(input)
# Build triangular lower matrix
idx = tf.range(tf.shape(input)[0])
triangular = idx[:, tf.newaxis] > idx
# Reorder the columns according to the order
result = tf.gather(triangular, order, axis=1)
# Cast result from bool to int or float as needed
result = tf.cast(result, tf.int32)
with tf.Session() as sess:
    print(sess.run(result, feed_dict={input: [3, 2, 4, 1, 0]}))

输出:

[[0 0 0 0 0]
 [0 0 0 1 0]
 [0 0 1 1 0]
 [0 0 1 1 1]
 [0 1 1 1 1]]

0

这段代码可以实现所需的效果。但它没有使用向量化函数,这可能会使它更容易。代码中有一些注释。

形状是基于问题假设的。如果输入发生变化,则需要进行更多测试。

init = tf.constant_initializer(np.zeros((5, 5)))

inputinit = tf.constant([3, 2, 4, 1, 0])

value = tf.gather( inputinit , [0,1,2,3,4])

sess = tf.Session()

#Combine rows to get the final desired tensor
def merge(a) :

    for i in range(0, ( value.get_shape()[0] - 1  )) :
        compare = tf.to_int32(
                    tf.not_equal(tf.gather(a, i ),
                                 tf.gather(a, ( i + 1 ))))
        a = tf.scatter_update(a, ( i + 1 ), compare)

    #Insert zeros in first row and move all other rows down by one position.
    #This eliminates the last row which isn't needed
    return tf.concat([tf.reshape([0,0,0,0,0],(1,5)),
                      a[0:1],a[1:2],a[2:3],a[3:4]],axis=0)


# Insert ones by stitching individual tensors together by inserting one in
# the desired position.
def insertones() :

    a = tf.get_variable("a", [5, 5], dtype=tf.int32, initializer=init)
    sess.run(tf.global_variables_initializer())

    for i in range(0, ( value.get_shape()[0]  )) :

        oldrow = tf.gather(a, i )

        index = tf.squeeze( value[i:( i + 1 )] )

        begin = oldrow[: index ]
        end = oldrow[index : 4]
        newrow = tf.concat([begin, tf.constant([1]), end], axis=0)

        if( i <= 4 ) :
            a = tf.scatter_update(a, i, newrow)
    return merge(a)

a = insertones()
print(sess.run(a))

输出是这样的。

[[0 0 0 0 0]

[0 0 0 1 0]

[0 0 1 1 0]

[0 0 1 1 1]

[0 1 1 1 1]]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接