在看莫烦python的RL源码时,他的DDPG记忆库Memory的实现是这样写的:

class Memory(object):
def __init__(self, capacity, dims):
self.capacity = capacity
self.data = np.zeros((capacity, dims))
self.pointer = 0 def store_transition(self, s, a, r, s_):
transition = np.hstack((s, a, [r], s_))
index = self.pointer % self.capacity # replace the old memory with new memory
self.data[index, :] = transition
self.pointer += 1 def sample(self, n):
assert self.pointer >= self.capacity, 'Memory has not been fulfilled'
indices = np.random.choice(self.capacity, size=n)
return self.data[indices, :]

其中sample方法用assert断言pointer >= capacity,也就是说Memory必须满了才能学习。

我在设计一种方案,一开始往记忆库里存比较好的transition(也就是reward比较高的),要是等记忆库填满再学习好像有点浪费,因为会在填满之后很快被差的transition所替代,甚至好的transition不能填满Memory,从而不能有效学习好的经验。

此时就需要关注np.random.choice方法了,看源码解释:

def choice(a, size=None, replace=True, p=None): # real signature unknown; restored from __doc__
"""
choice(a, size=None, replace=True, p=None) Generates a random sample from a given 1-D array .. versionadded:: 1.7.0 Parameters
-----------
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements.
If an int, the random sample is generated as if a were np.arange(a)
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
replace : boolean, optional
Whether the sample is with or without replacement
p : 1-D array-like, optional
The probabilities associated with each entry in a.
If not given the sample assumes a uniform distribution over all
entries in a. Returns
--------
samples : single item or ndarray
The generated random samples

主要第一个参数为ndarray,如果给的是int,np会自动将其通过np.arange(a)转换为ndarray。

此处主要关注的是,a(我们使用int)< size时,np会怎么取?

上代码测试

import numpy as np

samples = np.random.choice(3, 5)
print(samples)

输出:

[2 1 2 1 1]

所以,是会从np.array(a)重复取,可以推断出,np.random.choice是“有放回地取”(具体我也没看源码,从重复情况来看,至少a<size时是这样的)

然后我分别测试了np.random.choice(5, 5)、np.random.choice(10, 5)等。多试几次会发现samples中确实是会有重复的。:

import numpy as np

samples = np.random.choice(10, 5)
print(samples) [3 4 3 4 5]

np.random.choices的使用的更多相关文章

  1. 怎么理解np.random.seed()?

    在使用numpy时,难免会用到随机数生成器.我一直对np.random.seed(),随机数种子搞不懂.很多博客也就粗略的说,利用随机数种子,每次生成的随机数相同. 我有两个疑惑:1, 利用随机数种子 ...

  2. 对抗生成网络-图像卷积-mnist数据生成(代码) 1.tf.layers.conv2d(卷积操作) 2.tf.layers.conv2d_transpose(反卷积操作) 3.tf.layers.batch_normalize(归一化操作) 4.tf.maximum(用于lrelu) 5.tf.train_variable(训练中所有参数) 6.np.random.uniform(生成正态数据

    1. tf.layers.conv2d(input, filter, kernel_size, stride, padding) # 进行卷积操作 参数说明:input输入数据, filter特征图的 ...

  3. NP:建立可视化输入的二次函数数据点集np.linspace+np.random.shuffle+np.random.normal

    import numpy as np import matplotlib.pyplot as plt def fix_seed(seed=1): #重复观看一样东西 # reproducible np ...

  4. np.random.rand均匀分布随机数和np.random.randn正态分布随机数函数使用方法

    np.random.rand用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me 生成特定形状下[0,1)下的均匀分布随机数 np.random.rand(a1,a2,a3...)生成形状为( ...

  5. np.random.choice方法

    np.random.choice方法 觉得有用的话,欢迎一起讨论相互学习~Follow Me def choice(a, size=None, replace=True, p=None) 表示从a中随 ...

  6. numpy中的np.random.mtrand.RandomState

    1 RandomState 的应用场景概述 在训练神经网络时,苦于没有数据,此时numpy为我们提供了 “生产” 数据集的一种方式. 例如在搭建神经网络(一)中的 4.3 准备数据集 章节中就是采用n ...

  7. np.random.normal()正态分布

    高斯分布的概率密度函数 numpy中 numpy.random.normal(loc=0.0, scale=1.0, size=None) 参数的意义为: loc:float 概率分布的均值,对应着整 ...

  8. np.random.randn()、np.random.rand()、np.random.randint()

    (1)np.random.randn()函数 语法: np.random.randn(d0,d1,d2……dn) 1)当函数括号内没有参数时,则返回一个浮点数: 2)当函数括号内有一个参数时,则返回秩 ...

  9. np.random.random()系列函数

    1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...

随机推荐

  1. class类 - static

    不需要实例化类,即可直接通过该类来调用的方法,即称之为"静态方法".将类中的方法设为静态方法也很简单,在方法前加上static关键字即可.这样该方法就不会被实例继承! class ...

  2. reduce方法的使用

    reduce(收敛):接收一个回调函数作为累加器,数组中的每个值(从左到右)开始缩减,最终为一个值,是ES5中新增的又一个数组逐项处理方法. reduce(callback,initialValue) ...

  3. vue-cli搭建vue项目环境

    该篇文章是继https://www.cnblogs.com/qing-5/p/11321585.html来写 1.打开终端,输入指令"npm install --global vue-cli ...

  4. Go 信道Channel

    信道(Channel) 信道(Channel)可以被认为是协程之间通信的管道.数据可以从信道的一端发送并在另一端接收. 默认为同步模式,需要发送和接收配对.否则会被阻塞,直到另外的信道准备好后被唤醒. ...

  5. 在IOS中根据圆心坐标、半径和角度计算圆弧上的点坐标

    /** 日期:2015-10-15 版本: 1.0.0 -------------------------------------------------------------- 功能说明 ---- ...

  6. moment——日期格式化常用示例

    <template> <div id="app" style="text-align: center;"> <h1>{{ m ...

  7. 2019 GIAC-全球互联网架构大会课件

    百度云:https://pan.baidu.com/s/1Lt40UTP3hCIVS7LhPl2bKw密码:ovrh

  8. MVC方式使用EasyUI - 搭建环境

    _Layout.cshtml <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Typ ...

  9. 阿里P7浅谈SpringMVC

    一.前言 既然是浅谈 SpringMVC,那么我们就先从基础说起,本章节主要讲解以下内容: 1.三层结构介绍 2.MVC 设计模式介绍 3.SpringMVC 介绍 4.入门程序的实现 注:介绍方面的 ...

  10. Python学习记录8-继承2

    继承 单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类允许继承多个类 >>> class A(): pass >>> class B(A): pass ...