AttributeError:' str'对象没有属性' name'在Tensorflow

时间:2018-02-05 09:31:57

标签: python tensorflow

我正在尝试使用Dnnregressor预测商品的价格,而我无法弄清楚这种不断出现的错误。我从pandas dataframe创建了数字和分类列,并将其输入DNNRegressor。关于这个特殊错误,网上帮助不大。

请帮我修复此错误。感谢

AttributeError                            Traceback (most recent call last)
<ipython-input-27-790ecef8c709> in <module>()
 92 
 93 if __name__ == '__main__':
---> 94     main()

<ipython-input-27-790ecef8c709> in main()
 81      #   learning_rate=0.1, l1_regularization_strength=0.001))
 82     est = tf.estimator.DNNRegressor(feature_columns = feature_columns,  hidden_units = [10, 10], model_dir = 'data')
---> 83     est.train(input_fn = get_train_input_fn(Xtrain, ytrain), steps = 500)
 84     scores = est.evaluate(input_fn = get_test_input_fn(Xtest, ytest))
 85     print('Loss Score: {0:f}' .format(scores['average_loss']))

C:\Users\user\Anaconda3\lib\site-   packages\tensorflow\python\estimator\estimator.py in train(self, input_fn, hooks, steps, max_steps)
239       hooks.append(training.StopAtStepHook(steps, max_steps))
240 
--> 241     loss = self._train_model(input_fn=input_fn, hooks=hooks)
242     logging.info('Loss for final step: %s.', loss)
243     return self

C:\Users\user\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py in _train_model(self, input_fn, hooks)
628           input_fn, model_fn_lib.ModeKeys.TRAIN)
629       estimator_spec = self._call_model_fn(features, labels,
--> 630                                            model_fn_lib.ModeKeys.TRAIN)
631       ops.add_to_collection(ops.GraphKeys.LOSSES, estimator_spec.loss)
632       all_hooks.extend(hooks)

C:\Users\user\Anaconda3\lib\site- packages\tensorflow\python\estimator\estimator.py in _call_model_fn(self, features, labels, mode)
613     if 'config' in model_fn_args:
614       kwargs['config'] = self.config
--> 615     model_fn_results = self._model_fn(features=features, **kwargs)
616 
617     if not isinstance(model_fn_results, model_fn_lib.EstimatorSpec):

C:\Users\user\Anaconda3\lib\site-packages\tensorflow\python\estimator\canned\dnn.py in _model_fn(features, labels, mode, config)
389           dropout=dropout,
390           input_layer_partitioner=input_layer_partitioner,
--> 391           config=config)
392     super(DNNRegressor, self).__init__(
393         model_fn=_model_fn, model_dir=model_dir, config=config)

C:\Users\user\Anaconda3\lib\site-packages\tensorflow\python\estimator\canned\dnn.py in _dnn_model_fn(features, labels, mode, head, hidden_units, feature_columns, optimizer, activation_fn, dropout, input_layer_partitioner, config)
100       net = feature_column_lib.input_layer(
101           features=features,
--> 102           feature_columns=feature_columns)
103 
104     for layer_id, num_hidden_units in enumerate(hidden_units):

C:\Users\user\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column.py in input_layer(features, feature_columns, weight_collections, trainable)
205     ValueError: if an item in `feature_columns` is not a `_DenseColumn`.
206   """
--> 207   _check_feature_columns(feature_columns)
208   for column in feature_columns:
209     if not isinstance(column, _DenseColumn):

 C:\Users\user\Anaconda3\lib\site-  packages\tensorflow\python\feature_column\feature_column.py in     _check_feature_columns(feature_columns)
1660   name_to_column = dict()
1661   for column in feature_columns:
-> 1662     if column.name in name_to_column:
1663       raise ValueError('Duplicate feature column name found for columns: {} '
1664                        'and {}. This usually means that these columns refer to '

C:\Users\user\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column.py in name(self)
2451   @property
2452   def name(self):
-> 2453     return '{}_indicator'.format(self.categorical_column.name)
2454 
2455   def _transform_feature(self, inputs):

AttributeError: 'str' object has no attribute 'name'

以下是代码:

def get_train_input_fn(Xtrain, ytrain):
return tf.estimator.inputs.pandas_input_fn(
        x = Xtrain,
        y = ytrain,
        batch_size = 30,
        num_epochs = None,
        shuffle = True)

def get_test_input_fn(Xtest, ytest):
return tf.estimator.inputs.pandas_input_fn(
        x = Xtest,
        y = ytest,
        batch_size = 32,
        num_epochs = 1,
        shuffle = False)
def main():
Xtrain, Xtest, ytrain, ytest = train_test_split(merc, ytr, test_size = 0.4, random_state = 42)
feature_columns = []
brand_rating = tf.feature_column.numeric_column('brand_rating')
feature_columns.append(brand_rating)
sentiment = tf.feature_column.numeric_column('description_polarity')
feature_columns.append(sentiment)
item_condition = tf.feature_column.numeric_column('item_condition_id')
feature_columns.append(item_condition)
shipping = tf.feature_column.indicator_column('shipping')
feature_columns.append(shipping)
name = tf.feature_column.embedding_column('item_name', 34) #(column name, dimension(no. of unique values ** 0.25))
feature_columns.append(name)
general = tf.feature_column.categorical_column_with_hash_bucket('General', 12)
feature_columns.append(general)
sc1 = tf.feature_column.categorical_column_with_hash_bucket('SC1', 120)
feature_columns.append(sc1)
sc2 = tf.feature_column.categorical_column_with_hash_bucket('SC2', 900)
feature_columns.append(sc2)
print(feature_columns)
#est = tf.estimator.DNNRegressor(feature_columns, hidden_units = [10, 10], optimizer=tf.train.ProximalAdagradOptimizer(
 #   learning_rate=0.1, l1_regularization_strength=0.001))
est = tf.estimator.DNNRegressor(feature_columns = feature_columns, hidden_units = [10, 10], model_dir = 'data')
est.train(input_fn = get_train_input_fn(Xtrain, ytrain), steps = 500)

2 个答案:

答案 0 :(得分:1)

tf.feature_column.embedding_column的第一个参数必须是分类列,而不是字符串。请参阅API spec

代码中的违规行是:

tf.feature_column.embedding_column('item_name', 34) 

答案 1 :(得分:0)

使用后

general = tf.feature_column.categorical_column_with_hash_bucket('General', 12)

和其他feature_column.categorical_column_with ...,则应使用

general_indicator = tf.feature_column.indicator_column(general)

,然后将其附加到feature_columns列表中。

feature_columns.append(general_indicator)