How can I convert a tensor into a ndarray in TensorFlow?

前端 未结 1 403
独厮守ぢ
独厮守ぢ 2021-01-14 18:53

My goal is to convert a tensor into a ndarray without \'run\' or \'eval\'. I wanted to perform the same operation as the example.



        
相关标签:
1条回答
  • 2021-01-14 19:15

    tf.make_ndarray is used to convert TensorProto values into NumPy arrays. These values are generally the constants used in a graph. For example, when you use tf.constant, you create a Const operation with an attribute value holding the constant value that the operation will produce. That attribute is stored as a TensorProto. Hence, you can "extract" the value of a Const operation as a NumPy array like this:

    import tensorflow as tf
    
    A = tf.constant(5)
    C = tf.make_ndarray(A.op.get_attr('value'))
    print(C, type(C))
    # 5 <class 'numpy.ndarray'>
    

    In general, though, you cannot convert arbitrary tensors into NumPy arrays, as their values will depend on the values of the variables and the fed inputs within a particular session.

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