我在tensorflow中创建了一个OP,在其中进行一些处理时需要将我的数据从张量对象转换为numpy数组。我知道我们可以使用tf.eval()或sess.run评估任何张量对象。我真正想知道的是,有没有办法在无需运行任何会话的情况下将张量转换为数组,因此我们又避免使用.eval()or .run()。
任何帮助深表感谢!
def tensor_to_array(tensor1):
'''Convert tensor object to numpy array'''
array1 = SESS.run(tensor1) **#====== need to bypass this line**
return array1.astype("uint8")
def array_to_tensor(array):
'''Convert numpy array to tensor object'''
tensor_data = tf.convert_to_tensor(array, dtype=tf.float32)
return tensor_data
Run Code Online (Sandbox Code Playgroud) python ×1