简体   繁体   中英

How to use tf.make_tensor_proto to convert tensor to a ndarray in Tensorflow version 1 with session establish?

I can use the tf.make_tensor_proto in tensorflow2 to convert the tensor into ndarray like this way

action=tf.Variable([20.,10.,6.,9.],dtype=tf.float64)
bound=tf.Variable([10,20,5,10],dtype=tf.float64)
mask=tf.greater(bound, action )
proto_tensor=tf.make_tensor_proto(mask)
ndarry=tf.make_ndarray(proto_tensor)
print(ndarry)

and this can outpt [False True False True] with ndarry type

However, I want to do this in tensorflow1 with session and I try this way

#using sess.run must disable eager execution in tensorflow 2
tf.compat.v1.disable_eager_execution() 
action=tf.Variable([20.,10.,6.,9.],dtype=tf.float64)
bound=tf.Variable([10,20,5,10],dtype=tf.float64)
sess = tf.compat.v1.Session()
mask = tf.greater(bound,action)
proto_tensor=tf.compat.v1.make_tensor_proto(mask)
ndarry=tf.compat.v1.make_ndarray(proto_tensor)
print(sess.run(ndarry))

it will have this error

Traceback (most recent call last):

  File "C:\Users\stitch\Desktop\CG_metting0\gym-BSS-master\DDPG-With-EnvBSS\tensor_test_v2.py", line 407, in <module>
    proto_tensor=tf.compat.v1.make_tensor_proto(mask)

  File "C:\Users\stitch\anaconda\envs\CG_tensor2\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 451, in make_tensor_proto
    _AssertCompatible(values, dtype)

  File "C:\Users\stitch\anaconda\envs\CG_tensor2\lib\site-packages\tensorflow_core\python\framework\tensor_util.py", line 328, in _AssertCompatible
    raise TypeError("List of Tensors when single Tensor expected")

TypeError: List of Tensors when single Tensor expected

And I try case by case see that, if do not using session in v1(therefore we don't need to disable the eager execution), but in my study case, I need to have session in my code, is there anyway that I can change it to the ndarray under this function(make_tesensor_proto and make_ndarry in tensorflow) without error?

What was wrong: You cannot convert a tf.variable to a numpy array using graph execution and need to use a tf.constant instead. Also using tf.make_tensor_proto is pointless since sess.run converts the tensor into a numpy array for you.

Explanation: Convert your actions to TF constants instead of Variables or use Constants instead (not possible to convert variables). Then apply the same logic to your bound. Next, create the mask as you did previously. Finally, create a session and evaluate the mask in the session.

Here is the code to convert your mask into a numpy array using graph execution:

import tensorflow as tf
import numpy as np

graph = tf.Graph()

with graph.as_default():
  action=tf.constant([20.,10.,6.,9.],dtype=tf.float64)
  bound=tf.constant([10,20,5,10],dtype=tf.float64)
  mask = tf.greater(bound,action)

  with tf.compat.v1.Session() as sess:
    result = sess.run(mask)

print(result)

Based on your situation I think this is the best way of doing it. But you could also use tf.py_func to create a function that would convert the mask into a numpy array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM