我正在尝试使用两个功能在输入上运行DNNRegressor
,其中一个功能是分类的。我想我需要在一个热门编码中表示这个输入,但我很难设置feature_columns
(错误如下所示)。
如何使用提供的样本数据训练DNNRegressor
(在下面的代码中)?
import tensorflow as tf
import pandas as pd
def main():
data = pd.DataFrame({
"make": ["Mazda", "Mazda", "Mazda", "Toyota", "Toyota", "Toyota"],
"years_old": [0, 2, 5, 0, 2, 5],
"price": [20000, 12000, 8000, 18000, 11000, 8000]
})
def get_train_inputs():
feature_columns = {
"make": tf.constant(data["make"].values),
"years_old": tf.constant(data["years_old"].values)
}
labels = tf.constant(data["price"].values)
return feature_columns, labels
feature_columns = [
tf.contrib.layers.one_hot_column("make"),
tf.contrib.layers.real_valued_column("years_old")
]
regressor = tf.contrib.learn.DNNRegressor(
feature_columns=feature_columns,
hidden_units=[10, 10]
)
regressor.fit(input_fn=get_train_inputs, steps=2)
if __name__ == "__main__":
main()
我在这段代码中遇到的错误:
Traceback (most recent call last):
File "/app/run.py", line 33, in <module>
main()
File "/app/run.py", line 30, in main
regressor.fit(input_fn=get_train_inputs, steps=2)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 281, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 430, in fit
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 927, in _train_model
model_fn_ops = self._get_train_ops(features, labels)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1132, in _get_train_ops
return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1103, in _call_model_fn
model_fn_results = self._model_fn(features, labels, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 132, in _dnn_model_fn
scope=input_layer_scope)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 249, in input_from_feature_columns
default_name='input_from_feature_columns')
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.py", line 159, in _input_from_feature_columns
default_name=column.name,
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/layers/python/layers/feature_column.py", line 817, in name
return "{}_one_hot".format(self.sparse_id_column.name)
AttributeError: 'str' object has no attribute 'name'
答案 0 :(得分:0)
您不应将字符串作为one_hot_column的参数传递。使用“feature_column.sparse_column_with_hash_bucket”(或种类)如下:
sparse_column = feature_column.sparse_column_with_hash_bucket(
'make', hash_bucket_size=6)
feature_columns = [
feature_column.one_hot_column(sparse_column),
feature_column.real_valued_column('years_old')
]
而不是
feature_columns = [
tf.contrib.layers.one_hot_column("make"),
tf.contrib.layers.real_valued_column("years_old")
]