numpy.random.shuffle打乱顺序函数的实现

作者:jasonzzj 时间:2021-02-04 07:19:34 

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

来源:https://blog.csdn.net/jasonzzj/article/details/53932645

标签:numpy.random.shuffle,打乱
0
投稿

猜你喜欢

  • 编写兼容IE和FireFox的脚本

    2009-05-19 12:01:00
  • ASP使用MYSQL数据库全攻略

    2009-11-08 18:27:00
  • Python如何快速上手? 快速掌握一门新语言的方法

    2023-05-07 12:00:48
  • matplotlib图例legend语法及设置的方法

    2023-01-07 04:23:37
  • numpy中的log和ln函数解读

    2023-06-14 22:46:40
  • 用header 发送cookie的php代码

    2023-07-11 11:15:06
  • 原来CSS也可以把IE6弄死

    2007-08-14 09:30:00
  • 十“问”DreamWeaver

    2007-02-03 11:39:00
  • 用doctype激活浏览器模式

    2009-06-15 19:02:00
  • 基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解

    2023-04-09 17:28:45
  • python中字符串最常用的十三个处理操作记录

    2023-10-19 23:25:32
  • Swoole webSocket消息服务系统方案设计详解

    2023-06-12 16:16:32
  • python Django连接MySQL数据库做增删改查

    2023-11-14 10:44:35
  • 设计者在网页排版中应注意的一些问题

    2012-04-20 13:13:58
  • python实现代理服务功能实例

    2023-10-04 05:36:57
  • sqlalchemy实现时间列自动更新教程

    2021-08-18 20:12:58
  • Python调用系统命令os.system()和os.popen()的实现

    2021-06-27 23:56:29
  • django静态文件加载的方法

    2022-12-26 13:57:56
  • PHP PDOStatement::fetchObject讲解

    2023-06-09 19:13:37
  • asp如何用FSO对象显示一个文本文件?

    2010-06-09 18:41:00
  • asp之家 网络编程 m.aspxhome.com