- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
tensorflow 的网站给出了这个例子
tf.random.categorical(tf.log([[10., 10.]]), 5)
产生一个“形状为 [1, 5] 的张量,其中每个值为 0 或 1 的概率相等”
我已经知道了,基本demo , tf.log([[10., 10.]])
的含义。
我想知道的是 [batch_size, num_classes] 是做什么的,谁能举个小例子来解释这些参数?
最佳答案
如您所见,tf.random.categorical
接受两个参数:
logits
,一个二维浮点张量,形状为 [batch_size, num_classes]
num_samples
,一个整数标量。输出是一个二维整数张量,形状为[batch_size, num_samples]
。
logits
张量的每个“行”(logits[0, :]
, logits[1, :]
, ... ) 表示不同 categorical distribution 的事件概率.不过,该函数并不期望实际的概率值,而是非标准化的对数概率;所以实际概率将是 softmax(logits[0, :])
、softmax(logits[1, :])
等。这样做的好处是您可以基本上给出任何真实值作为输入(例如神经网络的输出)并且它们将是有效的。此外,使用对数来使用特定的概率值或比例也很简单。例如,[log(0.1), log(0.3), log(0.6)]
和 [log(1), log(3), log(6)]
表示相同的概率,其中第二类的可能性是第一类的三倍,但只有第三类的一半。
对于每一行(非标准化对数)概率,您从分布中获得 num_samples
个样本。每个样本都是 0
和 num_classes - 1
之间的整数,根据给定的概率抽取。因此,结果是形状为 [batch_size, num_samples]
的二维张量,每个分布都有采样整数。
编辑:函数的一个小例子。
import tensorflow as tf
with tf.Graph().as_default(), tf.Session() as sess:
tf.random.set_random_seed(123)
logits = tf.log([[1., 1., 1., 1.],
[0., 1., 2., 3.]])
num_samples = 30
cat = tf.random.categorical(logits, num_samples)
print(sess.run(cat))
# [[3 3 1 1 0 3 3 0 2 3 1 3 3 3 1 1 0 2 2 0 3 1 3 0 1 1 0 1 3 3]
# [2 2 3 3 2 3 3 3 2 2 3 3 2 2 2 1 3 3 3 2 3 2 2 1 3 3 3 3 3 2]]
在这种情况下,结果是一个包含两行和 30 列的数组。第一行中的值是从分类分布中采样的,其中每个类 ([0, 1, 2, 3]
) 具有相同的概率。在第二行中,类 3
是最有可能的,类 0
只是没有被采样的概率。
关于python - 谁能举一个小例子来解释 tf.random.categorical 的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55063120/
例如,如果我提供 [0.5, 0.5] 的概率数组,两个函数将以相等的概率对索引 [0,1] 进行采样? 最佳答案 是的: [torch.distributions.categorical.Categ
我正在尝试使用 sklearn 中的隔离森林检测乳腺癌数据集中的异常。我正在尝试将 Iolation Forest 应用于混合数据集,当我拟合模型时,它会给我值错误。 这是我的数据集: https:/
我目前有一个包含产品信息的表(AllProducts)。它有 16 列,大约 125000 行。 我需要在数据库中创建一个唯一值,因为表中没有唯一值。我无法使用自动增量功能,因为我的数据库每天都会被清
CategoricalArray构造函数和 categorical函数来自 CategoricalArrays.jl行为似乎几乎相同: julia> using CategoricalArrays j
假设我有: df = pd.DataFrame({'gender': np.random.choice([1, 2], 10), 'height': np.random.randint(150, 21
注意:源代码包含多个类,因此为了节省您的时间,我不会发布它,但会为您提供上下文。如果我不能很好地解释,请原谅。我已经在这方面做了很多工作,我的解释对我来说有意义,但对其他人来说可能没有意义。 我的任务
有没有办法使用 sklearn.preprocessing 对象来估算分类值?我想最终创建一个预处理对象,我可以将其应用于新数据并以与旧数据相同的方式对其进行转换。 我正在寻找一种方法来做到这一点,以
我正在尝试识别数据集中的特征类型,该特征可以是分类/词袋/ float 。 但是,由于以下原因,我无法找到区分分类词和词袋的准确解决方案。 分类数据可以是对象或 float 。计算特征中的唯一值并不能
我有一个查询,该查询从多个数据库中进行选择,其中列“auth”= mad: $result = mysql_query("SELECT * FROM test.1 WHERE auth = 'mad'
我正在尝试在 tensorflow 中实现 np.random.choice。这是我的实现 import numpy as np import tensorflow as tf p=tf.Variab
我在大型数据集上使用 PIG 0.12,我需要创建一个分类变量,例如 FOREACH mydata GENERATE category = 1 IF condition1
我正在尝试使用 ggplot2 在一张图表中绘制多个箱线图。我有 1 个连续变量和几个因素。我想要一个 Y 轴,每对箱线图都有自己的 x 轴和自己的因子水平。到目前为止,我尝试使用 cowplot::
我正在使用 psych 运行一系列主成分分析包裹在 R .我混合了连续(读取离散)、二进制和有序变量。请参阅下面的数据子集,其中包含 10 个连续(读取离散)变量( c1 到 c10 )和一个二分变量
我正在使用 2 个分类变量构建二项式回归模型。这是来自《Statistical rethinking》一书中的一个例子。在书中,在使用 rethinking 包时,我们可以为每个分类变量设置先验,如下
我的问题涉及优化 Pandas 系列的内存使用。文档 note , The memory usage of a Categorical is proportional to the number of
为了对 10 种类型的图像进行分类,我使用 Keras 建模了基于卷积的序列模型。我使用了 categorical_crossentropy。我的问题是,虽然通过包含验证数据我在训练模型上获得了 99
我的目标是实现一个检测嵌套 using 是否存在的谓词别名(或 typedef )充当轻量级标签以指示类具有某些属性(用于泛型编程)。例如,has_my_tag谓词的行为应如下所示: struct A
tensorflow 的网站给出了这个例子 tf.random.categorical(tf.log([[10., 10.]]), 5) 产生一个“形状为 [1, 5] 的张量,其中每个值为 0 或
我正在处理分类数据的大型数据帧,我发现当我在两个数据帧上使用 pandas.merge 时,任何分类数据列都会自动向上转换为更大的数据类型。 (这会显着增加 RAM 消耗。)一个简单的例子来说明: 编
我想制作一个图表来表示一周中每一天每小时发生事件的频率。如何更改星期几的颜色?我想从黑色切换到红色以接近周末 weekday_hour_pickup % mutate(hour_pic
我是一名优秀的程序员,十分优秀!