Commit 2c0e6df5 authored by Gao, Xiang's avatar Gao, Xiang Committed by Farhad Ramezanghorbani
Browse files

Use modern type annotations for nn.py (#373)

* Use modern type annotations for nn.py

* flake8
parent 3af8c49e
import torch import torch
from torch import Tensor
from typing import Tuple from typing import Tuple
...@@ -25,8 +26,7 @@ class ANIModel(torch.nn.Module): ...@@ -25,8 +26,7 @@ class ANIModel(torch.nn.Module):
def __getitem__(self, i): def __getitem__(self, i):
return self.module_list[i] return self.module_list[i]
def forward(self, species_aev): def forward(self, species_aev: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tensor]:
# type: (Tuple[torch.Tensor, torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]
species, aev = species_aev species, aev = species_aev
species_ = species.flatten() species_ = species.flatten()
aev = aev.flatten(0, 1) aev = aev.flatten(0, 1)
...@@ -51,8 +51,7 @@ class Ensemble(torch.nn.Module): ...@@ -51,8 +51,7 @@ class Ensemble(torch.nn.Module):
self.modules_list = torch.nn.ModuleList(modules) self.modules_list = torch.nn.ModuleList(modules)
self.size = len(self.modules_list) self.size = len(self.modules_list)
def forward(self, species_input): def forward(self, species_input: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tensor]:
# type: (Tuple[torch.Tensor, torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]
sum_ = 0 sum_ = 0
for x in self.modules_list: for x in self.modules_list:
sum_ += x(species_input)[1] sum_ += x(species_input)[1]
...@@ -70,8 +69,7 @@ class Sequential(torch.nn.Module): ...@@ -70,8 +69,7 @@ class Sequential(torch.nn.Module):
super(Sequential, self).__init__() super(Sequential, self).__init__()
self.modules_list = torch.nn.ModuleList(modules) self.modules_list = torch.nn.ModuleList(modules)
def forward(self, input_): def forward(self, input_: Tuple[Tensor, Tensor]) -> Tuple[Tensor, Tensor]:
# type: (Tuple[torch.Tensor, torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]
for module in self.modules_list: for module in self.modules_list:
input_ = module(input_) input_ = module(input_)
return input_ return input_
...@@ -79,5 +77,5 @@ class Sequential(torch.nn.Module): ...@@ -79,5 +77,5 @@ class Sequential(torch.nn.Module):
class Gaussian(torch.nn.Module): class Gaussian(torch.nn.Module):
"""Gaussian activation""" """Gaussian activation"""
def forward(self, x): def forward(self, x: Tensor) -> Tensor:
return torch.exp(- x * x) return torch.exp(- x * x)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment