【问题标题】:How to create a boolean tensor whose values are drawn from a Bernoulli distribution?如何创建一个布尔张量,其值来自伯努利分布?
【发布时间】:2020-06-07 11:54:25
【问题描述】:

我想实现以下功能:

def bernoulli_tensor(shape, probability) -> tf.Tensor:
    ...

【问题讨论】:

    标签: python tensorflow tensor bernoulli-probability


    【解决方案1】:

    这是一种可能的实现方式:

    from typing import List
    
    import tensorflow as tf
    import tensorflow_probability as tfp
    
    
    
    def bernoulli(shape: List[int], truth_probability: float = 0.5) -> tf.Tensor:
        distribution = tfp.distributions.Bernoulli(probs=truth_probability)
        return tf.cast(distribution.sample(shape), dtype=tf.dtypes.bool)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2014-02-05
      • 1970-01-01
      • 2020-08-09
      • 2020-05-03
      相关资源
      最近更新 更多