16 PyTorch神经网络基础 [动手学深度学习v2]

  1. 参数绑定

    在不同的网络之间共享权重

    shared = nn.Linear(8, 8)
    net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), shared, nn.ReLU(), shared, nn.ReLU(), nn.Linear(8, 1))
    print(net[2].weight.data[0] == net[4].weight.data[0])
    # tensor([True, True, True, True, True, True, True, True])
    net[2].weight.data[0, 0] = 100
    print(net[2].weight.data[0] == net[4].weight.data[0])
    # tensor([True, True, True, True, True, True, True, True])
    

你可能感兴趣的:(跟李沐学AI,深度学习,pytorch)