Skip to content

multiple_containers

MultipleContainers

Bases: BaseContainer

Contains other containers and initializes them.

Source code in pytorch_adapt\containers\multiple_containers.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class MultipleContainers(BaseContainer):
    """
    Contains other containers and initializes them.
    """

    def __init__(self, **kwargs):
        self.store = kwargs

    def merge(self, **kwargs: Union[BaseContainer, None]):
        """
        Merges the input containers into any existing sub-containers.
        """
        for k, v in kwargs.items():
            if isinstance(v, BaseContainer):
                if k in self:
                    self[k].merge(v)
                else:
                    self[k] = v
            elif v is None:
                if k not in self:
                    self[k] = get_container(k)
            else:
                raise TypeError(
                    f"Input to {c_f.cls_name(self)}.merge must be BaseContainer or None"
                )

    def create(self):
        """
        Calls [```.create()```][pytorch_adapt.containers.BaseContainer.create]
        or [```.create_with()```][pytorch_adapt.containers.BaseContainer.create_with]
        on sub-containers.

        - Optimizers are created with models as input.
        - LR schedulers are created with optimizers as input.
        """
        self["models"].create()
        self["optimizers"].create_with(self["models"])
        self["lr_schedulers"].create_with(self["optimizers"])
        self["misc"].create()

create()

Calls .create() or .create_with() on sub-containers.

  • Optimizers are created with models as input.
  • LR schedulers are created with optimizers as input.
Source code in pytorch_adapt\containers\multiple_containers.py
50
51
52
53
54
55
56
57
58
59
60
61
62
def create(self):
    """
    Calls [```.create()```][pytorch_adapt.containers.BaseContainer.create]
    or [```.create_with()```][pytorch_adapt.containers.BaseContainer.create_with]
    on sub-containers.

    - Optimizers are created with models as input.
    - LR schedulers are created with optimizers as input.
    """
    self["models"].create()
    self["optimizers"].create_with(self["models"])
    self["lr_schedulers"].create_with(self["optimizers"])
    self["misc"].create()

merge(**kwargs)

Merges the input containers into any existing sub-containers.

Source code in pytorch_adapt\containers\multiple_containers.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def merge(self, **kwargs: Union[BaseContainer, None]):
    """
    Merges the input containers into any existing sub-containers.
    """
    for k, v in kwargs.items():
        if isinstance(v, BaseContainer):
            if k in self:
                self[k].merge(v)
            else:
                self[k] = v
        elif v is None:
            if k not in self:
                self[k] = get_container(k)
        else:
            raise TypeError(
                f"Input to {c_f.cls_name(self)}.merge must be BaseContainer or None"
            )