Python torch.log() Examples
The following are 30
code examples of torch.log().
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
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: networks.py From connecting_the_dots with MIT License | 6 votes |
def tforward(self, disp, edge=None): self.sobel=self.sobel.to(disp.device) if edge is not None: grad = self.sobel(disp) grad = torch.sqrt(grad[:,0:1,...]**2 + grad[:,1:2,...]**2 + 1e-8) pdf = (1-edge)/self.b0 * torch.exp(-torch.abs(grad)/self.b0) + \ edge/self.b1 * torch.exp(-torch.abs(grad)/self.b1) val = torch.mean(-torch.log(pdf.clamp(min=1e-4))) else: # on qifeng's data we don't have ambient info # therefore we supress edge everywhere grad = self.sobel(disp) grad = torch.sqrt(grad[:,0:1,...]**2 + grad[:,1:2,...]**2 + 1e-8) grad= torch.clamp(grad, 0, 1.0) val = torch.mean(grad) return val
Example #2
Source File: nn_lib.py From ConvLab with MIT License | 6 votes |
def forward(self, logits, temperature=1.0, hard=False, return_max_id=False): """ :param logits: [batch_size, n_class] unnormalized log-prob :param temperature: non-negative scalar :param hard: if True take argmax :param return_max_id :return: [batch_size, n_class] sample from gumbel softmax """ y = self.gumbel_softmax_sample(logits, temperature, self.use_gpu) _, y_hard = th.max(y, dim=1, keepdim=True) if hard: y_onehot = cast_type(Variable(th.zeros(y.size())), FLOAT, self.use_gpu) y_onehot.scatter_(1, y_hard, 1.0) y = y_onehot if return_max_id: return y, y_hard else: return y
Example #3
Source File: utils.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def cxcy_to_gcxgcy(cxcy, priors_cxcy): """ Encode bounding boxes (that are in center-size form) w.r.t. the corresponding prior boxes (that are in center-size form). For the center coordinates, find the offset with respect to the prior box, and scale by the size of the prior box. For the size coordinates, scale by the size of the prior box, and convert to the log-space. In the model, we are predicting bounding box coordinates in this encoded form. :param cxcy: bounding boxes in center-size coordinates, a tensor of size (n_priors, 4) :param priors_cxcy: prior boxes with respect to which the encoding must be performed, a tensor of size (n_priors, 4) :return: encoded bounding boxes, a tensor of size (n_priors, 4) """ # The 10 and 5 below are referred to as 'variances' in the original Caffe repo, completely empirical # They are for some sort of numerical conditioning, for 'scaling the localization gradient' # See https://github.com/weiliu89/caffe/issues/155 return torch.cat([(cxcy[:, :2] - priors_cxcy[:, :2]) / (priors_cxcy[:, 2:] / 10), # g_c_x, g_c_y torch.log(cxcy[:, 2:] / priors_cxcy[:, 2:]) * 5], 1) # g_w, g_h
Example #4
Source File: box_utils.py From CSD-SSD with MIT License | 6 votes |
def encode(matched, priors, variances): """Encode the variances from the priorbox layers into the ground truth boxes we have matched (based on jaccard overlap) with the prior boxes. Args: matched: (tensor) Coords of ground truth for each prior in point-form Shape: [num_priors, 4]. priors: (tensor) Prior boxes in center-offset form Shape: [num_priors,4]. variances: (list[float]) Variances of priorboxes Return: encoded boxes (tensor), Shape: [num_priors, 4] """ # dist b/t match center and prior's center g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2] # encode variance g_cxcy /= (variances[0] * priors[:, 2:]) # match wh / prior wh g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:] g_wh = torch.log(g_wh) / variances[1] # return target for smooth_l1_loss return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4] # Adapted from https://github.com/Hakuyume/chainer-ssd
Example #5
Source File: sac_ae.py From pytorch_sac_ae with MIT License | 6 votes |
def update_critic(self, obs, action, reward, next_obs, not_done, L, step): with torch.no_grad(): _, policy_action, log_pi, _ = self.actor(next_obs) target_Q1, target_Q2 = self.critic_target(next_obs, policy_action) target_V = torch.min(target_Q1, target_Q2) - self.alpha.detach() * log_pi target_Q = reward + (not_done * self.discount * target_V) # get current Q estimates current_Q1, current_Q2 = self.critic(obs, action) critic_loss = F.mse_loss(current_Q1, target_Q) + F.mse_loss(current_Q2, target_Q) L.log('train_critic/loss', critic_loss, step) # Optimize the critic self.critic_optimizer.zero_grad() critic_loss.backward() self.critic_optimizer.step() self.critic.log(L, step)
Example #6
Source File: sac_ae.py From pytorch_sac_ae with MIT License | 6 votes |
def update_decoder(self, obs, target_obs, L, step): h = self.critic.encoder(obs) if target_obs.dim() == 4: # preprocess images to be in [-0.5, 0.5] range target_obs = utils.preprocess_obs(target_obs) rec_obs = self.decoder(h) rec_loss = F.mse_loss(target_obs, rec_obs) # add L2 penalty on latent representation # see https://arxiv.org/pdf/1903.12436.pdf latent_loss = (0.5 * h.pow(2).sum(1)).mean() loss = rec_loss + self.decoder_latent_lambda * latent_loss self.encoder_optimizer.zero_grad() self.decoder_optimizer.zero_grad() loss.backward() self.encoder_optimizer.step() self.decoder_optimizer.step() L.log('train_ae/ae_loss', loss, step) self.decoder.log(L, step, log_freq=LOG_FREQ)
Example #7
Source File: sac_ae.py From pytorch_sac_ae with MIT License | 6 votes |
def update(self, replay_buffer, L, step): obs, action, reward, next_obs, not_done = replay_buffer.sample() L.log('train/batch_reward', reward.mean(), step) self.update_critic(obs, action, reward, next_obs, not_done, L, step) if step % self.actor_update_freq == 0: self.update_actor_and_alpha(obs, L, step) if step % self.critic_target_update_freq == 0: utils.soft_update_params( self.critic.Q1, self.critic_target.Q1, self.critic_tau ) utils.soft_update_params( self.critic.Q2, self.critic_target.Q2, self.critic_tau ) utils.soft_update_params( self.critic.encoder, self.critic_target.encoder, self.encoder_tau ) if self.decoder is not None and step % self.decoder_update_freq == 0: self.update_decoder(obs, obs, L, step)
Example #8
Source File: trade_utils.py From ConvLab with MIT License | 6 votes |
def masked_cross_entropy_for_slot(logits, target, mask, use_softmax=True): # print("logits", logits) # print("target", target) # -1 means infered from other dimentions logits_flat = logits.view(-1, logits.size(-1)) # print(logits_flat.size()) if use_softmax: log_probs_flat = functional.log_softmax(logits_flat, dim=1) else: log_probs_flat = logits_flat # torch.log(logits_flat) # print("log_probs_flat", log_probs_flat) target_flat = target.view(-1, 1) # print("target_flat", target_flat) losses_flat = -torch.gather(log_probs_flat, dim=1, index=target_flat) losses = losses_flat.view(*target.size()) # b * |s| losses = losses * mask.float() loss = losses.sum() / (losses.size(0)*losses.size(1)) # print("loss inside", loss) return loss
Example #9
Source File: model_utils.py From medicaldetectiontoolkit with Apache License 2.0 | 6 votes |
def apply_box_deltas_2D(boxes, deltas): """Applies the given deltas to the given boxes. boxes: [N, 4] where each row is y1, x1, y2, x2 deltas: [N, 4] where each row is [dy, dx, log(dh), log(dw)] """ # Convert to y, x, h, w height = boxes[:, 2] - boxes[:, 0] width = boxes[:, 3] - boxes[:, 1] center_y = boxes[:, 0] + 0.5 * height center_x = boxes[:, 1] + 0.5 * width # Apply deltas center_y += deltas[:, 0] * height center_x += deltas[:, 1] * width height *= torch.exp(deltas[:, 2]) width *= torch.exp(deltas[:, 3]) # Convert back to y1, x1, y2, x2 y1 = center_y - 0.5 * height x1 = center_x - 0.5 * width y2 = y1 + height x2 = x1 + width result = torch.stack([y1, x1, y2, x2], dim=1) return result
Example #10
Source File: box_utils.py From hand-detection.PyTorch with MIT License | 6 votes |
def encode(matched, priors, variances): """Encode the variances from the priorbox layers into the ground truth boxes we have matched (based on jaccard overlap) with the prior boxes. Args: matched: (tensor) Coords of ground truth for each prior in point-form Shape: [num_priors, 4]. priors: (tensor) Prior boxes in center-offset form Shape: [num_priors,4]. variances: (list[float]) Variances of priorboxes Return: encoded boxes (tensor), Shape: [num_priors, 4] """ # dist b/t match center and prior's center g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2] # encode variance g_cxcy /= (variances[0] * priors[:, 2:]) # match wh / prior wh g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:] g_wh = torch.log(g_wh) / variances[1] # return target for smooth_l1_loss return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4] # Adapted from https://github.com/Hakuyume/chainer-ssd
Example #11
Source File: trade_utils.py From ConvLab with MIT License | 6 votes |
def masked_cross_entropy_(logits, target, length, take_log=False): if USE_CUDA: length = Variable(torch.LongTensor(length)).cuda() else: length = Variable(torch.LongTensor(length)) # logits_flat: (batch * max_len, num_classes) # -1 means infered from other dimentions logits_flat = logits.view(-1, logits.size(-1)) if take_log: logits_flat = torch.log(logits_flat) # target_flat: (batch * max_len, 1) target_flat = target.view(-1, 1) # losses_flat: (batch * max_len, 1) losses_flat = -torch.gather(logits_flat, dim=1, index=target_flat) # losses: (batch, max_len) losses = losses_flat.view(*target.size()) # mask: (batch, max_len) mask = sequence_mask(sequence_length=length, max_len=target.size(1)) losses = losses * mask.float() loss = losses.sum() / length.float().sum() return loss
Example #12
Source File: bbox_transform.py From cascade-rcnn_Pytorch with MIT License | 6 votes |
def bbox_transform(ex_rois, gt_rois): ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0 ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0 ex_ctr_x = ex_rois[:, 0] + 0.5 * ex_widths ex_ctr_y = ex_rois[:, 1] + 0.5 * ex_heights gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0 gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0 gt_ctr_x = gt_rois[:, 0] + 0.5 * gt_widths gt_ctr_y = gt_rois[:, 1] + 0.5 * gt_heights targets_dx = (gt_ctr_x - ex_ctr_x) / ex_widths targets_dy = (gt_ctr_y - ex_ctr_y) / ex_heights targets_dw = torch.log(gt_widths / ex_widths) targets_dh = torch.log(gt_heights / ex_heights) targets = torch.stack( (targets_dx, targets_dy, targets_dw, targets_dh),1) return targets
Example #13
Source File: tsd_net.py From ConvLab with MIT License | 6 votes |
def forward(self, z_enc_out, u_enc_out, u_input_np, m_t_input, degree_input, last_hidden, z_input_np): sparse_z_input = Variable(self.get_sparse_selective_input(z_input_np), requires_grad=False) m_embed = self.emb(m_t_input) z_context = self.attn_z(last_hidden, z_enc_out) u_context = self.attn_u(last_hidden, u_enc_out) gru_in = torch.cat([m_embed, u_context, z_context, degree_input.unsqueeze(0)], dim=2) gru_out, last_hidden = self.gru(gru_in, last_hidden) gen_score = self.proj(torch.cat([z_context, u_context, gru_out], 2)).squeeze(0) z_copy_score = F.tanh(self.proj_copy2(z_enc_out.transpose(0, 1))) z_copy_score = torch.matmul(z_copy_score, gru_out.squeeze(0).unsqueeze(2)).squeeze(2) z_copy_score = z_copy_score.cpu() z_copy_score_max = torch.max(z_copy_score, dim=1, keepdim=True)[0] z_copy_score = torch.exp(z_copy_score - z_copy_score_max) # [B,T] z_copy_score = torch.log(torch.bmm(z_copy_score.unsqueeze(1), sparse_z_input)).squeeze( 1) + z_copy_score_max # [B,V] z_copy_score = cuda_(z_copy_score) scores = F.softmax(torch.cat([gen_score, z_copy_score], dim=1), dim=1) gen_score, z_copy_score = scores[:, :cfg.vocab_size], \ scores[:, cfg.vocab_size:] proba = gen_score + z_copy_score[:, :cfg.vocab_size] # [B,V] proba = torch.cat([proba, z_copy_score[:, cfg.vocab_size:]], 1) return proba, last_hidden, gru_out
Example #14
Source File: blow.py From blow with Apache License 2.0 | 6 votes |
def __init__(self,in_channel): super(InvConv,self).__init__() weight=np.random.randn(in_channel,in_channel) q,_=linalg.qr(weight) w_p,w_l,w_u=linalg.lu(q.astype(np.float32)) w_s=np.diag(w_u) w_u=np.triu(w_u,1) u_mask=np.triu(np.ones_like(w_u),1) l_mask=u_mask.T self.register_buffer('w_p',torch.from_numpy(w_p)) self.register_buffer('u_mask',torch.from_numpy(u_mask)) self.register_buffer('l_mask',torch.from_numpy(l_mask)) self.register_buffer('l_eye',torch.eye(l_mask.shape[0])) self.register_buffer('s_sign',torch.sign(torch.from_numpy(w_s))) self.w_l=torch.nn.Parameter(torch.from_numpy(w_l)) self.w_s=torch.nn.Parameter(torch.log(1e-7+torch.abs(torch.from_numpy(w_s)))) self.w_u=torch.nn.Parameter(torch.from_numpy(w_u)) self.weight=None self.invweight=None return
Example #15
Source File: utils.py From conv-social-pooling with MIT License | 6 votes |
def maskedNLL(y_pred, y_gt, mask): acc = torch.zeros_like(mask) muX = y_pred[:,:,0] muY = y_pred[:,:,1] sigX = y_pred[:,:,2] sigY = y_pred[:,:,3] rho = y_pred[:,:,4] ohr = torch.pow(1-torch.pow(rho,2),-0.5) x = y_gt[:,:, 0] y = y_gt[:,:, 1] # If we represent likelihood in feet^(-1): out = 0.5*torch.pow(ohr, 2)*(torch.pow(sigX, 2)*torch.pow(x-muX, 2) + torch.pow(sigY, 2)*torch.pow(y-muY, 2) - 2*rho*torch.pow(sigX, 1)*torch.pow(sigY, 1)*(x-muX)*(y-muY)) - torch.log(sigX*sigY*ohr) + 1.8379 # If we represent likelihood in m^(-1): # out = 0.5 * torch.pow(ohr, 2) * (torch.pow(sigX, 2) * torch.pow(x - muX, 2) + torch.pow(sigY, 2) * torch.pow(y - muY, 2) - 2 * rho * torch.pow(sigX, 1) * torch.pow(sigY, 1) * (x - muX) * (y - muY)) - torch.log(sigX * sigY * ohr) + 1.8379 - 0.5160 acc[:,:,0] = out acc[:,:,1] = out acc = acc*mask lossVal = torch.sum(acc)/torch.sum(mask) return lossVal ## NLL for sequence, outputs sequence of NLL values for each time-step, uses mask for variable output lengths, used for evaluation
Example #16
Source File: bbox_transform.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def bbox_transform(ex_rois, gt_rois): ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0 ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0 ex_ctr_x = ex_rois[:, 0] + 0.5 * ex_widths ex_ctr_y = ex_rois[:, 1] + 0.5 * ex_heights gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0 gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0 gt_ctr_x = gt_rois[:, 0] + 0.5 * gt_widths gt_ctr_y = gt_rois[:, 1] + 0.5 * gt_heights targets_dx = (gt_ctr_x - ex_ctr_x) / ex_widths targets_dy = (gt_ctr_y - ex_ctr_y) / ex_heights targets_dw = torch.log(gt_widths / ex_widths) targets_dh = torch.log(gt_heights / ex_heights) targets = torch.stack( (targets_dx, targets_dy, targets_dw, targets_dh), 1) return targets
Example #17
Source File: semi_sup_net.py From SEDST with MIT License | 6 votes |
def forward(self, p_proba, q_proba): # [B, T, V] mask = torch.ones(p_proba.size(0), p_proba.size(1)) cnt = 0 for i in range(q_proba.size(0)): flg = False for j in range(q_proba.size(1)): topv, topi = torch.max(q_proba[i,j], -1) if flg: mask[i,j] = 0 else: mask[i,j] = 1 cnt += 1 if topi.item() in self.special_tokens: flg = True mask = cuda_(Variable(mask)) loss = q_proba * (torch.log(q_proba) - torch.log(p_proba)) masked_loss = torch.sum(mask.unsqueeze(-1) * loss) return masked_loss / (cnt + 1e-10)
Example #18
Source File: ner_model.py From Doc2EDAG with MIT License | 6 votes |
def get_log_parition(self, seq_emit_score): """ Calculate the log of the partition function :param seq_emit_score: [seq_len, batch_size, tag_size] :return: Tensor with Size([batch_size]) """ seq_len, batch_size, tag_size = seq_emit_score.size() # dynamic programming table to store previously summarized tag logits dp_table = seq_emit_score.new_full( (batch_size, tag_size), self.NEG_LOGIT, requires_grad=False ) dp_table[:, self.start_tag] = 0. batch_trans_mat = self.trans_mat.unsqueeze(0).expand(batch_size, tag_size, tag_size) for token_idx in range(seq_len): prev_logit = dp_table.unsqueeze(1) # [batch_size, 1, tag_size] batch_emit_score = seq_emit_score[token_idx].unsqueeze(-1) # [batch_size, tag_size, 1] cur_logit = batch_trans_mat + batch_emit_score + prev_logit # [batch_size, tag_size, tag_size] dp_table = log_sum_exp(cur_logit) # [batch_size, tag_size] batch_logit = dp_table + self.trans_mat[self.end_tag, :].unsqueeze(0) log_partition = log_sum_exp(batch_logit) # [batch_size] return log_partition
Example #19
Source File: criterions.py From ConvLab with MIT License | 5 votes |
def forward(self, preds, goals_id, outcomes_id): # preds: (batch_size, outcome_len, outcome_vocab_size) # goals_id: list of list, id, batch_size*goal_len # outcomes_id: list of list, id, batch_size*outcome_len batch_size = len(goals_id) losses = [] for bth in range(batch_size): pred = preds[bth] # (outcome_len, outcome_vocab_size) goal = goals_id[bth] # list, id, len=goal_len goal_str = self.corpus.id2goal(goal) # list, str, len=goal_len outcome = outcomes_id[bth] # list, id, len=outcome_len outcome_str = self.corpus.id2outcome(outcome) # list, str, len=outcome_len if outcome_str[0] in self.bad_tokens: continue # get all the possible choices choices = self.domain.generate_choices(goal_str) sel_outs = [pred[i] for i in range(pred.size(0))] # outcome_len*(outcome_vocab_size, ) choices_logits = [] # outcome_len*(option_amount, 1) for i in range(self.domain.selection_length()): idxs = np.array([self.dictionary[c[i]] for c in choices]) idxs_var = self.np2var(idxs, LONG) # (option_amount, ) choices_logits.append(th.gather(sel_outs[i], 0, idxs_var).unsqueeze(1)) choice_logit = th.sum(th.cat(choices_logits, 1), 1, keepdim=False) # (option_amount, ) choice_logit = choice_logit.sub(choice_logit.max().item()) # (option_amount, ) prob = F.softmax(choice_logit, dim=0) # (option_amount, ) label = choices.index(outcome_str) target_prob = prob[label] losses.append(-th.log(target_prob)) return sum(losses) / float(len(losses))
Example #20
Source File: learn.py From neuralcoref with MIT License | 5 votes |
def get_top_pair_loss(n): def top_pair_loss(scores, targets, debug=False): """ Top pairs (best true and best mistaken) and single mention probabilistic loss """ true_ants = targets[2] false_ants = targets[3] if len(targets) == 5 else None s_scores = clipped_sigmoid(scores) true_pairs = torch.gather(s_scores, 1, true_ants) top_true, top_true_arg = torch.log(true_pairs).max( dim=1 ) # max(log(p)), p=sigmoid(s) if debug: print("true_pairs", true_pairs.data) print("top_true", top_true.data) print("top_true_arg", top_true_arg.data) out_score = torch.sum(top_true).neg() if ( false_ants is not None ): # We have no false antecedents when there are no pairs false_pairs = torch.gather(s_scores, 1, false_ants) top_false, _ = torch.log(1 - false_pairs).min( dim=1 ) # min(log(1-p)), p=sigmoid(s) out_score = out_score + torch.sum(top_false).neg() return out_score / n return top_pair_loss
Example #21
Source File: blow.py From blow with Apache License 2.0 | 5 votes |
def forward(self,h,emb): # Squeeze h=self.squeeze.forward(h) # Run flows & accumulate log-det log_det=0 for flow in self.flows: h,ldet=flow.forward(h,emb) log_det+=ldet return h,log_det
Example #22
Source File: blow.py From blow with Apache License 2.0 | 5 votes |
def forward(self,h): # Init if not self.initialized: sbatch,nsq,lchunk=h.size() flatten=h.permute(1,0,2).contiguous().view(nsq,-1).data self.m.data=-flatten.mean(1).view(1,nsq,1) self.logs.data=torch.log(1/(flatten.std(1)+1e-7)).view(1,nsq,1) self.initialized=True # Normalize h=torch.exp(self.logs)*(h+self.m) logdet=self.logs.sum()*h.size(2) return h,logdet
Example #23
Source File: blow.py From blow with Apache License 2.0 | 5 votes |
def forward(self,h,s): # Prepare sbatch,lchunk=h.size() h=h.unsqueeze(1) emb=self.embedding(s) # Run blocks & accumulate log-det log_det=0 for block in self.blocks: h,ldet=block.forward(h,emb) log_det+=ldet # Back to original dim h=h.view(sbatch,lchunk) return h,log_det
Example #24
Source File: ner_model.py From Doc2EDAG with MIT License | 5 votes |
def log_sum_exp(batch_logit): """ Caculate the log-sum-exp operation for the last dimension. :param batch_logit: Size([*, logit_size]), * should at least be 1 :return: Size([*]) """ batch_max, _ = batch_logit.max(dim=-1) batch_broadcast = batch_max.unsqueeze(-1) return batch_max + \ torch.log(torch.sum(torch.exp(batch_logit - batch_broadcast), dim=-1))
Example #25
Source File: criterions.py From ConvLab with MIT License | 5 votes |
def forward(self, log_qy, batch_size=None, unit_average=False): """ -qy log(qy) """ if log_qy.dim() > 2: log_qy = log_qy.squeeze() qy = th.exp(log_qy) h_q = th.sum(-1 * log_qy * qy, dim=1) if unit_average: return th.mean(h_q) else: return th.sum(h_q) / batch_size
Example #26
Source File: utils.py From pixel-cnn-pp with MIT License | 5 votes |
def sample_from_discretized_mix_logistic(l, nr_mix): # Pytorch ordering l = l.permute(0, 2, 3, 1) ls = [int(y) for y in l.size()] xs = ls[:-1] + [3] # unpack parameters logit_probs = l[:, :, :, :nr_mix] l = l[:, :, :, nr_mix:].contiguous().view(xs + [nr_mix * 3]) # sample mixture indicator from softmax temp = torch.FloatTensor(logit_probs.size()) if l.is_cuda : temp = temp.cuda() temp.uniform_(1e-5, 1. - 1e-5) temp = logit_probs.data - torch.log(- torch.log(temp)) _, argmax = temp.max(dim=3) one_hot = to_one_hot(argmax, nr_mix) sel = one_hot.view(xs[:-1] + [1, nr_mix]) # select logistic parameters means = torch.sum(l[:, :, :, :, :nr_mix] * sel, dim=4) log_scales = torch.clamp(torch.sum( l[:, :, :, :, nr_mix:2 * nr_mix] * sel, dim=4), min=-7.) coeffs = torch.sum(F.tanh( l[:, :, :, :, 2 * nr_mix:3 * nr_mix]) * sel, dim=4) # sample from logistic & clip to interval # we don't actually round to the nearest 8bit value when sampling u = torch.FloatTensor(means.size()) if l.is_cuda : u = u.cuda() u.uniform_(1e-5, 1. - 1e-5) u = Variable(u) x = means + torch.exp(log_scales) * (torch.log(u) - torch.log(1. - u)) x0 = torch.clamp(torch.clamp(x[:, :, :, 0], min=-1.), max=1.) x1 = torch.clamp(torch.clamp( x[:, :, :, 1] + coeffs[:, :, :, 0] * x0, min=-1.), max=1.) x2 = torch.clamp(torch.clamp( x[:, :, :, 2] + coeffs[:, :, :, 1] * x0 + coeffs[:, :, :, 2] * x1, min=-1.), max=1.) out = torch.cat([x0.view(xs[:-1] + [1]), x1.view(xs[:-1] + [1]), x2.view(xs[:-1] + [1])], dim=3) # put back in Pytorch ordering out = out.permute(0, 3, 1, 2) return out
Example #27
Source File: criterions.py From ConvLab with MIT License | 5 votes |
def forward(self, log_qy, log_py, batch_size=None, unit_average=False): """ qy * log(q(y)/p(y)) """ qy = th.exp(log_qy) y_kl = th.sum(qy * (log_qy - log_py), dim=1) if unit_average: return th.mean(y_kl) else: return th.sum(y_kl)/batch_size
Example #28
Source File: utils.py From conv-social-pooling with MIT License | 5 votes |
def maskedMSETest(y_pred, y_gt, mask): acc = torch.zeros_like(mask) muX = y_pred[:, :, 0] muY = y_pred[:, :, 1] x = y_gt[:, :, 0] y = y_gt[:, :, 1] out = torch.pow(x - muX, 2) + torch.pow(y - muY, 2) acc[:, :, 0] = out acc[:, :, 1] = out acc = acc * mask lossVal = torch.sum(acc[:,:,0],dim=1) counts = torch.sum(mask[:,:,0],dim=1) return lossVal, counts ## Helper function for log sum exp calculation:
Example #29
Source File: utils.py From conv-social-pooling with MIT License | 5 votes |
def logsumexp(inputs, dim=None, keepdim=False): if dim is None: inputs = inputs.view(-1) dim = 0 s, _ = torch.max(inputs, dim=dim, keepdim=True) outputs = s + (inputs - s).exp().sum(dim=dim, keepdim=True).log() if not keepdim: outputs = outputs.squeeze(dim) return outputs
Example #30
Source File: tsd_net.py From ConvLab with MIT License | 5 votes |
def forward(self, u_input, u_input_np, m_input, m_input_np, z_input, u_len, m_len, turn_states, degree_input, mode, **kwargs): if mode == 'train' or mode == 'valid': pz_proba, pm_dec_proba, turn_states = \ self.forward_turn(u_input, u_len, m_input=m_input, m_len=m_len, z_input=z_input, mode='train', turn_states=turn_states, degree_input=degree_input, u_input_np=u_input_np, m_input_np=m_input_np, **kwargs) loss, pr_loss, m_loss = self.supervised_loss(torch.log(pz_proba), torch.log(pm_dec_proba), z_input, m_input) return loss, pr_loss, m_loss, turn_states elif mode == 'test': m_output_index, pz_index, turn_states = self.forward_turn(u_input, u_len=u_len, mode='test', turn_states=turn_states, degree_input=degree_input, u_input_np=u_input_np, m_input_np=m_input_np, **kwargs ) return m_output_index, pz_index, turn_states elif mode == 'rl': loss = self.forward_turn(u_input, u_len=u_len, is_train=False, mode='rl', turn_states=turn_states, degree_input=degree_input, u_input_np=u_input_np, m_input_np=m_input_np, **kwargs ) return loss