首页 > 解决方案 > Tensorflow 张量无法转换为 numpy

问题描述

我想转换tensorflow.python.framework.ops.Tensor为 NumPy。

TensorFlow 版本是2.4.1

我试图搜索一些方法,但没有奏效。

x = tf.reshape(tf.io.decode_raw(x, tf.float64), (3, 4096))
x = x.numpy()

以上结果AttributeError: 'Tensor' object has no attribute 'numpy' (我tf.executing_eagerly()明确添加,但没有工作)

我试过x = x.eval(session=tf.compat.v1.Session())了,但发生了以下错误。

InvalidArgumentError: You must feed a value for placeholder tensor 'args_0' with dtype string

请告诉我好主意。

标签: pythontensorflow

解决方案


示例工作代码

import tensorflow as tf
x=[["1"],["23"]]
x = tf.reshape(tf.io.decode_raw(tf.constant(x), tf.uint8, fixed_length=4), (2,4))
x = x.numpy()
x

输出

array([[49,  0,  0,  0],
       [50, 51,  0,  0]], dtype=uint8)

推荐阅读