我试图亲自动手使用 Tensorflow 并关注 Wide and Deep Learning示例代码。我修改了某些导入以使其在 centos 7 上与 python 3.4 一起工作。
变化的重点是:
-import urllib
+import urllib.request
...
-urllib.urlretrieve
+urllib.request.urlretrieve
...
运行代码时出现如下错误
Training data is downloaded to /tmp/tmpw06u4_xl
Test data is downloaded to /tmp/tmpjliqxhwh
model directory = /tmp/tmpcyll7kck
WARNING:tensorflow:Setting feature info to {'education': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'capital_gain': TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False), 'capital_loss': TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False), 'hours_per_week': TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False), 'gender': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'occupation': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'native_country': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'race': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'age': TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False), 'education_num': TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False), 'marital_status': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'workclass': TensorSignature(dtype=tf.string, shape=None, is_sparse=True), 'relationship': TensorSignature(dtype=tf.string, shape=None, is_sparse=True)}
WARNING:tensorflow:Setting targets info to TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(32561)]), is_sparse=False)
Traceback (most recent call last):
File "wide_n_deep_tutorial.py", line 213, in <module>
tf.app.run()
File "/usr/lib/python3.4/site-packages/tensorflow/python/platform/app.py", line 30, in run
sys.exit(main(sys.argv))
File "wide_n_deep_tutorial.py", line 209, in main
train_and_eval()
File "wide_n_deep_tutorial.py", line 202, in train_and_eval
m.fit(input_fn=lambda: input_fn(df_train), steps=FLAGS.train_steps)
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 240, in fit
max_steps=max_steps)
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 550, in _train_model
train_op, loss_op = self._get_train_ops(features, targets)
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 182, in _get_train_ops
logits = self._logits(features, is_training=True)
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 260, in _logits
dnn_feature_columns = self._get_dnn_feature_columns()
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 224, in _get_dnn_feature_columns
feature_column_ops.check_feature_columns(self._dnn_feature_columns)
File "/usr/lib/python3.4/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 318, in check_feature_columns
f.name))
ValueError: Duplicate feature column key found for column: education_embedding. This usually means that the column is almost identical to another column, and one must be discarded.
是我更改了一些变量还是这是 python 3 问题。我怎样才能继续学习本教程。
最佳答案
最终更新 我在推荐的 0.10rc0 分支上遇到了这个问题,但是在使用 master 重新安装后(git clone 上没有分支)这个问题就消失了。我检查了源代码,他们修复了它。在修复您已经提到的 urllib.request 问题之后,Python 3 现在在 wide_n_deep 模式下获得与 Python 2 相同的结果。
对于任何后来仍在使用 0.10rc0 分支的人,请继续阅读:
遇到了同样的问题,调试了一下。看起来像 _EmbeddingColumn 类中的 tensorflow/contrib/layers/python/layers/feature_column.py 中的错误。 key(self) 属性受到此错误的困扰: https://bugs.python.org/issue24931
因此,我们为所有 _EmbeddingColumn 实例获取以下 key ,而不是使用一个漂亮的唯一 key : '_EmbeddingColumn()'
这会导致 feature_column_ops.py 的 check_feature_columns() 函数确定第二个 _EmbeddingColumn 实例是重复的,因为它们的键都是相同的。
我是个 Python 菜鸟,我不知道如何猴子修补一个属性。所以我通过在 wide_n_deep 教程文件的顶部创建一个子类来解决这个问题:
# EmbeddingColumn for Python 3.4 has a problem with key property
# can't monkey patch a property, so subclass it and make a method to create the
# subclass to use instead of "embedding_column"
from tensorflow.contrib.layers.python.layers.feature_column import _EmbeddingColumn
class _MonkeyEmbeddingColumn(_EmbeddingColumn):
# override the key property
@property
def key(self):
return "{}".format(self)
def monkey_embedding_column(sparse_id_column,
dimension,
combiner="mean",
initializer=None,
ckpt_to_load_from=None,
tensor_name_in_ckpt=None):
return _MonkeyEmbeddingColumn(sparse_id_column, dimension, combiner, initializer, ckpt_to_load_from, tensor_name_in_ckpt)
然后找到这样的调用:
tf.contrib.layers.embedding_column(workclass, dimension=8)
并替换“tf.contrib.layers”。使用“monkey_”所以你现在有:
deep_columns = [
monkey_embedding_column(workclass, dimension=8),
monkey_embedding_column(education, dimension=8),
monkey_embedding_column(marital_status,
dimension=8),
monkey_embedding_column(gender, dimension=8),
monkey_embedding_column(relationship, dimension=8),
monkey_embedding_column(race, dimension=8),
monkey_embedding_column(native_country,
dimension=8),
monkey_embedding_column(occupation, dimension=8),
age,
education_num,
capital_gain,
capital_loss,
hours_per_week,
]
现在它使用具有修改后的键属性的 MonkeyEmbeddingColumn 类(与 feature_column.py 中的所有其他键属性一样工作)。这让代码运行完成,但我不能 100% 确定它是正确的,因为它报告的准确性如下:
accuracy: 0.818316
由于这比仅宽训练稍差,我想知道它在 Python 2 中是否具有这种准确性,或者我的修复是否因导致训练问题而降低了准确性。
更新 我在 Python 2 中安装并且 wide_n_deep 的精度超过 0.85,因此此“修复”允许代码运行但似乎做错了事。我将调试并查看 Python 2 为这些值获取了什么,并查看它是否可以在 Python 3 中正确修复。我也很好奇。
关于python - tensorflow : ValueError Duplicate feature column key found for column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249704/