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编写爬虫的基本模块及框架使用指南
Jan 20 Python
Python3.6 Schedule模块定时任务(实例讲解)
Nov 09 Python
python中文乱码不着急,先看懂字节和字符
Dec 20 Python
使用Python搭建虚拟环境的配置方法
Feb 28 Python
TensorFlow变量管理详解
Mar 10 Python
python 读取目录下csv文件并绘制曲线v111的方法
Jul 06 Python
pytorch中tensor的合并与截取方法
Jul 26 Python
python re正则匹配网页中图片url地址的方法
Dec 20 Python
django自带调试服务器的使用详解
Aug 29 Python
django中related_name的用法说明
May 20 Python
django rest framework使用django-filter用法
Jul 15 Python
你喜欢篮球吗?Python实现篮球游戏
Jun 11 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
《超神学院》霸气归来, 天使彦上演维多利亚的秘密
2020/03/02 国漫
从C/C++迁移到PHP——判断字符类型的函数
2006/10/09 PHP
浅谈thinkphp的实例化模型
2015/01/04 PHP
php一个文件搞定微信jssdk配置
2016/12/12 PHP
thinkphp5实现微信扫码支付
2019/12/23 PHP
In Javascript Class, how to call the prototype method.(three method)
2007/01/09 Javascript
javascript笔记 String类replace函数的一些事
2011/09/22 Javascript
JS操作Cookie写入和读取实例代码
2013/10/20 Javascript
键盘KeyCode值列表汇总
2013/11/26 Javascript
百度UEditor编辑器如何关闭抓取远程图片功能
2015/03/03 Javascript
JavaScript模版引擎的基本实现方法浅析
2016/02/15 Javascript
jquery二级目录选中当前页的css样式
2016/12/08 Javascript
jquery插件锦集【推荐】
2016/12/16 Javascript
react-native-fs实现文件下载、文本存储的示例代码
2017/09/22 Javascript
react-navigation 如何判断用户是否登录跳转到登录页的方法
2017/12/01 Javascript
解决vue中post方式提交数据后台无法接收的问题
2018/08/11 Javascript
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
使用VUE实现在table中文字信息超过5个隐藏鼠标移到时弹窗显示全部
2019/09/16 Javascript
微信小程序分享小程序码的生成(带参数)以及参数的获取
2020/03/25 Javascript
Python 匹配任意字符(包括换行符)的正则表达式写法
2009/10/29 Python
python 划分数据集为训练集和测试集的方法
2018/12/11 Python
Jupyter notebook在mac:linux上的配置和远程访问的方法
2019/01/14 Python
django模型类中,null=True,blank=True用法说明
2020/07/09 Python
基于Django集成CAS实现流程详解
2020/11/28 Python
flask框架中的cookie和session使用
2021/01/31 Python
品质主管的岗位职责
2013/12/04 职场文书
《四季》教学反思
2014/04/08 职场文书
婚前协议书
2014/04/15 职场文书
房地产资料员岗位职责
2014/07/02 职场文书
2015大学生党员自我评价范文
2015/03/03 职场文书
领导干部失职检讨书
2015/05/05 职场文书
我的中国梦主题班会
2015/08/14 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书
JS监听Esc 键触发事键
2021/04/14 Javascript
Java实现二分搜索树的示例代码
2022/03/17 Java/Android
德劲DE1102数字调谐收音机机评
2022/04/07 无线电