Here is a small sample of code:
import torch
import torch.nn as nn
def func(m: nn.Module):
p_grad = m.weight.grad.data
return None
I am wondering, is it good to assign m.weight.grad.data
to a variable and use it later? The reason is m.weight.grad
can also be None
. In pytorch 1.13.0, mypy throws error: Item "None" of "Union[Tensor, None, Module]" has no attribute "data"
.
I use the data
attribute to implement a custom optimizer.