numpy.random.shuffle打乱顺序函数

来源:互联网 发布:r语言编程艺术 pdf 编辑:程序博客网 时间:2024/04/26 00:09

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)>>> arrarray([[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

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怀孕肾结石疼怎么办啊 孕晚期肾绞痛怎么办 肾绞疼引起的呕吐怎么办 肾绞痛肚子胀气怎么办 iga肾炎肉眼血尿怎么办 结石疼怎么办怎么缓解 肾有问题严重怎么办 肾结石突然很疼怎么办 输尿管结石肉眼血尿该怎么办 结石引起肾绞痛怎么办 尿结石支架后尿里老有血怎么办? 结石堵在输尿管怎么办 尿路结石痛怎么办 怀孕了有肾结石怎么办 怀孕有肾结石怎么办啊 肾结石无疼血尿怎么办 胆囊胆管都结石怎么办 肾里面有肿瘤怎么办 肾癌手术后发烧怎么办 尿结石堵住尿道怎么办 尿结石不能排尿怎么办 肾癌小便有血怎么办 膀胱癌膀胱全切怎么办 怀孕了有阑尾炎怎么办 食物堵塞在食管怎么办 食物卡在食管怎么办 小孩食道卡异物怎么办 八十岁老人得了膀胱癌怎么办 肾结石引起吐血尿血怎么办 肾结石引起的尿血怎么办 食道感觉有异物怎么办 膀胱出血有血块怎么办 肾小球滤过率20怎么办 膀胱癌术后有血尿怎么办 肾病贫血怎么办吃什么 低蛋白血症怎么办 慢性肾炎患者感冒了怎么办 透析病人磷高怎么办 尿毒症透析磷高怎么办 怀孕了有膀胱炎怎么办 宝宝拉肚子尿少怎么办