Code: Select all
from torch import nn
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.param = nn.Parameter(torch.randn(1, 1))
def forward(self, x):
x = x * self.param
return x
model = MyModel()
print(dict(model.named_parameters()))
# {'param': Parameter containing:
# tensor([[0.6077]], requires_grad=True)}
out = model(torch.randn(1, 1))
loss = out.mean()
loss.backward()
print(model.param.grad)
# tensor([[-1.3033]])
P.S ist. Ich markiere sowohl Python als auch Julia , da ich der Meinung bin>