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实现倒计时的示例
Feb 14 Python
python使用pil生成缩略图的方法
Mar 26 Python
python fabric使用笔记
May 09 Python
Python的Django框架中的表单处理示例
Jul 17 Python
python PIL模块与随机生成中文验证码
Feb 27 Python
Python中的字符串替换操作示例
Jun 27 Python
详解python实现读取邮件数据并下载附件的实例
Aug 03 Python
Python实现图片滑动式验证识别方法
Nov 09 Python
Python对象中__del__方法起作用的条件详解
Nov 01 Python
python中删除某个元素的方法解析
Nov 05 Python
python爬虫模块URL管理器模块用法解析
Feb 03 Python
Python爬虫爬取杭州24时温度并展示操作示例
Mar 27 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 中的一些经验积累
2006/10/09 PHP
php中处理mysql_fetch_assoc返回来的数组 不用foreach----echo
2011/05/04 PHP
解析php中memcache的应用
2013/06/18 PHP
Yii使用migrate命令执行sql语句的方法
2016/03/15 PHP
php判断是否连接上网络的方法实例详解
2016/12/14 PHP
JQuery 动态扩展对象之另类视角
2010/05/25 Javascript
jQuery UI 应用不同Theme的办法
2010/09/12 Javascript
jQuery插件的写法分享
2013/06/12 Javascript
js对文章内容进行分页示例代码
2014/03/05 Javascript
JavaScript学习心得之概述
2015/01/20 Javascript
JavaScript实现动态添加,删除行的方法实例详解
2015/07/02 Javascript
基于JavaScript实现移动端TAB触屏切换效果
2015/10/20 Javascript
JavaScript程序中的流程控制语句用法总结
2016/05/23 Javascript
jQuery动态修改字体大小的方法【测试可用】
2016/09/09 Javascript
JS将unicode码转中文方法
2017/05/08 Javascript
利用yarn代替npm管理前端项目模块依赖的方法详解
2017/09/04 Javascript
vue技术分享之你可能不知道的7个秘密
2018/04/09 Javascript
NodeJS安装图文教程
2018/04/19 NodeJs
js form表单input框限制20个字符,10个汉字代码实例
2019/04/12 Javascript
vue项目首屏加载时间优化实战
2019/04/23 Javascript
微信小程序实现卡片左右滑动效果的示例代码
2019/05/01 Javascript
微信小程序和H5页面间相互跳转代码实例
2019/09/19 Javascript
jQuery HTML css()方法与css类实例详解
2020/05/20 jQuery
聊聊vue 中的v-on参数问题
2021/01/29 Vue.js
Python入门_浅谈字符串的分片与索引、字符串的方法
2017/05/16 Python
python 接口_从协议到抽象基类详解
2017/08/24 Python
解决nohup重定向python输出到文件不成功的问题
2018/05/11 Python
python爬取足球直播吧五大联赛积分榜
2018/06/13 Python
flask session组件的使用示例
2018/12/25 Python
python实现大战外星人小游戏实例代码
2019/12/26 Python
Python自动发送和收取邮件的方法
2020/08/12 Python
纯净、自信、100%的羊绒服装:360Cashmere
2021/02/20 全球购物
2014年人力资源部工作总结
2014/11/19 职场文书
go语言中http超时引发的事故解决
2021/06/02 Golang
Docker下安装Oracle19c
2022/04/13 Servers
教你nginx跳转配置的四种方式
2022/07/07 Servers