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类型强制转换long to int的代码
Feb 10 Python
简单实现python画圆功能
Jan 25 Python
Python可变参数*args和**kwargs用法实例小结
Apr 27 Python
Django 使用Ajax进行前后台交互的示例讲解
May 28 Python
解析Python的缩进规则的使用
Jan 16 Python
详解Python爬取并下载《电影天堂》3千多部电影
Apr 26 Python
python requests使用socks5的例子
Jul 25 Python
Python使用Pandas读写Excel实例解析
Nov 19 Python
python pyenv多版本管理工具的使用
Dec 23 Python
python实现打砖块游戏
Feb 25 Python
python seaborn heatmap可视化相关性矩阵实例
Jun 03 Python
python Xpath语法的使用
Nov 26 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中使用redis队列操作实例代码
2013/02/07 PHP
ThinkPHP表单自动提交验证实例教程
2014/07/18 PHP
完善CodeIgniter在IDE中代码提示功能的方法
2014/07/19 PHP
ThinkPHP5.0框架控制器继承基类和自定义类示例
2018/05/25 PHP
php和html的区别点详细总结
2019/09/24 PHP
js类中获取外部函数名的方法
2007/08/19 Javascript
javascript 密码框防止用户粘贴和复制的实现代码
2014/02/17 Javascript
javascript实现des解密加密全过程
2014/04/03 Javascript
使用upstart把nodejs应用封装为系统服务实例
2014/06/01 NodeJs
js+jquery实现图片裁剪功能
2015/01/02 Javascript
js跨域请求的5中解决方式
2015/07/02 Javascript
基于JavaScript代码实现兼容各浏览器的设为首页和加入收藏
2016/01/07 Javascript
jQuery实现的鼠标滑过弹出放大图片特效
2016/01/08 Javascript
AngualrJS中每次$http请求时的一个遮罩层Directive
2016/01/26 Javascript
js如何准确获取当前页面url网址信息
2020/09/13 Javascript
解决JS外部文件中文注释出现乱码问题
2017/07/09 Javascript
Vue-cli-webpack搭建斗鱼直播步骤详解
2017/11/17 Javascript
浅谈Vue.js路由管理器 Vue Router
2018/08/16 Javascript
AngularJS 多指令Scope问题的解决
2018/10/25 Javascript
小程序数据通信方法大全(推荐)
2019/04/15 Javascript
Vue3.0数据响应式原理详解
2019/10/09 Javascript
vue 数据遍历筛选 过滤 排序的应用操作
2020/11/17 Javascript
[39:19]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第二场 11.26
2020/11/30 DOTA
python字典get()方法用法分析
2015/04/17 Python
Python3使用requests登录人人影视网站的方法
2016/05/11 Python
完美解决python中ndarray 默认用科学计数法显示的问题
2018/07/14 Python
Python3 批量扫描端口的例子
2019/07/25 Python
python打印直角三角形与等腰三角形实例代码
2019/10/20 Python
pytorch实现onehot编码转为普通label标签
2020/01/02 Python
python pandas.DataFrame.loc函数使用详解
2020/03/26 Python
python接入支付宝的实例操作
2020/07/20 Python
Bloomingdale’s阿联酋:选购奢华时尚、美容及更多
2020/09/22 全球购物
给领导的检讨书
2014/02/16 职场文书
学校对教师的评语
2014/04/28 职场文书
python 利用PyAutoGUI快速构建自动化操作脚本
2021/05/31 Python
vue.js Router中嵌套路由的实用示例
2021/06/27 Vue.js