当前位置:   article > 正文

RNN的前向传播函数_result = _vf.lstm(input, hx, self._flat_weights, s

result = _vf.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,

 

程序:

  1. import torch as t
  2. from torch import nn
  3. from torch.autograd import Variable as V
  4. t.manual_seed(1000)
  5. # 输入 batch_size=3,序列长度都为2,序列中每个元素占4
  6. input=V(t.randn(2,3,4))
  7. print(input)
  8. # lstm 输入向量4维,3个隐藏元,1
  9. lstm=nn.LSTM(4,3,1)
  10. # 初始状态:1层,batch_size=3,3个隐层元
  11. h0=V(t.randn(1,3,3))
  12. c0=V(t.randn(1,3,3))
  13. out,(hn,cn)=lstm((input,h0),c0)
  14. out,(hn,cn)=lstm(input,(h0,c0))
  15. print(out)
  16. print("hn=\n",hn)
  17. print("cn=\n",cn)
  18. out,hn=lstm(input,(h0,c0))
  19. print(out)
  20. print("hn=\n",hn)

 

这样写,out,(hn,cn)=lstm((input,h0),c0) 会报错:

  1. /home/wangbin/anaconda3/envs/deep_learning/bin/python3.7 /home/wangbin/anaconda3/envs/deep_learning/project/main.py
  2. Traceback (most recent call last):
  3. File "/home/wangbin/anaconda3/envs/deep_learning/project/main.py", line 21, in <module>
  4. out,(hn,cn)=lstm((input,h0),c0)
  5. File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
  6. result = self.forward(*input, **kwargs)
  7. File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/rnn.py", line 564, in forward
  8. max_batch_size = input.size(0) if self.batch_first else input.size(1)
  9. AttributeError: 'tuple' object has no attribute 'size'
  10. tensor([[[-0.5306, -1.1300, -0.6734, -0.7669],
  11. [-0.7029, 0.9896, -0.4482, 0.8927],
  12. [-0.6043, 1.0726, 1.0481, 1.0527]],
  13. [[-0.6424, -1.2234, -1.0794, -0.6037],
  14. [-0.7926, -0.1414, -1.0225, -0.0482],
  15. [ 0.6610, -0.8908, 1.4793, -0.3934]]])
  16. Process finished with exit code 1

 

根据报错信息查看:

可以发现lstm的forward具有三个参数,self、input、hx,但是hx可以再分。

  1. def forward(self, input, hx=None): # noqa: F811
  2. orig_input = input
  3. # xxx: isinstance check needs to be in conditional for TorchScript to compile
  4. if isinstance(orig_input, PackedSequence):
  5. input, batch_sizes, sorted_indices, unsorted_indices = input
  6. max_batch_size = batch_sizes[0]
  7. max_batch_size = int(max_batch_size)
  8. else:
  9. batch_sizes = None
  10. max_batch_size = input.size(0) if self.batch_first else input.size(1)
  11. sorted_indices = None
  12. unsorted_indices = None
  13. if hx is None:
  14. num_directions = 2 if self.bidirectional else 1
  15. zeros = torch.zeros(self.num_layers * num_directions,
  16. max_batch_size, self.hidden_size,
  17. dtype=input.dtype, device=input.device)
  18. hx = (zeros, zeros)
  19. else:
  20. # Each batch of the hidden state should match the input sequence that
  21. # the user believes he/she is passing in.
  22. hx = self.permute_hidden(hx, sorted_indices)
  23. self.check_forward_args(input, hx, batch_sizes)
  24. if batch_sizes is None:
  25. result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
  26. self.dropout, self.training, self.bidirectional, self.batch_first)
  27. else:
  28. result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias,
  29. self.num_layers, self.dropout, self.training, self.bidirectional)
  30. output = result[0]
  31. hidden = result[1:]
  32. # xxx: isinstance check needs to be in conditional for TorchScript to compile
  33. if isinstance(orig_input, PackedSequence):
  34. output_packed = PackedSequence(output, batch_sizes, sorted_indices, unsorted_indices)
  35. return output_packed, self.permute_hidden(hidden, unsorted_indices)
  36. else:
  37. return output, self.permute_hidden(hidden, unsorted_indices)

 

 

如果这样写out,(hn,cn)=lstm(input,h0,c0) ,仍然报错:

  1. /home/wangbin/anaconda3/envs/deep_learning/bin/python3.7 /home/wangbin/anaconda3/envs/deep_learning/project/main.py
  2. tensor([[[-0.5306, -1.1300, -0.6734, -0.7669],
  3. [-0.7029, 0.9896, -0.4482, 0.8927],
  4. [-0.6043, 1.0726, 1.0481, 1.0527]],
  5. [[-0.6424, -1.2234, -1.0794, -0.6037],
  6. [-0.7926, -0.1414, -1.0225, -0.0482],
  7. [ 0.6610, -0.8908, 1.4793, -0.3934]]])
  8. Traceback (most recent call last):
  9. File "/home/wangbin/anaconda3/envs/deep_learning/project/main.py", line 21, in <module>
  10. out,(hn,cn)=lstm(input,h0,c0)
  11. File "/home/wangbin/anaconda3/envs/deep_learning/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
  12. result = self.forward(*input, **kwargs)
  13. TypeError: forward() takes from 2 to 3 positional arguments but 4 were given
  14. Process finished with exit code 1

 

错误原因是self是人家的默认参数,虽然实参里面没写,但是形参里面已经存在,是默认传递的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/362569
推荐阅读
相关标签
  

闽ICP备14008679号