赞
踩
源码见Pytorch官网,https://pytorch.apachecn.org/docs/1.0/nlp_word_embeddings_tutorial.html
分析写在注释了,有什么不足或者不对的地方,请回复讨论,加油
-
- # 代码作者: Robert Guthrie
-
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
- import torch.optim as optim
-
-
- CONTEXT_SIZE = 2
- #CONTEXT_SIZE表示我们想由前面的几个单词来预测这个单词,
- #这里设置为2,就是说我们希望通过这个单词的前两个单词来预测这一个单词。
- EMBEDDING_DIM = 10
- # 我们用莎士比亚的十四行诗 Sonnet 2
- test_sentence = """When forty winters shall besiege thy brow,
- And dig deep trenches in thy beauty's field,
- Thy youth's proud livery so gazed on now,
- Will be a totter'd weed of small worth held:
- Then being asked, where all thy beauty lies,
- Where all the treasure of thy lusty days;
- To say, within thine own deep sunken eyes,
- Were an all-eating shame, and thriftless praise.
- How much more praise deserv'd thy beauty's use,
- If thou couldst answer 'This fair child of mine
- Shall sum my count, and make my old excuse,'
- Proving his beauty by succession thine!
- This were to be new made when thou art old,
- And see thy blood warm when thou feel'st it cold.""".split()
- # 应该对输入变量进行标记,但暂时忽略。
- # 创建一系列的元组,每个元组都是([ word_i-2, word_i-1 ], target word)的形式。

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。