ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-25 19:58:52  阅读:248  来源: 互联网

标签:tensorflow google-bigquery 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)

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

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有