当前位置:   article > 正文

解释Pytorch 官方中文教程代码解析Word Embeddings词嵌入N-Gram语言模型_完成word embeddings in pytorch,打印hello和word词向量;完成an

完成word embeddings in pytorch,打印hello和word词向量;完成an example: n-gram

 

源码见Pytorch官网,https://pytorch.apachecn.org/docs/1.0/nlp_word_embeddings_tutorial.html

分析写在注释了,有什么不足或者不对的地方,请回复讨论,加油

  1. # 代码作者: Robert Guthrie
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.functional as F
  5. import torch.optim as optim
  6. CONTEXT_SIZE = 2
  7. #CONTEXT_SIZE表示我们想由前面的几个单词来预测这个单词,
  8. #这里设置为2,就是说我们希望通过这个单词的前两个单词来预测这一个单词。
  9. EMBEDDING_DIM = 10
  10. # 我们用莎士比亚的十四行诗 Sonnet 2
  11. test_sentence = """When forty winters shall besiege thy brow,
  12. And dig deep trenches in thy beauty's field,
  13. Thy youth's proud livery so gazed on now,
  14. Will be a totter'd weed of small worth held:
  15. Then being asked, where all thy beauty lies,
  16. Where all the treasure of thy lusty days;
  17. To say, within thine own deep sunken eyes,
  18. Were an all-eating shame, and thriftless praise.
  19. How much more praise deserv'd thy beauty's use,
  20. If thou couldst answer 'This fair child of mine
  21. Shall sum my count, and make my old excuse,'
  22. Proving his beauty by succession thine!
  23. This were to be new made when thou art old,
  24. And see thy blood warm when thou feel'st it cold.""".split()
  25. # 应该对输入变量进行标记,但暂时忽略。
  26. # 创建一系列的元组,每个元组都是([ word_i-2, word_i-1 ], target word)的形式。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/786472
推荐阅读
相关标签
  

闽ICP备14008679号