Spaces:
Sleeping
Sleeping
File size: 11,341 Bytes
03f6091 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# -*- coding: utf-8 -*-
r"""
Polos Ranker Model
======================
The goal of this model is to rank good translations closer to the reference and source text
and bad translations further by a small margin.
https://pytorch.org/docs/stable/nn.html#tripletmarginloss
"""
from argparse import Namespace
from typing import Dict, List, Tuple, Union
import torch
import torch.nn.functional as F
from tqdm import tqdm
from polos.models.ranking.ranking_base import RankingBase
from polos.models.utils import move_to_cuda
from torchnlp.utils import collate_tensors
class PolosRanker(RankingBase): # extends ptl.LightningModule
"""
Polos Ranker class that uses a pretrained encoder to extract features
from the sequences and then passes those features through a Triplet Margin Loss.
:param hparams: Namespace containing the hyperparameters.
"""
def __init__(self, hparams: Namespace) -> None:
super().__init__(hparams)
def compute_metrics(self, outputs: List[Dict[str, torch.Tensor]]) -> dict:
""" Computes WMT19 shared task kendall tau like metric. """
distance_pos, distance_neg = [], []
for minibatch in outputs:
minibatch = minibatch["val_prediction"]
src_embedding = minibatch["src_sentemb"]
ref_embedding = minibatch["ref_sentemb"]
pos_embedding = minibatch["pos_sentemb"]
neg_embedding = minibatch["neg_sentemb"]
distance_src_pos = F.pairwise_distance(pos_embedding, src_embedding)
distance_ref_pos = F.pairwise_distance(pos_embedding, ref_embedding)
harmonic_distance_pos = (2 * distance_src_pos * distance_ref_pos) / (
distance_src_pos + distance_ref_pos
)
distance_pos.append(harmonic_distance_pos)
distance_src_neg = F.pairwise_distance(neg_embedding, src_embedding)
distance_ref_neg = F.pairwise_distance(neg_embedding, ref_embedding)
harmonic_distance_neg = (2 * distance_src_neg * distance_ref_neg) / (
distance_src_neg + distance_ref_neg
)
distance_neg.append(harmonic_distance_neg)
return {
"kendall": self.metrics.compute(
torch.cat(distance_pos), torch.cat(distance_neg)
)
}
def compute_loss(self, model_out: Dict[str, torch.Tensor], *args) -> torch.Tensor:
"""
# forwardの結果がmodel_outに入っているのでlossを計算
Computes Triplet Margin Loss for both the reference and the source.
:param model_out: model specific output with src_anchor, ref_anchor, pos and neg
sentence embeddings.
"""
# 参考
# "src_sentemb": self.get_sentence_embedding(src_tokens, src_lengths),
# "ref_sentemb": self.get_sentence_embedding(ref_tokens, ref_lengths),
# "pos_sentemb": self.get_sentence_embedding(pos_tokens, pos_lengths),
# "neg_sentemb": self.get_sentence_embedding(neg_tokens, neg_lengths),
ref_anchor = model_out["ref_sentemb"]
src_anchor = model_out["src_sentemb"]
positive = model_out["pos_sentemb"]
negative = model_out["neg_sentemb"]
return self.loss(src_anchor, positive, negative) + self.loss(
ref_anchor, positive, negative
)
def predict(
self,
samples: Dict[str, str],
cuda: bool = False,
show_progress: bool = False,
batch_size: int = -1,
) -> (Dict[str, Union[str, float]], List[float]):
"""Function that runs a model prediction,
:param samples: List of dictionaries with 'mt' and 'ref' keys.
:param cuda: Flag that runs inference using 1 single GPU.
:param show_progress: Flag to show progress during inference of multiple examples.
:para batch_size: Batch size used during inference. By default uses the same batch size used during training.
:return: Dictionary with model outputs
"""
if self.training:
self.eval()
if cuda and torch.cuda.is_available():
self.to("cuda")
batch_size = self.hparams.batch_size if batch_size < 1 else batch_size
with torch.no_grad():
batches = [
samples[i : i + batch_size] for i in range(0, len(samples), batch_size)
]
model_inputs = []
if show_progress:
pbar = tqdm(
total=len(batches), desc="Preparing batches....", dynamic_ncols=True
)
for batch in batches:
model_inputs.append(self.prepare_sample(batch, inference=True))
if show_progress:
pbar.update(1)
if show_progress:
pbar.close()
if show_progress:
pbar = tqdm(
total=len(batches), desc="Scoring hypothesis...", dynamic_ncols=True
)
distance_weighted, distance_src, distance_ref = [], [], []
for k, model_input in enumerate(model_inputs):
src_input, mt_input, ref_input, alt_input = model_input
if cuda and torch.cuda.is_available():
src_embeddings = self.get_sentence_embedding(
**move_to_cuda(src_input)
)
mt_embeddings = self.get_sentence_embedding(
**move_to_cuda(mt_input)
)
ref_embeddings = self.get_sentence_embedding(
**move_to_cuda(ref_input)
)
ref_distances = F.pairwise_distance(
mt_embeddings, ref_embeddings
).cpu()
src_distances = F.pairwise_distance(
mt_embeddings, src_embeddings
).cpu()
# When 2 references are given the distance to the reference is the Min between
# both references.
if alt_input is not None:
alt_embeddings = self.get_sentence_embedding(
**move_to_cuda(alt_input)
)
alt_distances = F.pairwise_distance(
mt_embeddings, alt_embeddings
).cpu()
ref_distances = torch.stack([ref_distances, alt_distances])
ref_distances = ref_distances.min(dim=0).values
else:
src_embeddings = self.get_sentence_embedding(**src_input)
mt_embeddings = self.get_sentence_embedding(**mt_input)
ref_embeddings = self.get_sentence_embedding(**ref_input)
ref_distances = F.pairwise_distance(mt_embeddings, ref_embeddings)
src_distances = F.pairwise_distance(mt_embeddings, src_embeddings)
# Harmonic mean between the distances:
distances = (2 * ref_distances * src_distances) / (
ref_distances + src_distances
)
src_distances = ref_distances.numpy().tolist()
ref_distances = ref_distances.numpy().tolist()
distances = distances.numpy().tolist()
for i in range(len(distances)):
distance_weighted.append(1 / (1 + distances[i]))
distance_src.append(1 / (1 + src_distances[i]))
distance_ref.append(1 / (1 + ref_distances[i]))
if show_progress:
pbar.update(1)
if show_progress:
pbar.close()
assert len(distance_weighted) == len(samples)
scores = []
for i in range(len(samples)):
scores.append(distance_weighted[i])
samples[i]["predicted_score"] = scores[-1]
samples[i]["reference_distance"] = distance_ref[i]
samples[i]["source_distance"] = distance_src[i]
return samples, scores
def prepare_sample(
self, sample: List[Dict[str, Union[str, float]]], inference: bool = False
) -> Union[Tuple[Dict[str, torch.Tensor], None], List[Dict[str, torch.Tensor]]]:
"""
Function that prepares a sample to input the model.
:param sample: list of dictionaries.
:param inference: If set to to False, then the model expects
a MT and reference instead of anchor, pos, and neg segments.
:return: Tuple with a dictionary containing the model inputs and None OR List
with source, MT and reference tokenized and vectorized.
"""
sample = collate_tensors(sample)
if inference:
src_inputs = self.encoder.prepare_sample(sample["src"])
mt_inputs = self.encoder.prepare_sample(sample["mt"])
ref_inputs = self.encoder.prepare_sample(sample["ref"])
alt_inputs = (
self.encoder.prepare_sample(sample["alt"]) if "alt" in sample else None
)
return src_inputs, mt_inputs, ref_inputs, alt_inputs
ref_inputs = self.encoder.prepare_sample(sample["ref"])
src_inputs = self.encoder.prepare_sample(sample["src"])
pos_inputs = self.encoder.prepare_sample(sample["pos"])
neg_inputs = self.encoder.prepare_sample(sample["neg"])
ref_inputs = {"ref_" + k: v for k, v in ref_inputs.items()}
src_inputs = {"src_" + k: v for k, v in src_inputs.items()}
pos_inputs = {"pos_" + k: v for k, v in pos_inputs.items()}
neg_inputs = {"neg_" + k: v for k, v in neg_inputs.items()}
return {**ref_inputs, **src_inputs, **pos_inputs, **neg_inputs}, torch.empty(0)
def forward(
self,
src_tokens: torch.tensor,
ref_tokens: torch.tensor,
pos_tokens: torch.tensor,
neg_tokens: torch.tensor,
src_lengths: torch.tensor,
ref_lengths: torch.tensor,
pos_lengths: torch.tensor,
neg_lengths: torch.tensor,
**kwargs
) -> Dict[str, torch.Tensor]:
"""
Function that encodes the anchor, positive samples and negative samples
and returns embeddings for the triplet.
:param src_tokens: anchor sequences [batch_size x anchor_seq_len]
:param ref_tokens: anchor sequences [batch_size x anchor_seq_len]
:param pos_tokens: positive sequences [batch_size x pos_seq_len]
:param neg_tokens: negative sequences [batch_size x neg_seq_len]
:param src_lengths: anchor lengths [batch_size]
:param ref_lengths: anchor lengths [batch_size]
:param pos_lengths: positive lengths [batch_size]
:param neg_lengths: negative lengths [batch_size]
:return: Dictionary with model outputs to be passed to the loss function.
"""
return {
"src_sentemb": self.get_sentence_embedding(src_tokens, src_lengths),
"ref_sentemb": self.get_sentence_embedding(ref_tokens, ref_lengths),
"pos_sentemb": self.get_sentence_embedding(pos_tokens, pos_lengths),
"neg_sentemb": self.get_sentence_embedding(neg_tokens, neg_lengths),
}
|