code: def _test_rosenbrock_sparse(self, constructor, sparse_only=False):
      params = torch.tensor([1.5, 1.5], requires_grad=True)
      optimizer = constructor([params])
  
      if not sparse_only:
          params_c = params.detach().clone().requires_grad_()
          optimizer_c = constructor([params_c])
  
      solution = torch.tensor([1.0, 1.0])
      initial_dist = params.data.dist(solution)
  
      def eval(params, sparse_grad, w):
          # Depending on w, provide only the x or y gradient
          optimizer.zero_grad()
          loss = rosenbrock(params)
          loss.backward()
          grad = drosenbrock(params.data)
          # NB: We torture test the optimizer by returning an
          # uncoalesced sparse tensor
          if w:
              i = torch.LongTensor([[0, 0]])
              x = grad[0]
              v = torch.DoubleTensor([x / 4.0, x - x / 4.0])
          else:
              i = torch.LongTensor([[1, 1]])
              y = grad[1]
              v = torch.DoubleTensor([y - y / 4.0, y / 4.0])
          x = sparse.DoubleTensor(i, v, torch.Size([2]))
          if sparse_grad:
              params.grad.data = x
          else:
              params.grad.data = x.to_dense()
          return loss
  
      for i in range(2000):
          # Do cyclic coordinate descent
          w = i % 2
          optimizer.step(functools.partial(eval, params, True, w))
          if not sparse_only:
              optimizer_c.step(
                  functools.partial(eval, params_c, False, w)
              )
              assert equal(params.data, params_c.data)
  
      self.assertLessEqual(params.data.dist(solution), initial_dist)
  
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 3 for calling the TestSWA obj's underlying member method code with example usage: obj._test_rosenbrock_sparse(constructor, sparse_only) and returns: loss