TensorFlow:有没有办法将 None 类型的列表转换为张量? [英] TensorFlow: Is there a way to convert a list with None type to a Tensor?
问题描述
对于我的应用程序,我尝试使用 tf.convert_to_tensor([None, 1, 1, 64])
,但这给了我错误:
For my application, I am trying to convert a list with [None, 1, 1, 64]
to a tensor using tf.convert_to_tensor([None, 1, 1, 64])
, but this gives me the error:
TypeError: 无法转换类型为
理想情况下,我希望 None
成为第一个维度,因为它代表 batch_size.目前,我可以避免此错误的唯一方法是将batch_size 显式提供给操作,但我希望有一种更简洁的方法将此类列表转换为张量.
Ideally, I want None
to be the first dimension because it represents the batch_size. Currently, the only way I could avoid this error is to explicitly give the batch_size to the operation, but I am hoping there is a cleaner way to convert such a list to a tensor.
推荐答案
No,因为 None
和 64
的类型不同,所有的张量都是类型化的:你可以't 在一个张量中有不同类型的元素.
No, because None
and 64
have different types, and all tensors are typed: You can't have elements of different types in one tensor.
你能做的最接近的事情是nan
:
The closest thing you could do is nan
:
tf.convert_to_tensor([np.nan, 1, 1, 64])
虽然我无法想象你为什么想要那个.
although I can't imagine why you'd want that.
然而你可以创建一个TensorShape
:
tf.TensorShape([None, 1, 1, 64])
这篇关于TensorFlow:有没有办法将 None 类型的列表转换为张量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!