创建形状有序整数的张量(无,1)

2024-04-19 19:54:16 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个大小为(None,1)的输入批,是否可以创建一个形状相同的有序整数张量?你知道吗

例如:

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

例如:

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

Tags: noneinputoutput整数形状有序
2条回答

^{}做你需要的,你只需要根据你的输入张量的大小来提供大小。因为人们已经告诉你了,我将给你展示另一种方法。你知道吗

^{}在一个向量上:

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]})

你可以试试这个:

x = tf.placeholder(tf.float32, shape=(None, 1))
op = tf.range(tf.size(x))[:,tf.newaxis]

# test with different sizes
sess.run(op, {x: np.expand_dims(range(10), axis=-1)})
sess.run(op, {x: np.expand_dims(range(3), axis=-1)})

相关问题 更多 >