code: def test_strict_shape(self):
      net1 = torch.nn.Sequential(*[torch.nn.PReLU(num_parameters=5)])
      data1 = net1.state_dict()
      data1["0.weight"] = torch.tensor([1, 2, 3, 4, 5])
      data1["new"] = torch.tensor(0.1)
      net1.load_state_dict(data1, strict=False)
      opt1 = optim.SGD(net1.parameters(), lr=0.02)
  
      net2 = torch.nn.Sequential(*[torch.nn.PReLU(), torch.nn.PReLU()])
      data2 = net2.state_dict()
      data2["0.weight"] = torch.tensor([0.2])
      data2["1.weight"] = torch.tensor([0.3])
      net2.load_state_dict(data2)
      opt2 = optim.SGD(net2.parameters(), lr=0.02)
  
      with tempfile.TemporaryDirectory() as tempdir:
          engine = Engine(lambda e, b: None)
          CheckpointSaver(
              save_dir=tempdir,
              save_dict={"net": net1, "opt": opt1},
              save_final=True,
          ).attach(engine)
          engine.run([0] * 8, max_epochs=5)
          path = tempdir + "/checkpoint_final_iteration=40.pt"
          engine = Engine(lambda e, b: None)
          CheckpointLoader(
              load_path=path,
              # expect to print a warning because it loads not only `net` but also `opt` with `strict_shape=False`
              load_dict={"net": net2, "opt": opt2},
              strict=False,
              strict_shape=False,
          ).attach(engine)
          engine.run([0] * 8, max_epochs=1)
          assert_allclose(
              net2.state_dict()["0.weight"].cpu(), torch.tensor([0.2])
          )
          # test whether `opt2` had been skipped when loading with `strict_shape=False`,
          # it should have 2 items in `params`(0.weight and 1.weight) while the checkpoint has 1 item(0.weight)
          self.assertEqual(
              len(opt1.state_dict()["param_groups"][0]["params"]), 1
          )
          self.assertEqual(
              len(opt2.state_dict()["param_groups"][0]["params"]), 2
          )
  
file_path: MONAI/tests/test_handler_checkpoint_loader.py
imports: from __future__ import annotations
  import tempfile
  import unittest
  import torch
  import torch.optim
  from ignite.engine import Engine, Events
  from monai.handlers import CheckpointLoader, CheckpointSaver
  from tests.utils import assert_allclose
label_desc: Use image node_id 6 for calling the TestHandlerCheckpointLoader obj's underlying member method code with example usage: obj.test_strict_shape() without return types