当前位置:   article > 正文

54 循环神经网络 RNN【动手学深度学习v2】

54 循环神经网络 RNN【动手学深度学习v2】

  1. %matplotlib inline
  2. import math
  3. import torch
  4. from torch import nn
  5. from torch.nn import functional as F
  6. from d2l import torch as d2l
  7. batch_size, num_steps = 32, 35
  8. train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
  9. F.one_hot(torch.tensor([0, 2]), len(vocab))
  10. X = torch.arange(10).reshape((2, 5))
  11. F.one_hot(X.T, 28).shape
  12. def get_params(vocab_size, num_hiddens, device):
  13. num_inputs = num_outputs = vocab_size
  14. def normal(shape):
  15. return torch.randn(size=shape, device=device) * 0.01
  16. # 隐藏层参数
  17. W_xh = normal((num_inputs, num_hiddens))
  18. W_hh = normal((num_hiddens, num_hiddens))
  19. b_h = torch.zeros(num_hiddens, device=device)
  20. # 输出层参数
  21. W_hq = normal((num_hiddens, num_outputs))
  22. b_q = torch.zeros(num_outputs, device=device)
  23. # 附加梯度
  24. params = [W_xh, W_hh, b_h, W_hq, b_q]
  25. for param in params:
  26. param.requires_grad_(True)
  27. return params

以后根据学习进度会随时补偿每个笔记的知识的

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

闽ICP备14008679号