当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.random.stateless_binomial用法及代码示例


从二项分布输出确定性伪随机值。

用法

tf.random.stateless_binomial(
    shape, seed, counts, probs, output_dtype=tf.dtypes.int32, name=None
)

参数

  • shape 一维整数张量或 Python 数组。输出张量的形状。
  • seed 形状 [2] 张量,随机数生成器的种子。必须具有数据类型 int32int64 。 (使用 XLA 时,仅允许使用 int32。)
  • counts 张量。二项分布的计数。必须可以使用 probs 进行广播,并且可以使用 shape 的最右侧维度进行广播。
  • probs 张量。二项分布的成功概率。必须可以使用 counts 进行广播,并且可以使用 shape 的最右侧维度进行广播。
  • output_dtype 输出的类型。默认值:tf.int32
  • name 操作的名称(可选)。

返回

  • samples 用随机二项式值填充的指定形状的张量。对于每个 i,每个 samples[..., i] 都是从 counts[i] 试验的二项式分布中独立抽取的,具有成功概率 probs[i]。

生成的值遵循具有指定计数和成功概率参数的二项分布。

这是tf.random.Generator.binomial 的无状态版本:如果使用相同的种子和形状运行两次,它将产生相同的伪随机数。输出在同一硬件(以及 CPU 和 GPU 之间)上的多次运行中是一致的,但可能会在 TensorFlow 版本之间或在非 CPU/GPU 硬件上发生变化。

例子:

counts = [10., 20.]
# Probability of success.
probs = [0.8]

binomial_samples = tf.random.stateless_binomial(
    shape=[2], seed=[123, 456], counts=counts, probs=probs)

counts = ... # Shape [3, 1, 2]
probs = ...  # Shape [1, 4, 2]
shape = [3, 4, 3, 4, 2]
# Sample shape will be [3, 4, 3, 4, 2]
binomial_samples = tf.random.stateless_binomial(
    shape=shape, seed=[123, 456], counts=counts, probs=probs)

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.random.stateless_binomial。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。