-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
Sequential
: Allow pickling (#9369)
- Loading branch information
Showing
4 changed files
with
215 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,22 @@ | ||
import typing | ||
from typing import * | ||
|
||
import torch | ||
from torch import Tensor | ||
|
||
import torch_geometric.typing | ||
from torch_geometric.typing import * | ||
{% for module in modules %} | ||
from {{module}} import * | ||
{%- endfor %} | ||
|
||
|
||
class Sequential(torch.nn.Module): | ||
def reset_parameters(self) -> None: | ||
{%- for child in children %} | ||
if hasattr(self.{{child.name}}, 'reset_parameters'): | ||
self.{{child.name}}.reset_parameters() | ||
def forward( | ||
self, | ||
{%- for param in signature.param_dict.values() %} | ||
{{param.name}}: {{param.type_repr}}, | ||
{%- endfor %} | ||
) -> {{signature.return_type_repr}}: | ||
|
||
def forward(self, {{ input_types|join(', ') }}) -> {{return_type}}: | ||
{%- for child in children %} | ||
{{child.return_names|join(', ')}} = self.{{child.name}}({{child.param_names|join(', ')}}) | ||
{{child.return_names|join(', ')}} = self.{{child.name}}({{child.param_names|join(', ')}}) | ||
{%- endfor %} | ||
return {{children[-1].return_names|join(', ')}} | ||
|
||
def __getitem__(self, idx: int) -> torch.nn.Module: | ||
return getattr(self, self._module_names[idx]) | ||
|
||
def __len__(self) -> int: | ||
return {{children|length}} | ||
|
||
def __repr__(self) -> str: | ||
module_reprs = [ | ||
f' ({i}) - {self[i]}: {self._module_descs[i]}' | ||
for i in range(len(self)) | ||
] | ||
return 'Sequential(\n{}\n)'.format('\n'.join(module_reprs)) | ||
return {{children[-1].return_names|join(', ')}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters