In [187]:
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 [188]:
time = Variable(T.linspace(0, 2*math.pi, m), requires_grad = True)

In [189]:
x = forward(time)

In [190]:
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 [191]:
plt.plot(time.data.numpy(), x.data.numpy(),'red')


Out[191]:
[<matplotlib.lines.Line2D at 0x7f20a136d400>]

In [192]:
plt.plot(time.data.numpy(), vel.data.numpy(),'red')


Out[192]:
[<matplotlib.lines.Line2D at 0x7f20a128bb38>]

In [193]:
plt.plot(time.data.numpy(), (a+x).data.numpy(),'red')


Out[193]:
[<matplotlib.lines.Line2D at 0x7f20a11b10b8>]

In [ ]:


In [ ]: