Python 如何创建1和0的二维张量,如下所示:

Python 如何创建1和0的二维张量,如下所示:,python,tensorflow,Python,Tensorflow,我需要根据这样的输入张量创建一个1和0的张量 input = tf.constant([3, 2, 4, 1, 0]) 输出= 0 0 0 0 0 0 1 0 0110 01 01 本质上,输入张量+1的每个值的索引(i)指定了我开始在该列中放置1s的行。此代码提供了所需的效果。但它并没有使用矢量化函数来简化这一过程。代码中有一些注释 形状是根据问题假设的。如果输入发生变化,则需要进行更多测试 init = tf.constant_initializer(np.zeros((5, 5)))

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

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

0 0 0 0

0 0 1 0

0110

01

01


本质上,输入张量+1的每个值的索引(i)指定了我开始在该列中放置1s的行。

此代码提供了所需的效果。但它并没有使用矢量化函数来简化这一过程。代码中有一些注释

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

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))
init=tf.constant\u初始值设定项(np.zeros((5,5)))
inputinit=tf.常数([3,2,4,1,0])
value=tf.gather(输入[0,1,2,3,4])
sess=tf.Session()
#合并行以获得最终所需的张量
def合并(a):
对于范围(0,(value.get_shape()[0]-1])中的i:
比较=tf.to_int32(
tf.not_equal(tf.聚集(a,i),
tf.聚集(a,(i+1)))
a=tf.scatter\u更新(a,(i+1),比较)
#在第一行中插入零,并将所有其他行向下移动一个位置。
#这将消除不需要的最后一行
返回tf.concat([tf.reformate([0,0,0,0,0],(1,5)),
a[0:1]、a[1:2]、a[2:3]、a[3:4]、轴=0)
#将单个张量缝合在一起,插入一个张量
#所需位置。
def insertones():
a=tf.get_变量(“a”,[5,5],dtype=tf.int32,初始值设定项=init)
sess.run(tf.global\u variables\u initializer())
对于范围(0,(value.get_shape()[0])中的i:
oldrow=tf.gather(a,i)
指数=tf.挤压(值[i:(i+1)])
begin=oldrow[:索引]
结束=旧行[索引:4]
newrow=tf.concat([begin,tf.constant([1]),end],轴=0)

if(i这里是一个带有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]]

你能澄清一下吗?