Nn.init: AttributeError: 'module' object has no attribute 'init'

In the official documents, i find this method:

torch.nn.init.xavier_uniform(tensor, gain=1)

but when i use it, it raise an error:

xavier = nn.init.xavier_normal

AttributeError: ‘module’ object has no attribute ‘init’

and i import torch in ipython to test it, i didn’t find init either

why?

You need to import it before you can use it. Its a submodule
import torch.nn.init as weight_init

Thank you~ seems i am not very familiar with python :hugging: