Link Rules & MLP with sigle or multi output

import torch
import torch.nn.functional as F


def main():
    # link rule
    # x0 = torch.tensor(1)
    # w1 = torch.tensor(1., requires_grad=True)
    # b1 = torch.tensor(1.)
    # y1 = x0*w1+b1
    # w2 = torch.tensor(1., requires_grad=True)
    # b2 = torch.tensor(1.)
    # y2 = y1*w2+b2
    # dy2_dy1 = torch.autograd.grad(y2, [y1], retain_graph=True)[0]
    # dy1_dw1 = torch.autograd.grad(y1, [w1], retain_graph=True)[0]
    # dy2_dw1 = torch.autograd.grad(y2, [w1], retain_graph=True)[0]
    # print(torch.eq(dy2_dy1*dy1_dw1, dy2_dw1))
    # single output perceptron
    # X = torch.rand(1,10)
    # W = torch.randn(1,10, requires_grad=True)
    # O = torch.sigmoid([email protected]())
    # print(O)
    # t = torch.ones(1,1)
    # E = 0.5 * F.mse_loss(O, t)
    # E.backward()
    # print(W.grad)
    # print((O-t)*O*(1-O)*X)
    # print(torch.eq(W.grad, ((O-t)*O*(1-O)*X).detach()))
    # multi output perceptron
    X1 = torch.rand(1,10)
    W1 = torch.randn(5,10, requires_grad=True)
    O1 = torch.sigmoid([email protected]()) # [1,5]
    t1 = torch.ones(1,5)
    E1 = 0.5 * F.mse_loss(O1, t1)
    E1.backward()
    temp = ((O1-t1)*O1*(1-O1)).detach() # [1,5]
    print("X1:\n{}\nW1:\n{}\nO1:\n{}".format(X1,W1,O1))
    print("W1.grad:\n{}".format(W1.grad))
    print("W1.grad`:\n{}".format((X1.t()@temp).t()/5))
    print(torch.eq(W1.grad, (X1.t()@temp).t()/5))
    pass


if __name__ == '__main__':
    main()

你可能感兴趣的:(Deep,Learning,python,pytorch)