Skip to main content
 首页 » 编程设计

python之创建形状为 (None, 1) 的有序整数的张量

2025年02月15日8zhenyulu

给定大小为 (None, 1) 的输入批处理,是否可以创建形状相同的有序整数张量?

例如:

input = [3, 2, 3, 7], output = [0, 1, 2, 3] 

例如:

input = [9, 3, 12, 4, 34 .....], output = [0, 1, 2, 3, ....] 

请您参考如下方法:

tf.range()做你需要的,你只需要根据输入张量的大小提供大小。因为人们已经告诉过您这一点,所以我将向您展示另一种方法。

tf.cumsum()在那些向量上:

import tensorflow as tf 
x = tf.placeholder(tf.int32, shape=(None)) 
y = tf.cumsum(tf.ones_like(x)) - 1 
 
 
with tf.Session() as sess: 
    print sess.run(y, {x: [4, 3, 2, 6, 3]})