【问题标题】:Tensorflow Logistic RegressionTensorFlow 逻辑回归
【发布时间】:2016-10-16 12:06:48
【问题描述】:

我正在尝试实现:https://www.tensorflow.org/versions/r0.11/tutorials/wide/index.html 在我的数据集上。

我基本上是在尝试基于一些连续和分类特征进行二进制分类(0 或 1)。

NaN 被移除,新功能被创建:

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")

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'])

-- 我有更多更多的功能,但我认为这传达了要点。

我复制了这些函数:

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)

后面会用到

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)

如果我进行一些调试,函数 input_fn() 是否会返回一个有效的 dictclassification。 但是,我收到此错误:

/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:
--> 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'

feature_cols 的调试输出(仅摘录):

'square_feet': <tf.Tensor 'Const_15:0' shape=(10000,) dtype=float64>,
'guests_included': <tf.Tensor 'Const_16:0' shape=(10000,) dtype=float64>,
'security_deposit': <tf.Tensor 'Const_17:0' shape=(10000,) dtype=float64>,
'cleaning_fee': <tf.Tensor 'Const_18:0' shape=(10000,) dtype=float64>,
..

对于标签

<tf.Tensor 'Const_27:0' shape=(10000,) dtype=int64>

对于 df[LABEL_COLUMN].values

array([1, 1, 1, ..., 1, 1, 1])

非常感谢帮助、提示和提示。这些是我使用 Tensorflow 的第一步,我不知道如何继续或进一步解决错误。

谢谢!

--- 更新---

我尝试使用

import tensorflow.contrib.learn.python.learn as learn

现在在 DNN 分类器上,就在连续列上

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)

得到同样的错误

/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:
--> 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'

【问题讨论】:

  • 你能显示堆栈跟踪吗?

标签: python machine-learning tensorflow logistic-regression


【解决方案1】:

当您训练模型时,您会将列表 FEATURE_COLUMNS 传递给它,我相信您将其作为字符串列表。当张量流在此列表上循环时,它试图访问失败的字符串上的 key 属性。您可能想将您的 tensorflow 变量列表传递给它,即定义一个新列表 wide_columns:

wide_columns=[square_feet, guests_included,...]
m = tf.contrib.learn.LinearClassifier(feature_columns=wide_columns, model_dir=model_dir)
m.fit(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多