Tensorflow:Py_func返回未知形状 [英] Tensorflow: Py_func returns unknown shape
问题描述
我对tf.py_func
函数有一个简单的问题.
I have a simple question re the tf.py_func
function.
我有一个形状为(1,224,224,3)
的图像张量my_img
.为了测试py_func
,我将张量输入到python函数return_tf
中,该函数应返回相同的张量(按照文档转换为numpy数组后).
I have an image tensor my_img
of shape (1,224,224,3)
. To test py_func
, I feed the tensor to a python function return_tf
that should give back the same tensor (after being converted to a numpy array as per docs).
代码如下:
def return_tf(x):
return np.array(x)
test = tf.py_func(return_tf,[my_img],[tf.float32])
但是当我检查返回的张量的形状称为test
时,我得到:
But when I checked the shape of the returned tensor called test
, I get:
tf.Tensor 'PyFunc:0' shape=unknown dtype=float32
由于出现错误,我也无法在张量上运行eval()
:
I am also unable to run eval()
on the tensor, since I get the error:
AttributeError: 'list' object has no attribute 'eval'.
任何人都知道如何修复tf.py_func
返回的张量的张量形状吗?
Anyone knows how could I fix the tensor shape of the tensor returned by tf.py_func
?
推荐答案
刚刚找到了解决方法..由于py_func返回张量流列表,我可以执行以下操作:
Just found a work-around.. since py_func returns a tensorflow list, I can do the ff:
test = tf.reshape(tf.concat(1, test), [ <<a shape>> ])
获得具有所需形状的张量
to get a tensor with a desired shape
这篇关于Tensorflow:Py_func返回未知形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!