An error occurs when executing the code below.
import torch
import torch.nn as nn
class AAA(nn.Module):
def __init__(self):
super(AAA, self).__init__()
def forward(self, x):
return x
class BBB(nn.Module):
def __init__(self, other: AAA=None):
super(BBB, self).__init__()
print(type([other]))
self.other = [other]
def forward(self, x):
src = self.other[0] # !!!Error!!!
return x
if __name__ == '__main__':
from utils.functions import init_console
init_console()
net_a = AAA()
net_b = BBB(other=net_a)
script_a = torch.jit.script(net_a)
script_b = torch.jit.script(net_b)
print('done')
Module ‘BBB’ has no attribute ‘other’ (This attribute exists on the Python module, but we failed to convert Python type: ‘list’ to a TorchScript type. Could not infer type of list element: Cannot infer concrete type of torch.nn.Module. Its type was inferred; try adding a type annotation for the attribute.):
File “C:/xxx/test.py”, line 20
def forward(self, x):
src = self.other[0]
~~~~~~~~~~ <— HERE
return x
Is there a way to make it work?