美文网首页
GPT图解:代码记录-RNN模型

GPT图解:代码记录-RNN模型

作者: 万州客 | 来源:发表于2024-02-21 08:27 被阅读0次

    一,代码

    sentences = ["我 喜欢 玩具", "我 爱 爸爸", "我 讨厌 挨打"]
    word_list = list(set(" ".join(sentences).split()))
    word_to_idx = {word: idx for idx, word in enumerate(word_list)}
    idx_to_word = {idx: word for idx, word in enumerate(word_list)}
    voc_size = len(word_list)
    print("词汇表:",word_to_idx)
    print("词汇表大小:", voc_size)
    
    import torch
    import random
    batch_size = 2
    def make_batch():
        input_batch = []
        target_batch = []
        selected_sentences = random.sample(sentences,batch_size)
        for sen in selected_sentences:
            word = sen.split()
            input = [word_to_idx[n] for n in word[:-1]]
            target = word_to_idx[word[-1]]
            input_batch.append(input)
            target_batch.append(target)
        input_batch = torch.LongTensor(input_batch)
        target_batch = torch.LongTensor(target_batch)
        return input_batch, target_batch
    
    input_batch, target_batch = make_batch()
    print("输入批处理数据:", input_batch)
    input_words = []
    for input_idx in input_batch:
        input_words.append([idx_to_word[idx.item()] for idx in input_idx])
    print("输入批处理数据对应的原始词: ", input_words)
    print("目标批处理数据: ", target_batch)
    target_words = [idx_to_word[idx.item()] for idx in target_batch]
    print("目标批处理数据对应的原始词: ", target_words)
    
    n_step = 2
    n_hidden = 2
    embedding_size = 2
    import torch.nn as nn
    class NPLM(nn.Module):
        def __init__(self):
            super(NPLM, self).__init__()
            self.C = nn.Embedding(voc_size, embedding_size)
            self.lstm = nn.LSTM(embedding_size, n_hidden, batch_first=True)
            self.linear = nn.Linear(n_hidden, voc_size)
        def forward(self, X):
            X = self.C(X)
            lstm_out, _ = self.lstm(X)
            output = self.linear(lstm_out[:, -1, :])
            return output
    
    model = NPLM()
    print("RNN模型结构: ", model)
    
    import torch.optim as option
    criterion = nn.CrossEntropyLoss()
    optimizer = option.Adam(model.parameters(), lr=0.1)
    for epoch in range(5000):
        optimizer.zero_grad()
        input_batch, target_batch = make_batch()
        output = model(input_batch)
        loss = criterion(output, target_batch)
        if (epoch+1) % 1000 == 0:
            print("Epoch: ", '%04d' % (epoch+1), 'cost=', '{:.6f}'.format(loss))
        loss.backward()
        optimizer.step()
    
    input_strs = [['我', '讨厌'], ['我', '喜欢']]
    input_indices = [[word_to_idx[word] for word in seq] for seq in input_strs]
    input_batch = torch.LongTensor(input_indices)
    predict = model(input_batch).data.max(1)[1]
    predict_strs = [idx_to_word[n.item()] for n in predict.squeeze()]
    for input_seq, pred in zip(input_strs, predict_strs):
        print(input_seq, '->', pred)
    
    
    

    二,输出

    D:\Python310\python.exe D:\tmp\GPT\RNN.py 
    词汇表: {'玩具': 0, '挨打': 1, '喜欢': 2, '爱': 3, '讨厌': 4, '我': 5, '爸爸': 6}
    词汇表大小: 7
    输入批处理数据: tensor([[5, 4],
            [5, 2]])
    输入批处理数据对应的原始词:  [['我', '讨厌'], ['我', '喜欢']]
    目标批处理数据:  tensor([1, 0])
    目标批处理数据对应的原始词:  ['挨打', '玩具']
    RNN模型结构:  NPLM(
      (C): Embedding(7, 2)
      (lstm): LSTM(2, 2, batch_first=True)
      (linear): Linear(in_features=2, out_features=7, bias=True)
    )
    Epoch:  1000 cost= 0.000779
    Epoch:  2000 cost= 0.000248
    Epoch:  3000 cost= 0.000122
    Epoch:  4000 cost= 0.000082
    Epoch:  5000 cost= 0.000035
    ['我', '讨厌'] -> 挨打
    ['我', '喜欢'] -> 玩具
    
    Process finished with exit code 0
    
    

    相关文章

      网友评论

          本文标题:GPT图解:代码记录-RNN模型

          本文链接:https://www.haomeiwen.com/subject/eajundtx.html