Python torch.distributions.normal.Normal() Examples
The following are 30
code examples of torch.distributions.normal.Normal().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.distributions.normal
, or try the search function
.
Example #1
Source File: actor.py From ReAgent with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _log_prob(self, r, scale_log): """ Compute log probability from normal distribution the same way as torch.distributions.normal.Normal, which is: ``` -((value - loc) ** 2) / (2 * var) - log_scale - math.log(math.sqrt(2 * math.pi)) ``` In the context of this class, `value = loc + r * scale`. Therefore, this function only takes `r` & `scale`; it can be reduced to below. The primary reason we don't use Normal class is that it currently cannot be exported through ONNX. """ return -(r ** 2) / 2 - scale_log - self.const
Example #2
Source File: cem_planner.py From ReAgent with BSD 3-Clause "New" or "Revised" License | 6 votes |
def sample_reward_next_state_terminal( self, state: rlt.FeatureData, action: rlt.FeatureData, mem_net: MemoryNetwork ): """ Sample one-step dynamics based on the provided world model """ wm_output = mem_net(state, action) num_mixtures = wm_output.logpi.shape[2] mixture_idx = ( Categorical(torch.exp(wm_output.logpi.view(num_mixtures))) .sample() .long() .item() ) next_state = Normal( wm_output.mus[0, 0, mixture_idx], wm_output.sigmas[0, 0, mixture_idx] ).sample() reward = wm_output.reward[0, 0] if self.terminal_effective: not_terminal_prob = torch.sigmoid(wm_output.not_terminal[0, 0]) not_terminal = Bernoulli(not_terminal_prob).sample().long().item() else: not_terminal_prob = 1.0 not_terminal = 1 return reward, next_state, not_terminal, not_terminal_prob
Example #3
Source File: synthesize.py From FloWaveNet with MIT License | 6 votes |
def synthesize(model): global global_step for batch_idx, (x, c) in enumerate(synth_loader): if batch_idx < args.num_samples: x, c = x.to(device), c.to(device) q_0 = Normal(x.new_zeros(x.size()), x.new_ones(x.size())) z = q_0.sample() * args.temp torch.cuda.synchronize() start_time = time.time() with torch.no_grad(): y_gen = model.reverse(z, c).squeeze() torch.cuda.synchronize() print('{} seconds'.format(time.time() - start_time)) wav = y_gen.to(torch.device("cpu")).data.numpy() wav_name = '{}/{}/generate_{}_{}_{}.wav'.format(args.sample_path, args.model_name, global_step, batch_idx, args.temp) librosa.output.write_wav(wav_name, wav, sr=22050) print('{} Saved!'.format(wav_name))
Example #4
Source File: predictor_estimator.py From OpenKiwi with GNU Affero General Public License v3.0 | 6 votes |
def predict_sentence(self, sentence_input): """Compute Sentence Score predictions.""" outputs = OrderedDict() sentence_scores = self.sentence_pred(sentence_input).squeeze() outputs[const.SENTENCE_SCORES] = sentence_scores if self.sentence_sigma: # Predict truncated Gaussian on [0,1] sigma = self.sentence_sigma(sentence_input).squeeze() outputs[const.SENT_SIGMA] = sigma outputs['SENT_MU'] = outputs[const.SENTENCE_SCORES] mean = outputs['SENT_MU'].clone().detach() # Compute log-likelihood of x given mu, sigma normal = Normal(mean, sigma) # Renormalize on [0,1] for truncated Gaussian partition_function = (normal.cdf(1) - normal.cdf(0)).detach() outputs[const.SENTENCE_SCORES] = mean + ( ( sigma ** 2 * (normal.log_prob(0).exp() - normal.log_prob(1).exp()) ) / partition_function ) return outputs
Example #5
Source File: train_apex.py From FloWaveNet with MIT License | 6 votes |
def synthesize(model): global global_step model.eval() for batch_idx, (x, c) in enumerate(synth_loader): if batch_idx == 0: x, c = x.to(device), c.to(device) q_0 = Normal(x.new_zeros(x.size()), x.new_ones(x.size())) z = q_0.sample() start_time = time.time() with torch.no_grad(): y_gen = model.module.reverse(z, c).squeeze() wav = y_gen.to(torch.device("cpu")).data.numpy() wav_name = '{}/{}/generate_{}_{}.wav'.format(args.sample_path, args.model_name, global_step, batch_idx) print('{} seconds'.format(time.time() - start_time)) librosa.output.write_wav(wav_name, wav, sr=22050) print('{} Saved!'.format(wav_name)) del x, c, z, q_0, y_gen, wav
Example #6
Source File: train.py From FloWaveNet with MIT License | 6 votes |
def synthesize(model): global global_step model.eval() for batch_idx, (x, c) in enumerate(synth_loader): if batch_idx == 0: x, c = x.to(device), c.to(device) q_0 = Normal(x.new_zeros(x.size()), x.new_ones(x.size())) z = q_0.sample() start_time = time.time() with torch.no_grad(): if args.num_gpu == 1: y_gen = model.reverse(z, c).squeeze() else: y_gen = model.module.reverse(z, c).squeeze() wav = y_gen.to(torch.device("cpu")).data.numpy() wav_name = '{}/{}/generate_{}_{}.wav'.format(args.sample_path, args.model_name, global_step, batch_idx) print('{} seconds'.format(time.time() - start_time)) librosa.output.write_wav(wav_name, wav, sr=22050) print('{} Saved!'.format(wav_name)) del x, c, z, q_0, y_gen, wav
Example #7
Source File: model.py From voxelmorph with GNU General Public License v3.0 | 6 votes |
def __init__(self, vol_size, enc_nf, dec_nf, full_size=True): """ Instiatiate 2018 model :param vol_size: volume size of the atlas :param enc_nf: the number of features maps for encoding stages :param dec_nf: the number of features maps for decoding stages :param full_size: boolean value full amount of decoding layers """ super(cvpr2018_net, self).__init__() dim = len(vol_size) self.unet_model = unet_core(dim, enc_nf, dec_nf, full_size) # One conv to get the flow field conv_fn = getattr(nn, 'Conv%dd' % dim) self.flow = conv_fn(dec_nf[-1], dim, kernel_size=3, padding=1) # Make flow weights + bias small. Not sure this is necessary. nd = Normal(0, 1e-5) self.flow.weight = nn.Parameter(nd.sample(self.flow.weight.shape)) self.flow.bias = nn.Parameter(torch.zeros(self.flow.bias.shape)) self.spatial_transform = SpatialTransformer(vol_size)
Example #8
Source File: core.py From firedup with MIT License | 6 votes |
def forward(self, x, a=None, old_log_std=None, old_mu=None): mu = self.mu(x) policy = Normal(mu, self.log_std.exp()) pi = policy.sample() logp_pi = policy.log_prob(pi).sum(dim=1) if a is not None: logp = policy.log_prob(a).sum(dim=1) else: logp = None if (old_mu is not None) or (old_log_std is not None): old_policy = Normal(old_mu, old_log_std.exp()) d_kl = kl_divergence(old_policy, policy).mean() else: d_kl = None info = { "old_mu": np.squeeze(mu.detach().numpy()), "old_log_std": self.log_std.detach().numpy(), } return pi, logp, logp_pi, info, d_kl
Example #9
Source File: ddpg.py From autonomous-learning-library with MIT License | 6 votes |
def __init__(self, q, policy, replay_buffer, action_space, discount_factor=0.99, minibatch_size=32, noise=0.1, replay_start_size=5000, update_frequency=1, ): # objects self.q = q self.policy = policy self.replay_buffer = replay_buffer # hyperparameters self.replay_start_size = replay_start_size self.update_frequency = update_frequency self.minibatch_size = minibatch_size self.discount_factor = discount_factor # private self._noise = Normal(0, noise * torch.tensor((action_space.high - action_space.low) / 2).to(policy.device)) self._low = torch.tensor(action_space.low, device=policy.device) self._high = torch.tensor(action_space.high, device=policy.device) self._state = None self._action = None self._frames_seen = 0
Example #10
Source File: core.py From firedup with MIT License | 6 votes |
def forward(self, x): output = self.net(x) mu = self.mu(output) if self.output_activation: mu = self.output_activation(mu) log_std = self.log_std(output) log_std = LOG_STD_MIN + 0.5 * (LOG_STD_MAX - LOG_STD_MIN) * (log_std + 1) policy = Normal(mu, torch.exp(log_std)) pi = policy.rsample() # Critical: must be rsample() and not sample() logp_pi = torch.sum(policy.log_prob(pi), dim=1) mu, pi, logp_pi = self._apply_squashing_func(mu, pi, logp_pi) # make sure actions are in correct range mu_scaled = mu * self.action_scale pi_scaled = pi * self.action_scale return pi_scaled, mu_scaled, logp_pi
Example #11
Source File: core.py From firedup with MIT License | 5 votes |
def forward(self, x, a=None): policy = Normal(self.mu(x), self.log_std.exp()) pi = policy.sample() logp_pi = policy.log_prob(pi).sum(dim=1) if a is not None: logp = policy.log_prob(a).sum(dim=1) else: logp = None return pi, logp, logp_pi
Example #12
Source File: actor.py From ReAgent with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_log_prob(self, state, squashed_action): """ Action is expected to be squashed with tanh """ loc, scale_log = self._get_loc_and_scale_log(state) # This is not getting exported; we can use it n = Normal(loc, scale_log.exp()) raw_action = self._atanh(squashed_action) log_prob = n.log_prob(raw_action) squash_correction = self._squash_correction(squashed_action) if SummaryWriterContext._global_step % 1000 == 0: SummaryWriterContext.add_histogram( "actor/get_log_prob/loc", loc.detach().cpu() ) SummaryWriterContext.add_histogram( "actor/get_log_prob/scale_log", scale_log.detach().cpu() ) SummaryWriterContext.add_histogram( "actor/get_log_prob/log_prob", log_prob.detach().cpu() ) SummaryWriterContext.add_histogram( "actor/get_log_prob/squash_correction", squash_correction.detach().cpu() ) log_prob = torch.sum(log_prob - squash_correction, dim=1).reshape(-1, 1) return log_prob
Example #13
Source File: core.py From firedup with MIT License | 5 votes |
def forward(self, x, a=None): mu = self.mu(x) policy = Normal(mu, self.log_std.exp()) pi = policy.sample() logp_pi = policy.log_prob(pi).sum(dim=1) if a is not None: logp = policy.log_prob(a).sum(dim=1) else: logp = None return pi, logp, logp_pi
Example #14
Source File: actor.py From ReAgent with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__( self, state_dim: int, action_dim: int, sizes: List[int], activations: List[str], use_batch_norm: bool = False, action_activation: str = "tanh", exploration_variance: Optional[float] = None, ): super().__init__() assert state_dim > 0, "state_dim must be > 0, got {}".format(state_dim) assert action_dim > 0, "action_dim must be > 0, got {}".format(action_dim) self.state_dim = state_dim self.action_dim = action_dim assert len(sizes) == len( activations ), "The numbers of sizes and activations must match; got {} vs {}".format( len(sizes), len(activations) ) self.action_activation = action_activation self.fc = FullyConnectedNetwork( [state_dim] + sizes + [action_dim], activations + [self.action_activation], use_batch_norm=use_batch_norm, ) # Gaussian noise for exploration. self.exploration_variance = exploration_variance if exploration_variance is not None: assert exploration_variance > 0 loc = torch.zeros(action_dim).float() scale = torch.ones(action_dim).float() * exploration_variance self.noise_dist = Normal(loc=loc, scale=scale)
Example #15
Source File: model.py From WaveVAE with MIT License | 5 votes |
def forward(self, x, c): # Encode mu_logs = self.encoder(x, c) mu = mu_logs[:, 0:1, :-1] logs = mu_logs[:, 1:, :-1] q_0 = Normal(mu.new_zeros(mu.size()), mu.new_ones(mu.size())) mean_q = (x[:, :, 1:] - mu) * torch.exp(-logs) # Reparameterization, Sampling from prior z = q_0.sample() * torch.exp(self.log_eps) + mean_q z_prior = q_0.sample() z = F.pad(z, pad=(1, 0), mode='constant', value=0) z_prior = F.pad(z_prior, pad=(1, 0), mode='constant', value=0) c_up = self.encoder.upsample(c) # Decode # x_rec : [B, 1, T] (first time step zero-padded) # mu_tot, logs_tot : [B, 1, T-1] x_rec, mu_p, log_p = self.decoder(z, c_up) x_prior = self.decoder.generate(z_prior, c_up) loss_recon = -0.5 * (- log(2.0 * pi) - 2. * log_p - torch.pow(x[:, :, 1:] - mu_p, 2) * torch.exp((-2.0 * log_p))) loss_kl = 0.5 * (mean_q ** 2 + torch.exp(self.log_eps) ** 2 - 1) - self.log_eps return x_rec, x_prior, loss_recon.mean(), loss_kl.mean()
Example #16
Source File: train.py From WaveVAE with MIT License | 5 votes |
def synthesize(model, ema=None): global global_step if ema is not None: model_ema = clone_as_averaged_model(model, ema) model_ema.eval() for batch_idx, (x, _, c, _) in enumerate(synth_loader): if batch_idx == 0: x, c = x.to(device), c.to(device) q_0 = Normal(x.new_zeros(x.size()), x.new_ones(x.size())) z = q_0.sample() wav_truth_name = '{}/{}/generate_{}_{}_truth.wav'.format(args.sample_path, args.model_name, global_step, batch_idx) librosa.output.write_wav(wav_truth_name, x.to(torch.device("cpu")).squeeze().numpy(), sr=22050) print('{} Saved!'.format(wav_truth_name)) torch.cuda.synchronize() start_time = time.time() with torch.no_grad(): if args.num_gpu == 1: x_prior = model_ema.generate(z, c).squeeze() else: x_prior = model_ema.module.generate(z, c).squeeze() torch.cuda.synchronize() print('{} seconds'.format(time.time() - start_time)) wav = x_prior.to(torch.device("cpu")).data.numpy() wav_name = '{}/{}/generate_{}_{}.wav'.format(args.sample_path, args.model_name, global_step, batch_idx) librosa.output.write_wav(wav_name, wav, sr=22050) print('{} Saved!'.format(wav_name)) del x_prior, wav, x, c, z, q_0 del model_ema
Example #17
Source File: loss.py From ClariNet with MIT License | 5 votes |
def sample_from_gaussian(y_hat): assert y_hat.size(1) == 2 y_hat = y_hat.transpose(1, 2) mean = y_hat[:, :, :1] log_std = y_hat[:, :, 1:] dist = Normal(mean, torch.exp(log_std)) sample = dist.sample() sample = torch.clamp(torch.clamp(sample, min=-1.), max=1.) del dist return sample
Example #18
Source File: train_student.py From ClariNet with MIT License | 5 votes |
def synthesize(model_t, model_s, ema=None): global global_step if ema is not None: model_s_ema = clone_as_averaged_model(model_s, ema) model_s_ema.eval() for batch_idx, (x, y, c, _) in enumerate(synth_loader): if batch_idx == 0: x, c = x.to(device), c.to(device) q_0 = Normal(x.new_zeros(x.size()), x.new_ones(x.size())) z = q_0.sample() wav_truth_name = '{}/{}/{}/generate_{}_{}_truth.wav'.format(args.sample_path, args.teacher_name, args.model_name, global_step, batch_idx) librosa.output.write_wav(wav_truth_name, y.squeeze().numpy(), sr=22050) print('{} Saved!'.format(wav_truth_name)) torch.cuda.synchronize() start_time = time.time() c_up = model_t.upsample(c) with torch.no_grad(): if args.num_gpu == 1: y_gen = model_s_ema.generate(z, c_up).squeeze() else: y_gen = model_s_ema.module.generate(z, c_up).squeeze() torch.cuda.synchronize() print('{} seconds'.format(time.time() - start_time)) wav = y_gen.to(torch.device("cpu")).data.numpy() wav_name = '{}/{}/{}/generate_{}_{}.wav'.format(args.sample_path, args.teacher_name, args.model_name, global_step, batch_idx) librosa.output.write_wav(wav_name, wav, sr=22050) print('{} Saved!'.format(wav_name)) del y_gen, wav, x, y, c, c_up, z, q_0 del model_s_ema
Example #19
Source File: abc_smc.py From hypothesis with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _allocate_pertubator(self): dimensionality = self.covariance.dim() if dimensionality <= 1: pertubator = Normal(0, self.covariance) else: zeros = torch.zeros(dimensionality) pertubator = MultivariateNormal(zeros, covariance_matrix=self.covariance) self.pertubator = pertubator
Example #20
Source File: transition_distribution.py From hypothesis with BSD 3-Clause "New" or "Revised" License | 5 votes |
def log_prob(self, mean, conditionals): normal = NormalDistribution(mean, self.sigma) log_probabilities = normal.log_prob(conditionals) del Normal return log_probabilities
Example #21
Source File: util.py From hypothesis with BSD 3-Clause "New" or "Revised" License | 5 votes |
def log_likelihood(theta, x): return Normal(theta, 1).log_prob(x)
Example #22
Source File: simulator.py From hypothesis with BSD 3-Clause "New" or "Revised" License | 5 votes |
def forward(self, inputs, designs=None): if designs is None: designs = self.uncertainty return Normal(inputs, designs).sample()
Example #23
Source File: util.py From hypothesis with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self): self.normal = Normal(1, 1) self.uniform = Uniform(0, 10)
Example #24
Source File: networks.py From PCC-pytorch with MIT License | 5 votes |
def MultivariateNormalDiag(loc, scale_diag): if loc.dim() < 1: raise ValueError("loc must be at least one-dimensional.") return Independent(Normal(loc, scale_diag), 1)
Example #25
Source File: ppo_simple_continuous_action.py From cleanrl with MIT License | 5 votes |
def get_action(self, x): mean, logstd = self.forward(x) std = torch.exp(logstd) probs = Normal(mean, std) action = probs.sample() return action, probs.log_prob(action).sum(1)
Example #26
Source File: core.py From spinningup with MIT License | 5 votes |
def _log_prob_from_distribution(self, pi, act): return pi.log_prob(act).sum(axis=-1) # Last axis sum needed for Torch Normal distribution
Example #27
Source File: core.py From spinningup with MIT License | 5 votes |
def forward(self, obs, deterministic=False, with_logprob=True): net_out = self.net(obs) mu = self.mu_layer(net_out) log_std = self.log_std_layer(net_out) log_std = torch.clamp(log_std, LOG_STD_MIN, LOG_STD_MAX) std = torch.exp(log_std) # Pre-squash distribution and sample pi_distribution = Normal(mu, std) if deterministic: # Only used for evaluating policy at test time. pi_action = mu else: pi_action = pi_distribution.rsample() if with_logprob: # Compute logprob from Gaussian, and then apply correction for Tanh squashing. # NOTE: The correction formula is a little bit magic. To get an understanding # of where it comes from, check out the original SAC paper (arXiv 1801.01290) # and look in appendix C. This is a more numerically-stable equivalent to Eq 21. # Try deriving it yourself as a (very difficult) exercise. :) logp_pi = pi_distribution.log_prob(pi_action).sum(axis=-1) logp_pi -= (2*(np.log(2) - pi_action - F.softplus(-2*pi_action))).sum(axis=1) else: logp_pi = None pi_action = torch.tanh(pi_action) pi_action = self.act_limit * pi_action return pi_action, logp_pi
Example #28
Source File: core.py From spinningup with MIT License | 5 votes |
def _distribution(self, obs): mu = self.mu_net(obs) std = torch.exp(self.log_std) return Normal(mu, std)
Example #29
Source File: core.py From spinningup with MIT License | 5 votes |
def _log_prob_from_distribution(self, pi, act): return pi.log_prob(act).sum(axis=-1) # Last axis sum needed for Torch Normal distribution
Example #30
Source File: predictor_estimator.py From OpenKiwi with GNU Affero General Public License v3.0 | 5 votes |
def sentence_loss(self, model_out, batch): """Compute Sentence score loss""" sentence_pred = model_out[const.SENTENCE_SCORES] sentence_scores = batch.sentence_scores if not self.sentence_sigma: return self.mse_loss(sentence_pred, sentence_scores) else: sigma = model_out[const.SENT_SIGMA] mean = model_out['SENT_MU'] # Compute log-likelihood of x given mu, sigma normal = Normal(mean, sigma) # Renormalize on [0,1] for truncated Gaussian partition_function = (normal.cdf(1) - normal.cdf(0)).detach() nll = partition_function.log() - normal.log_prob(sentence_scores) return nll.sum()