示例#1
0
 def __init__(self,
              mean=None,
              logit=None,
              use_gumbel_softmax=True,
              tau=0.1):
     super().__init__()
     if mean is not None:
         self.mean = asarray(mean)
         assert ((self.mean.value >= 0).all()
                 and np.allclose(self.mean.value.sum(axis=-1), 1))
         self.logit = log(self.mean)
         self._log_pdf = self._log_pdf_mean
     elif logit is not None:
         self.mean = softmax(logit)
         self.logit = asarray(logit)
         self._log_pdf = self._log_pdf_logit
     else:
         raise ValueError
     self.n_category = self.mean.shape[-1]
     if use_gumbel_softmax:
         self.forward = self._forward_gumbel_softmax
     else:
         self.forward = self._forward
     self.tau = tau
     self.eye = np.eye(self.n_category)
示例#2
0
 def _forward(self):
     if self.mean.ndim == 1:
         index = np.random.choice(self.n_category, p=self.mean.value)
         return asarray(self.eye[index])
     else:
         mean = self.mean.value.reshape(-1, self.n_category)
         indices = [np.random.choice(self.n_category, p=p) for p in mean]
         onehot = self.eye[np.array(indices)]
         return asarray(onehot)
示例#3
0
 def __init__(self, mean=None, logit=None):
     super().__init__()
     if mean is not None:
         self.mean = asarray(mean)
         assert ((self.mean.value >= 0).all()
                 and (self.mean.value <= 1).all())
         self.logit = logit_func(mean)
         self._log_pdf = self._log_pdf_mu
     elif logit is not None:
         self.mean = sigmoid(logit)
         self.logit = asarray(logit)
         self._log_pdf = self._log_pdf_logit
     else:
         raise ValueError
示例#4
0
 def _convert2array(arg):
     if not isinstance(arg, Array):
         return asarray(arg)
     else:
         return arg
示例#5
0
def truncnormal(min, max, scale, size):
    return asarray(truncnorm(a=min, b=max, scale=scale).rvs(size))
示例#6
0
def normal(mean, std, size):
    return asarray(np.random.normal(mean, std, size))
示例#7
0
 def forward(self):
     binary = (np.random.uniform(size=self.mean.shape) < self.mean.value)
     return asarray(binary)
示例#8
0
 def __init__(self, std, ndim: int):
     super().__init__()
     self.std = asarray(std)
     self.ndim = ndim
     assert ((self.std.value >= 0).all())
示例#9
0
 def __init__(self, mean, std):
     super().__init__()
     self.mean = asarray(mean)
     self.std = asarray(std)
     assert ((self.std.value > 0).all())
示例#10
0
def uniform(min, max, size):
    return asarray(np.random.uniform(min, max, size))
示例#11
0
文件: normal.py 项目: sunriver/PRML
def truncnormal(min, max, scale, size):
    #https://www.zhihu.com/question/49923924 截断正态分布
    return asarray(truncnorm(a=min, b=max, scale=scale).rvs(size))