Tensorflow: How do I convert a EagerTensor into a numpy array?

前端 未结 1 615
温柔的废话
温柔的废话 2020-12-29 18:52

With standard Tensorflow:

import tensorflow as tf

x = tf.convert_to_tensor([0,1,2,3,4], dtype=tf.int64)
y = x + 10

sess = tf.InteractiveSession()
sess.run(         


        
相关标签:
1条回答
  • 2020-12-29 19:36

    There is a .numpy() function which you can use, alternatively you could also do numpy.array(y). For example:

    import tensorflow as tf
    import numpy as np
    
    tf.enable_eager_execution()
    
    x = tf.constant([1., 2.])
    print(type(x))            # <type 'EagerTensor'>
    print(type(x.numpy()))    # <type 'numpy.ndarray'>
    print(type(np.array(x)))  # <type 'numpy.ndarray'>
    

    See the section in the eager execution guide.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题