【问题标题】:reading data with BigQueryReader in Tensorflow在 Tensorflow 中使用 BigQueryReader 读取数据
【发布时间】:2017-08-21 11:18:35
【问题描述】:

我尝试使用来自 Tensorflow 的 BigQueryReader,但我没有成功读取数据。这是我的代码:

import tensorflow as tf
from tensorflow.contrib.cloud.python.ops.bigquery_reader_ops import BigQueryReader
import time

features = dict(
    weight_pounds=tf.FixedLenFeature([1], tf.float32),
    mother_age=tf.FixedLenFeature([1], tf.float32),
    father_age=tf.FixedLenFeature([1], tf.float32),
    gestation_weeks=tf.FixedLenFeature([1], tf.float32))

millis = int(round(time.time() * 1000))

reader = BigQueryReader(project_id="bigquery-public-data",
    dataset_id="samples",
    table_id="natality",
    timestamp_millis=millis,
    num_partitions=10,
    features=features)

queue = tf.train.string_input_producer(reader.partitions())
row_id, examples_serialized = reader.read(queue)
examples = tf.parse_example(examples_serialized, features=features)

执行此代码示例时,我得到:

File "/home/juta/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 659, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample_3/ParseExample' (op: 'ParseExample') with input shapes: [], [0], [], [], [], [], [0], [0], [0], [0].

解析可能失败,因为 reader.read(queue) 似乎返回空对象:

ReaderRead(key=<tf.Tensor 'ReaderRead:0' shape=() dtype=string>, value=<tf.Tensor 'ReaderRead:1' shape=() dtype=string>)

为什么阅读器没有返回任何数据?

【问题讨论】:

    标签: python tensorflow google-bigquery


    【解决方案1】:

    阅读器没有返回 empty 对象:它返回的是 标量(即等级为 0 的张量,或“空”形状)。有关详细信息,请参阅 TensorFlow programmers guide on tensor shapes。

    形状错误“Shape must be rank 1 but is rank 0”表示tf.parse_example() op 需要一个向量(1 级张量)作为输入,而不是一个标量。至少有两种可能的解决方案:

    1. 改用 tf.parse_single_example() 操作,它需要一个标量输入。
    2. 将reader.read() 返回的值重新整形为向量,例如使用tf.expand_dims(examples_serialized, 0)。

    【讨论】:

    • 谢谢,我现在可以使用张量通过 example = tf.parse_single_example(examples_serialized, features=features) 访问 bigquery 中的值。但是我仍然没有得到实际值。运行会话时它卡住了,我没有得到返回值。知道为什么 sess.run(example) 没有返回任何东西吗?
    • 你打电话给tf.train.start_queue_runners(sess)了吗?
    【解决方案2】:

    我也遇到了同样的问题,尝试了三种方法。以下解决方案都应该运行:

    examples = tf.parse_example(tf.expand_dims(examples_serialized, 0), features=features)
    ## or
    examples = tf.parse_example([examples_serialized], features=features)
    ## or
    examples = tf.parse_single_example(examples_serialized, features=features)
    

    但是,即使此代码运行,以下代码也会挂起,不会产生任何输出:

    weight_pounds = examples['weight_pounds']    
    with tf.Session() as session:
        print(session.run(weight_pounds))
    

    推测一下,也许这个问题源于tf.train.string_input_producer 现在被tf.data 弃用,所以也许不再支持这种用法。另一方面,我找不到任何关于如何将 tf.data 与 BigQuery 一起使用的文档。我尝试过这样的事情:

    dataset = tf.data.Dataset.from_tensor_slices(reader.read(queue))
    

    但这会返回一个IndexError: list index out of range 错误

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2017-10-27
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      相关资源
      最近更新 更多