谁能举一个小例子来解释 tf.random.categorical 的参数? [英] can anyone give a tiny example to explain the params of tf.random.categorical?
问题描述
tensorflow 的网站给出了这个例子
tensorflow's site gives this example
tf.random.categorical(tf.log([[10., 10.]]), 5)
产生一个形状为 [1, 5] 的张量,其中每个值为 0 或 1 的概率相等"
produces a tensor that "has shape [1, 5], where each value is either 0 or 1 with equal probability"
我已经知道,基本的demo的含义tf.log([[10., 10.]])
.
I have already known, the basic demo, the meaning of tf.log([[10., 10.]])
.
我想知道 [batch_size, num_classes] 是做什么的,谁能举一个小例子来解释参数?
what I want to know is what does [batch_size, num_classes] do, can anyone give a tiny example to explain the params?
推荐答案
如您所见,tf.random.categorical
有两个参数:
As you note, tf.random.categorical
takes two parameters:
logits
,一个形状为[batch_size, num_classes]
的二维浮点张量num_samples
,一个整数标量.
logits
, a 2D float tensor with shape[batch_size, num_classes]
num_samples
, an integer scalar.
输出是一个形状为 [batch_size, num_samples]
的二维整数张量.
The output is a 2D integer tensor with shape [batch_size, num_samples]
.
logits
张量的每一行" (logits[0, :]
, logits[1, :]
, ...) 表示不同分类分布的事件概率.不过,该函数并不期望实际概率值,而是期望未归一化的对数概率;所以实际概率将是 softmax(logits[0, :])
, softmax(logits[1, :])
等.这样做的好处是你可以基本上给出任何实际值作为输入(例如神经网络的输出),它们将是有效的.此外,使用对数来使用特定的概率值或比例也很简单.例如,[log(0.1), log(0.3), log(0.6)]
和 [log(1), log(3), log(6)]
表示相同的概率,其中第二类的可能性是第一类的三倍,但只有第三类的一半.
Each "row" of the logits
tensor (logits[0, :]
, logits[1, :]
, ...) represents the event probabilities of a different categorical distribution. The function does not expect actual probability values, though, but unnormalized log-probabilities; so the actual probabilities would be softmax(logits[0, :])
, softmax(logits[1, :])
, etc. The benefit of this is that you can give basically any real values as input (e.g. the output of a neural network) and they will be valid. Also, it's trivial to use specific probability values, or proportions, using logarithms. For example, both [log(0.1), log(0.3), log(0.6)]
and [log(1), log(3), log(6)]
represent the same probability, where the second class is three times as likely as the first one but only half as likely as the third one.
对于(非标准化对数)概率的每一行,您从分布中获得 num_samples
个样本.每个样本都是一个介于 0
和 num_classes - 1
之间的整数,根据给定的概率绘制.因此,结果是形状为 [batch_size, num_samples]
的二维张量,每个分布的采样整数.
For each row of (unnormalized log-)probabilities, you get num_samples
samples from the distribution. Each sample is an integer between 0
and num_classes - 1
, drawn according to the given probabilities. So, the result is the 2D tensor with shape [batch_size, num_samples]
with the sampled integers for each distribution.
函数的一个小例子.
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
类没有被采样的概率.
In this case, the result is an array with two rows and 30 columns. The values in the first row are sampled from a categorical distribution where every class ([0, 1, 2, 3]
) has the same probability. In the second row, the class 3
is the most likely one, and class 0
has just no probability of being sampled.
这篇关于谁能举一个小例子来解释 tf.random.categorical 的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!