Python torch.long() Examples
The following are 30
code examples of torch.long().
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
.
Example #1
Source File: my_data.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def get_train_data(self, batch_size=8): samples = random.sample(self.train_dict.keys(), batch_size) texts = [self.train_dict[k][0] for k in samples] labels = [self.train_dict[k][1] for k in samples] robust_padding(texts, labels) maxlen = max(len(t) for t in texts) text_tensor = torch.zeros(maxlen, batch_size, dtype=torch.long) for i, text in enumerate(texts): text_tensor[:, i] = torch.LongTensor([VOCAB.find(c) for c in text]) truth_tensor = torch.zeros(maxlen, batch_size, dtype=torch.long) for i, label in enumerate(labels): truth_tensor[:, i] = torch.LongTensor(label) return text_tensor.to(self.device), truth_tensor.to(self.device)
Example #2
Source File: coco2017_animal.py From easy-faster-rcnn.pytorch with MIT License | 6 votes |
def __getitem__(self, index: int) -> Tuple[str, Tensor, Tensor, Tensor, Tensor]: image_id = self._image_ids[index] annotation = self._image_id_to_annotation_dict[image_id] bboxes = [obj.bbox.tolist() for obj in annotation.objects] labels = [COCO2017Animal.CATEGORY_TO_LABEL_DICT[COCO2017.LABEL_TO_CATEGORY_DICT[obj.label]] for obj in annotation.objects] # mapping from original `COCO2017` dataset bboxes = torch.tensor(bboxes, dtype=torch.float) labels = torch.tensor(labels, dtype=torch.long) image = Image.open(annotation.filename).convert('RGB') # for some grayscale images # random flip on only training mode if self._mode == COCO2017Animal.Mode.TRAIN and random.random() > 0.5: image = ImageOps.mirror(image) bboxes[:, [0, 2]] = image.width - bboxes[:, [2, 0]] # index 0 and 2 represent `left` and `right` respectively image, scale = COCO2017Animal.preprocess(image, self._image_min_side, self._image_max_side) scale = torch.tensor(scale, dtype=torch.float) bboxes *= scale return image_id, image, scale, bboxes, labels
Example #3
Source File: CBP.py From fast-MPN-COV with MIT License | 6 votes |
def __init__(self, thresh=1e-8, projDim=8192, input_dim=512): super(CBP, self).__init__() self.thresh = thresh self.projDim = projDim self.input_dim = input_dim self.output_dim = projDim torch.manual_seed(1) self.h_ = [ torch.randint(0, self.output_dim, (self.input_dim,),dtype=torch.long), torch.randint(0, self.output_dim, (self.input_dim,),dtype=torch.long) ] self.weights_ = [ (2 * torch.randint(0, 2, (self.input_dim,)) - 1).float(), (2 * torch.randint(0, 2, (self.input_dim,)) - 1).float() ] indices1 = torch.cat((torch.arange(input_dim, dtype=torch.long).reshape(1, -1), self.h_[0].reshape(1, -1)), dim=0) indices2 = torch.cat((torch.arange(input_dim, dtype=torch.long).reshape(1, -1), self.h_[1].reshape(1, -1)), dim=0) self.sparseM = [ torch.sparse.FloatTensor(indices1, self.weights_[0], torch.Size([self.input_dim, self.output_dim])).to_dense(), torch.sparse.FloatTensor(indices2, self.weights_[1], torch.Size([self.input_dim, self.output_dim])).to_dense(), ]
Example #4
Source File: coco2017_person.py From easy-faster-rcnn.pytorch with MIT License | 6 votes |
def __getitem__(self, index: int) -> Tuple[str, Tensor, Tensor, Tensor, Tensor]: image_id = self._image_ids[index] annotation = self._image_id_to_annotation_dict[image_id] bboxes = [obj.bbox.tolist() for obj in annotation.objects] labels = [COCO2017Person.CATEGORY_TO_LABEL_DICT[COCO2017.LABEL_TO_CATEGORY_DICT[obj.label]] for obj in annotation.objects] # mapping from original `COCO2017` dataset bboxes = torch.tensor(bboxes, dtype=torch.float) labels = torch.tensor(labels, dtype=torch.long) image = Image.open(annotation.filename).convert('RGB') # for some grayscale images # random flip on only training mode if self._mode == COCO2017Person.Mode.TRAIN and random.random() > 0.5: image = ImageOps.mirror(image) bboxes[:, [0, 2]] = image.width - bboxes[:, [2, 0]] # index 0 and 2 represent `left` and `right` respectively image, scale = COCO2017Person.preprocess(image, self._image_min_side, self._image_max_side) scale = torch.tensor(scale, dtype=torch.float) bboxes *= scale return image_id, image, scale, bboxes, labels
Example #5
Source File: dqn.py From Pytorch-Project-Template with MIT License | 6 votes |
def select_action(self, state): """ The action selection function, it either uses the model to choose an action or samples one uniformly. :param state: current state of the model :return: """ if self.cuda: state = state.cuda() sample = random.random() eps_threshold = self.config.eps_start + (self.config.eps_start - self.config.eps_end) * math.exp( -1. * self.current_iteration / self.config.eps_decay) self.current_iteration += 1 if sample > eps_threshold: with torch.no_grad(): return self.policy_model(state).max(1)[1].view(1, 1) else: return torch.tensor([[random.randrange(2)]], device=self.device, dtype=torch.long)
Example #6
Source File: sumbt.py From ConvLab with MIT License | 6 votes |
def get_label_embedding(labels, max_seq_length, tokenizer, device): features = [] for label in labels: label_tokens = ["[CLS]"] + tokenizer.tokenize(label) + ["[SEP]"] label_token_ids = tokenizer.convert_tokens_to_ids(label_tokens) label_len = len(label_token_ids) label_padding = [0] * (max_seq_length - len(label_token_ids)) label_token_ids += label_padding assert len(label_token_ids) == max_seq_length features.append((label_token_ids, label_len)) all_label_token_ids = torch.tensor([f[0] for f in features], dtype=torch.long).to(device) all_label_len = torch.tensor([f[1] for f in features], dtype=torch.long).to(device) return all_label_token_ids, all_label_len
Example #7
Source File: coco2017.py From easy-faster-rcnn.pytorch with MIT License | 6 votes |
def __getitem__(self, index: int) -> Tuple[str, Tensor, Tensor, Tensor, Tensor]: image_id = self._image_ids[index] annotation = self._image_id_to_annotation_dict[image_id] bboxes = [obj.bbox.tolist() for obj in annotation.objects] labels = [obj.label for obj in annotation.objects] bboxes = torch.tensor(bboxes, dtype=torch.float) labels = torch.tensor(labels, dtype=torch.long) image = Image.open(annotation.filename).convert('RGB') # for some grayscale images # random flip on only training mode if self._mode == COCO2017.Mode.TRAIN and random.random() > 0.5: image = ImageOps.mirror(image) bboxes[:, [0, 2]] = image.width - bboxes[:, [2, 0]] # index 0 and 2 represent `left` and `right` respectively image, scale = COCO2017.preprocess(image, self._image_min_side, self._image_max_side) scale = torch.tensor(scale, dtype=torch.float) bboxes *= scale return image_id, image, scale, bboxes, labels
Example #8
Source File: trainer.py From treelstm.pytorch with MIT License | 6 votes |
def train(self, dataset): self.model.train() self.optimizer.zero_grad() total_loss = 0.0 indices = torch.randperm(len(dataset), dtype=torch.long, device='cpu') for idx in tqdm(range(len(dataset)), desc='Training epoch ' + str(self.epoch + 1) + ''): ltree, linput, rtree, rinput, label = dataset[indices[idx]] target = utils.map_label_to_target(label, dataset.num_classes) linput, rinput = linput.to(self.device), rinput.to(self.device) target = target.to(self.device) output = self.model(ltree, linput, rtree, rinput) loss = self.criterion(output, target) total_loss += loss.item() loss.backward() if idx % self.args.batchsize == 0 and idx > 0: self.optimizer.step() self.optimizer.zero_grad() self.epoch += 1 return total_loss / len(dataset) # helper function for testing
Example #9
Source File: coco2017_car.py From easy-faster-rcnn.pytorch with MIT License | 6 votes |
def __getitem__(self, index: int) -> Tuple[str, Tensor, Tensor, Tensor, Tensor]: image_id = self._image_ids[index] annotation = self._image_id_to_annotation_dict[image_id] bboxes = [obj.bbox.tolist() for obj in annotation.objects] labels = [COCO2017Car.CATEGORY_TO_LABEL_DICT[COCO2017.LABEL_TO_CATEGORY_DICT[obj.label]] for obj in annotation.objects] # mapping from original `COCO2017` dataset bboxes = torch.tensor(bboxes, dtype=torch.float) labels = torch.tensor(labels, dtype=torch.long) image = Image.open(annotation.filename).convert('RGB') # for some grayscale images # random flip on only training mode if self._mode == COCO2017Car.Mode.TRAIN and random.random() > 0.5: image = ImageOps.mirror(image) bboxes[:, [0, 2]] = image.width - bboxes[:, [2, 0]] # index 0 and 2 represent `left` and `right` respectively image, scale = COCO2017Car.preprocess(image, self._image_min_side, self._image_max_side) scale = torch.tensor(scale, dtype=torch.float) bboxes *= scale return image_id, image, scale, bboxes, labels
Example #10
Source File: predictor.py From ConvLab with MIT License | 6 votes |
def predict(self, state): example, kb = self.gen_example(state) feature = self.gen_feature(example) input_ids = torch.tensor([feature.input_ids], dtype=torch.long).to(self.device) input_masks = torch.tensor([feature.input_mask], dtype=torch.long).to(self.device) segment_ids = torch.tensor([feature.segment_ids], dtype=torch.long).to(self.device) with torch.no_grad(): logits = self.model(input_ids, segment_ids, input_masks, labels=None) logits = torch.sigmoid(logits) preds = (logits > 0.4).float() preds_numpy = preds.cpu().nonzero().squeeze().numpy() # for i in preds_numpy: # if i < 10: # print(Constants.domains[i], end=' ') # elif i < 17: # print(Constants.functions[i-10], end=' ') # else: # print(Constants.arguments[i-17], end=' ') # print() return preds, kb
Example #11
Source File: score_hlr_sampler.py From mmdetection with Apache License 2.0 | 6 votes |
def random_choice(gallery, num): """Randomly select some elements from the gallery. If `gallery` is a Tensor, the returned indices will be a Tensor; If `gallery` is a ndarray or list, the returned indices will be a ndarray. Args: gallery (Tensor | ndarray | list): indices pool. num (int): expected sample num. Returns: Tensor or ndarray: sampled indices. """ assert len(gallery) >= num is_tensor = isinstance(gallery, torch.Tensor) if not is_tensor: gallery = torch.tensor( gallery, dtype=torch.long, device=torch.cuda.current_device()) perm = torch.randperm(gallery.numel(), device=gallery.device)[:num] rand_inds = gallery[perm] if not is_tensor: rand_inds = rand_inds.cpu().numpy() return rand_inds
Example #12
Source File: random_sampler.py From mmdetection with Apache License 2.0 | 6 votes |
def random_choice(self, gallery, num): """Random select some elements from the gallery. If `gallery` is a Tensor, the returned indices will be a Tensor; If `gallery` is a ndarray or list, the returned indices will be a ndarray. Args: gallery (Tensor | ndarray | list): indices pool. num (int): expected sample num. Returns: Tensor or ndarray: sampled indices. """ assert len(gallery) >= num is_tensor = isinstance(gallery, torch.Tensor) if not is_tensor: gallery = torch.tensor( gallery, dtype=torch.long, device=torch.cuda.current_device()) perm = torch.randperm(gallery.numel(), device=gallery.device)[:num] rand_inds = gallery[perm] if not is_tensor: rand_inds = rand_inds.cpu().numpy() return rand_inds
Example #13
Source File: dee_model.py From Doc2EDAG with MIT License | 6 votes |
def forward(self, batch_elem_emb, sent_pos_ids=None): if sent_pos_ids is None: num_elem = batch_elem_emb.size(-2) sent_pos_ids = torch.arange( num_elem, dtype=torch.long, device=batch_elem_emb.device, requires_grad=False ) elif not isinstance(sent_pos_ids, torch.Tensor): sent_pos_ids = torch.tensor( sent_pos_ids, dtype=torch.long, device=batch_elem_emb.device, requires_grad=False ) batch_pos_emb = self.embedding(sent_pos_ids) out = batch_elem_emb + batch_pos_emb out = self.dropout(self.layer_norm(out)) return out
Example #14
Source File: stage.py From TVQAplus with MIT License | 6 votes |
def get_ts_loss(self, temporal_scores, ts_labels, answer_indices): """ Args: temporal_scores: (N, 5, Li, 2) ts_labels: dict(st=(N, ), ed=(N, )) answer_indices: (N, ) Returns: """ bsz = len(answer_indices) # compute loss ca_temporal_scores_st_ed = \ temporal_scores[torch.arange(bsz, dtype=torch.long), answer_indices] # (N, Li, 2) loss_st = self.temporal_criterion(ca_temporal_scores_st_ed[:, :, 0], ts_labels["st"]) loss_ed = self.temporal_criterion(ca_temporal_scores_st_ed[:, :, 1], ts_labels["ed"]) return (loss_st + loss_ed) / 2.
Example #15
Source File: made.py From naru with Apache License 2.0 | 6 votes |
def nll(self, logits, data): """Calculates -log p(data), given logits (the conditionals). Args: logits: [batch size, hidden] where hidden can either be sum(dom sizes), or emb_dims. data: [batch size, nin]. Returns: nll: [batch size]. """ if data.dtype != torch.long: data = data.long() nll = torch.zeros(logits.size()[0], device=logits.device) for i in range(self.nin): logits_i = self.logits_for_col(i, logits) nll += F.cross_entropy(logits_i, data[:, i], reduction='none') return nll
Example #16
Source File: utils.py From ScenarioMeta with MIT License | 6 votes |
def forward(self, nodes): if torch.is_tensor(nodes): if self.neighbor_dict is not None: neighbors = [random.sample(self.neighbor_dict[idx.item()], self.max_degree) if len( self.neighbor_dict[idx.item()]) > self.max_degree else self.neighbor_dict[idx.item()] for idx in nodes] else: if self.neighbor_dict is not None: neighbors = [random.sample(self.neighbor_dict[idx], self.max_degree) if len( self.neighbor_dict[idx]) > self.max_degree else self.neighbor_dict[idx] for idx in nodes] nodes = torch.tensor(nodes, dtype=torch.long, device=self.flag.device) if self.neighbor_dict is not None: degrees = torch.tensor(list(map(len, neighbors)), dtype=torch.long, device=self.flag.device) neighbors = list2tensor(neighbors, self.padding_idx, device=self.flag.device) return nodes, neighbors, degrees else: return (nodes,)
Example #17
Source File: modules.py From ScenarioMeta with MIT License | 6 votes |
def forward(self, query_users, target_items, auxiliary_items=None): only_target = False if auxiliary_items is None: only_target = True auxiliary_items = [ random.choice(self.source_ratings[user_id.item()]) if len( self.source_ratings[user_id.item()]) > 0 else self.item_padding_idx for user_id in query_users[0]] auxiliary_items = (torch.tensor(auxiliary_items, dtype=torch.long, device=query_users[0].device),) query_users = list(map(lambda x: x.expand(target_items[0].size(0)), query_users)) auxiliary_items = list(map(lambda x: x.expand(target_items[0].size(0)), auxiliary_items)) query_users = self.useritem_embeds(*query_users, is_user=True) target_items, auxiliary_items = self.useritem_embeds(*target_items, is_user=False), self.useritem_embeds( *auxiliary_items, is_user=False) target_x = torch.cat((*query_users, *target_items), dim=1) auxiliary_x = torch.cat((*query_users, *auxiliary_items), dim=1) for target_layer, auxiliary_layer, transfer_layer in zip(self.target_layers, self.auxiliary_layers, self.transfer_layers): new_target_x = target_layer(target_x) + transfer_layer(auxiliary_x) new_auxiliary_x = auxiliary_layer(auxiliary_x) + transfer_layer(target_x) target_x, auxiliary_x = new_target_x, new_auxiliary_x target_x, auxiliary_x = torch.relu_(target_x), torch.relu_(auxiliary_x) if only_target: return self.target_output(target_x).squeeze(-1) else: return self.target_output(target_x).squeeze(-1), self.auxiliary_output(auxiliary_x).squeeze(-1)
Example #18
Source File: transformer.py From naru with Apache License 2.0 | 6 votes |
def nll(self, logits, data): """Calculates -log p(data), given logits (the conditionals). Args: logits: [batch size, ncols+1, d_model]. data: [batch size, ncols]. Returns: nll: [batch size]. """ if data.dtype != torch.long: data = data.long() nll = torch.zeros(logits.size()[0], device=logits.device) for i in range(self.nin): logits_i = self.logits_for_col(i, logits) ce = F.cross_entropy(logits_i, data[:, i], reduction='none') nll += ce return nll
Example #19
Source File: ner_model.py From Doc2EDAG with MIT License | 6 votes |
def forward(self, batch_token_ids): batch_size, sent_len = batch_token_ids.size() device = batch_token_ids.device batch_pos_ids = torch.arange( sent_len, dtype=torch.long, device=device, requires_grad=False ) batch_pos_ids = batch_pos_ids.unsqueeze(0).expand_as(batch_token_ids) batch_token_emb = self.token_embedding(batch_token_ids) batch_pos_emb = self.pos_embedding(batch_pos_ids) batch_token_emb = batch_token_emb + batch_pos_emb batch_token_out = self.layer_norm(batch_token_emb) batch_token_out = self.dropout(batch_token_out) return batch_token_out
Example #20
Source File: my_data.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def get_val_data(self, batch_size=8, device="cpu"): keys = random.sample(self.val_dict.keys(), batch_size) texts = [self.val_dict[k][0] for k in keys] labels = [self.val_dict[k][1] for k in keys] maxlen = max(len(s) for s in texts) texts = [s.ljust(maxlen, " ") for s in texts] labels = [ numpy.pad(a, (0, maxlen - len(a)), mode="constant", constant_values=0) for a in labels ] text_tensor = torch.zeros(maxlen, batch_size, dtype=torch.long) for i, text in enumerate(texts): text_tensor[:, i] = torch.LongTensor([VOCAB.find(c) for c in text]) truth_tensor = torch.zeros(maxlen, batch_size, dtype=torch.long) for i, label in enumerate(labels): truth_tensor[:, i] = torch.LongTensor(label) return keys, text_tensor.to(self.device), truth_tensor.to(self.device)
Example #21
Source File: ner_task.py From Doc2EDAG with MIT License | 5 votes |
def convert_ner_features_to_dataset(ner_features): all_input_ids = torch.tensor([f.input_ids for f in ner_features], dtype=torch.long) # very important to use the mask type of uint8 to support advanced indexing all_input_masks = torch.tensor([f.input_masks for f in ner_features], dtype=torch.uint8) all_segment_ids = torch.tensor([f.segment_ids for f in ner_features], dtype=torch.long) all_label_ids = torch.tensor([f.label_ids for f in ner_features], dtype=torch.long) all_seq_len = torch.tensor([f.seq_len for f in ner_features], dtype=torch.long) ner_tensor_dataset = TensorDataset(all_input_ids, all_input_masks, all_segment_ids, all_label_ids, all_seq_len) return ner_tensor_dataset
Example #22
Source File: loss.py From Fast_Seg with Apache License 2.0 | 5 votes |
def forward(self, pred, target): b, c, h, w = pred.size() target = target.view(-1) valid_mask = target.ne(self.ignore_label) target = target * valid_mask.long() num_valid = valid_mask.sum() prob = F.softmax(pred, dim=1) prob = (prob.transpose(0, 1)).reshape(c, -1) if self.min_kept > num_valid: print('Labels: {}'.format(num_valid)) elif num_valid > 0: prob = prob.masked_fill_(1 - valid_mask, 1) mask_prob = prob[ target, torch.arange(len(target), dtype=torch.long)] threshold = self.thresh if self.min_kept > 0: _, index = mask_prob.sort() threshold_index = index[min(len(index), self.min_kept) - 1] if mask_prob[threshold_index] > self.thresh: threshold = mask_prob[threshold_index] kept_mask = mask_prob.le(threshold) target = target * kept_mask.long() valid_mask = valid_mask * kept_mask target = target.masked_fill_(1 - valid_mask, self.ignore_label) target = target.view(b, h, w) return self.criterion(pred, target)
Example #23
Source File: meta.py From ScenarioMeta with MIT License | 5 votes |
def forward(self, support_pairs, support_users, support_items, support_candidates, with_attr=False): self.loss = [] for module in self.model.modules(): if isinstance(module, nn.Linear): module.reset_parameters() parameters = list(filter(lambda p: p.requires_grad, self.model.parameters())) optimizer = torch.optim.Adam(parameters, lr=self.lr) user_set = set(map(lambda x: x[0], support_pairs.tolist())) source_data = {user_id: self.source_ratings[user_id] for user_id in user_set} for batch_id, data in enumerate( self.transfer_generator(source_data, support_pairs.tolist(), self.source_candidates, support_candidates , self.item_padding_idx, self.batch_size, self.negative_ratio)): data = list(map(lambda x: torch.tensor(x, dtype=torch.long, device=support_pairs.device), data)) target_users, target_items, source_items, target_labels, source_labels = data target_values, source_values = self.model((target_users,), (target_items,), (source_items,)) loss = self.criterion(target_values, target_labels.type(torch.float)) + self.criterion(source_values, source_labels.type( torch.float)) for module in self.model.transfer_layers.modules(): if isinstance(module, nn.Linear): loss += self.sparse_ratio * torch.abs(module.weight).sum() self.loss.append(loss.item()) optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_value_(parameters, 0.25) optimizer.step() if batch_id > self.step: break
Example #24
Source File: BeliefTrackerSlotQueryMultiSlot.py From ConvLab with MIT License | 5 votes |
def initialize_slot_value_lookup(self, label_ids, slot_ids): self.sv_encoder.eval() # Slot encoding slot_type_ids = torch.zeros(slot_ids.size(), dtype=torch.long).to(self.device) slot_mask = slot_ids > 0 hid_slot, _ = self.sv_encoder(slot_ids.view(-1, self.max_label_length), slot_type_ids.view(-1, self.max_label_length), slot_mask.view(-1, self.max_label_length), output_all_encoded_layers=False) hid_slot = hid_slot[:, 0, :] hid_slot = hid_slot.detach() self.slot_lookup = nn.Embedding.from_pretrained(hid_slot, freeze=True) for s, label_id in enumerate(label_ids): label_type_ids = torch.zeros(label_id.size(), dtype=torch.long).to(self.device) label_mask = label_id > 0 hid_label, _ = self.sv_encoder(label_id.view(-1, self.max_label_length), label_type_ids.view(-1, self.max_label_length), label_mask.view(-1, self.max_label_length), output_all_encoded_layers=False) hid_label = hid_label[:, 0, :] hid_label = hid_label.detach() self.value_lookup[s] = nn.Embedding.from_pretrained(hid_label, freeze=True) self.value_lookup[s].padding_idx = -1 print("Complete initialization of slot and value lookup")
Example #25
Source File: bert_basic_layer.py From mrc-for-flat-nested-ner with Apache License 2.0 | 5 votes |
def forward(self, input_ids, token_type_ids=None): seq_length = input_ids.size(1) position_ids = torch.arange(seq_length, dtype=torch.long, device=input_ids.device) position_ids = position_ids.unsqueeze(0).expand_as(input_ids) if token_type_ids is None: token_type_ids = torch.zeros_like(input_ids) words_embeddings = self.word_embeddings(input_ids) position_embeddings = self.position_embeddings(position_ids) token_type_embeddings = self.token_type_embeddings(token_type_ids) embeddings = words_embeddings + position_embeddings + token_type_embeddings embeddings = self.LayerNorm(embeddings) embeddings = self.dropout(embeddings) return embeddings
Example #26
Source File: loss_funcs.py From mrc-for-flat-nested-ner with Apache License 2.0 | 5 votes |
def cross_entropy_loss(): # loss loss = nn.CrossEntropyLoss() input = torch.randn(3, 5, requires_grad=True) target = torch.empty(3, dtype=torch.long).random_(5) output = loss(input, target) output.backward()
Example #27
Source File: ner_model.py From Doc2EDAG with MIT License | 5 votes |
def old_forward(self, input_ids, input_masks, token_type_ids=None, label_ids=None, eval_flag=False, eval_for_metric=True): """Assume input size [batch_size, seq_len]""" if input_masks.dtype != torch.uint8: input_masks = input_masks == 1 enc_seq_out, _ = self.bert(input_ids, token_type_ids=token_type_ids, attention_mask=input_masks, output_all_encoded_layers=False) # [batch_size, seq_len, hidden_size] enc_seq_out = self.dropout(enc_seq_out) # [batch_size, seq_len, num_entity_labels] seq_logits = self.classifier(enc_seq_out) if eval_flag: # if for evaluation purpose if label_ids is None: raise Exception('Cannot do evaluation without label info') else: if eval_for_metric: batch_metrics = produce_ner_batch_metrics(seq_logits, label_ids, input_masks) return batch_metrics else: seq_logp = F.log_softmax(seq_logits, dim=-1) seq_pred = seq_logp.argmax(dim=-1, keepdim=True) # [batch_size, seq_len, 1] seq_gold = label_ids.unsqueeze(-1) # [batch_size, seq_len, 1] seq_mask = input_masks.unsqueeze(-1).long() # [batch_size, seq_len, 1] seq_pred_gold_mask = torch.cat([seq_pred, seq_gold, seq_mask], dim=-1) # [batch_size, seq_len, 3] return seq_pred_gold_mask elif label_ids is not None: # if has label_ids, calculate the loss # [num_valid_token, num_entity_labels] batch_logits = seq_logits[input_masks, :] # [num_valid_token], lid \in {0,..., num_entity_labels-1} batch_labels = label_ids[input_masks] loss = F.cross_entropy(batch_logits, batch_labels) return loss, enc_seq_out else: # just reture seq_pred_logps return F.log_softmax(seq_logits, dim=-1), enc_seq_out
Example #28
Source File: dee_model.py From Doc2EDAG with MIT License | 5 votes |
def forward(self, batch_mention_emb, mention_type_ids): if not isinstance(mention_type_ids, torch.Tensor): mention_type_ids = torch.tensor( mention_type_ids, dtype=torch.long, device=batch_mention_emb.device, requires_grad=False ) batch_mention_type_emb = self.embedding(mention_type_ids) out = batch_mention_emb + batch_mention_type_emb out = self.dropout(self.layer_norm(out)) return out
Example #29
Source File: ner_task.py From Doc2EDAG with MIT License | 5 votes |
def get_total_prediction(self, eval_dataset): self.logging('='*20 + 'Get Total Prediction' + '='*20) total_pred_gold_mask = self.base_eval( eval_dataset, get_ner_pred_on_batch, reduce_info_type='none' ) # torch.Tensor(dtype=torch.long, device='cpu') # size = [batch_size, seq_len, 3] # value = [[(pred_label, gold_label, token_mask), ...], ...] return total_pred_gold_mask
Example #30
Source File: dee_helper.py From Doc2EDAG with MIT License | 5 votes |
def __init__(self, guid, ex_idx, doc_token_id_mat, doc_token_mask_mat, doc_token_label_mat, span_token_ids_list, span_dranges_list, event_type_labels, event_arg_idxs_objs_list, valid_sent_num=None): self.guid = guid self.ex_idx = ex_idx # example row index, used for backtracking self.valid_sent_num = valid_sent_num # directly set tensor for dee feature to save memory # self.doc_token_id_mat = doc_token_id_mat # self.doc_token_mask_mat = doc_token_mask_mat # self.doc_token_label_mat = doc_token_label_mat self.doc_token_ids = torch.tensor(doc_token_id_mat, dtype=torch.long) self.doc_token_masks = torch.tensor(doc_token_mask_mat, dtype=torch.uint8) # uint8 for mask self.doc_token_labels = torch.tensor(doc_token_label_mat, dtype=torch.long) # sorted by the first drange tuple # [(token_id, ...), ...] # span_idx -> span_token_id tuple self.span_token_ids_list = span_token_ids_list # [[(sent_idx, char_s, char_e), ...], ...] # span_idx -> [drange tuple, ...] self.span_dranges_list = span_dranges_list # [event_type_label, ...] # length = the total number of events to be considered # event_type_label \in {0, 1}, 0: no 1: yes self.event_type_labels = event_type_labels # event_type is denoted by the index of event_type_labels # event_type_idx -> event_obj_idx -> event_arg_idx -> span_idx # if no event objects, event_type_idx -> None self.event_arg_idxs_objs_list = event_arg_idxs_objs_list # event_type_idx -> event_field_idx -> pre_path -> {span_idx, ...} # pre_path is tuple of span_idx self.event_idx2field_idx2pre_path2cur_span_idx_set = self.build_dag_info(self.event_arg_idxs_objs_list) # event_type_idx -> key_sent_idx_set, used for key-event sentence detection self.event_idx2key_sent_idx_set, self.doc_sent_labels = self.build_key_event_sent_info()