在看莫烦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. Unity使用Resources读取Resources路径下的二进制文件(Binary Data)必须使用 .bytes扩展名

    将某二进制文件放在Resources目录下,希望用Resources.Load<TextAsset>的方式读取,发现TextAsset是null 查阅Unity文档得知,使用Resourc ...

  2. vue html属性绑定

    业务场景: element ui里面select的组件,通过勾选是否启动搜索功能 解决: 在select组件上绑定一个 :filterable,值就是是否可搜索的checkbox的值  true和fa ...

  3. OpenCV手工实现灰度及RGB直方图

    手工实现灰度及RGB直方图 !库 1. 灰度图像直方图 算法 1. 图片灰度化: 2. 遍历Mat,统计各灰度级的像素个数: 3. 根据opencv画点线函数,绘制坐标轴及像素分布图 源码(编译环境: ...

  4. 如何在Web应用里消费SAP Leonardo的机器学习API

    去年5月的时候,Jerry曾经写了一篇文章:使用Java程序消费SAP Leonardo的机器学习API,而最近另外做的一个项目,需要在Web应用里做同样的事情. 因为有了前一篇文章的铺垫,避免了很多 ...

  5. java入门学习总结_04

    1.循环结构 2.方法 循环结构 概述 1.对于某些需要重复执行的,相同或者相似的语句,使用某种格式来完成对代码的简化. 2.实现的语句: for语句[常用] while语句[常用] do...whi ...

  6. bash基础——终端

    前言 自学Linux的时候,我们用的显示器+键盘 是物理终端.Linux开机后,会在物理终端(显示器)之上,以软件的方式虚拟出多个终端,CentOS是6个.Ctrl+Alt+F1~6切换 默认情况下, ...

  7. git切换账号邮箱

    git config user.name 查看当前用户名 git config user.email 查看当前邮箱 git config --global user.name "用户名&qu ...

  8. 系统---《windows + ubuntu双系统》

    安装 Windows + Ubuntu双系统 不是第一次安装 Windows + Ubuntu双系统了,每一遇见的问题都不一样,收获也不一样. 制作U盘的部分截图: 电脑的基本配置截图:

  9. ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问

    报错:ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问.: 'E:\\Anoconda\\ ...

  10. Subordinates(贪心)

    题目大意: 一共有N个员工,其中最高领导人是编号s的人,每个人都只有一个直接领导,每个人都说出了自己领导的个数,问最少有几个人撒谎了. 思路: 合理的贪心是该把排最后的数变成缺少的数字,然后继续判断. ...