【发布时间】:2018-07-13 16:23:13
【问题描述】:
这可能完全是一个与 python 模块导入有关的菜鸟问题,但我不明白为什么以下内容有效:
> import tensorflow as tf
> f = tf.train.Feature()
> from tensorflow import train
> f = train.Feature()
但是下面的语句会报错:
> from tensorflow.train import Feature
ModuleNotFoundError: No module named 'tensorflow.train'
请有人解释一下为什么它不能这样工作?我的目标是在代码中使用更短的符号,如下所示:
> example = Example(
features=Features(feature={
'x1': Feature(float_list=FloatList(value=feature_x1.ravel())),
'x2': Feature(float_list=FloatList(value=feature_x2.ravel())),
'y': Feature(int64_list=Int64List(value=label))
})
)
tensorflow 版本为 1.7.0
【问题讨论】:
-
我不知道为什么会抛出这个错误(我不想详细介绍),但我知道实现你想要的“解决方法”。您可以导入
train,如您的示例之一,即from tensorflow import train,然后执行Feature = train.Feature(请注意,我将一个类对象分配给一个名为Feature的变量,即创建一个别名)。之后,您可以直接使用Feature(...),而无需在其前面加上train(或任何其他前缀)。 -
是的,该解决方法将起作用。但是如果我想对 3-6 个以上的类(FloatList、Int64List、BytesList、Example 等)做同样的事情,代码会变得有点混乱 - 必须对每个类重复相同的过程。
标签: python-3.x tensorflow