code: def test_works_with_model(self):
      class FakeModel(Model):
          def __init__(self) -> None:
              super().__init__(None)
              self.w = torch.nn.Parameter(torch.randn(1))
  
          def forward(self, t: torch.Tensor) -> Dict[str, torch.Tensor]:  # type: ignore
              return {"loss": (t * self.w).sum()}
  
      model = FakeModel()
      moving_average = ExponentialMovingAverage(
          model.named_parameters()
      )
  
      optimizer = torch.optim.SGD(list(model.parameters()), lr=0.1)
  
      for _ in range(10):
          optimizer.zero_grad()
          t = torch.randn(10)
          loss = model.forward(t)["loss"]
          loss.backward()
          optimizer.step()
          moving_average.apply()
  
      w_value = model.w.item()
      shadow_value = moving_average._shadows["w"].item()
  
      assert w_value != shadow_value
  
      moving_average.assign_average_value()
  
      assert model.w.item() == shadow_value
  
      moving_average.restore()
  
      assert model.w.item() == w_value
  
      # Now keep training:
  
      for _ in range(10):
          optimizer.zero_grad()
          t = torch.randn(10)
          loss = model.forward(t)["loss"]
          loss.backward()
          optimizer.step()
          moving_average.apply()
  
file_path: allennlp/tests/training/moving_average_test.py
imports: from typing import Dict
  import torch
  import numpy
  from allennlp.common.params import Params
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.models.model import Model
  from allennlp.training.moving_average import MovingAverage, ExponentialMovingAverage
label_desc: Use image node_id 4 for calling the MovingAverageTest obj's underlying member method code with example usage: obj.test_works_with_model() and returns: dict