def _sampleWidthByAlphas(self): # define Binomial distribution on n-1 layer filters (because we have to choose at least one filter) dist = Binomial(self.outputChannels() - 1, logits=self._alphas) # draw from distribution width = 1 + dist.sample().type(int32).item() return width
def calcNewWidthFunc(width: int, alphaWidth: AlphaPerWidthBlock.AlphaWidth): # define Binomial distribution on n-1 layer filters (because we have to choose at least one filter) dist = Binomial(width - 1, logits=alphaWidth.tensor()) # draw from distribution return 1 + dist.sample().type(int32).item()
def create_distribution(self, distribution_params): return Binomial(1, logits=distribution_params)
# In[141]: dist = Beta(torch.tensor([0.5]), torch.tensor([0.5])) dist # In[142]: dist.sample() # In[143]: from torch.distributions.binomial import Binomial # In[144]: dist = Binomial(100, torch.tensor([0, .2, .8, 1])) # In[147]: dist.sample() # In[148]: # 100- count of trials # 0, 0.2, 0.8 and 1 are event probabilities # In[174]: from torch.distributions.categorical import Categorical # In[175]:
model_path = os.path.join('checkpoints', model_name) if not os.path.exists(model_path): os.makedirs(model_path) from tensorboardX import SummaryWriter writer = SummaryWriter(os.path.join('runs', model_name)) n_iter = 0 num_samples = 50 sample = torch.randn(64, opts.nz).double().to(device) for epoch in range(opts.epochs): print('=> Epoch {}'.format(epoch)) model.train() running_loss = [] for data in tqdm(train_loader): image = data[0] m = Binomial(1, image.view(-1, 784)) # inputs = m.sample(torch.Size([num_samples])).double().to(device) inputs = m.sample().expand(num_samples, image.shape[0], 784).double().to(device) optimizer.zero_grad() loss, bce, kld = model.train_loss(inputs) loss.backward() optimizer.step() running_loss.append(loss.item()) writer.add_scalar('bce', bce, n_iter) writer.add_scalar('kld', kld, n_iter) writer.add_scalar('loss', loss, n_iter) n_iter += 1 writer.add_scalar('loss_epoch', np.mean(running_loss), epoch)
def p_infected(self, n, p): binom_dist = Binomial(n, p) # returns 1 - p(0 transmissions to susceptible | 'n' nearby infected, 'p' prob. of transmission) p0 = binom_dist.log_prob(torch.tensor(0.)) out = torch.log(1 - torch.exp(p0)) return out