温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用numpy.random.shuffle函数怎么实现一个顺序打乱功能

发布时间:2021-03-17 14:49:22 来源:亿速云 阅读:220 作者:Leah 栏目:开发技术

这篇文章给大家介绍使用numpy.random.shuffle函数怎么实现一个顺序打乱功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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:

关于使用numpy.random.shuffle函数怎么实现一个顺序打乱功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI