テンソルをintに変換する方法



How Convert 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]

もちろん、リストにインデックスを付けるために10進数を整数に変換したいだけの場合は、向きを変える必要はありません。

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