【问题标题】:Import csv row as array in tensorflow在tensorflow中将csv行作为数组导入
【发布时间】:2018-09-21 15:34:51
【问题描述】:

我有一个 csv 文件,其中包含大量 N 列:第一列包含标签,另一列 N-1 是我的数据的数字表示(来自音乐录音的色度特征)。

我的想法是将输入数据表示为一个数组。在实践中,我想要一个等效于计算机视觉中数据的标准表示。由于我的数据存储在 csv 中,因此在输入 train 函数的定义中,我需要一个 csv 解析器。我是这样做的

def parse_csv(line):
    columns = tf.decode_csv(line, record_defaults=DEFAULTS)  # take a line at a time
    features = {'songID': columns[0], 'x': columns[1:]}  # create a dictionary out of the features
    labels = features.pop('songID')  # define the label
    return features, labels


def train_input_fn(data_file=fp, batch_size=128):
    """Generate an input function for the Estimator."""

    # Extract lines from input files using the Dataset API.
    dataset = tf.data.TextLineDataset(data_file)
    dataset = dataset.map(parse_csv)
    dataset = dataset.shuffle(1_000_000).repeat().batch(batch_size)
    return dataset.make_one_shot_iterator().get_next()

但是,这会返回一个意义不大的错误:AttributeError: 'list' object has no attribute 'get_shape'。我知道罪魁祸首是特征字典中 x 的定义,但我不知道如何纠正它,因为从根本上说,我还没有真正了解 tensorflow 的数据结构。

【问题讨论】:

    标签: python csv tensorflow data-import


    【解决方案1】:

    事实证明,特征需要是张量。但是,每一列本身就是一个张量,采用columns[1:] 会产生一个张量列表。要创建一个存储来自N-1 列的信息的高维张量,应该使用tf.stack

    features = {'songID': columns[0], 'x': tf.stack(columns[1:])}  # create a dictionary out of the features
    

    【讨论】:

      【解决方案2】:

      tf.stack 应该可以解决。

      下面的线程中有一个完整的代码示例。

      Tensorflow Python reading 2 files

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        • 2015-07-04
        • 2015-07-06
        • 2014-09-19
        相关资源
        最近更新 更多