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中的readline()方法的使用
May 24 Python
Python基础教程之tcp socket编程详解及简单实例
Feb 23 Python
Python实现类的创建与使用方法示例
Jul 25 Python
python处理DICOM并计算三维模型体积
Feb 26 Python
Python任意字符串转16, 32, 64进制的方法
Jun 12 Python
pytz格式化北京时间多出6分钟问题的解决方法
Jun 21 Python
python字典的遍历3种方法详解
Aug 10 Python
Python实现遗传算法(二进制编码)求函数最优值方式
Feb 11 Python
浅谈python print(xx, flush = True) 全网最清晰的解释
Feb 21 Python
Python selenium页面加载慢超时的解决方案
Mar 18 Python
Python unittest装饰器实现原理及代码
Sep 08 Python
Python 高效编程技巧分享
Sep 10 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面向对象全攻略 (六)__set() __get() __isset() __unset()的用法
2009/09/30 PHP
PHP+XML 制作简单的留言本 图文教程
2009/11/02 PHP
laravel实现批量更新多条记录的方法示例
2017/10/22 PHP
php利用云片网实现短信验证码功能的示例代码
2017/11/18 PHP
thinkPHP框架实现多表查询的方法
2018/06/14 PHP
ASP.NET jQuery 实例6 (实现CheckBoxList成员全选或全取消)
2012/01/13 Javascript
框架页面高度自动刷新的Javascript脚本
2013/11/01 Javascript
自定义Angular指令与jQuery实现的Bootstrap风格数据双向绑定的单选与多选下拉框
2015/12/12 Javascript
javascript合并表格单元格实例代码
2016/01/03 Javascript
JavaScript制作简单的日历效果
2016/03/10 Javascript
JS中如何比较两个Json对象是否相等实例代码
2016/07/13 Javascript
详解Bootstrap的iCheck插件checkbox和radio
2016/08/24 Javascript
输入框点击时边框变色效果的实现方法
2016/12/26 Javascript
详解Vue中添加过渡效果
2017/03/20 Javascript
Vue.js组件高级特性实例详解
2018/12/24 Javascript
学习RxJS之JavaScript框架Cycle.js
2019/06/17 Javascript
微信小程序弹窗禁止页面滚动的实现代码
2020/12/30 Javascript
[02:07]2018DOTA2亚洲邀请赛主赛事第三日五佳镜头 fy极限反杀
2018/04/06 DOTA
[01:30:55]VG vs Mineski Supermajor 败者组 BO3 第三场 6.6
2018/06/07 DOTA
Python中的集合类型知识讲解
2015/08/19 Python
django定期执行任务(实例讲解)
2017/11/03 Python
Python实现爬虫爬取NBA数据功能示例
2018/05/28 Python
Python DataFrame 设置输出不显示index(索引)值的方法
2018/06/07 Python
selenium处理元素定位点击无效问题
2019/06/12 Python
python中pygame安装过程(超级详细)
2019/08/04 Python
Django获取应用下的所有models的例子
2019/08/30 Python
python如何求数组连续最大和的示例代码
2020/02/04 Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
2020/11/28 Python
瑞典时尚耳机品牌:Urbanears
2017/07/26 全球购物
LACOSTE波兰官网:Polo衫、服装和鞋类
2020/09/29 全球购物
酷瑞网络科技面试题
2012/03/30 面试题
《和我们一样享受春天》教学反思
2014/02/07 职场文书
文秘应届生求职信
2014/07/05 职场文书
2019年教师节:送给所有老师的祝福语
2019/09/05 职场文书
MySQL 存储过程的优缺点分析
2021/05/20 MySQL