def __init__(self, nfeat_in, nets=[torch.nn.Identity()], p=math.inf, sigma=1.0, lam=0.001, nfeat=300, train_sigma=False, train_lam=False, stable=False, reweight=False): super().__init__() Fdata = [] self.log_lam = nn.Parameter(torch.tensor(math.log(lam)), requires_grad=train_lam) self.log_sigma = nn.Parameter(torch.tensor(math.log(sigma)), requires_grad=train_sigma) self.kernel_networks = nets self.nfeat_in = nfeat_in self.nfeat = nfeat self.p = p if self.p == math.inf: self.W = nn.Parameter(torch.randn(nfeat_in, nfeat), requires_grad=False) elif self.p == 0.5: self.W = nn.Parameter(td.Cauchy(0, 1).sample([nfeat_in, nfeat]), requires_grad=False) else: raise NotImplementedError("wrong p") self.b = nn.Parameter(torch.rand(nfeat) * 2 * math.pi, requires_grad=False)
def __init__(self, in_features: int, out_channels: int, num_repetitions: int = 1, dropout=0.0): """Creat a cauchy layer. Args: out_channels: Number of parallel representations for each input feature. in_features: Number of input features. num_repetitions: Number of parallel repetitions of this layer. """ super().__init__(in_features, out_channels, num_repetitions, dropout) self.means = nn.Parameter(torch.randn(1, in_features, out_channels, num_repetitions)) self.stds = nn.Parameter(torch.rand(1, in_features, out_channels, num_repetitions)) self.cauchy = dist.Cauchy(loc=self.means, scale=self.stds)
def __init__(self, multiplicity, in_features, dropout=0.0): """Creat a cauchy layer. Args: multiplicity: Number of parallel representations for each input feature. in_channels: Number of input channels. in_features: Number of input features. """ super().__init__(multiplicity, in_features, dropout) self.means = nn.Parameter(torch.randn(1, in_features, multiplicity)) self.stds = nn.Parameter(torch.rand(1, in_features, multiplicity)) self.cauchy = dist.Cauchy(loc=self.means, scale=self.stds)
def regenerate(self): if self.p == math.inf: self.W.data = torch.randn(self.nfeat_in, self.nfeat, device=self.W.device, dtype=self.W.dtype) elif self.p == 0.5: self.W.data = nn.Parameter( td.Cauchy(0, 1).sample([nfeat_in, nfeat], device=self.W.device, dtype=self.W.dtype)) else: raise (NotImplementedError, "wrong p") self.b.data = torch.rand( self.nfeat, device=self.W.device, dtype=self.W.dtype) * 2 * math.pi
def get_random_projections(self, latent_dim: int, num_samples: int) -> Tensor: """ Returns random samples from latent distribution's (Gaussian) unit sphere for projecting the encoded samples and the distribution samples. :param latent_dim: (Int) Dimensionality of the latent space (D) :param num_samples: (Int) Number of samples required (S) :return: Random projections from the latent unit sphere """ if self.proj_dist == 'normal': rand_samples = torch.randn(num_samples, latent_dim) elif self.proj_dist == 'cauchy': rand_samples = dist.Cauchy(torch.tensor([0.0]), torch.tensor([1.0])).sample((num_samples, latent_dim)).squeeze() else: raise ValueError('Unknown projection distribution.') rand_proj = rand_samples / rand_samples.norm(dim=1).view(-1,1) return rand_proj # [S x D]
# aa =m.sample() # print (m.sample()) # print (m.log_prob(aa)) n_samples = 500 cols = 2 rows = 2 fig = plt.figure(figsize=(7 + cols, 2 + rows), facecolor='white', dpi=150) x = np.linspace(-8, 8, 200) x = torch.from_numpy(x).float() # print (m.log_prob(x)) m = d.Cauchy(torch.tensor([0.0]), torch.tensor([1.])) probs = torch.exp(m.log_prob(x)) m = d.Normal(torch.tensor([0.0]), torch.tensor([1.])) probs2 = torch.exp(m.log_prob(x)) m = d.StudentT(torch.tensor([2.0])) probs3 = torch.exp(m.log_prob(x)) ax = plt.subplot2grid((rows, cols), (0, 0), frameon=False) ax.plot(numpy(x), numpy(probs), label='Cauchy') ax.plot(numpy(x), numpy(probs2), label='Normal') ax.plot(numpy(x), numpy(probs3), label='StudentT') ax.legend()