numpy.random.shuffle打乱顺序函数的实现


Posted in Python onSeptember 10, 2019

numpy.random.shuffle

在做将caffe模型和预训练的参数转化为tensorflow的模型和预训练的参数,以便微调,遇到如下函数:

def gen_data(source):
  while True:
    indices = range(len(source.images)) # indices = the number of images in the source data set
    random.shuffle(indices)
    for i in indices:
      image = np.reshape(source.images[i], (28, 28, 1))
      label = source.labels[i]
      yield image, label

之前卑鄙陋寡闻,不知道这个用法,按照字面上的意思是打乱,那么这里就应该是让训练数据集中的数据打乱顺序,然后一个挨着一个地(for i in indices)生成训练数据对。下面就从docs.scipy.org中查到的random.shuffle的用法:

numpy.random.shuffle(x)

Modify a sequence in-place by shuffling its contents.

Parameters: x : array_like The array or list to be shuffled.
Returns: None

举例

python>>>
>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]

This function only shuffles the array along the first index of a multi-dimensional array(多维矩阵中,只对第一维(行)做打乱顺序操作):

python>>>
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
    [6, 7, 8],
    [0, 1, 2]])This function only shuffles the array along the first index of a multi-dimensional array:

参考:

[1] https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.shuffle.html#numpy-random-shuffle

[2] https://github.com/ethereon/caffe-tensorflow/blob/master/examples/mnist/finetune_mnist.py

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python和ruby,我选谁?
Sep 13 Python
基于python(urlparse)模板的使用方法总结
Oct 13 Python
python cx_Oracle的基础使用方法(连接和增删改查)
Nov 19 Python
python消费kafka数据批量插入到es的方法
Dec 27 Python
pyspark操作MongoDB的方法步骤
Jan 04 Python
Django项目创建到启动详解(最全最详细)
Sep 07 Python
python list多级排序知识点总结
Oct 23 Python
Python 中如何实现参数化测试的方法示例
Dec 10 Python
python使用SQLAlchemy操作MySQL
Jan 02 Python
python turtle工具绘制四叶草的实例分享
Feb 14 Python
python三引号如何输入
Jul 06 Python
Python基础之Socket通信原理
Apr 22 Python
python+pygame实现坦克大战
Sep 10 #Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
Sep 10 #Python
Python将视频或者动态图gif逐帧保存为图片的方法
Sep 10 #Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 #Python
Python字符串中添加、插入特定字符的方法
Sep 10 #Python
详解python uiautomator2 watcher的使用方法
Sep 09 #Python
一行Python代码制作动态二维码的实现
Sep 09 #Python
You might like
用PHP生成html分页列表的代码
2007/03/18 PHP
ezSQL PHP数据库操作类库
2010/05/16 PHP
解析PHP提交后跳转
2013/06/23 PHP
Laravel框架数据库CURD操作、连贯操作总结
2014/09/03 PHP
PHP接收App端发送文件流的方法
2016/09/23 PHP
php中try catch捕获异常实例详解
2020/08/06 PHP
JAVASCRIPT  THIS详解 面向对象
2009/03/25 Javascript
JS 弹出层 定位至屏幕居中示例
2014/05/21 Javascript
基于JavaScript实现简单的随机抽奖小程序
2016/01/05 Javascript
js+canvas绘制五角星的方法
2016/01/28 Javascript
jQuery实现table中的tr上下移动并保持序号不变的实例代码
2016/07/11 Javascript
基于jQuery实现咖啡订单管理简单应用
2017/02/10 Javascript
javascript基础知识之html5轮播图实例讲解(44)
2017/02/17 Javascript
Ionic项目中Native Camera的使用方法
2017/06/07 Javascript
JS实现搜索关键词的智能提示功能
2017/07/07 Javascript
vue.js配合$.post从后台获取数据简单demo分享
2018/08/11 Javascript
学习React中ref的两个demo示例
2018/08/14 Javascript
微信小程序激励式视频广告组件使用详解
2019/12/06 Javascript
ant design vue中表格指定格式渲染方式
2020/10/28 Javascript
python re正则表达式模块(Regular Expression)
2014/07/16 Python
Django学习笔记之Class-Based-View
2017/02/15 Python
浅析python实现scrapy定时执行爬虫
2018/03/04 Python
对python GUI实现完美进度条的示例详解
2018/12/13 Python
python实现画五角星和螺旋线的示例
2019/01/20 Python
pyqt5 使用cv2 显示图片,摄像头的实例
2019/06/27 Python
python的time模块和datetime模块实例解析
2019/11/29 Python
使用遗传算法求二元函数的最小值
2020/02/11 Python
玛蒂尔达简服装:Matilda Jane Clothing
2019/02/13 全球购物
美国婴儿用品及配件购买网站:Munchkin
2019/04/03 全球购物
Yahoo-PHP面试题4
2012/05/05 面试题
如何清空Session
2015/02/23 面试题
化妆品促销方案
2014/02/24 职场文书
2014年党员公开承诺书范文
2014/03/28 职场文书
二年级小学生评语
2014/04/21 职场文书
关于军训的感想
2015/08/07 职场文书
Python爬虫基础之简单说一下scrapy的框架结构
2021/06/26 Python