Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. import torch
  2. y = torch.tensor(data[4 * sr : 4 * sr + 1000])
  3. x = torch.linspace(0.0, 1.0, steps=len(y))
  4.  
  5. w = torch.rand(100, requires_grad=True)
  6. phi = torch.rand(100, requires_grad=True)
  7.  
  8. for t in range(200):
  9. y_pred = sum(w[i] * torch.sin((x + phi[i]) * i) for i in range(len(w)))
  10.  
  11. loss = torch.mean((y_pred - y) ** 2)
  12. loss.backward()
  13.  
  14. w.data = w.data - 0.05 * w.grad.data
  15. phi.data = phi.data - 0.05 * phi.grad.data
  16.  
  17. w.grad.data.zero_()
  18. phi.grad.data.zero_()
  19. if t % 10 == 0:
  20. plt.plot(y_pred.data.numpy())
  21. plt.plot(y.numpy())
  22. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement