1
我試圖在輸入上運行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'