Tensorflow功能列:AttributeError:'tuple'对象没有属性'name'

时间:2018-09-05 11:18:13

标签: python tensorflow

我正在尝试使用8个要素列,3个数字和5个分类来训练一个简单的数据集。我目前的基本训练脚本如下所示:

import tensorflow as tf

feature_names = [
    'col1', 'col2', 'col3', 'col4', 'col5', 'col6',
    'col7', 'col8'
]


def input_fn(file_path, perform_shuffle=False, repeat_count=1):
    """
    """

    def decode_csv(line):
        """
        """
        parsed_line = tf.decode_csv(line, [[0], [0], [0], [''],[''],[''],[''],[''], [0]], na_value='?')

        label = parsed_line[-1:] # Last element is the label
        del parsed_line[-1] # Delete last element
        features = parsed_line
        d = dict(zip(feature_names, features)), label

        return d

    # Read in our file, skip the header row, and transform each element
    dataset = (tf.data.TextLineDataset(file_path)
       .skip(1)
       .map(decode_csv)) 

    # Randomizes input using a window of 256 elements (read into memory)
    if perform_shuffle:
        dataset = dataset.shuffle(buffer_size=256)

    dataset = dataset.repeat(repeat_count) # Repeats dataset this # times
    dataset = dataset.batch(32)  # Batch size to use

    iterator = dataset.make_one_shot_iterator()
    batch_features, batch_labels = iterator.get_next()

    return batch_features, batch_labels

然后我建立功能列:

col4 = tf.feature_column.categorical_column_with_identity(key='col4', num_buckets=3), 
col5 = tf.feature_column.categorical_column_with_identity(key='col5', num_buckets=3), 
col6 = tf.feature_column.categorical_column_with_identity(key='col6', num_buckets=2), 
col7 = tf.feature_column.categorical_column_with_identity(key='col7', num_buckets=2), 
col8 = tf.feature_column.categorical_column_with_identity(key='col8', num_buckets=2)    

feature_columns = [
    tf.feature_column.numeric_column(key='col1'), 
    tf.feature_column.numeric_column(key='col2'), 
    tf.feature_column.numeric_column(key='col3'), 
    tf.feature_column.indicator_column(col4),
    tf.feature_column.indicator_column(col5),
    tf.feature_column.indicator_column(col6),
    tf.feature_column.indicator_column(col7),
    tf.feature_column.indicator_column(col8)
]

classifier = tf.estimator.DNNClassifier(
    feature_columns=feature_columns,
    hidden_units=[10,10],
    n_classes=14,
    model_dir="data"
)

classifier.train(input_fn=lambda: input_fn("data/development.csv", True, 8))

但是,我收到一个非常平淡的错误消息:

  

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

堆栈跟踪指示分类列包装的指示符列需要一个名称。但是我不确定为什么会这样,据我所知,所有有关使用功能列的文档和教程都没有引用任何需要的name参数。

1 个答案:

答案 0 :(得分:0)

我确定您现在已经自己弄清楚了,但是对于任何有相同错误的人,可能只是从列表中以逗号结尾将其复制出来了。

结尾的逗号使“ col4”成为元组

col4 = tf.feature_column.categorical_column_with_identity(key='col4', num_buckets=3),