【问题标题】:TensorFlow Dataset API Parsing ErrorTensorFlow 数据集 API 解析错误
【发布时间】:2017-12-05 00:53:29
【问题描述】:

我正在使用 TensorFlow Dataset API 来解析 CSV 文件并运行逻辑回归。我正在按照 TF 文档 here 中的示例进行操作。

以下代码 sn-p 显示了我是如何设置模型的:

def input_fn(path, num_epochs, batch_size):
    dataset = tf.data.TextLineDataset(path)
    dataset = dataset.map(parse_table, num_parallel_calls=12)
    dataset = dataset.repeat(num_epochs)
    dataset.batch(batch_size)

    iterator = dataset.make_one_shot_iterator()
    features, labels = iterator.get_next()
    return features, labels

def parse_table(value):
    cols = tf.decode_csv(value, record_defaults=TAB_COLUMN_DEFAULTS)
    indep_vars = dict(zip(CSV_COLS, cols))
    y = indep_vars.pop('y')
    return indep_vars, y

def build_indep_vars():
    continuous_vars = [
        tf.feature_column.numeric_column(x, shape=1) for x in CONT_COLS]
    categorical_vars = [
        tf.feature_column.categorical_column_with_hash_bucket(
            x, hash_bucket_size=100) for x in CAT_COLS]
    return categorical_vars + continuous_vars

当调用lr.train(input_fn = lambda: input_fn(data_path, 1, 100))(注意:批量大小为 100)时,我收到了错误

ValueError: Feature (key: V1) cannot have rank 0. Give: Tensor("IteratorGetNext:0", shape=(), dtype=float32, device=/device:CPU:0)

所以我假设这意味着tf.feature_column.numeric_column 调用之一正在获取它不喜欢的标量值。但是,我无法弄清楚为什么会这样。我已将batch_size 设置为正整数,根据文档,tf.feature_column.numeric_column 产生的 NDarray 的形状默认应为1Xbatch_size。谁能解释一下为什么 TensorFlow 返回这个错误?

我相信这个问题有一个简单的答案,会让我因为没有弄清楚而感到愚蠢,但是在花了一些时间之后我仍然很难过。

【问题讨论】:

    标签: python tensorflow tensorflow-datasets


    【解决方案1】:

    引发错误是因为 tf.feature_column 方法期望输入被批处理,并且 我认为原因是一个简单的错字,即放弃了Dataset.batch() 转换。将dataset.batch(batch_size) 替换为以下行:

    dataset = dataset.batch(batch_size)
    

    调用任何tf.data.Dataset 转换方法(例如Dataset.map()Dataset.repeat()Dataset.batch())不会修改您调用这些方法的对象。相反,这些方法返回一个 Dataset 对象,您可以将其用于进一步的转换,或创建一个Iterator

    【讨论】:

    • 确实问题在于没有分配dataset.batch(batch_size) 的输出。谢谢!
    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 2019-05-07
    • 2019-02-08
    • 2018-08-22
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多