当前位置:   article > 正文

NNLM的Pytorch实现_mamba torch 实现

mamba torch 实现
import torch
import torch.nn as nn
import torch.optim as optimizer
import torch.utils.data as Data

dtype = torch.FloatTensor

sentences = ['i like cat','i love coffee','i hate milk']
sentences_list = " ".join(sentences).split() # ['i', 'like', 'cat', 'i', 'love', 'coffee', 'i', 'hate', 'milk']
# print(sentences_list)
# 构建词汇表
vocab = list(set(sentences_list)) # 用set去重,再转换成list # ['i', 'like', 'cat', 'love', 'coffee', 'hate', 'milk']
word2idx = {
   w:i for i, w in enumerate(vocab)} # {'i':0, 'like':1, 'cat':2, 'love':3, 'coffee':4, 'hate':5, 'milk':6}
idx2word = {
   i:w for i, w in enumerate(vocab)}  # {0:'i', 1:'like', 2:'cat', 3:'love', 4:'coffee', 5:'hate', 6:'milk'}

n_class = len(vocab) # number of Vocabulary, just like |V|, in this task n_class=7

# 定义参数  NNLM(Neural Network Language Model) Parameter
m = 2 # m in paper, word embedding dim
n_step = 2 # 输入句子的长度 # n-1 in paper, look back n_step words and predict next word. In this task n_step=2
n_hidden = 10 # 隐藏层神经元个数 # h in paper

# 构建X
def make_data(sentences):
    input_data = [
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/939746
推荐阅读
相关标签
  

闽ICP备14008679号