| 370 | return x |
| 371 | |
| 372 | class LpLoss(object): |
| 373 | def __init__(self, d=2, p=2, size_average=True, reduction=True): |
| 374 | super(LpLoss, self).__init__() |
| 375 | |
| 376 | #Dimension and Lp-norm type are postive |
| 377 | assert d > 0 and p > 0 |
| 378 | |
| 379 | self.d = d |
| 380 | self.p = p |
| 381 | self.reduction = reduction |
| 382 | self.size_average = size_average |
| 383 | |
| 384 | def abs(self, x, y): |
| 385 | num_examples = x.size()[0] |
| 386 | |
| 387 | # Assume uniform mesh |
| 388 | h = 1.0 / (x.size()[1] - 1.0) |
| 389 | |
| 390 | all_norms = (h**(self.d/self.p))*torch.norm(x.view(num_examples,-1) - y.view(num_examples,-1), self.p, 1) |
| 391 | |
| 392 | if self.reduction: |
| 393 | if self.size_average: |
| 394 | return torch.mean(all_norms) |
| 395 | else: |
| 396 | return torch.sum(all_norms) |
| 397 | |
| 398 | return all_norms |
| 399 | |
| 400 | def rel(self, x, y): |
| 401 | num_examples = x.size()[0] |
| 402 | |
| 403 | diff_norms = torch.norm(x.reshape(num_examples,-1) - y.reshape(num_examples,-1), self.p, 1) |
| 404 | y_norms = torch.norm(y.reshape(num_examples,-1), self.p, 1) |
| 405 | |
| 406 | if self.reduction: |
| 407 | if self.size_average: |
| 408 | return torch.mean(diff_norms/y_norms) |
| 409 | else: |
| 410 | return torch.sum(diff_norms/y_norms) |
| 411 | |
| 412 | return diff_norms/y_norms |
| 413 | |
| 414 | def __call__(self, x, y): |
| 415 | return self.rel(x, y) |
nothing calls this directly
no outgoing calls
no test coverage detected