【问题标题】:How to create `input_fn` using `read_batch_examples` with `num_epochs` set?如何使用设置了“num_epochs”的“read_batch_examples”创建“input_fn”?
【发布时间】:2016-10-05 15:08:50
【问题描述】:

我有一个基本的input_fn,可以与下面的 Tensorflow Estimators 一起使用。无需设置num_epochs 参数即可完美运行;获得的张量具有离散的形状。传入num_epochs,因为None 以外的任何内容都会导致未知形状。我的问题在于在使用num_epochs 时构造稀疏张量;在不知道输入张量的形状的情况下,我无法弄清楚如何一般地创建所述张量。

谁能想到这个问题的解决方案?我希望能够通过num_epochs=1 能够仅对数据集进行一次评估,以及传递给predict 以产生一组数据集大小的预测,不再没有少。

def input_fn(batch_size):
    examples_op = tf.contrib.learn.read_batch_examples(
        FILE_NAMES,
        batch_size=batch_size,
        reader=tf.TextLineReader,
        num_epochs=1,
        parse_fn=lambda x: tf.decode_csv(x, [tf.constant([''], dtype=tf.string)] * len(HEADERS)))

    examples_dict = {}
    for i, header in enumerate(HEADERS):
        examples_dict[header] = examples_op[:, i]

    continuous_cols = {k: tf.string_to_number(examples_dict[k], out_type=tf.float32)
                       for k in CONTINUOUS_FEATURES}

    # Problems lay here while creating sparse categorical tensors
    categorical_cols = {
        k: tf.SparseTensor(
            indices=[[i, 0] for i in range(examples_dict[k].get_shape()[0])],
            values=examples_dict[k],
            shape=[int(examples_dict[k].get_shape()[0]), 1])
        for k in CATEGORICAL_FEATURES}

    feature_cols = dict(continuous_cols)
    feature_cols.update(categorical_cols)
    label = tf.string_to_number(examples_dict[LABEL], out_type=tf.int32)

    return feature_cols, label

【问题讨论】:

    标签: python tensorflow skflow


    【解决方案1】:

    我通过创建一个特定于input_fn 预期的函数解决了上述问题;它接受一个密集的列并在不知道形状的情况下创建一个 SparseTensor。使用tf.rangetf.shape 使该功能成为可能。事不宜迟,下面是工作通用的input_fn 代码,它独立于设置的num_epochs

    def input_fn(batch_size):
        examples_op = tf.contrib.learn.read_batch_examples(
            FILE_NAMES,
            batch_size=batch_size,
            reader=tf.TextLineReader,
            num_epochs=1,
            parse_fn=lambda x: tf.decode_csv(x, [tf.constant([''], dtype=tf.string)] * len(HEADERS)))
    
        examples_dict = {}
        for i, header in enumerate(HEADERS):
            examples_dict[header] = examples_op[:, i]
    
        feature_cols = {k: tf.string_to_number(examples_dict[k], out_type=tf.float32)
                        for k in CONTINUOUS_FEATURES}
    
        feature_cols.update({k: dense_to_sparse(examples_dict[k])
                             for k in CATEGORICAL_FEATURES})
    
        label = tf.string_to_number(examples_dict[LABEL], out_type=tf.int32)
    
        return feature_cols, label
    
    
    def dense_to_sparse(dense_tensor):
        indices = tf.to_int64(tf.transpose([tf.range(tf.shape(dense_tensor)[0]), tf.zeros_like(dense_tensor, dtype=tf.int32)]))
        values = dense_tensor
        shape = tf.to_int64([tf.shape(dense_tensor)[0], tf.constant(1)])
    
        return tf.SparseTensor(
            indices=indices,
            values=values,
            shape=shape
        )
    

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 2021-12-01
      相关资源
      最近更新 更多