-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextending_pytorch_functions.py
71 lines (57 loc) · 1.86 KB
/
extending_pytorch_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import torch
import numpy as np
#def __init__(self, reduce = False):
# super(U, self).__init__()
# self.reduce = reduce
# print self.reduce
class MDSim:
def __init__(self, strinit):
self.strinit = strinit
class U(torch.autograd.Function):
# This method should provide the potential energy or -log(p(x)). This needs to be decided.
@staticmethod
def forward(ctx, input, mdsimulator, reduce = False):
output = input.data*input.data
print output
#print 'Reduce {}'.format(reduce)
if reduce or input.dim()==1:
output = output.sum()
else:
output = output.sum(dim=1, keepdim=True)
ctx.save_for_backward(input)
ctx.reduce = reduce
ctx.bDebug = False
#output = torch.tensor(43.)
print mdsimulator.strinit
return output
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
#grad = input.mul(2.)
dtypeinput = input.dtype
if input.dim() == 1:
grad = torch.from_numpy(np.array([2., 4., 6.])).type(dtypeinput)
else:
grad = torch.from_numpy(np.array([[2., 4., 6.],[4.,6.,8.]])).type(dtypeinput)
gradorig = input.mul(2.)
grad_input = grad_output*grad
if ctx.bDebug:
print 'Grad_output'
print grad_output
print 'Grad_input'
print grad_input
return grad_input, None, None
#x = torch.autograd.Variable(torch.tensor([1,2,3], dtype=torch.float32, requires_grad = True))
x = torch.tensor([[1,2,3], [2,3,4]], dtype=torch.float32, requires_grad = True)
#x = torch.tensor([1,2,3], dtype=torch.float32, requires_grad = True)
mdsim = MDSim('inits')
#f = uinstance(x, uinstance, True)
f = U.apply(x, mdsim, True)
y = f
z = y.pow(2.)
z.backward(retain_graph=True)
torch.autograd.grad(z,y, retain_graph=True)
torch.autograd.grad(y,x, retain_graph=True)
torch.autograd.grad(z,x, retain_graph=True)
print x.grad
quit()