class_docstr: Tests regularizer methods.
docstr: Tests virtual_adv_regularizer returning expected loss.
docstr_tok: ["Tests","virtual_adv_regularizer","returning","expected","loss","."]
code: def testVirtualAdvRegularizer(self):
      """Tests virtual_adv_regularizer returning expected loss."""
      np_input = np.array([[1.0, -1.0]])
      tf_input = tf.constant(np_input)
      np_weights = np.array([[1.0, 5.0], [2.0, 2.0]])
      tf_weights = tf.constant(np_weights)
      # Linear transformation and L2 loss makes the Hessian matrix constant.
      embedding_fn = lambda x: tf.matmul(x, tf_weights)
      step_size = 0.1
      vadv_config = configs.VirtualAdvConfig(
          adv_neighbor_config=configs.AdvNeighborConfig(
              feature_mask=None,
              adv_step_size=step_size,
              adv_grad_norm=configs.NormType.L2,
          ),
          distance_config=configs.DistanceConfig(
              distance_type=configs.DistanceType.L2, sum_over_axis=-1
          ),
          num_approx_steps=1,
          approx_difference=1e-3,
      )  # enlarged for numerical stability
      np_seed = np.array([[0.6, 0.8]])
      tf_seed = tf.constant(np_seed)
      vadv_loss = regularizer._virtual_adv_regularizer(
          tf_input,
          embedding_fn,
          vadv_config,
          embedding_fn(tf_input),
          tf_seed,
      )
  
      actual_loss = self.evaluate(vadv_loss)
  
      hessian = 2 * np.dot(np_weights, np_weights.T)
      approx = np.matmul(np_seed, hessian)
      approx *= step_size / np.linalg.norm(
          approx, axis=-1, keepdims=True
      )
      expected_loss = np.linalg.norm(np.matmul(approx, np_weights)) ** 2
      self.assertNear(actual_loss, expected_loss, err=1e-5)
  
file_path: neural-structured-learning/neural_structured_learning/lib/regularizer_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import neural_structured_learning.configs
  from neural_structured_learning.lib import regularizer
  import numpy
  import tensorflow
label_desc: Use image node_id 2 for calling the RegularizerTest obj's underlying member method code with example usage: obj.testVirtualAdvRegularizer() without return types