百度飞浆batch分析

简介: 百度飞浆batch分析
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__all__ = ['batch']
def batch(reader, batch_size, drop_last=False):
    """
    This operator creates a batched reader which combines the data from the 
    input reader to batched data.
    Args:
        reader(generator): the data reader to read from.
        batch_size(int): size of each mini-batch.
        drop_last(bool, optional): If set to True, the last batch is dropped when 
            the size of last batch is not equal to batch_size, if set to False,
            it will not. Default: False.
    Returns:
        The batched reader. 
    Return Type:
        generator   
    Examples:
        .. code-block:: python
            import paddle.fluid as fluid
            def reader():
                for i in range(10):
                    yield i
            batch_reader = fluid.io.batch(reader, batch_size=2)
            for data in batch_reader():
                print(data)
            # Output is
            # [0, 1]
            # [2, 3]
            # [4, 5]
            # [6, 7]
            # [8, 9]
    """
    def batch_reader():
        r = reader()
        b = []
        for instance in r:
            b.append(instance)
            if len(b) == batch_size:
                yield b
                b = []
        if drop_last == False and len(b) != 0:
            yield b
    # Batch size check
    batch_size = int(batch_size)
    if batch_size <= 0:
        raise ValueError("batch_size should be a positive integeral value, "
                         "but got batch_size={}".format(batch_size))
    return batch_reader

这就是一个将数据分批读取的小东西。


读取顺序如下:

import paddle.fluid as fluid
def reader():
    for i in range(100):
        print(i)
        yield i
batch_reader = fluid.io.batch(reader, batch_size=10)
for data in batch_reader():
    print(data)
输出:
0
1
2
3
4
5
6
7
8
9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
10
11
12
13
14
15
16
17
18
19
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
...............


目录
相关文章
|
自然语言处理 BI 数据处理
【数据对比】综合分析百度情感分析以及华为情感分析的差异,我有了如下结果
【数据对比】综合分析百度情感分析以及华为情感分析的差异,我有了如下结果
379 0
|
监控 JavaScript 前端开发
百度统计分析埋点最佳实战篇
百度统计分析埋点最佳实战篇
1937 0
百度统计分析埋点最佳实战篇
|
6月前
|
数据可视化 Python
R语言量化技术分析的百度指数关注度交易策略可视化
R语言量化技术分析的百度指数关注度交易策略可视化
|
JSON 数据可视化 前端开发
百度Echarts消防训练成绩大数据可视化综合分析系统开发实录(1)饼图表格互动篇
百度Echarts消防训练成绩大数据可视化综合分析系统开发实录(1)饼图表格互动篇
93 0
百度Echarts消防训练成绩大数据可视化综合分析系统开发实录(1)饼图表格互动篇
|
数据可视化 搜索推荐 JavaScript
数据可视化大屏Echarts高级开发散点图实战案例分析(地图扩展插件bmap.min.js、散点图、百度地图控件、柱图、涟漪动图、条件判断颜色)
数据可视化大屏Echarts高级开发散点图实战案例分析(地图扩展插件bmap.min.js、散点图、百度地图控件、柱图、涟漪动图、条件判断颜色)
588 0
|
定位技术 API 容器
百度地图覆盖物加载svg图片的实战案例分析
百度地图覆盖物加载svg图片的实战案例分析
572 0
|
数据可视化 大数据 关系型数据库
百度Echarts消防训练成绩大数据可视化综合分析系统开发实录(2)雷达图表格梯次排列互动篇
百度Echarts消防训练成绩大数据可视化综合分析系统开发实录(2)雷达图表格梯次排列互动篇
88 0
|
数据采集
百度蜘蛛ip段大全分析爬虫式
百度蜘蛛ip段大全分析爬虫式:https://www.20200824.com
430 0
|
数据采集
百度图片参数分析
爬虫这几年应用场景越来越广泛,数据越来越重要。今天vike和你们一起来看看百度图片参数。本文仅供技术交流,如有侵权请联系作者删除
397 0
百度图片参数分析