10

Is it possible to transform a 1D tensor to a list ?

thank you

David
  • 175
  • 1
  • 2
  • 7
  • 2
    Have a look at this answer: https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow - it is relatively straightforward to convert to an array, from which you can use `list()` to convert to a list – 4Oh4 Nov 27 '17 at 13:09
  • Thank you ! I didn't see this question – David Nov 27 '17 at 13:11
  • 2
    Possible duplicate of [How can I convert a tensor into a numpy array in TensorFlow?](https://stackoverflow.com/questions/34097281/how-can-i-convert-a-tensor-into-a-numpy-array-in-tensorflow) – Sunreef Nov 27 '17 at 13:13

2 Answers2

16
l = tf.Variable([0, 1, 2, 3]).numpy().tolist()
type(l)
>>> list

l
>>> [0, 1, 2, 3]

This method lets convert to list not only 1D tensor, but also any other shape

MariaMsu
  • 406
  • 6
  • 14
0
import tensorflow as tf

a = tf.constant([[1,2,3],[4,5,6]])
a = tf.make_tensor_proto(a)
a = tf.make_ndarray(a)
a = a.tolist()

OR

import tensorflow as tf

a = tf.constant([[1,2,3],[4,5,6]])
a = a.numpy().tolist()