class_docstr: Tests linear regression used in few-shot evaluation.
docstr: Test linear regression.
docstr_tok: ["Test","linear","regression","."]
code: def test_linear_regression(self):
      """Test linear regression."""
      # Generate random data.
      num_points = 512
      dim = 16
      num_classes = 5
      l2_regs = [1.0, 2.0, 8.0, 0.0]
      rng = random.PRNGKey(0)
  
      x = random.normal(rng, shape=(num_points, dim))
      x_test = random.normal(rng, shape=(num_points, dim))
      y = random.randint(
          rng, shape=(num_points,), minval=0, maxval=num_classes
      )
      y_test = random.randint(
          rng, shape=(num_points,), minval=0, maxval=num_classes
      )
  
      for l2_reg in l2_regs:
          # Compute predictions.
          accuracy = fewshot_utils._fewshot_acc_fn(  # pylint: disable=protected-access (testing a private function)
              x,
              y,
              x_test,
              y_test,
              l2_reg,
              num_classes,
              target_is_one_hot=False,
          )
  
          # Compare with big_vision.
          expected_accuracy = big_vision_linear_regression(
              x, y, x_test, y_test, l2_reg, num_classes
          )
          self.assertGreater(accuracy, 0)
          self.assertLess(accuracy, 1)
          self.assertAlmostEqual(
              accuracy, expected_accuracy, delta=1e-6
          )
  
          # Check they are identical when labels are one-hot.
          y_one_hot = jax.nn.one_hot(y, num_classes)
          y_test_one_hot = jax.nn.one_hot(y_test, num_classes)
  
          accuracy_one_hot = fewshot_utils._fewshot_acc_fn(  # pylint: disable=protected-access (testing a private function)
              x,
              y_one_hot,
              x_test,
              y_test_one_hot,
              l2_reg,
              num_classes,
              target_is_one_hot=True,
          )
          self.assertEqual(accuracy, accuracy_one_hot)
  
file_path: scenic/scenic/train_lib/transfer/tests/test_fewshot_utils.py
imports: from absl.testing import absltest
  from big_vision.evaluators import fewshot
  import jax
  from jax import random
  from scenic.train_lib.transfer import fewshot_utils
label_desc: Use image node_id 1 for calling the LinearRegressionTest obj's underlying member method code with example usage: obj.test_linear_regression() without return types