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批量修改文件名的实现代码
Sep 01 Python
基python实现多线程网页爬虫
Sep 06 Python
Python(TensorFlow框架)实现手写数字识别系统的方法
May 29 Python
用Python实现数据的透视表的方法
Nov 16 Python
python 弹窗提示警告框MessageBox的实例
Jun 18 Python
Python3 JSON编码解码方法详解
Sep 06 Python
django实现类似触发器的功能
Nov 15 Python
python3光学字符识别模块tesserocr与pytesseract的使用详解
Feb 26 Python
Python中lru_cache的使用和实现详解
Jan 25 Python
pycharm 实现调试窗口恢复
Feb 05 Python
Python如何配置环境变量详解
May 18 Python
在Python中如何使用yield
Jun 07 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 批量替换程序的具体实现代码
2013/10/04 PHP
ThinkPHP实现生成和校验验证码功能
2017/04/28 PHP
phpcmsv9.0任意文件上传漏洞解析
2020/10/20 PHP
JavaScript动态调整TextArea高度的代码
2010/12/28 Javascript
jQuery代码优化之基本事件
2011/11/01 Javascript
比较新旧两个数组值得增加和删除的JS代码
2013/10/30 Javascript
jQuery中:last-child选择器用法实例
2014/12/31 Javascript
AngularJS入门教程之表格实例详解
2016/07/27 Javascript
谈谈JavaScript中的几种借用方法
2016/08/09 Javascript
JS二叉树的简单实现方法示例
2017/04/05 Javascript
jQuery制作input提示内容(兼容IE8以上)
2017/07/05 jQuery
微信小程序实现顶部选项卡(swiper)
2020/06/19 Javascript
详细介绍RxJS在Angular中的应用
2017/09/23 Javascript
[01:17]Ti4 循环赛第一日回顾
2014/07/11 DOTA
Python 专题六 局部变量、全局变量global、导入模块变量
2017/03/20 Python
python清理子进程机制剖析
2017/11/23 Python
Python实现文件信息进行合并实例代码
2018/01/17 Python
Python 函数基础知识汇总
2018/03/09 Python
numpy中实现ndarray数组返回符合特定条件的索引方法
2018/04/17 Python
Python实现按中文排序的方法示例
2018/04/25 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
python 计算方位角实例(根据两点的坐标计算)
2020/01/17 Python
手对手的教你用canvas画一个简单的海报的方法示例
2018/12/10 HTML / CSS
详解window.open被浏览器拦截的解决方案
2019/07/18 HTML / CSS
英国邮购活的植物主要供应商:Gardening Direct
2019/01/28 全球购物
全球领先的全景影像品牌:Insta360
2019/08/21 全球购物
哈曼俄罗斯官方网上商店:Harman.club
2020/07/24 全球购物
大学旷课检讨书
2014/01/28 职场文书
公司合作协议书范本
2014/04/18 职场文书
作文批改评语大全
2014/04/23 职场文书
体育活动总结范文
2014/05/04 职场文书
生态养殖创业计划书
2014/05/06 职场文书
计算机应用专业毕业生求职信
2014/06/03 职场文书
文员试用期转正自我鉴定
2014/09/14 职场文书
幼儿园教师安全责任书
2015/05/08 职场文书
高考要来啦!用Python爬取历年高考数据并分析
2021/06/03 Python