tensor也可以作为索引

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_38314865/article/details/84767220

在TensorFlow中,tensor也可以作为索引,但只能作为同样为tensor类型变量的索引,不能作为list类型变量的索引如下面的例子:

import tensorflow as tf

index = tf.to_int32([0,1,2])  # index是一个tensor
a = [[1,2,3], [4,5,6]]
b = a[index[0]]
sess = tf.InteractiveSession()
print(b)

TypeError: list indices must be integers or slices, not Tensor

若将a变为tensor类型

import tensorflow as tf

index = tf.to_int32([0,1,2])  # index是一个tensor
a = tf.convert_to_tensor([[1,2,3], [4,5,6]])
b = a[index[0]]
sess = tf.InteractiveSession()
print(sess.run(b))  # [1 2 3]

猜你喜欢

转载自blog.csdn.net/weixin_38314865/article/details/84767220