def compute_loss(self, docs: Sequence[Doc], golds: Sequence[GoldParse], outputs: torch.Tensor) -> None: self.require_model() targets = outputs.new_tensor( [[gold.cats[label] for label in self.labels] for gold in golds]) loss = F.binary_cross_entropy_with_logits(outputs, targets) add_loss_to_docs(docs, loss)
def compute_loss(self, docs: Sequence[Doc], golds: Sequence[GoldParse], outputs: torch.Tensor) -> None: target = self._create_target_from_docs_golds(docs, golds, outputs) loss = F.cross_entropy(outputs.transpose(1, 2), target, ignore_index=self.ignore_label_index) add_loss_to_docs(docs, loss)
def compute_loss(self, docs: Sequence[Doc], golds: Sequence[GoldParse], outputs: torch.Tensor) -> None: self.require_model() targets = self.golds_to_tensor(golds) weight = self.label_weights.to(device=self.device) # type: ignore loss = F.cross_entropy(outputs, targets, weight=weight) add_loss_to_docs(docs, loss)
def train(): docs, golds = nlp._format_docs_and_golds(texts, labels) before = sum_param(pipe.optim_parameters()) nlp._update_pipes(docs, golds) h = get_last_hidden_state_from_docs(docs) loss = h.sum() + torch.tensor(0.0, requires_grad=True) add_loss_to_docs(docs, loss) nlp._update_params(docs, optimizer) return abs(before - sum_param(pipe.optim_parameters()))
def update(self, docs: List[Doc], golds: List[GoldParse], **kwargs): # type: ignore assert isinstance(docs, list) self.require_model() logits = self.model(get_last_hidden_state_from_docs(docs)) target = self._create_target_from_docs_golds(docs, golds, logits) loss = F.cross_entropy(logits.transpose(1, 2), target, ignore_index=self.ignore_label_index) add_loss_to_docs(docs, loss)
def update( # type: ignore self, docs: List[Doc], golds: Iterable[GoldParse], **kwargs): self.require_model() self.model.train() x = get_last_hidden_state_from_docs(docs) preds: torch.Tensor = self.model(x) preds = preds.view(-1, self.model.config.vocab_size) targets = (get_maskedlm_labels(docs).view(-1)[:len(preds)].to( device=self.device)) loss = F.cross_entropy(preds, targets, ignore_index=-1) add_loss_to_docs(docs, loss)
def update( # type: ignore self, docs: List[Doc], golds: Iterable[GoldParse], **kwargs): assert isinstance(docs, list) self.require_model() self.model.train() x = get_last_hidden_state_from_docs(docs) logits = self.model(x) targets = self.golds_to_tensor(golds) weight = self.label_weights.to(device=self.device) # type: ignore loss = F.cross_entropy(logits, targets, weight=weight) add_loss_to_docs(docs, loss)