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 相关文章推荐
Pandas标记删除重复记录的方法
Apr 08 Python
python3 pandas 读取MySQL数据和插入的实例
Apr 20 Python
Python实现的个人所得税计算器示例
Jun 01 Python
python3爬虫之设计签名小程序
Jun 19 Python
Python获取网段内ping通IP的方法
Jan 31 Python
Python常见数字运算操作实例小结
Mar 22 Python
python爬虫 正则表达式解析
Sep 28 Python
python 类之间的参数传递方式
Dec 20 Python
在Python 的线程中运行协程的方法
Feb 24 Python
python+selenium 脚本实现每天自动登记的思路详解
Mar 11 Python
关于tf.matmul() 和tf.multiply() 的区别说明
Jun 18 Python
python中pandas.read_csv()函数的深入讲解
Mar 29 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 数组元素快速去重
2017/05/05 PHP
php文件操作之文件写入字符串、数组的方法分析
2019/04/15 PHP
Laravel重定向,a链接跳转,控制器跳转示例
2019/10/22 PHP
游戏人文件夹程序 ver 3.0
2006/07/14 Javascript
js 自定义的联动下拉框
2010/02/07 Javascript
Javascript调用C#代码
2011/01/17 Javascript
基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码(带平滑移动的效果)
2011/05/24 Javascript
深入理解JavaScript系列(13) This? Yes,this!
2012/01/18 Javascript
图片Slider 带左右按钮的js示例
2013/08/30 Javascript
JavaScript中Date对象的常用方法示例
2015/10/24 Javascript
详解JS正则replace的使用方法
2016/03/06 Javascript
jQuery实现百叶窗焦点图动画效果代码分享(附源码下载)
2016/03/14 Javascript
详解自动生成博客目录案例
2016/12/09 Javascript
Angularjs自定义指令Directive详解
2017/05/27 Javascript
vue的常用组件操作方法应用分析
2018/04/13 Javascript
vue定义全局变量和全局方法的方法示例
2018/08/01 Javascript
详解html-webpack-plugin插件(用法总结)
2018/09/12 Javascript
如何从0开始用node写一个自己的命令行程序
2018/12/29 Javascript
Postman环境变量全局变量使用方法详解
2020/08/13 Javascript
vue 数据遍历筛选 过滤 排序的应用操作
2020/11/17 Javascript
[02:40]DOTA2超级联赛专访430 从小就爱玩对抗性游戏
2013/06/18 DOTA
[43:41]OG vs Newbee 2019国际邀请赛淘汰赛 胜者组 BO3 第一场 8.21.mp4
2020/07/19 DOTA
python追加元素到列表的方法
2015/07/28 Python
Python从函数参数类型引出元组实例分析
2019/05/28 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
基于Python和PyYAML读取yaml配置文件数据
2020/01/13 Python
python GUI框架pyqt5 对图片进行流式布局的方法(瀑布流flowlayout)
2020/03/12 Python
Python基于requests实现模拟上传文件
2020/04/21 Python
python如何查看网页代码
2020/06/07 Python
应届生妇产科护士求职信
2013/10/27 职场文书
大学生应聘推荐信范文
2013/11/19 职场文书
主题婚礼策划方案
2014/02/10 职场文书
广告宣传策划方案
2014/05/21 职场文书
2015年党风廉政承诺书
2015/01/22 职场文书
MySQL如何解决幻读问题
2021/08/07 MySQL
mysql函数之截取字符串的实现
2022/08/14 MySQL