在看莫烦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. MySQL5.6.11安装步骤(Windows7 64位)

    1. 下载MySQL Community Server 5.6.21,注意选择系统类型(32位/64位) 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下. 3. 添加环境变量 ...

  2. S2-016、S2-017

    前言 由于S2-016.S2-017出现的原因时相同的,只是由于poc不一样,造成了不同的攻击.S2-016是RCE,S2-017是开发型重定向漏洞.这里将两个漏洞放一起分析.另外“Struts2系列 ...

  3. ASIHTTPRequest源码简单分析

      1.前言      ASIHttprequest 是基于CFNetwork的,由于CFNetwork是比较底层的http库,功能比较少,因此,在ASIHttprequest中实现了http协议中比 ...

  4. python连接impala时,执行SQL报错expecting list of size 2 for struct args

    这个错误困扰了好久,因为集群有多台,暂放到其他几台机器上执行了SQL操作,一直在找解决方法,无意间得到真传,喜出望外啊 报错信息: Traceback (most recent call last): ...

  5. apidoc 工具的使用

    使用rest framerok时,需要写API接口文档,此时就需要用到 apidoc(个人觉得这个用的比较顺手) 需要安装nodejs,,, windows 下 1 然后验证是否安装成功  node ...

  6. Flutter中的替换路由、返回到根路由

    替换路由 当我们有三个页面,页面1,页面2,页面3. 期望点击页面1按钮,跳转到页面2,页面2点击返回,跳转到页面1: 点击页面2按钮,跳转到页面3,页面3点击返回,跳转到页面1,而不是页面2. 这时 ...

  7. day 02(作业)

    作业 1.什么是编程 编程即编写程序,基于某种语法格式将想要实现的事情写出可以让计算机能够理解的文件,文件的集合即为程序.目的是使计算机操作更简单及大众化,提高工作效率. 2.简述计算机五大组成. 控 ...

  8. python_json模块和pickle模块

    json 优点:所有语言通用:缺点:只能序列化基本的数据类型list/dict/int... json格式中,字符串必须是双引号,字符都是小写. 序列化: import json v = [12,3, ...

  9. 02C#操作rabbitmq

    以前用过memcacheq.msmq.redis的list做队列,在用memcacheq的时候,还是在linux下,当然这个不是我安装的,我只是用c#操作而已,从那以后对队列处理并发能力有了新的认识, ...

  10. [唐胡璐]Android自动化- 测试环境搭建中遇到的问题

    这里主要讲一下在配置过程中遇到一个小问题,其他的步骤会略过。 安装JDK,并设置环境变量 下载Android ADT, 解压后,文件夹显示如下: Download the ADT bundle for ...