单层感知机
x=torch.randn(1,10)
w=torch.randn(1,10,requires_grad=True)
o=torch.sigmoid(x@w.t())
o.shape
loss=F.mse_loss(torch.ones(1,1),o)
loss.shape
loss.backward()
w.grad
51
tensor([[-0.0007, -0.0001, -0.0038, 0.0019, 0.0002, -0.0032, 0.0047, -0.0029,
-0.0018, 0.0030]])
MLP及梯度
x=torch.randn(1,10)
w=torch.randn(2,10,requires_grad=True)
o=torch.sigmoid(x@w.t())
o.shape
loss=F.mse_loss(torch.ones(1,1),o)
loss.shape
loss.backward()
w.grad
D:\Software\anaconda3\envs\pytorch\lib\site-packages\ipykernel_launcher.py:7: UserWarning: Using a target size (torch.Size([1, 2])) that is different to the input size (torch.Size([1, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
import sys
52
tensor([[-2.2506e-04, 5.8902e-05, 2.8970e-04, -1.0290e-04, -2.3394e-04,
6.1625e-05, 7.8519e-05, 2.3708e-04, 1.4745e-05, 1.7960e-05],
[-3.2697e-02, 8.5576e-03, 4.2089e-02, -1.4950e-02, -3.3987e-02,
8.9532e-03, 1.1408e-02, 3.4444e-02, 2.1422e-03, 2.6092e-03]])