编程语言
首页 > 编程语言> > python-在Tensorflow中使用BigQueryReader读取数据

python-在Tensorflow中使用BigQueryReader读取数据

作者:互联网

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

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

解决方法:

读取器未返回空对象:它正在返回标量(即,等级为0或“空”形状的张量).有关更多详细信息,请参见TensorFlow programmers guide on tensor shapes.

形状错误“形状必须为等级1但等级为0”表示tf.parse_example() op希望输入的是向量(等级1张量),而不是标量.至少有两种可能的解决方案:

>请改用需要标量输入的tf.parse_single_example()操作.
>将reader.read()返回的值整形为矢量,例如使用tf.expand_dims(examples_serialized, 0).

标签:tensorflow,google-bigquery,python
来源: https://codeday.me/bug/20191025/1931068.html