如何将tensor转成int

import tensorflow as tf
#
# a = tf.constant([[[1,2],[3,2],[2,2]],[[1,2],[3,2],[2,2]]])
# b = a[:,:,0]
# with tf.Session() as sess:
#     print(sess.run(b))
#     print(sess.run(tf.reduce_sum(a,1)))

a = [1,2,3,4,5]
b = tf.round(len(a)/2)
print(type(b))
# c = tf.to_int32(b.eval())

with tf.Session() as sess:
    print(sess.run(b))
    x = b.eval()
    print(x)
    print(type(x))
    b = int(x)
    print(b)
    d = a[:b]
    e = a[b:]
    print(d)
    print(e)

2019-05-26 10:48:10.499484: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2.0
2.0

2
[1, 2]
[3, 4, 5]

当然如果只是想将一个小数变成整数来索引list则不用这么转来转去,

q = round(2.5)
print(type(q))
print(a[:q])
2

[1, 2]
但是如果是在sess返回之前转,得在之前的代码里使用 b = tf.to_int32(b)

你可能感兴趣的:(python)