np.random.choices的使用
在看莫烦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的使用的更多相关文章
- 怎么理解np.random.seed()?
在使用numpy时,难免会用到随机数生成器.我一直对np.random.seed(),随机数种子搞不懂.很多博客也就粗略的说,利用随机数种子,每次生成的随机数相同. 我有两个疑惑:1, 利用随机数种子 ...
- 对抗生成网络-图像卷积-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特征图的 ...
- 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 ...
- np.random.rand均匀分布随机数和np.random.randn正态分布随机数函数使用方法
np.random.rand用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me 生成特定形状下[0,1)下的均匀分布随机数 np.random.rand(a1,a2,a3...)生成形状为( ...
- np.random.choice方法
np.random.choice方法 觉得有用的话,欢迎一起讨论相互学习~Follow Me def choice(a, size=None, replace=True, p=None) 表示从a中随 ...
- numpy中的np.random.mtrand.RandomState
1 RandomState 的应用场景概述 在训练神经网络时,苦于没有数据,此时numpy为我们提供了 “生产” 数据集的一种方式. 例如在搭建神经网络(一)中的 4.3 准备数据集 章节中就是采用n ...
- np.random.normal()正态分布
高斯分布的概率密度函数 numpy中 numpy.random.normal(loc=0.0, scale=1.0, size=None) 参数的意义为: loc:float 概率分布的均值,对应着整 ...
- np.random.randn()、np.random.rand()、np.random.randint()
(1)np.random.randn()函数 语法: np.random.randn(d0,d1,d2……dn) 1)当函数括号内没有参数时,则返回一个浮点数: 2)当函数括号内有一个参数时,则返回秩 ...
- np.random.random()系列函数
1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...
随机推荐
- 安装mysql采坑记录
安装之前彻底卸载之前的mysql,再次安装,初始化数据库那一步失败. 再次彻底卸载mysql,把原先的安装路径的文件夹删除,文件夹路径:C:\ProgramData,再次安装,成功. 总结:重装mys ...
- document对象详解
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- 自定义centos
目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...
- SAP UI5应用入口App.controller.js是如何被UI5框架加载的?
首先在UI5应用的manifes.json里,定义了UI5应用的入口视图为App: 调试器里的pending数组的两个元素: 实际上对应了我在App.controller.js里定义的两个依赖: 而a ...
- ASE19团队项目beta阶段Backend组 scrum8 记录
本次会议于12月17日,19:30在微软北京西二号楼sky garden召开,持续10分钟. 与会人员:Hao Wang, Lihao Ran, Xin Kang, Zhikai Chen 每个人的工 ...
- dubbo线程池作用于接口而不是方法
记一次线上dubbo服务超时和线程池满问题排查 可能调用的接口没问题,但是该服务中的其他接口占用完了线程池,导致调用超时被拒绝处理.
- php使用播放插件播放m3u8,mp4,flv格式的视频
一.这里我主要是播放m3u8的视频,有两款比较好的插件,swise和ckpalyer,我介绍的是ckplayer,这是在pc端播放的,并且是需要flash支持的,不过现在的最新浏览器都是默认安装的 二 ...
- oracle通过一个字段分组,取另一个字段的最大值
select * from bdcdj.lqentry1 a where 顺序号 in (select max(顺序号) from bdcdj.lqentry1 b WHERE b.archival ...
- python链接sql server 乱码问题
import pymssql import sys import os reload(sys) sys.setdefaultencoding('utf-8') os.environ['NLS_LANG ...
- Canal的简单使用(监控数据库数据的变化)
原文:https://www.cnblogs.com/java-spring/p/8930740.html canal可以用来监控数据库数据的变化,从而获得新增数据,或者修改的数据,用于实际工作中,比 ...