2024-04-26 04:46:30 发布
网友
例如,长度为5的张量,指数为1、3和4。结果张量应为:
[0, 1, 0, 1, 1]
谢谢
import tensorflow as tf import numpy a = [] indices = [1, 3, 4] for i in range(5): if i in indices: a.append(1) else: a.append(0) arr = numpy.array(a) x = tf.convert_to_tensor(a, dtype=tf.float32) with tf.Session() as sess: print(sess.run(x))
输出:
[0. 1. 0. 1. 1.]
如果需要,可以调整索引的参数和数组的大小
输出:
如果需要,可以调整索引的参数和数组的大小
相关问题 更多 >
编程相关推荐