code: def test_regularization(self):
      penalty = self.model.get_regularization_penalty().data
      assert (penalty > 0).all()
  
      penalty2 = 0
  
      # Config specifies penalty as
      #   "regularizer": [
      #     ["weight$", {"type": "l2", "alpha": 10}],
      #     ["bias$", {"type": "l1", "alpha": 5}]
      #   ]
      for name, parameter in self.model.named_parameters():
          if name.endswith("weight"):
              weight_penalty = 10 * torch.sum(torch.pow(parameter, 2))
              penalty2 += weight_penalty
          elif name.endswith("bias"):
              bias_penalty = 5 * torch.sum(torch.abs(parameter))
              penalty2 += bias_penalty
  
      assert (penalty == penalty2.data).all()
  
      # You get a RuntimeError if you call `model.forward` twice on the same inputs.
      # The data and config are such that the whole dataset is one batch.
      training_batch = next(iter(self.data_loader))
      validation_batch = next(iter(self.data_loader))
  
      training_batch_outputs = self.trainer.batch_outputs(
          training_batch, for_training=True
      )
      training_loss = training_batch_outputs["loss"].data
  
      assert (penalty == training_batch_outputs["reg_loss"]).all()
  
      validation_loss = self.trainer.batch_outputs(
          validation_batch, for_training=False
      )["loss"].data
  
      # Training loss should have the regularization penalty, but validation loss should not.
      assert (training_loss != validation_loss).all()
  
      # Training loss should equal the validation loss plus the penalty.
      penalized = validation_loss + penalty
      assert (training_loss == penalized).all()
  
imports: from flaky import flaky
  import numpy
  import pytest
  import torch
  from allennlp.common.testing import ModelTestCase
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.params import Params
  from allennlp.data.dataset_readers import DatasetReader
  from allennlp.data.data_loaders import DataLoader, SimpleDataLoader
  from allennlp.models import Model
  from allennlp.training import GradientDescentTrainer, Trainer
label_desc: Use image node_id 2 for calling the SimpleTaggerRegularizationTest obj's underlying member method code with example usage: obj.test_regularization() without return types