使用BigQueryReader以十为单位读取数据

2024-09-28 17:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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)

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

^{pr2}$

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

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

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


Tags: 代码importidstringtimequeuetfexamples
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)

但是,即使此代码运行,以下代码仍将挂起而不生成任何输出:

^{pr2}$

推测一下,这个问题可能源于tf.train.string_input_producer现在被弃用,取而代之的是tf.data,因此可能不再支持这种用法。另一方面,我找不到任何关于如何在BigQuery中使用tf.data的文档。我试过这样的方法:

^{3}$

但这将返回IndexError: list index out of range错误

读取器不返回对象:它返回标量(即秩为0的张量或“空”形状)。有关详细信息,请参见TensorFlowprogrammers guide on tensor shapes。在

{or-rank>表示输入的形状必须小于1^而不是标量。至少有两种可能的解决方案:

  1. 使用^{}op,它需要标量输入。在
  2. reader.read()返回的值重塑为向量,例如使用^{}。在

相关问题 更多 >