2017-06-09 15 views
1

入力にDNNRegressorを実行しようとしていますが、そのうちの1つはカテゴリです。私はこの入力を1つのホットエンコーディングで表現する必要があると思うが、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

あなたは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") 
] 
関連する問題