In [199]:
import torch as T
import math
import numpy as np
from matplotlib import mlab
from matplotlib import pylab as plt
from torch.autograd import Variable, grad
%matplotlib inline
m=150
def forward(t):
return T.sin(t)
In [200]:
time = Variable(T.linspace(0, 4*math.pi, m), requires_grad = True)
x = forward(time)
x.backward(Variable(T.Tensor(m).fill_(1)), retain_graph = True, create_graph = True)
vel = time.grad.clone()
time.grad.data.zero_()
vel.backward(Variable(T.Tensor(m).fill_(1)))
a = time.grad.clone()
In [201]:
plt.plot(time.data.numpy(), x.data.numpy(),'red')
Out[201]:
In [202]:
plt.plot(time.data.numpy(), vel.data.numpy(),'red')
Out[202]:
In [203]:
plt.plot(time.data.numpy(), (a+x).data.numpy(),'red')
Out[203]:
In [ ]: