def dropout_validate(model, N, validloader): model.eval() mask_probs = np.ones(600)*0.5 bern = Bernoulli(torch.from_numpy(mask_probs)) correct = 0 total = 0 for x, y in validloader: x = Variable(x, volatile=True).view(-1,784) y = Variable(y, volatile=True).view(-1) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) for i in range(N): dropout_mask = bern.sample() sum_out += F.relu(x*dropout_mask) x = sum_out / N outputs = F.Softmax(self.fc3(x)) _, predicted = torch.max(outputs.data, 1) total += y.size(0) correct += (predicted == y).sum() return correct/total
def forward(self, in_feat): # in_feat is the concatenation of Feature_extract. # x = nn.MaxPool2d(in_feat, (2, 2), 1) # can be change x = nn.ReLU(self.layer1(in_feat)) x = nn.Dropout(p=0.5) # can be change x = nn.ReLU(self.layer2(x)) x = nn.Dropout(p=0.5) return F.Softmax(nn.ReLU(self.layer3(x)))
def forward(self, im_data, im_info, gt_boxes, num_boxes): ''' im_data: input image --> tensor([B,3,H,W]) im_info: image origin scale --> dict{ 'scale' : [P,Q] } gt_boxes: ground truth bounding box --> tensor([B,N,4]) [x1,y1,x2,y2] num_boxes: bbox number in each image --> list ex. [2,5,3] ''' batch_size = im_data.size(0) fmap = self.backbone(im_data) rois, rpn_cls, rpn_reg = self.rpn(fmap, im_info, gt_boxes, num_boxes) roi_feature = self.roi_pool(fmap, rois) roi_feature = self.fastRCNN(roi_feature) bbox_pred = self.fastRCNN_reg(roi_feature) cls_pred = self.fastRCNN_cls(roi_feature) cls_pred = F.Softmax(cls_pred, 1) bbox_pred = bbox_pred.view(batch_size, rois.size(1), -1) cls_pred = cls_pred.view(batch_size, rois.size(1), -1) return bbox_pred, cls_pred, rpn_cls, rpn_reg
def forward(self, x): #dim = dimension out = self.conv_module(x) dim = 1 for d in out.size()[1:]: dim = dim * d out = out.view(-1, dim) out = self.fc_module(out) return F.Softmax(out, dim=1)
def decompose_uncertainty(predictions, apply_Softmax=False): """ Decomposes the predictive uncertainty into an epistemic and aleatoric part. Technique described in Y. Kwon et al, taken from https://openreview.net/pdf?id=Sk_P2Q9sG Args: predictions: the outputs of the network apply_Softmax: bool, whether to apply Softmax to the predictions when unactivated. """ # Softmax usually already done in sample_weights if apply_Softmax == True: predictions = F.Softmax(predictions, dim=1) # Use biased variance because it is equivalent to calculations done by Shridhar epistemic_uncertainty = predictions.var(dim=1, unbiased=False) aleatoric_uncertainty = (predictions - predictions**2).mean(dim=1) return epistemic_uncertainty, aleatoric_uncertainty
def forward(self, encoder_state, initial_hidden_state, target_sequence, sample_probability=0.0): """The forward pass of the model Args: encoder_state (torch.Tensor): the output of the NMTEncoder initial_hidden_state (torch.Tensor): The last hidden state in the NMTEncoder target_sequence (torch.Tensor): the target text data tensor Returns: output_vectors (torch.Tensor): prediction vectors at each output step """ if target_sequence is None: sample_probability = 1.0 else: target_sequence = target_sequence.permute(1, 0) output_sequence_size = target_sequence.size(0) #use_sample= np.random.random() < sample_probability #if not use_sample: # y_t_index = target_sequence # use the provided encoder hidden state as the initial hidden state h_t = self.hidden_map(initial_hidden_state) batch_size = encoder_state.size(0) # initialize context vectors to zeros context_vectors = self._init_context_vectors(batch_size) # initialize first y_t word as BOS y_t_index = self._init_indices(batch_size) h_t = h_t.to(encoder_state.device) y_t_index = y_t_index.to(encoder_state.device) context_vectors = context_vectors.to(encoder_state.device) output_vectors = [] self._cached_p_attn = [] self._cached_ht = [] self._cached_decoder_state = encoder_state.cpu().detach().numpy() for i in range(output_sequence_size): use_sample = np.random.random() < sample_probability if not use_sample: y_t_index = target_sequence[i] # Step 1: Embed word and concat with previous context y_input_vector = self.target_embedding(y_t_index) rnn_input = torch.cat([y_input_vector, context_vectors], dim=1) # Step 2: Make a GRU step, getting a new hidden vector h_t = self.gru_cell(rnn_input, h_t) self._cached_ht.append(h_t.cpu().detach().numpy()) # Step 3: Use the current hidden to attend to the encoder state context_vectors, p_attn, _ = verbose_attention( encoder_state_vectors=encoder_state, query_vector=h_t) # auxillary: cache the attention probabilities for visualization self._cached_p_attn.append(p_attn.cpu().detach().numpy()) # Step 4: Use the current hidden and context vectors to make a prediction to the next word prediction_vector = torch.cat((context_vectors, h_t), dim=1) score_for_y_t_index = self.classifier( F.dropout(prediction_vector, 0.3)) if use_sample: p_y_t_index = F.Softmax(score_for_y_t_index * self._sampling_temperature) y_t_index = torch.multinomial(p_y_t_index, 1).squeeze() # auxillary: collect the prediction scores output_vectors.append(score_for_y_t_index) output_vectors = torch.stack(output_vectors).permute(1, 0, 2) return output_vectors
self.dropout = nn.Dropout(p=0.5), #last layer dropout self.fc3 = nn.Linear(200, 10), def forward(self, x): <<<<<<< HEAD pdb.set_trace() x = self.pool(F.relu(self.bn64(self.conv1(x)))) x = self.pool(F.relu(self.bn64(self.conv2(x)))) x = self.pool(F.relu(self.bn64(self.conv3(x)))) x = x.view(-1, 64 * 4 * 4) ======= >>>>>>> c16ba36044adf45533a207e330a2bc3a5dd77ad4 x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.dropout(x)) x = F.Softmax(self.fc3(x)) return x # In[30]: class CNNa(nn.Module): def __init__(self): super(CNNa, self).__init__() self.conv = nn.Sequential( # Layer 1 nn.Conv2d(in_channels=1, out_channels=16, kernel_size=(3, 3), padding=1), nn.Dropout(p=0.5), nn.ReLU(), nn.MaxPool2d(kernel_size=(2, 2), stride=2),
def forward(self, vec): vec = F.relu(self.layer1(vec)) vec = F.Softmax(self.layer2(vec)) return vec
def forward(self, prediction, labels, mask0, mask1): """ Parameters ---------- prediction: predicted affinity matrix labels: gt matrix: [B,1,N+1,N+1] mask0: [B,1,N+1] [True....True,False...False,True] : [0...Nm-1,...N] mask1: [B,1,N+1] [True....True,False...False,True] : [0...Nn-1,...N] Returns ------- loss_pre: Lf=sum(L1*(-log(A1)))/num(L1) loss_next: Lb=sum(L2*(-log(A2)))/num(L2) loss_similarity: Lc=L4*|A1-A2| loss: La=sum(L3*(-log(max(A1,A2))))/num(L3) loss=(loss_pre + loss_next + loss_a + loss_similarity)/4.0 accuray_pre: (rowwise_max_of max(A1,A2)[mask] == rowwise_max_of A1[mask])/num(mask) accuracy_next:(colwise_max_of max(A1,A2)[mask] == colwise_max_of A1[mask])/num(mask) mean_accuray: (accuracy_pre+accuracy_next)/2.0 index_pre: """ mask_pre = mask0[:, :, :] mask_next = mask1[:, :, :] mask0 = mask0.unsqueeze(3).repeat(1, 1, 1, self.max_object+1) # [B,1,N+1,N+1] mask1 = mask1.unsqueeze(2).repeat(1, 1, self.max_object+1, 1) # [B,1,N+1,N+1] mask0 = mask0.detach().to(device=self.device) mask1 = mask1.detach().to(device=self.device) # get valid mask region mask_region = (mask0 * mask1).float() # the valid position mask [B,1,N+1,N+1] mask_region_pre = mask_region.clone() mask_region_pre[:, :, self.max_object, :] = 0 mask_region_next = mask_region.clone() mask_region_next[:, :, :, self.max_object] = 0 mask_region_union = mask_region_pre*mask_region_next # get A1, A2, max(A1[:N,:N],A2[:N,:N]) prediction_pre = F.Softmax(mask_region_pre*prediction, dim=3) # softmax in each row,A1 prediction_next = F.Softmax(mask_region_next*prediction, dim=2) # softmax in each col,A2 prediction_all = prediction_pre.clone() # max(A1[:N,:N],A2[:N,:N]) prediction_all[:, :, :self.max_object, :self.max_object] =\ torch.max(prediction_pre, prediction_next)[:, :, :self.max_object, :self.max_object] # mask labels and get loss labels = labels.float() labels_pre = mask_region_pre * labels labels_next = mask_region_next * labels labels_union = mask_region_union * labels labels_num = labels.sum().item() labels_num_pre = labels_pre.sum().item() labels_num_next = labels_next.sum().item() labels_num_union = labels_union.sum().item() # Lf=sum(L1*(-log(A1)))/num(L1) if labels_num_pre != 0: loss_pre = - (labels_pre * torch.log(prediction_pre)).sum() / labels_num_pre else: loss_pre = - (labels_pre * torch.log(prediction_pre)).sum() # Lb=sum(L2*(-log(A2)))/num(L2) if labels_num_next != 0: loss_next = - (labels_next * torch.log(prediction_next)).sum() / labels_num_next else: loss_next = - (labels_next * torch.log(prediction_next)).sum() # La=sum(L3*(-log(max(A1,A2))))/num(L3) if labels_num_pre != 0 and labels_num_next != 0: loss = -(labels_pre * torch.log(prediction_all)).sum() / labels_num_pre else: loss = -(labels_pre * torch.log(prediction_all)).sum() # Lc=L4*|A1-A2| if labels_num_union != 0: loss_similarity = (labels_union * (torch.abs((1-prediction_pre) - (1-prediction_next)))).sum() / labels_num else: loss_similarity = (labels_union * (torch.abs((1-prediction_pre) - (1-prediction_next)))).sum() # (rowwise_max_of max(A1,A2)[mask] == rowwise_max_of A1[mask])/num(mask) _, indexes_ = labels_pre.max(3) # max of each row indexes_ = indexes_[:, :, :-1] _, indexes_pre = prediction_all.max(3) indexes_pre = indexes_pre[:, :, :-1] mask_pre_num = mask_pre[:, :, :-1].sum().detach().item() # number of valid targets in pre frame if mask_pre_num > 0: accuracy_pre = (indexes_pre[mask_pre[:, :, :-1]] == indexes_[mask_pre[:,:, :-1]]).float().sum() / mask_pre_num else: accuracy_pre = (indexes_pre[mask_pre[:, :, :-1]] == indexes_[mask_pre[:, :, :-1]]).float().sum() + 1 # (colwise_max_of max(A1,A2)[mask] == colwise_max_of A1[mask])/num(mask) _, indexes_ = labels_next.max(2) # max of each col indexes_ = indexes_[:, :, :-1] _, indexes_next = prediction_next.max(2) indexes_next = indexes_next[:, :, :-1] mask_next_num = mask_next[:, :, :-1].sum().detach().item() # number of valid targets in next frame if mask_next_num > 0: accuracy_next = (indexes_next[mask_next[:, :, :-1]] == indexes_[mask_next[:, :, :-1]]).float().sum() / mask_next_num else: accuracy_next = (indexes_next[mask_next[:, :, :-1]] == indexes_[mask_next[:, :, :-1]]).float().sum() + 1 return loss_pre, loss_next, loss_similarity, \ (loss_pre + loss_next + loss + loss_similarity)/4.0, \ accuracy_pre, accuracy_next, (accuracy_pre + accuracy_next)/2.0, indexes_pre
def select_action(self, state): probs = F.Softmax(self.model(Variable(state, volatile=True)) * 7) #t=7 action = probs.muntinomial() return action.data[0, 0]