File size: 23,135 Bytes
6ade49b |
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Literal
import torch
import numpy
from transformers import Trainer, PreTrainedModel, RobertaForSequenceClassification, BatchEncoding, RobertaConfig, \
EvalPrediction
from transformers.modeling_outputs import SequenceClassifierOutput, BaseModelOutput
from loguru import logger
def val_nov_loss(is_val: torch.Tensor, should_val: torch.Tensor, is_nov: torch.Tensor, should_nov: torch.Tensor,
weights: Optional[torch.Tensor] = None, reduce: bool = True) -> torch.Tensor:
if weights is None:
weights = torch.ones_like(should_val)
logger.debug("No weights-vector - assume, all {} samples should count equally", weights.size())
loss_validity = torch.pow(is_val - torch.where(torch.isnan(should_val), is_val, should_val), 2)
loss_novelty = torch.pow(is_nov - torch.where(torch.isnan(should_nov), is_nov, should_nov), 2)
logger.trace("loss_validity: {} / loss_novelty: {}", loss_validity, loss_novelty)
loss = (.5 * (loss_validity * loss_novelty) + .5 * loss_validity + .5 * loss_novelty) * weights
return torch.mean(loss) if reduce else loss
def val_nov_metric(eval_data: EvalPrediction) -> Dict[str, float]:
if isinstance(eval_data.predictions, Tuple) and isinstance(eval_data.label_ids, Tuple) \
or min(len(eval_data.predictions), len(eval_data.label_ids)) >= 2:
logger.trace("Format is as processable ({}: {})", type(eval_data.predictions), len(eval_data.predictions))
if len(eval_data.predictions) != 2:
logger.debug("We expect 2 tuples, but get {}: {}", len(eval_data.predictions), eval_data.predictions)
is_validity = eval_data.predictions[-2]
should_validity = eval_data.label_ids[-2]
is_novelty = eval_data.predictions[-1]
should_novelty = eval_data.label_ids[-1]
return _val_nov_metric(is_validity=is_validity, should_validity=should_validity,
is_novelty=is_novelty, should_novelty=should_novelty)
else:
logger.warning("This metric can't return all metrics properly, "
"because validity and novelty are not distinguishable")
return {
"size": numpy.size(eval_data.label_ids),
"mse_validity": numpy.mean((eval_data.predictions-eval_data.label_ids) ** 2),
"mse_novelty": numpy.mean((eval_data.predictions-eval_data.label_ids) ** 2),
"error_validity": numpy.mean(numpy.abs(eval_data.predictions-eval_data.label_ids)),
"error_novelty": numpy.mean(numpy.abs(eval_data.predictions-eval_data.label_ids)),
"approximately_hits_validity": -1,
"approximately_hits_novelty": -1,
"exact_hits_validity": -1,
"exact_hits_novelty": -1,
"approximately_hits": numpy.count_nonzero(
numpy.where(numpy.abs(eval_data.predictions-eval_data.label_ids) < .2, 1, 0)
) / numpy.size(eval_data.predictions),
"exact_hits": numpy.count_nonzero(
numpy.where(numpy.abs(eval_data.predictions-eval_data.label_ids) < .05, 1, 0)
) / numpy.size(eval_data.predictions),
"accuracy_validity": -1,
"accuracy_novelty": -1,
"accuracy": -1,
"f1_validity": -1,
"f1_novelty": -1,
"f1_macro": -1,
"never_predicted_classes": 4
}
def _val_nov_metric(is_validity: numpy.ndarray, should_validity: numpy.ndarray,
is_novelty: numpy.ndarray, should_novelty: numpy.ndarray) -> Dict[str, float]:
ret = {
"size": numpy.size(is_validity),
"mse_validity": numpy.mean((is_validity - should_validity) ** 2),
"mse_novelty": numpy.mean((is_novelty - should_novelty) ** 2),
"error_validity": numpy.mean(numpy.abs(is_validity - should_validity)),
"error_novelty": numpy.mean(numpy.abs(is_novelty - should_novelty)),
"approximately_hits_validity": numpy.sum(
numpy.where(numpy.abs(is_validity - should_validity) < .2, 1, 0)) / numpy.size(is_validity),
"approximately_hits_novelty": numpy.sum(
numpy.where(numpy.abs(is_novelty - should_novelty) < .2, 1, 0)) / numpy.size(is_novelty),
"exact_hits_validity": numpy.sum(
numpy.where(numpy.abs(is_validity - should_validity) < .05, 1, 0)) / numpy.size(is_validity),
"exact_hits_novelty": numpy.sum(
numpy.where(numpy.abs(is_novelty - should_novelty) < .05, 1, 0)) / numpy.size(is_novelty),
"approximately_hits": numpy.sum(
numpy.where(numpy.abs(is_validity - should_validity) + numpy.abs(is_novelty - should_novelty) < .25, 1, 0)
) / numpy.size(is_validity),
"exact_hits": numpy.sum(
numpy.where(numpy.abs(is_validity - should_validity) + numpy.abs(is_novelty - should_novelty) < .05, 1, 0)
) / numpy.size(is_validity),
"accuracy_validity": numpy.sum(numpy.where(
numpy.any(numpy.stack([
numpy.all(numpy.stack([is_validity >= .5, should_validity >= .5]), axis=0),
numpy.all(numpy.stack([is_validity < .5, should_validity < .5]), axis=0)
]), axis=0),
1, 0
)) / numpy.size(is_validity),
"accuracy_novelty": numpy.sum(numpy.where(
numpy.any(numpy.stack([
numpy.all(numpy.stack([is_novelty >= .5, should_novelty >= .5]), axis=0),
numpy.all(numpy.stack([is_novelty < .5, should_novelty < .5]), axis=0)
]), axis=0),
1, 0
)) / numpy.size(is_validity),
"accuracy": numpy.sum(numpy.where(
numpy.any(numpy.stack([
numpy.all(numpy.stack([is_validity >= .5, should_validity >= .5, is_novelty >= .5, should_novelty >= .5]),
axis=0),
numpy.all(numpy.stack([is_validity >= .5, should_validity >= .5, is_novelty < .5, should_novelty < .5]),
axis=0),
numpy.all(numpy.stack([is_validity < .5, should_validity < .5, is_novelty >= .5, should_novelty >= .5]),
axis=0),
numpy.all(numpy.stack([is_validity < .5, should_validity < .5, is_novelty < .5, should_novelty < .5]),
axis=0)
]), axis=0),
1, 0
)) / numpy.size(is_validity),
"never_predicted_classes": sum(
[int(numpy.all(numpy.abs(is_validity-validity) < .5) and numpy.all(numpy.abs(is_novelty-novelty) < .5))
for validity, novelty in [(1, 1), (1, 0), (0, 1), (0, 0)]]
)
}
ret_base_help = {
"true_positive_validity": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity >= .5, should_validity >= .5]), axis=0),
1, 0)),
"true_negative_validity": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity < .5, should_validity < .5]), axis=0),
1, 0)),
"true_positive_novelty": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_novelty >= .5, should_novelty >= .5]), axis=0),
1, 0)),
"true_negative_novelty": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_novelty < .5, should_novelty < .5]), axis=0),
1, 0)),
"true_positive_valid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity >= .5, is_novelty >= .5,
should_validity >= .5, should_novelty >= .5]), axis=0),
1, 0)),
"true_positive_nonvalid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity < .5, is_novelty >= .5,
should_validity < .5, should_novelty >= .5]), axis=0),
1, 0)),
"true_positive_valid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity >= .5, is_novelty < .5,
should_validity >= .5, should_novelty < .5]), axis=0),
1, 0)),
"true_positive_nonvalid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity < .5, is_novelty < .5,
should_validity < .5, should_novelty < .5]), axis=0),
1, 0)),
"classified_positive_validity": numpy.sum(numpy.where(is_validity >= .5, 1, 0)),
"classified_negative_validity": numpy.sum(numpy.where(is_validity < .5, 1, 0)),
"classified_positive_novelty": numpy.sum(numpy.where(is_novelty >= .5, 1, 0)),
"classified_negative_novelty": numpy.sum(numpy.where(is_novelty < .5, 1, 0)),
"classified_positive_valid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity >= .5, is_novelty >= .5]), axis=0),
1, 0)),
"classified_positive_nonvalid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity < .5, is_novelty >= .5]), axis=0),
1, 0)),
"classified_positive_valid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity >= .5, is_novelty < .5]), axis=0),
1, 0)),
"classified_positive_nonvalid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([is_validity < .5, is_novelty < .5]), axis=0),
1, 0)),
"indeed_positive_validity": numpy.sum(numpy.where(should_validity >= .5, 1, 0)),
"indeed_negative_validity": numpy.sum(numpy.where(should_validity < .5, 1, 0)),
"indeed_positive_novelty": numpy.sum(numpy.where(should_novelty >= .5, 1, 0)),
"indeed_negative_novelty": numpy.sum(numpy.where(should_novelty < .5, 1, 0)),
"indeed_positive_valid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([should_validity >= .5, should_novelty >= .5]), axis=0),
1, 0)),
"indeed_positive_nonvalid_novel": numpy.sum(numpy.where(
numpy.all(numpy.stack([should_validity < .5, should_novelty >= .5]), axis=0),
1, 0)),
"indeed_positive_valid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([should_validity >= .5, should_novelty < .5]), axis=0),
1, 0)),
"indeed_positive_nonvalid_nonnovel": numpy.sum(numpy.where(
numpy.all(numpy.stack([should_validity < .5, should_novelty < .5]), axis=0),
1, 0)),
}
ret_help = {
"precision_validity": ret_base_help["true_positive_validity"] /
max(1, ret_base_help["classified_positive_validity"]),
"precision_novelty": ret_base_help["true_positive_novelty"] /
max(1, ret_base_help["classified_positive_novelty"]),
"recall_validity": ret_base_help["true_positive_validity"] /
max(1, ret_base_help["indeed_positive_validity"]),
"recall_novelty": ret_base_help["true_positive_novelty"] /
max(1, ret_base_help["indeed_positive_novelty"]),
"precision_val_neg": ret_base_help["true_negative_validity"] /
max(1, ret_base_help["classified_negative_validity"]),
"precision_nov_neg": ret_base_help["true_negative_novelty"] /
max(1, ret_base_help["classified_negative_novelty"]),
"recall_val_neg": ret_base_help["true_negative_validity"] /
max(1, ret_base_help["indeed_negative_validity"]),
"recall_nov_neg": ret_base_help["true_negative_novelty"] /
max(1, ret_base_help["indeed_negative_novelty"]),
"precision_valid_novel": ret_base_help["true_positive_valid_novel"] /
max(1, ret_base_help["classified_positive_valid_novel"]),
"precision_valid_nonnovel": ret_base_help["true_positive_valid_nonnovel"] /
max(1, ret_base_help["classified_positive_valid_nonnovel"]),
"precision_nonvalid_novel": ret_base_help["true_positive_nonvalid_novel"] /
max(1, ret_base_help["classified_positive_nonvalid_novel"]),
"precision_nonvalid_nonnovel": ret_base_help["true_positive_nonvalid_nonnovel"] /
max(1, ret_base_help["classified_positive_nonvalid_nonnovel"]),
"recall_valid_novel": ret_base_help["true_positive_valid_novel"] /
max(1, ret_base_help["indeed_positive_valid_novel"]),
"recall_valid_nonnovel": ret_base_help["true_positive_valid_nonnovel"] /
max(1, ret_base_help["indeed_positive_valid_nonnovel"]),
"recall_nonvalid_novel": ret_base_help["true_positive_nonvalid_novel"] /
max(1, ret_base_help["indeed_positive_nonvalid_novel"]),
"recall_nonvalid_nonnovel": ret_base_help["true_positive_nonvalid_nonnovel"] /
max(1, ret_base_help["indeed_positive_nonvalid_nonnovel"])
}
ret.update({
"f1_validity": 2 * ret_help["precision_validity"] * ret_help["recall_validity"] /
max(1e-4, ret_help["precision_validity"] + ret_help["recall_validity"]),
"f1_novelty": 2 * ret_help["precision_novelty"] * ret_help["recall_novelty"] /
max(1e-4, ret_help["precision_novelty"] + ret_help["recall_novelty"]),
"f1_val_neg": 2 * ret_help["precision_val_neg"] * ret_help["recall_val_neg"] /
max(1e-4, ret_help["precision_val_neg"] + ret_help["recall_val_neg"]),
"f1_nov_neg": 2 * ret_help["precision_nov_neg"] * ret_help["recall_nov_neg"] /
max(1e-4, ret_help["precision_nov_neg"] + ret_help["recall_nov_neg"]),
"f1_valid_novel": 2 * ret_help["precision_valid_novel"] * ret_help["recall_valid_novel"] /
max(1e-4, ret_help["precision_valid_novel"] + ret_help["recall_valid_novel"]),
"f1_valid_nonnovel": 2 * ret_help["precision_valid_nonnovel"] * ret_help["recall_valid_nonnovel"] /
max(1e-4, ret_help["precision_valid_nonnovel"] + ret_help["recall_valid_nonnovel"]),
"f1_nonvalid_novel": 2 * ret_help["precision_nonvalid_novel"] * ret_help["recall_nonvalid_novel"] /
max(1e-4, ret_help["precision_nonvalid_novel"] + ret_help["recall_nonvalid_novel"]),
"f1_nonvalid_nonnovel": 2 * ret_help["precision_nonvalid_nonnovel"] * ret_help["recall_nonvalid_nonnovel"] /
max(1e-4, ret_help["precision_nonvalid_nonnovel"] + ret_help["recall_nonvalid_nonnovel"])
})
ret.update({
"f1_val_macro": (ret["f1_validity"] + ret["f1_val_neg"])/2,
"f1_nov_macro": (ret["f1_novelty"] + ret["f1_nov_neg"])/2,
"f1_macro": (ret["f1_valid_novel"]+ret["f1_valid_nonnovel"]+ret["f1_nonvalid_novel"]+ret["f1_nonvalid_nonnovel"])/4
})
logger.info("Clean the metric-dict before returning: {}",
" / ".join(map(lambda key: "{}: {}".format(key, ret.pop(key)),
["approximately_hits_validity", "approximately_hits_novelty", "exact_hits_validity",
"exact_hits_novelty", "size"])))
return ret
# noinspection PyMethodMayBeStatic
class ValNovTrainer(Trainer):
def compute_loss(self, model: PreTrainedModel, inputs: Dict[str, torch.Tensor], return_outputs=False):
try:
validity = inputs.pop("validity")
novelty = inputs.pop("novelty")
weights = inputs.pop("weight")
logger.trace("The batch contain following validity-scores ({}), novelty-scores ({}) and weights ({})",
validity, novelty, weights)
outputs = model(**inputs)
if isinstance(outputs, ValNovOutput) and outputs.loss is not None:
logger.debug("The loss was already computed: {}", outputs.loss)
return (outputs.loss, outputs) if return_outputs else outputs.loss
if isinstance(outputs, ValNovOutput):
is_val = outputs.validity
is_nov = outputs.novelty
else:
logger.warning("The output of you model {} is a {}, bit should be a ValNovOutput",
model.name_or_path, type(outputs))
is_val = outputs[0] if isinstance(outputs, Tuple) and len(outputs) >= 2 else outputs
is_nov = outputs[1] if isinstance(outputs, Tuple) and len(outputs) >= 2 else outputs
loss = val_nov_loss(is_val=is_val, is_nov=is_nov,
should_val=validity, should_nov=novelty,
weights=weights)
return (loss, outputs) if return_outputs else loss
except KeyError:
logger.opt(exception=True).error("Something in your configuration / plugged model is false")
return (torch.zeros((0,), dtype=torch.float), model(**inputs)) if return_outputs \
else torch.zeros((0,), dtype=torch.float)
@dataclass
class ValNovOutput(SequenceClassifierOutput):
validity: torch.FloatTensor = None
novelty: torch.FloatTensor = None
class ValNovRegressor(torch.nn.Module):
def __init__(self, transformer: PreTrainedModel,
loss: Literal["ignore", "compute", "compute and reduce"] = "ignore"):
super(ValNovRegressor, self).__init__()
self.transformer = transformer
try:
self.regression_layer_validity = torch.nn.Linear(in_features=transformer.config.hidden_size, out_features=1)
self.regression_layer_novelty = torch.nn.Linear(in_features=transformer.config.hidden_size, out_features=1)
except AttributeError:
logger.opt(exception=True).warning("No hidden-size... please use a XXXForMaskedLM-Model!")
self.regression_layer_validity = torch.nn.LazyLinear(out_features=1)
self.regression_layer_novelty = torch.nn.LazyLinear(out_features=1)
self.sigmoid = torch.nn.Sigmoid()
if loss == "ignore":
logger.info("torch-Module without an additional loss computation during the forward-pass - "
"has to be done explicitly in the training loop!")
self.loss = loss
logger.success("Successfully created {}", self)
def forward(self, x: BatchEncoding) -> ValNovOutput:
transformer_cls: BaseModelOutput = self.transformer(input_ids=x["input_ids"],
attention_mask=x["attention_mask"],
token_type_ids=x["token_type_ids"],
return_dict=True)
cls_logits = transformer_cls.last_hidden_state[0]
validity_logits = self.regression_layer_validity(cls_logits)
novelty_logits = self.regression_layer_novelty(cls_logits)
return ValNovOutput(
logits=torch.stack([validity_logits, novelty_logits]),
loss=val_nov_loss(is_val=self.sigmoid(validity_logits),
is_nov=self.sigmoid(novelty_logits),
should_val=x["validity"],
should_nov=x["novelty"],
weights=x.get("weight", None),
reduce=self.loss == "compute and reduce"
) if self.loss != "ignore" and "validity" in x and "novelty" in x else None,
hidden_states=transformer_cls.hidden_states,
attentions=transformer_cls.attentions,
validity=self.sigmoid(validity_logits),
novelty=self.sigmoid(novelty_logits)
)
def __str__(self) -> str:
return "() --> ({} --> validity/ {} --> novelty)".format(self.transformer.name_or_path,
self.regression_layer_validity,
self.regression_layer_novelty)
class RobertaForValNovRegression(RobertaForSequenceClassification):
def __init__(self, *model_args, **model_kwargs):
config = RobertaForValNovRegression.get_config()
configs = [arg for arg in model_args if isinstance(arg, RobertaConfig)]
if len(configs) >= 1:
logger.warning("Found already {} config {}... extend it", len(configs), configs[0])
model_args = [arg for arg in model_args if not isinstance(arg, RobertaConfig)]
config = configs[0]
config.num_labels = 2
config.id2label = {
0: "validity",
1: "novelty"
}
config.return_dict = True
super().__init__(config=config, *model_args, **model_kwargs)
self.loss = "compute"
self.sigmoid = torch.nn.Sigmoid()
@classmethod
def get_config(cls) -> RobertaConfig:
config = RobertaConfig()
config.finetuning_task = "Validity-Novelty-Prediction"
config.num_labels = 2
config.id2label = {
0: "validity",
1: "novelty"
}
config.return_dict = True
return config
def forward(self, **kwargs):
logger.trace("Found {} forward-params", len(kwargs))
if "labels" in kwargs:
labels = kwargs.pop("labels")
logger.warning("Found a disturbing param in forward-function: labels ({})", labels)
if "return_dict" in kwargs:
return_dict = kwargs.pop("return_dict")
logger.warning("Found a disturbing param in forward-function: return_dict ({})", return_dict)
should_validity = None
if "validity" in kwargs:
should_validity = kwargs.pop("validity")
logger.trace("Found a target validity-vector: {}", should_validity)
should_novelty = None
if "novelty" in kwargs:
should_novelty = kwargs.pop("novelty")
logger.trace("Found a target novelty-vector: {}", should_novelty)
weights = None
if "weight" in kwargs:
weights = kwargs.pop("weight")
logger.trace("Found a sample-weights-vector: {}", weights)
out: SequenceClassifierOutput = super().forward(**kwargs)
is_validity = self.sigmoid(out.logits[:, 0])
is_novelty = self.sigmoid(out.logits[:, 1])
return ValNovOutput(
attentions=out.attentions,
hidden_states=out.hidden_states,
logits=out.logits,
loss=val_nov_loss(
is_val=is_validity,
is_nov=is_novelty,
should_val=should_validity,
should_nov=should_novelty,
weights=weights,
reduce=self.loss == "compute and reduce"
) if self.loss != "ignore" and should_validity is not None and should_novelty is not None else None,
validity=is_validity,
novelty=is_novelty
)
|