Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. def receptive_field(model, X):
  2. half_size = 14
  3.  
  4. xs_f = torch.tensor(
  5. X.astype(np.float32),
  6. requires_grad=True
  7. ).to(DEVICE)
  8.  
  9. xs = torch.tensor(X)
  10.  
  11. ps = model1(xs_f)
  12. ps = ps[:, 0, half_size, half_size].reshape(-1, 1, 1, 1, 4)
  13. xs = xs[:, 0, half_size, half_size].reshape(-1, 1, 1, 1).to(DEVICE)
  14.  
  15. criterion = MyNLLLoss
  16.  
  17. loss = criterion(xs, ps)
  18. xs_f.retain_grad()
  19. loss.backward()
  20.  
  21. img = xs_f.grad.abs().sum(dim=(0, 1))
  22. img = img.float().cpu().numpy()
  23.  
  24. plt.imshow(img)
  25. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement