Skip to content

Instantly share code, notes, and snippets.

View pvjosue's full-sized avatar

Josué Page Vizcaíno pvjosue

View GitHub Profile
@dboyliao
dboyliao / pytorch_lagrange_multi.py
Last active January 29, 2023 15:48
Simple Example: Solving Lagrange Multiplier with PyTorch
import torch
x = torch.tensor(0, requires_grad=True, dtype=torch.float64)
y = torch.tensor(0, requires_grad=True, dtype=torch.float64)
l = torch.tensor(0, requires_grad=True, dtype=torch.float64)
lr = 0.1
# min x^2+y^2 s.t x+y = 1
for i in range(100):
L = x**2 + y**2 + l*(1-x-y)
@sbarratt
sbarratt / torch_jacobian.py
Created May 9, 2019 19:40
Get the jacobian of a vector-valued function that takes batch inputs, in pytorch.
def get_jacobian(net, x, noutputs):
x = x.squeeze()
n = x.size()[0]
x = x.repeat(noutputs, 1)
x.requires_grad_(True)
y = net(x)
y.backward(torch.eye(noutputs))
return x.grad.data