Tensorflow Logistic Regression
<p>I am trying to implement: <a href="https://www.tensorflow.org/versions/r0.11/tutorials/wide/index.html" rel="nofollow">https://www.tensorflow.org/versions/r0.11/tutorials/wide/index.html</a>
on my dataset.</p>

<p>I am basically trying to do a binary classification (0 or 1) based on some continuous and categorical features.</p>

<p>NaNs were removed, new features created:</p>

<pre><code>square_feet = tf.contrib.layers.real_valued_column("square_feet")
guests_included = tf.contrib.layers.real_valued_column("guests_included")
security_deposit = tf.contrib.layers.real_valued_column("security_deposit")
cleaning_fee = tf.contrib.layers.real_valued_column("cleaning_fee")
extra_people = tf.contrib.layers.real_valued_column("extra_people")
</code></pre>

<p>and</p>

<pre><code>neighbourhood_group_cleansed = tf.contrib.layers.sparse_column_with_keys(column_name="neighbourhood_group_cleansed", keys=['Bronx', 'Queens', 'Staten Island', 'Brooklyn', 'Manhattan'])

host_response_time = tf.contrib.layers.sparse_column_with_keys(column_name="host_response_time", keys=['within an hour', 'within a few hours', 'within a day', 'a few days or more'])
</code></pre>

<p>-- I got more a lot more features, but I think this conveys the gist.</p>

<p>I copied those functions:</p>

<pre><code>def input_fn(df):
    #  Creates a dictionary mapping from each continuous feature column name (k) to
    #  the values of that column stored in a constant Tensor.
    continuous_cols = {k: tf.constant(df[k].values) for k in CONTINUOUS_COLUMNS}
    #  Creates a dictionary mapping from each categorical feature column name (k)
    #  to the values of that column stored in a tf.SparseTensor.
    categorical_cols = {k: tf.SparseTensor(indices=[[i, 0] for i in range(df[k].size)], values=df[k].values, shape=[df[k].size, 1]) for k in CATEGORICAL_COLUMNS}
    #  Merges the two dictionaries into one.
    feature_cols = dict(continuous_cols.items() + categorical_cols.items())
    #  Converts the label column into a constant Tensor.
    label = tf.constant(df[LABEL_COLUMN].values)
    #  Returns the feature columns and the label.
    return feature_cols, label


def train_input_fn():
    return input_fn(df_train)


def eval_input_fn():
    return input_fn(df_test)
</code></pre>

<p>Which will be later used by </p>

<pre><code>model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.LinearClassifier(feature_columns=FEATURE_COLUMNS, model_dir=model_dir)
m.fit(input_fn=train_input_fn, steps=200)
</code></pre>

<p>If I do a bit of debugging does the function <strong>input_fn()</strong> return a valid <strong>dict</strong> and the <strong>classification</strong>.
However, I get this error:</p>

<pre><code>/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in check_feature_columns(feature_columns)
    510   seen_keys = set()
    511   for f in feature_columns:
--&gt; 512     key = f.key
    513     if key in seen_keys:
    514       raise ValueError('Duplicate feature column key found for column: {}. '
AttributeError: 'str' object has no attribute 'key'
</code></pre>

<p>Debugging output of <strong>feature_cols</strong> (only an excerpt):</p>

<pre><code>'square_feet': &lt;tf.Tensor 'Const_15:0' shape=(10000,) dtype=float64&gt;,
'guests_included': &lt;tf.Tensor 'Const_16:0' shape=(10000,) dtype=float64&gt;,
'security_deposit': &lt;tf.Tensor 'Const_17:0' shape=(10000,) dtype=float64&gt;,
'cleaning_fee': &lt;tf.Tensor 'Const_18:0' shape=(10000,) dtype=float64&gt;,
..
</code></pre>

<p>and for <strong>label</strong></p>

<pre><code>&lt;tf.Tensor 'Const_27:0' shape=(10000,) dtype=int64&gt;
</code></pre>

<p>and for <strong>df[LABEL_COLUMN].values</strong>:</p>

<pre><code>array([1, 1, 1, ..., 1, 1, 1])
</code></pre>

<p>Help, hints, tips much appreciated. Those are my first steps with Tensorflow and I don't how to proceed or to further troubleshoot the error.</p>

<p>Thank you!</p>

<p>--- Update ---</p>

<p>I tried to use</p>

<pre><code>import tensorflow.contrib.learn.python.learn as learn
</code></pre>

<p>and now on a DNN classifier, just on the continuous columns </p>

<pre><code>classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=2, feature_columns=CONTINUOUS_COLUMNS)

classifier.fit(df_train[CONTINUOUS_COLUMNS], df_train['classification'], steps=200, batch_size=32)
</code></pre>

<p>and get the same error</p>

<pre><code>/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/feature_column_ops.pyc in check_feature_columns(feature_columns)
    510   seen_keys = set()
    511   for f in feature_columns:
--&gt; 512     key = f.key
    513     if key in seen_keys:
    514       raise ValueError('Duplicate feature column key found for column: {}. '

AttributeError: 'str' object has no attribute 'key'
</code></pre>

<p>When you train the model you are passing it the list <code>FEATURE_COLUMNS</code> which I believe you have as a list of strings. When tensor flow loops over this list it is trying to access the key property on a string which fails. you probably want to pass it a list of your tensorflow variables i.e. define a new list <code>wide_columns</code>:</p>

<pre><code>wide_columns=[square_feet, guests_included,...]
m = tf.contrib.learn.LinearClassifier(feature_columns=wide_columns, model_dir=model_dir)
m.fit(...)
</code></pre>

python machine-learning tensorflow logistic-regression