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)
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)
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
def _convert2array(arg): if not isinstance(arg, Array): return asarray(arg) else: return arg
def truncnormal(min, max, scale, size): return asarray(truncnorm(a=min, b=max, scale=scale).rvs(size))
def normal(mean, std, size): return asarray(np.random.normal(mean, std, size))
def forward(self): binary = (np.random.uniform(size=self.mean.shape) < self.mean.value) return asarray(binary)
def __init__(self, std, ndim: int): super().__init__() self.std = asarray(std) self.ndim = ndim assert ((self.std.value >= 0).all())
def __init__(self, mean, std): super().__init__() self.mean = asarray(mean) self.std = asarray(std) assert ((self.std.value > 0).all())
def uniform(min, max, size): return asarray(np.random.uniform(min, max, size))
def truncnormal(min, max, scale, size): #https://www.zhihu.com/question/49923924 截断正态分布 return asarray(truncnorm(a=min, b=max, scale=scale).rvs(size))