code: def test_swa_auto_group_added_during_run(self):
      # Tests SWA in Auto mode with the second param group added after several
      # optimizations steps. The expected behavior is that the averaging for
      # the second param group starts at swa_start steps after it is added.
      # For the first group averaging should start swa_start steps after the
      # first step of the optimizer.
  
      x, y, loss_fun, _ = self._define_vars_loss_opt()
      opt = optim.SGD([x], lr=1e-3, momentum=0.9)
      swa_start = 5
      swa_freq = 2
      opt = StochasticWeightAveraging(
          opt, swa_start=swa_start, swa_freq=swa_freq, swa_lr=0.001
      )
  
      x_sum = torch.zeros_like(x)
      y_sum = torch.zeros_like(y)
      x_n_avg = 0
      y_n_avg = 0
      x_step = 0
      for i in range(1, 11):
          opt.zero_grad()
          loss = loss_fun(x, y)
          loss.backward()
          opt.step()
          x_step += 1
          if i % swa_freq == 0 and i > swa_start:
              x_n_avg += 1
              x_sum += x.data
  
      x_avg = x_sum / x_n_avg
  
      opt.add_param_group({"params": y, "lr": 1e-4})
  
      for y_step in range(1, 11):
          opt.zero_grad()
          loss = loss_fun(x, y)
          loss.backward()
          opt.step()
          x_step += 1
          if y_step % swa_freq == 0 and y_step > swa_start:
              y_n_avg += 1
              y_sum += y.data
          if x_step % swa_freq == 0 and x_step > swa_start:
              x_n_avg += 1
              x_sum += x.data
              x_avg = x_sum / x_n_avg
  
      opt.finalize()
      x_avg = x_sum / x_n_avg
      y_avg = y_sum / y_n_avg
      assert equal(x_avg, x)
      assert equal(y_avg, y)
  
imports: import contextlib
  import functools
  import re
  import warnings
  from unittest import TestCase
  import torch
  from pytext.optimizer import Lamb, StochasticWeightAveraging
  from torch import nn, optim, sparse
  from torch.autograd import Variable
  from torch.utils import data
label_desc: Use image node_id 13 for calling the TestSWA obj's underlying member method code with example usage: obj.test_swa_auto_group_added_during_run() without return types