code: def test_newton_step_trust(trust_radius, dims):
      batch_size = 100
      batch_shape = torch.Size((batch_size,))
      mode = (
          random_inside_unit_circle(
              batch_shape + (dims,), requires_grad=True
          )
          + 1
      )
      x = (
          random_inside_unit_circle(
              batch_shape + (dims,), requires_grad=True
          )
          - 1
      )
  
      # create a quadratic loss function
      noise = torch.randn(batch_size, dims, dims)
      hessian = noise + noise.transpose(-1, -2)
      diff = (x - mode).unsqueeze(-2)
      loss = 0.5 * diff.bmm(hessian).bmm(diff.transpose(-1, -2)).sum()
  
      # run method under test
      x_updated, cov = newton_step(loss, x, trust_radius=trust_radius)
  
      # check shapes
      assert x_updated.shape == x.shape
      assert cov.shape == hessian.shape
  
      # check values
      if trust_radius is None:
          assert (
              (x - x_updated).pow(2).sum(-1) > 1.0
          ).any(), "test is too weak"
      else:
          assert (
              (x - x_updated).pow(2).sum(-1) <= 1e-8 + trust_radius**2
          ).all(), "trust region violated"
  
file_path: pyro/tests/ops/test_newton.py
imports: import itertools
  import logging
  import pytest
  import torch
  from torch.autograd import grad
  from pyro.ops.newton import newton_step
  from tests.common import assert_equal
label_desc: Use image node_id 3 for calling a global function with example usage: test_newton_step_trust(trust_radius, dims) without return types