Send the model to device.
(self, device: torch.device)
| 113 | return parameters |
| 114 | |
| 115 | def to(self, device: torch.device): |
| 116 | """ |
| 117 | Send the model to device. |
| 118 | """ |
| 119 | self.device = device |
| 120 | |
| 121 | # base model |
| 122 | isModel = lambda net: isinstance(net, BaseModel) |
| 123 | nets = inspect.getmembers(self, isModel) # name, net |
| 124 | for name, net in nets: |
| 125 | getattr(self, name).to(device) |
| 126 | |
| 127 | # nn module |
| 128 | isNNModule = lambda net: isinstance(net, torch.nn.Module) |
| 129 | nets = inspect.getmembers(self, isNNModule) # name, net |
| 130 | for name, net in nets: |
| 131 | getattr(self, name).to(device) |
| 132 | |
| 133 | # buffer |
| 134 | for name in self.buffer_names: |
| 135 | b = getattr(self, name, None) |
| 136 | if b is None: |
| 137 | continue |
| 138 | else: |
| 139 | setattr(self, name, b.to(device=device)) |
| 140 | |
| 141 | # parameter |
| 142 | for name in self.parameter_names: |
| 143 | b = getattr(self, name, None) |
| 144 | if b is None: |
| 145 | continue |
| 146 | else: |
| 147 | setattr(self, name, b.to(device=device)) |
| 148 | |
| 149 | def train(self): |
| 150 | """Set all the nn.modules to train mode.""" |
no outgoing calls
no test coverage detected