Simulai losses

Loss Functions#

RMSELoss#

Bases: LossBasics

Source code in simulai/optimization/_losses.py
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
class RMSELoss(LossBasics):
    def __init__(self, operator: torch.nn.Module = None) -> None:
        """Vanilla mean-squared error loss function

        Args:
            operator (torch.nn.Module): the operator used for evaluating
                the loss function (usually a neural network)
        """
        super().__init__()

        self.operator = operator
        self.loss_states = {"loss": list()}

    def _data_loss(
        self,
        output_tilde: torch.Tensor = None,
        norm_value: torch.Tensor = None,
        target_data_tensor: torch.Tensor = None,
    ) -> torch.Tensor:
        """It executes the evaluation of the data-driven mean-squared error

        Args:
            output_tilde (torch.Tensor): the output generated by
                self.operator
            norm_value (torch.Tensor): the value used for normalizing
                the loss evaluation
            target_data_tensor (torch.Tensor): the target tensor to be
                compared with output_tilde

        Returns:
            torch.Tensor: the loss function value for a given state
        """

        if norm_value is not None:
            data_loss = torch.mean(
                torch.square((output_tilde - target_data_tensor) / norm_value)
            )
        else:
            data_loss = torch.mean(torch.square((output_tilde - target_data_tensor)))

        return data_loss

    def __call__(
        self,
        input_data: Union[dict, torch.Tensor] = None,
        target_data: torch.Tensor = None,
        call_back: str = "",
        norm_value: list = None,
        lambda_1: float = 0.0,
        device: str = "cpu",
        lambda_2: float = 0.0,
    ) -> Callable:
        """Main function for generating complete loss function workflow

        Args:
            input_data (Union[dict, torch.Tensor]): the data used as
                input for self.operator
            target_data (torch.Tensor): the target data used for
                training self.oeprator
            call_back (str): a string used for composing the logging of
                the optimization process
            norm_value (list): a list of values used for normalizing the
                loss temrms
            lambda_1 (float): the penalty for the L^1  regularization
                term
            lambda_2 (float): the penalty for the L^2  regularization
                term
            device (str): the device in which the loss evaluation will
                be executed, 'cpu' or 'gpu'

        Returns:
            Callable: the closure function used for evaluating the loss
            value
        """

        l1_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_1), term_type=type(self.operator.weights_l1)
        )

        l2_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_2), term_type=type(self.operator.weights_l2)
        )

        def closure():
            output_tilde = self.operator.forward(**input_data)

            data_loss = self._data_loss(
                output_tilde=output_tilde,
                norm_value=norm_value,
                target_data_tensor=target_data,
            )

            # L² and L¹ regularization term
            weights_l2 = self.operator.weights_l2
            weights_l1 = self.operator.weights_l1

            # beta *||W||_2 + alpha * ||W||_1
            l2_reg = l2_reg_multiplication(lambda_2, weights_l2)
            l1_reg = l1_reg_multiplication(lambda_1, weights_l1)

            # Loss = ||Ũ_t - U_t||_2  +
            #         lambda_1 *||W||_2 + lambda_2 * ||W||_1

            loss = data_loss + l2_reg + l1_reg

            # Back-propagation
            loss.backward()

            self.loss_states["loss"].append(float(loss.detach().data))

            sys.stdout.write(("\rloss: {} {}").format(loss, call_back))
            sys.stdout.flush()

            return loss

        return closure

__call__(input_data=None, target_data=None, call_back='', norm_value=None, lambda_1=0.0, device='cpu', lambda_2=0.0) #

Main function for generating complete loss function workflow

Parameters:

Name Type Description Default
input_data Union[dict, Tensor]

the data used as input for self.operator

None
target_data Tensor

the target data used for training self.oeprator

None
call_back str

a string used for composing the logging of the optimization process

''
norm_value list

a list of values used for normalizing the loss temrms

None
lambda_1 float

the penalty for the L^1 regularization term

0.0
lambda_2 float

the penalty for the L^2 regularization term

0.0
device str

the device in which the loss evaluation will be executed, 'cpu' or 'gpu'

'cpu'

Returns:

Name Type Description
Callable Callable

the closure function used for evaluating the loss

Callable

value

Source code in simulai/optimization/_losses.py
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
def __call__(
    self,
    input_data: Union[dict, torch.Tensor] = None,
    target_data: torch.Tensor = None,
    call_back: str = "",
    norm_value: list = None,
    lambda_1: float = 0.0,
    device: str = "cpu",
    lambda_2: float = 0.0,
) -> Callable:
    """Main function for generating complete loss function workflow

    Args:
        input_data (Union[dict, torch.Tensor]): the data used as
            input for self.operator
        target_data (torch.Tensor): the target data used for
            training self.oeprator
        call_back (str): a string used for composing the logging of
            the optimization process
        norm_value (list): a list of values used for normalizing the
            loss temrms
        lambda_1 (float): the penalty for the L^1  regularization
            term
        lambda_2 (float): the penalty for the L^2  regularization
            term
        device (str): the device in which the loss evaluation will
            be executed, 'cpu' or 'gpu'

    Returns:
        Callable: the closure function used for evaluating the loss
        value
    """

    l1_reg_multiplication = self._exec_multiplication_in_regularization(
        lambda_type=type(lambda_1), term_type=type(self.operator.weights_l1)
    )

    l2_reg_multiplication = self._exec_multiplication_in_regularization(
        lambda_type=type(lambda_2), term_type=type(self.operator.weights_l2)
    )

    def closure():
        output_tilde = self.operator.forward(**input_data)

        data_loss = self._data_loss(
            output_tilde=output_tilde,
            norm_value=norm_value,
            target_data_tensor=target_data,
        )

        # L² and L¹ regularization term
        weights_l2 = self.operator.weights_l2
        weights_l1 = self.operator.weights_l1

        # beta *||W||_2 + alpha * ||W||_1
        l2_reg = l2_reg_multiplication(lambda_2, weights_l2)
        l1_reg = l1_reg_multiplication(lambda_1, weights_l1)

        # Loss = ||Ũ_t - U_t||_2  +
        #         lambda_1 *||W||_2 + lambda_2 * ||W||_1

        loss = data_loss + l2_reg + l1_reg

        # Back-propagation
        loss.backward()

        self.loss_states["loss"].append(float(loss.detach().data))

        sys.stdout.write(("\rloss: {} {}").format(loss, call_back))
        sys.stdout.flush()

        return loss

    return closure

__init__(operator=None) #

Vanilla mean-squared error loss function

Parameters:

Name Type Description Default
operator Module

the operator used for evaluating the loss function (usually a neural network)

None
Source code in simulai/optimization/_losses.py
164
165
166
167
168
169
170
171
172
173
174
def __init__(self, operator: torch.nn.Module = None) -> None:
    """Vanilla mean-squared error loss function

    Args:
        operator (torch.nn.Module): the operator used for evaluating
            the loss function (usually a neural network)
    """
    super().__init__()

    self.operator = operator
    self.loss_states = {"loss": list()}

WRMSELoss#

Bases: LossBasics

Source code in simulai/optimization/_losses.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
class WRMSELoss(LossBasics):
    def __init__(self, operator=None):
        """Weighted mean-squared error loss function

        Args:
            operator (torch.nn.Module): the operator used for evaluating
                the loss function (usually a neural network)
        """

        super().__init__()

        self.operator = operator
        self.split_dim = 1
        self.tol = 1e-25

        self.loss_evaluator = None
        self.norm_evaluator = None

        self.axis_loss_evaluator = lambda res: torch.mean(torch.square((res)), dim=1)

        self.loss_states = {"loss": list()}

    def _data_loss(
        self,
        output_tilde: torch.Tensor = None,
        weights: list = None,
        target_data_tensor: torch.Tensor = None,
        axis: int = -1,
    ) -> List:
        """It executes the evaluation of the data-driven mean-squared error

        Args:
            output_tilde (torch.Tensor): the output generated by
                self.operator
            norm_value (torch.Tensor): the value used for normalizing
                the loss evaluation
            target_data_tensor (torch.Tensor): the target tensor to be
                compared with output_tilde

        Returns:
            torch.Tensor: the loss function value for a given state
        """

        target_split = torch.split(target_data_tensor, self.split_dim, dim=axis)
        output_split = torch.split(output_tilde, self.split_dim, dim=axis)[:len(target_split)]

        data_losses = [
            weights[i]
            * self.loss_evaluator(out_split - tgt_split)
            / self.norm_evaluator(tgt_split)
            for i, (out_split, tgt_split) in enumerate(zip(output_split, target_split))
        ]

        return data_losses

    def _no_data_loss_wrapper(
        self,
        output_tilde: torch.Tensor = None,
        weights: list = None,
        target_data_tensor: torch.Tensor = None,
        axis: int = -1,
    ) -> torch.Tensor:
        """It executes the evaluation of the data-driven mean-squared error without considering causality preserving

        Args:
            output_tilde (torch.Tensor): the output generated by
                self.operator
            weights (list): weights for rescaling each variable
                outputted by self.operator
            target_data_tensor (torch.Tensor): the target tensor to be
                compared with output_tilde
            axis (int): the axis in which the variables are split

        Returns:
            torch.Tensor: the loss function value for a given state
        """

        return self.data_loss(
            output_tilde=output_tilde,
            weights=weights,
            target_data_tensor=target_data_tensor,
            axis=axis,
        )

    def __call__(
        self,
        input_data: Union[dict, torch.Tensor] = None,
        target_data: torch.Tensor = None,
        call_back: str = "",
        lambda_1: float = 0.0,
        lambda_2: float = 0.0,
        axis: int = -1,
        relative: bool = False,
        device: str = "cpu",
        weights: list = None,
        use_mean: bool = True,
    ) -> Callable:
        """Main function for generating complete loss function workflow

        Args:
            input_data (Union[dict, torch.Tensor]): the data used as
                input for self.operator
            target_data (torch.Tensor): the target data used for
                training self.oeprator
            call_back (str): a string used for composing the logging of
                the optimization process
            norm_value (list): a list of values used for normalizing the
                loss temrms
            lambda_1 (float): the penalty for the L^1  regularization
                term
            lambda_2 (float): the penalty for the L^2  regularization
                term
            device (str): the device in which the loss evaluation will
                be executed, 'cpu' or 'gpu'
            weights (list): a list of weights for rescaling each
                variable outputted by self.operator
            use_mean (bool): use mean for evaluating the losses or not
                (the alternative is sum)

        Returns:
            Callable: the closure function used for evaluating the loss
            value
        """

        self.data_loss = self._data_loss

        l1_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_1), term_type=type(self.operator.weights_l1)
        )

        l2_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_2), term_type=type(self.operator.weights_l2)
        )

        # Using mean evaluation or not
        if use_mean == True:
            self.loss_evaluator = lambda res: torch.mean(torch.square((res)))
        else:
            self.loss_evaluator = lambda res: torch.sum(torch.square((res)))

        # Relative norm or not
        if relative == True:
            if use_mean == True:
                self.norm_evaluator = lambda ref: torch.mean(torch.square((ref)))
            else:
                self.norm_evaluator = lambda ref: torch.sum(torch.square((ref)))
        else:
            self.norm_evaluator = lambda ref: 1

        self.data_loss_wrapper = self._no_data_loss_wrapper

        def closure():
            output_tilde = self.operator.forward(**input_data)

            data_losses = self.data_loss_wrapper(
                output_tilde=output_tilde,
                weights=weights,
                target_data_tensor=target_data,
                axis=axis,
            )

            # L² and L¹ regularization term
            weights_l2 = self.operator.weights_l2
            weights_l1 = self.operator.weights_l1

            # beta *||W||_2 + alpha * ||W||_1
            l2_reg = l2_reg_multiplication(lambda_2, weights_l2)
            l1_reg = l1_reg_multiplication(lambda_1, weights_l1)

            # Loss = ||Ũ_t - U_t||_2  +
            #         lambda_1 *||W||_2 + lambda_2 * ||W||_1
            loss = sum(data_losses) + l2_reg + l1_reg

            # Back-propagation
            loss.backward()

            self.loss_states["loss"].append(float(loss.detach().data))

            sys.stdout.write(("\rloss: {} {}").format(loss, call_back))
            sys.stdout.flush()

        return closure

__call__(input_data=None, target_data=None, call_back='', lambda_1=0.0, lambda_2=0.0, axis=-1, relative=False, device='cpu', weights=None, use_mean=True) #

Main function for generating complete loss function workflow

Parameters:

Name Type Description Default
input_data Union[dict, Tensor]

the data used as input for self.operator

None
target_data Tensor

the target data used for training self.oeprator

None
call_back str

a string used for composing the logging of the optimization process

''
norm_value list

a list of values used for normalizing the loss temrms

required
lambda_1 float

the penalty for the L^1 regularization term

0.0
lambda_2 float

the penalty for the L^2 regularization term

0.0
device str

the device in which the loss evaluation will be executed, 'cpu' or 'gpu'

'cpu'
weights list

a list of weights for rescaling each variable outputted by self.operator

None
use_mean bool

use mean for evaluating the losses or not (the alternative is sum)

True

Returns:

Name Type Description
Callable Callable

the closure function used for evaluating the loss

Callable

value

Source code in simulai/optimization/_losses.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
def __call__(
    self,
    input_data: Union[dict, torch.Tensor] = None,
    target_data: torch.Tensor = None,
    call_back: str = "",
    lambda_1: float = 0.0,
    lambda_2: float = 0.0,
    axis: int = -1,
    relative: bool = False,
    device: str = "cpu",
    weights: list = None,
    use_mean: bool = True,
) -> Callable:
    """Main function for generating complete loss function workflow

    Args:
        input_data (Union[dict, torch.Tensor]): the data used as
            input for self.operator
        target_data (torch.Tensor): the target data used for
            training self.oeprator
        call_back (str): a string used for composing the logging of
            the optimization process
        norm_value (list): a list of values used for normalizing the
            loss temrms
        lambda_1 (float): the penalty for the L^1  regularization
            term
        lambda_2 (float): the penalty for the L^2  regularization
            term
        device (str): the device in which the loss evaluation will
            be executed, 'cpu' or 'gpu'
        weights (list): a list of weights for rescaling each
            variable outputted by self.operator
        use_mean (bool): use mean for evaluating the losses or not
            (the alternative is sum)

    Returns:
        Callable: the closure function used for evaluating the loss
        value
    """

    self.data_loss = self._data_loss

    l1_reg_multiplication = self._exec_multiplication_in_regularization(
        lambda_type=type(lambda_1), term_type=type(self.operator.weights_l1)
    )

    l2_reg_multiplication = self._exec_multiplication_in_regularization(
        lambda_type=type(lambda_2), term_type=type(self.operator.weights_l2)
    )

    # Using mean evaluation or not
    if use_mean == True:
        self.loss_evaluator = lambda res: torch.mean(torch.square((res)))
    else:
        self.loss_evaluator = lambda res: torch.sum(torch.square((res)))

    # Relative norm or not
    if relative == True:
        if use_mean == True:
            self.norm_evaluator = lambda ref: torch.mean(torch.square((ref)))
        else:
            self.norm_evaluator = lambda ref: torch.sum(torch.square((ref)))
    else:
        self.norm_evaluator = lambda ref: 1

    self.data_loss_wrapper = self._no_data_loss_wrapper

    def closure():
        output_tilde = self.operator.forward(**input_data)

        data_losses = self.data_loss_wrapper(
            output_tilde=output_tilde,
            weights=weights,
            target_data_tensor=target_data,
            axis=axis,
        )

        # L² and L¹ regularization term
        weights_l2 = self.operator.weights_l2
        weights_l1 = self.operator.weights_l1

        # beta *||W||_2 + alpha * ||W||_1
        l2_reg = l2_reg_multiplication(lambda_2, weights_l2)
        l1_reg = l1_reg_multiplication(lambda_1, weights_l1)

        # Loss = ||Ũ_t - U_t||_2  +
        #         lambda_1 *||W||_2 + lambda_2 * ||W||_1
        loss = sum(data_losses) + l2_reg + l1_reg

        # Back-propagation
        loss.backward()

        self.loss_states["loss"].append(float(loss.detach().data))

        sys.stdout.write(("\rloss: {} {}").format(loss, call_back))
        sys.stdout.flush()

    return closure

__init__(operator=None) #

Weighted mean-squared error loss function

Parameters:

Name Type Description Default
operator Module

the operator used for evaluating the loss function (usually a neural network)

None
Source code in simulai/optimization/_losses.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def __init__(self, operator=None):
    """Weighted mean-squared error loss function

    Args:
        operator (torch.nn.Module): the operator used for evaluating
            the loss function (usually a neural network)
    """

    super().__init__()

    self.operator = operator
    self.split_dim = 1
    self.tol = 1e-25

    self.loss_evaluator = None
    self.norm_evaluator = None

    self.axis_loss_evaluator = lambda res: torch.mean(torch.square((res)), dim=1)

    self.loss_states = {"loss": list()}

PIRMSELoss#

Bases: LossBasics

Source code in simulai/optimization/_losses.py
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
class PIRMSELoss(LossBasics):
    def __init__(self, operator: torch.nn.Module = None) -> None:
        """Physics-Informed mean-squared error loss function

        Args:
            operator (torch.nn.Module): the operator used for evaluating
                the loss function (usually a neural network)
        """

        super().__init__()

        self.split_dim = 1
        self.operator = operator
        self.loss_evaluator = None
        self.residual = None
        self.tol = 1e-15
        self.device = None

        self.axis_loss_evaluator = lambda res: torch.mean(torch.square((res)), dim=1)

        self.loss_states = {
            "pde": list(),
            "init": list(),
            "bound": list(),
            "extra_data": list(),
        }
        self.loss_tags = list(self.loss_states.keys())
        self.hybrid_data_pinn = False

        self.losses_terms_indices = {
            "pde": 0,
            "init": 1,
            "bound": 2,
            "extra_data": 3,
            "causality_weights": 4,
        }

    def _convert(
        self, input_data: Union[dict, np.ndarray] = None, device: str = None
    ) -> Union[dict, torch.Tensor]:
        """It converts a dataset to the proper format (torch.Tensor) and send it to
        the chosen execution device ('gpu' or 'cpu')

        Args:
            input_data (Union[dict, np.ndarray]): the data structure to
                be converted
            device: the device in which the converted dataset must be
                placed

        Returns:
            Union[dict, torch.Tensor]: the converted data structure
        """

        if type(input_data) == dict:
            return {
                key: torch.from_numpy(item.astype(ARRAY_DTYPE)).to(device)
                for key, item in input_data.items()
            }

        else:
            return torch.from_numpy(input_data.astype(ARRAY_DTYPE)).to(device)

    def _to_tensor(self, *args, device: str = "cpu") -> List[torch.Tensor]:
        """It converted a size indefined list of arrays to tensors

        Args:
            *args: list of arrays to be converted
            input_data (Union[dict, np.ndarray])
            device: the device in which the converted dataset must be
                placed
        :type np.array, np.array, ..., np.array

        Returns:
            List[torch.Tensor]: a list of tensors
        """
        return [self._convert(input_data=arg, device=device) for arg in args]

    def _data_loss(
        self,
        output_tilde: torch.Tensor = None,
        target_data_tensor: torch.Tensor = None,
        weights: List[float] = None,
    ) -> torch.Tensor:
        """It executes the evaluation of the data-driven mean-squared error

        Args:
            output_tilde (torch.Tensor): the output generated by
                self.operator
            target_data_tensor (torch.Tensor): the target tensor to be
                compared with output_tilde

        Returns:
            torch.Tensor: the loss function value
        """

        target_split = torch.split(target_data_tensor, self.split_dim, dim=-1)
        output_split = torch.split(output_tilde, self.split_dim, dim=-1)[:len(target_split)]

        data_losses = [
            self.loss_evaluator_data((out_split, tgt_split))
            / (self.norm_evaluator(tgt_split) or torch.tensor(1.0).to(self.device))
            for i, (out_split, tgt_split) in enumerate(zip(output_split, target_split))
        ]

        return self.weighted_loss_evaluator(data_losses, weights)

    def _data_loss_adaptive(
        self,
        output_tilde: torch.Tensor = None,
        target_data_tensor: torch.Tensor = None,
        **kwargs,
    ) -> torch.Tensor:
        """It executes the evaluation of the data-driven mean-squared error

        Args:
            output_tilde (torch.Tensor): the output generated by
                self.operator
            target_data_tensor (torch.Tensor): the target tensor to be
                compared with output_tilde

        Returns:
            torch.Tensor: the loss function value
        """

        output_split = torch.split(output_tilde, self.split_dim, dim=-1)
        target_split = torch.split(target_data_tensor, self.split_dim, dim=-1)

        data_discrepancy = [
            out_split - tgt_split
            for i, (out_split, tgt_split) in enumerate(zip(output_split, target_split))
        ]

        weights = self.data_weights_estimator(
            residual=data_discrepancy,
            loss_evaluator=self.loss_evaluator,
            loss_history=self.loss_states,
            operator=self.operator,
        )

        data_losses = [
            weights[i] * self.loss_evaluator_data((out_split, tgt_split))
            for i, (out_split, tgt_split) in enumerate(zip(output_split, target_split))
        ]

        return [sum(data_losses)]

    def _global_weights_bypass(
        self, initial_penalty: float = None, **kwargs
    ) -> List[float]:
        return [1.0, initial_penalty, 1.0, 1.0]

    def _global_weights_estimator(self, **kwargs) -> List[float]:
        weights = self.global_weights_estimator(**kwargs)

        return weights

    def _residual_loss(
        self, residual_approximation: List[torch.Tensor] = None, weights: list = None
    ) -> List[torch.Tensor]:
        """It evaluates the physics-driven residual loss

        Args:
            residual_approximation (List[torch.Tensor]): a list of
                tensors containing the evaluation for the physical
                residual for each sample in the dataset
            weights (list): a list of weights used for rescaling the
                residuals of each variable

        Returns:
            torch.Tensor: the list of residual losses
        """
        residual_losses = [self.loss_evaluator(res) for res in residual_approximation]

        return self.weighted_loss_evaluator(residual_losses, weights)

    def _residual_loss_adaptive(
        self, residual_approximation: List[torch.Tensor] = None, weights: list = None
    ) -> List[torch.Tensor]:
        """It evaluates the physics-driven residual loss

        Args:
            residual_approximation (List[torch.Tensor]): a list of
                tensors containing the evaluation for the physical
                residual for each sample in the dataset
            weights (list): a list of weights used for rescaling the
                residuals of each variable

        Returns:
            torch.Tensor: the list of residual losses
        """

        weights = self.residual_weights_estimator(
            residual=residual_approximation,
            loss_evaluator=self.loss_evaluator,
            loss_history=self.loss_states,
            operator=self.operator,
        )

        residual_loss = [
            weight * self.loss_evaluator(res)
            for weight, res in zip(weights, residual_approximation)
        ]

        return [sum(residual_loss)]

    def _extra_data(
        self, input_data: torch.Tensor = None, target_data: torch.Tensor = None, weights :list = None, 
    ) -> torch.Tensor:
        # Evaluating data for the initial condition
        output_tilde = self.operator(input_data=input_data)

        # Evaluating loss approximation for extra data
        data_loss = self._data_loss(
            output_tilde=output_tilde,
            target_data_tensor=target_data, 
            weights=weights,
        )

        return data_loss

    def _boundary_penalisation(
        self, boundary_input: dict = None, residual: SymbolicOperator = None
    ) -> List[torch.Tensor]:
        """It applies the boundary conditions

        Args:
            boundary_input (dict): a dictionary containing the
                coordinates of the boundaries
            residual (SymbolicOperator): a symbolic expression for the
                boundary condition

        Returns:
            list: the evaluation of each boundary condition
        """
        return [
            residual.eval_expression(k, boundary_input[k])
            for k in boundary_input.keys()
        ]

    def _no_boundary_penalisation(
        self, boundary_input: dict = None, residual: object = None
    ) -> List[torch.Tensor]:
        """It is used for cases in which no boundary condition is applied

        """

        return [torch.Tensor([0.0]).to(self.device) for k in boundary_input.keys()]

    def _no_boundary(
        self, boundary_input: dict = None, residual: object = None
    ) -> List[torch.Tensor]:
        """It is used for cases where there are not boundaries

        """

        return torch.Tensor([0.0]).to(self.device)

    def _no_extra_data(
        self, input_data: torch.Tensor = None, target_data: torch.Tensor = None, weights: list=None,
    ) -> torch.Tensor:
        return torch.Tensor([0.0]).to(self.device)

    def _no_residual_wrapper(self, input_data: torch.Tensor = None) -> torch.Tensor:
        return self.residual(input_data)

    def _causality_preserving_residual_wrapper(
        self, input_data: torch.Tensor = None
    ) -> List:
        return self.causality_preserving(self.residual(input_data))

    def _filter_necessary_loss_terms(self, residual: SymbolicOperator = None):
        tags = ["pde", "init"]
        indices = [0, 1]

        if residual.g_expressions:
            tags.append("bound")
            indices.append(2)
        else:
            pass

        if self.hybrid_data_pinn:
            tags.append("extra_data")
            indices.append(3)
        else:
            pass

        return tags, indices

    def _losses_states_str(self, tags: List[str] = None):
        losses_str = "\r"
        for item in tags:
            losses_str += f"{item}:{{}} "

        return losses_str

    def __call__(
        self,
        input_data: Union[dict, torch.Tensor] = None,
        target_data: Union[dict, torch.Tensor] = None,
        verbose: bool = False,
        call_back: str = "",
        residual: Callable = None,
        initial_input: Union[dict, torch.Tensor] = None,
        initial_state: Union[dict, torch.Tensor] = None,
        boundary_input: dict = None,
        boundary_penalties: list = [1],
        extra_input_data: Union[dict, torch.Tensor] = None,
        extra_target_data: Union[dict, torch.Tensor] = None,
        initial_penalty: float = 1,
        axis: int = -1,
        relative: bool = False,
        lambda_1: float = 0.0,
        lambda_2: float = 0.0,
        weights=None,
        weights_residual=None,
        weights_extra_data=None,
        device: str = "cpu",
        split_losses: bool = False,
        causality_preserving: Callable = None,
        global_weights_estimator: Callable = None,
        residual_weights_estimator: Callable = None,
        data_weights_estimator: Callable = None,
        use_mean: bool = True,
        use_data_log: bool = False,
    ) -> Callable:
        self.residual = residual

        self.device = device

        self.causality_preserving = causality_preserving

        # Handling expection when AnnealingWeights and split_losses
        # are used together.
        if isinstance(global_weights_estimator, AnnealingWeights):
            if split_losses:
                raise RuntimeError(
                    "Global weights estimator, AnnealingWeights, is not"
                    + "compatible with split loss terms."
                )
            else:
                pass

        self.global_weights_estimator = global_weights_estimator

        self.residual_weights_estimator = residual_weights_estimator

        self.data_weights_estimator = data_weights_estimator

        if split_losses:
            self.weighted_loss_evaluator = self._bypass_weighted_loss
        else:
            self.weighted_loss_evaluator = self._eval_weighted_loss

        if (
            isinstance(extra_input_data, np.ndarray)
            == isinstance(extra_target_data, np.ndarray)
            == True
        ):
            self.hybrid_data_pinn = True
        else:
            pass

        # When no weight is provided, they are
        # set to the default choice
        if weights is None:
            weights = len(residual.output_names) * [1]

        if weights_residual is None:
            weights_residual = len(residual.output_names) * [1]

        loss_tags, loss_indices = self._filter_necessary_loss_terms(residual=residual)
        loss_str = self._losses_states_str(tags=loss_tags)

        # Boundary conditions are optional, since they are not
        # defined in some cases, as ODE, for example.
        if residual.g_expressions:
            boundary = self._boundary_penalisation
        else:
            if boundary_input == None:
                boundary = self._no_boundary
            else:
                boundary = self._no_boundary_penalisation

        if self.causality_preserving:
            call_back = f", causality_weights: {self.causality_preserving.call_back}"
            self.residual_wrapper = self._causality_preserving_residual_wrapper

        else:
            self.residual_wrapper = self._no_residual_wrapper

        l1_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_1), term_type=type(self.operator.weights_l1)
        )

        l2_reg_multiplication = self._exec_multiplication_in_regularization(
            lambda_type=type(lambda_2), term_type=type(self.operator.weights_l2)
        )
        if type(input_data) is dict:
            try:
                input_data = input_data["input_data"]
            except Exception:
                pass

        initial_input, initial_state = self._to_tensor(
            initial_input, initial_state, device=device
        )

        # Preparing extra data, when necessary
        if self.hybrid_data_pinn:
            extra_input_data, extra_target_data = self._to_tensor(
                extra_input_data, extra_target_data, device=device
            )
            self.extra_data = self._extra_data
        else:
            self.extra_data = self._no_extra_data

        if use_data_log == True:
            self.inner_square = self._two_term_log_loss
        else:
            self.inner_square = self._two_term_loss

        if use_mean == True:
            self.loss_evaluator = lambda res: torch.mean(self._single_term_loss(res))
        else:
            self.loss_evaluator = lambda res: torch.sum(self._single_term_loss(res))

        if use_mean == True:
            self.loss_evaluator_data = lambda res: torch.mean(self.inner_square(*res))
        else:
            self.loss_evaluator_data = lambda res: torch.sum(self.inner_square(*res))

        # Relative norm or not
        if relative == True:
            if use_mean == True:
                self.norm_evaluator = lambda ref: torch.mean(torch.square((ref)))
            else:
                self.norm_evaluator = lambda ref: torch.sum(torch.square((ref)))
        else:
            self.norm_evaluator = lambda ref: 1

        # Determing the usage of special residual loss weighting
        if residual_weights_estimator:
            self.residual_loss = self._residual_loss_adaptive
        else:
            self.residual_loss = self._residual_loss

        # Determing the usage of special data loss weighting
        if data_weights_estimator:
            self.data_loss = self._data_loss_adaptive
        else:
            self.data_loss = self._data_loss

        # Determining the usage of special global loss weighting
        if global_weights_estimator:
            self.global_weights = self._global_weights_estimator
        else:
            self.global_weights = self._global_weights_bypass

        if verbose:
            self.pprint = self._pprint_verbose
        else:
            self.pprint = self._pprint_simple

        def closure():
            # Executing the symbolic residual evaluation
            residual_approximation = self.residual_wrapper(input_data)

            # Boundary, if appliable
            boundary_approximation = boundary(
                boundary_input=boundary_input, residual=residual
            )

            # Evaluating data for the initial condition
            initial_output_tilde = self.operator(input_data=initial_input)

            # Evaluating loss function for residual
            residual_loss = self.residual_loss(
                residual_approximation=residual_approximation, weights=weights_residual
            )

            # Evaluating loss for the boundary approaximation, if appliable
            boundary_loss = self._residual_loss(
                residual_approximation=boundary_approximation,
                weights=boundary_penalties,
            )

            # Evaluating loss approximation for initial condition
            initial_data_loss = self.data_loss(
                output_tilde=initial_output_tilde,
                target_data_tensor=initial_state,
                weights=weights,
            )

            # Evaluating extra data loss, when appliable
            extra_data = self.extra_data(
                input_data=extra_input_data,
                target_data=extra_target_data,
                weights=weights_extra_data,
            )

            # L² and L¹ regularization term
            weights_l2 = self.operator.weights_l2
            weights_l1 = self.operator.weights_l1

            # beta *||W||_2 + alpha * ||W||_1
            l2_reg = l2_reg_multiplication(lambda_2, weights_l2)
            l1_reg = l1_reg_multiplication(lambda_1, weights_l1)

            # The complete loss function
            pde = residual_loss
            init = initial_data_loss
            bound = boundary_loss

            loss_terms = self._aggregate_terms(*pde, *init, *bound, *extra_data)

            # Updating the loss weights if necessary
            loss_weights = self.global_weights(
                initial_penalty=initial_penalty,
                operator=self.operator,
                loss_evaluator=self.loss_evaluator,
                residual=loss_terms,
            )
            # Overall loss function
            loss = (
                sum(self._eval_weighted_loss(loss_terms, loss_weights))
                + l2_reg
                + l1_reg
            )

            # Back-propagation
            loss.backward()

            pde_detach = float(sum(pde).detach().data)
            init_detach = float(sum(init).detach().data)
            bound_detach = float(sum(bound).detach().data)
            extra_data_detach = float(sum(extra_data).detach().data)

            self.loss_states["pde"].append(pde_detach)
            self.loss_states["init"].append(init_detach)
            self.loss_states["bound"].append(bound_detach)
            self.loss_states["extra_data"].append(extra_data_detach)

            losses_list = np.array(
                [pde_detach, init_detach, bound_detach, extra_data_detach]
            )

            self.pprint(
                loss_str=loss_str,
                losses_list=losses_list,
                call_back=call_back,
                loss_indices=loss_indices,
                loss_terms=loss_terms,
                loss_weights=loss_weights,
            )

            _current_loss = loss

            return _current_loss

        return closure

__init__(operator=None) #

Physics-Informed mean-squared error loss function

Parameters:

Name Type Description Default
operator Module

the operator used for evaluating the loss function (usually a neural network)

None
Source code in simulai/optimization/_losses.py
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
def __init__(self, operator: torch.nn.Module = None) -> None:
    """Physics-Informed mean-squared error loss function

    Args:
        operator (torch.nn.Module): the operator used for evaluating
            the loss function (usually a neural network)
    """

    super().__init__()

    self.split_dim = 1
    self.operator = operator
    self.loss_evaluator = None
    self.residual = None
    self.tol = 1e-15
    self.device = None

    self.axis_loss_evaluator = lambda res: torch.mean(torch.square((res)), dim=1)

    self.loss_states = {
        "pde": list(),
        "init": list(),
        "bound": list(),
        "extra_data": list(),
    }
    self.loss_tags = list(self.loss_states.keys())
    self.hybrid_data_pinn = False

    self.losses_terms_indices = {
        "pde": 0,
        "init": 1,
        "bound": 2,
        "extra_data": 3,
        "causality_weights": 4,
    }