SHARE
TWEET

Untitled

a guest Aug 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import torch
  2. from torch.nn import Parameter
  3. from torch.autograd import Variable
  4. import torch.nn.functional as F
  5. torch.manual_seed(9)
  6.  
  7. #### create RNN cell ###########
  8. rnn_a = torch.nn.RNNCell(input_size=1,hidden_size=1,bias=False)
  9.  
  10. #### Print All Parameter in RnnCell ###########
  11. for weight in rnn_a.parameters():
  12.     print(weight)
  13.    
  14. #### Create Input (batch,sequence,feature vector) ###########
  15. random_input = Variable(torch.FloatTensor(1, 3, 1).normal_(), requires_grad=False)  #batch,sequence,feature
  16. random_input[0,0,0] = 1
  17. random_input[:, 0, 0]
  18. h0 = Variable(torch.zeros(1,1),requires_grad=False)
  19. print(random_input)
  20. print(h0)
  21.  
  22. #### Show Hidden_value in time step1 compare with manual calculate ###########
  23. h1 = rnn_a(random_input[:,0,:], h0)
  24. print(h1)
  25. print("ht = tanh(0.3116) = ",F.tanh(torch.tensor(0.3116)))
  26.  
  27. #### Show Hidden_value in time step2 compare with manual calculate ###########
  28. h2 = rnn_a(random_input[:,1,:], h1)
  29. print(h2)
  30. print("Manual calculate")
  31. print( F.tanh(torch.tensor((0.3116)*(-0.7775) + (-0.3960) * (0.3019))))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top