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 29 Python
Python求两个list的差集、交集与并集的方法
Nov 01 Python
Python Web框架Flask信号机制(signals)介绍
Jan 01 Python
浅谈python多线程和队列管理shell程序
Aug 04 Python
Python使用Turtle模块绘制五星红旗代码示例
Dec 11 Python
pandas 实现将重复表格去重,并重新转换为表格的方法
Apr 18 Python
Python基于property实现类的特性操作示例
Jun 15 Python
python实现朴素贝叶斯算法
Nov 19 Python
40行Python代码实现天气预报和每日鸡汤推送功能
Feb 27 Python
selenium WebDriverWait类等待机制的实现
Mar 18 Python
python如何控制进程或者线程的个数
Oct 16 Python
python利用后缀表达式实现计算器功能
Feb 22 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
swoole和websocket简单聊天室开发
2017/11/18 PHP
Laravel框架实现抢红包功能示例
2019/10/31 PHP
thinkphp5 框架结合plupload实现图片批量上传功能示例
2020/04/04 PHP
Thinkphp5框架中引入Markdown编辑器操作示例
2020/06/03 PHP
XP折叠菜单&仿QQ2006菜单
2006/12/16 Javascript
FormValidate 表单验证功能代码更新并提供下载
2008/08/23 Javascript
Javascript实现颜色rgb与16进制转换的方法
2015/04/18 Javascript
谈谈JavaScript异步函数发展历程
2015/09/29 Javascript
JS解决iframe之间通信和自适应高度的问题
2016/08/24 Javascript
jQuery发请求传输中文参数乱码问题的解决方案
2018/05/22 jQuery
JavaScript中发出HTTP请求最常用的方法
2018/07/12 Javascript
JS实现求5的阶乘示例
2019/01/21 Javascript
js实现for循环跳过undefined值示例
2019/07/02 Javascript
layui的表单验证支持ajax判断用户名是否重复的实例
2019/09/06 Javascript
python实现多线程的方式及多条命令并发执行
2016/06/07 Python
python基本语法练习实例
2017/09/19 Python
python修改list中所有元素类型的三种方法
2018/04/09 Python
python实现图片文件批量重命名
2020/03/23 Python
Python3实现的反转单链表算法示例
2019/03/08 Python
python实现手机销售管理系统
2019/03/19 Python
libreoffice python 操作word及excel文档的方法
2019/07/04 Python
django中账号密码验证登陆功能的实现方法
2019/07/15 Python
Django CSRF跨站请求伪造防护过程解析
2019/07/31 Python
SELENIUM自动化模拟键盘快捷键操作实现解析
2019/10/28 Python
Python几种常见算法汇总
2020/06/02 Python
Python爬虫入门有哪些基础知识点
2020/06/02 Python
html table呈现个人简历以及单元格宽度失效的问题解决
2021/01/22 HTML / CSS
Bluebella美国官网:英国性感内衣品牌
2018/10/04 全球购物
Java方面的关于数组和继承的笔面试题
2015/09/18 面试题
旅游管理毕业生自荐书
2014/02/02 职场文书
规划编制实施方案
2014/03/15 职场文书
专业技术职务聘任书
2014/03/29 职场文书
党性锻炼的心得体会
2014/09/03 职场文书
党支部三会一课计划
2014/09/24 职场文书
2016医师资格考试考生诚信考试承诺书
2016/03/25 职场文书
Python 中面向接口编程
2022/05/20 Python