国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

基于transformer的Seq2Seq機(jī)器翻譯模型訓(xùn)練、預(yù)測(cè)教程

這篇具有很好參考價(jià)值的文章主要介紹了基于transformer的Seq2Seq機(jī)器翻譯模型訓(xùn)練、預(yù)測(cè)教程。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

機(jī)器翻譯(Machine Translation, MT)是一類(lèi)將某種語(yǔ)言(源語(yǔ)言,source language)的句子 x x x翻譯成另一種語(yǔ)言(目標(biāo)語(yǔ)言,target language)的句子 y y y 的任務(wù)。機(jī)器翻譯的相關(guān)研究早在上世紀(jì)50年代美蘇冷戰(zhàn)時(shí)期就開(kāi)始了,當(dāng)時(shí)的機(jī)器翻譯系統(tǒng)是基于規(guī)則的,利用兩種語(yǔ)言的單詞、短語(yǔ)對(duì)應(yīng)關(guān)系將俄語(yǔ)翻譯成英語(yǔ)。

在早期的機(jī)器翻譯主要是依靠統(tǒng)計(jì)學(xué)模型,使用一種叫統(tǒng)計(jì)機(jī)器翻譯(Statistical Machine Translation, SMT)的方法,在1990年至2010年間是較為主流的方法,也取得了不錯(cuò)的效果。統(tǒng)計(jì)機(jī)器翻譯方法的核心思想是從數(shù)據(jù)中統(tǒng)計(jì)出概率模型。假設(shè)需要構(gòu)建一個(gè)將法語(yǔ)翻譯成英語(yǔ)的模型,我們可以將任務(wù)描述為給定法語(yǔ)句子 x x x ,找到最有可能是 x x x 的翻譯的英語(yǔ)句子 y y y,即 a r g m a x y P ( y ∣ x ) = a r g m a x y P ( x ∣ y ) P ( y ) argmax_y P(y|x) = argmax_y P(x|y)P(y) argmaxy?P(yx)=argmaxy?P(xy)P(y)
上式中的貝葉斯公式可分為兩部分,其中 P ( x ∣ y ) P(x|y) P(xy)可以看作翻譯模型,保證翻譯的正確性; P ( y ) P(y) P(y)則是語(yǔ)言模型,保證翻譯結(jié)果語(yǔ)言流暢。在當(dāng)時(shí)的技術(shù)條件下,這樣拆分后兩個(gè)任務(wù)相對(duì)獨(dú)立,分開(kāi)學(xué)習(xí)會(huì)更容易一些。

在2014年,機(jī)器翻譯研究迎來(lái)了重大的突破——神經(jīng)機(jī)器翻譯(Neural Machine Translation, NMT)。最簡(jiǎn)單的NMT系統(tǒng)使用單個(gè)神經(jīng)網(wǎng)絡(luò)就可以完成機(jī)器翻譯任務(wù),這個(gè)神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)就是Seq2Seq(sequence-to-sequence,序列到序列),它由兩個(gè)RNN組成,其中一個(gè)作為Encoder(編碼器),另一個(gè)則是Decoder(解碼器),Encoder的輸入為源語(yǔ)言的語(yǔ)句,經(jīng)過(guò)RNN處理并在最后一步得到輸出,這個(gè)輸出就可以看作輸入句子的向量編碼。

本篇文章中用于機(jī)器翻譯的網(wǎng)絡(luò)結(jié)構(gòu)仍然是Seq2Seq網(wǎng)絡(luò)結(jié)構(gòu),但是與兩個(gè)RNN或者RNN+attention方法不同,經(jīng)過(guò)簡(jiǎn)單的修改的transformer會(huì)是這個(gè)序列模型的主要結(jié)構(gòu)。

transformer這個(gè)模型,大家都比較熟悉了,由論文《Attention is All You Need》提出,主要是可以實(shí)現(xiàn)并行化的訓(xùn)練,網(wǎng)絡(luò)結(jié)構(gòu)主要由全連接層、注意力機(jī)制和normalization層組成。關(guān)于transformer的結(jié)構(gòu)在這里就不詳細(xì)介紹了,大家感興趣的可以去看看別的博主的文章,這里主要介紹如何將transformer用于機(jī)器翻譯的模型訓(xùn)練。

代碼

數(shù)據(jù)處理

數(shù)據(jù)主要使用的是torchtext中含英語(yǔ)和德語(yǔ)句子對(duì)的知名數(shù)據(jù)集Multi30k dataset,其包含大約30,000個(gè)英語(yǔ)和德語(yǔ)的句子對(duì)(平均長(zhǎng)度約為13)。下面代碼是數(shù)據(jù)處理部分和相關(guān)依賴(lài)庫(kù)導(dǎo)入:

import torch
import torch.nn as nn
import torch.optim as optim

import torchtext
from torchtext.datasets import  Multi30k
#from torchtext.legacy.data import Field, BucketIterator   # pytorch1.9+  torchtext==0.10.0
from torchtext.data  import Field, TabularDataset, BucketIterator, Iterator  ## torchtext==0.6.0 torch==1.7

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

import numpy as np

import random
import math
import time

SEED = 1234

random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True


def tokenize_de(text):
    """
    Tokenizes German text from a string into a list of strings
    """
    return [w for w in text.split() if w]

def tokenize_en(text):
    """
    Tokenizes English text from a string into a list of strings
    """
    return [w for w in text.split() if w]


SRC = Field(tokenize=tokenize_de,
            init_token='<sos>',
            eos_token='<eos>',
            lower=True,
            batch_first=True)

TRG = Field(tokenize=tokenize_en,
            init_token='<sos>',
            eos_token='<eos>',
            lower=True,
            batch_first=True)



#train_data, valid_data, test_data = Multi30k()   # pytorch1.9+  torchtext==0.10.0

train_data, valid_data, test_data = Multi30k.splits(exts = ('.de', '.en'),    ## torchtext==0.6.0 torch==1.7
                                                    fields = (SRC, TRG))

SRC.build_vocab(train_data, min_freq = 2)
TRG.build_vocab(train_data, min_freq = 2)


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('device:', device)


BATCH_SIZE = 64

train_iterator, valid_iterator, test_iterator = BucketIterator.splits(
    (train_data, valid_data, test_data),
    batch_sizes=(BATCH_SIZE, BATCH_SIZE, BATCH_SIZE),
    device=device,
    sort_key=lambda x: len(x.src), # 根據(jù)源語(yǔ)句的長(zhǎng)度
    sort_within_batch=False,   # 是否需要對(duì)批處理的內(nèi)容進(jìn)行排序
    repeat=False
)

特別說(shuō)明一下,數(shù)據(jù)導(dǎo)入,是直接從torchtext.datasets import的,但是torchtext版本不一樣,api接口會(huì)有所變化,詳細(xì)看代碼里面注釋?zhuān)P者這里用的torchtext 0.6.0, torch 1.7。

如果由于網(wǎng)絡(luò)的原因,下載數(shù)據(jù)失敗,可以在手動(dòng)下載該數(shù)據(jù)集并放到與代碼同級(jí)的.data/目錄下即可。

模型定義

class Encoder(nn.Module):
    def __init__(self, 
                 input_dim, 
                 hid_dim, 
                 n_layers, 
                 n_heads, 
                 pf_dim,
                 dropout, 
                 device,
                 max_length = 100):
        super().__init__()

        self.device = device
        
        self.tok_embedding = nn.Embedding(input_dim, hid_dim)
        self.pos_embedding = nn.Embedding(max_length, hid_dim)
        
        self.layers = nn.ModuleList([EncoderLayer(hid_dim, 
                                                  n_heads, 
                                                  pf_dim,
                                                  dropout, 
                                                  device) 
                                     for _ in range(n_layers)])
        
        self.dropout = nn.Dropout(dropout)
        
        self.scale = torch.sqrt(torch.FloatTensor([hid_dim])).to(device)
        
    def forward(self, src, src_mask):
        
        #src = [batch size, src len]
        #src_mask = [batch size, src len]
        
        batch_size = src.shape[0]
        src_len = src.shape[1]
        
        pos = torch.arange(0, src_len).unsqueeze(0).repeat(batch_size, 1).to(self.device)
        
        #pos = [batch size, src len]
        
        src = self.dropout((self.tok_embedding(src) * self.scale) + self.pos_embedding(pos))
        
        #src = [batch size, src len, hid dim]
        
        for layer in self.layers:
            src = layer(src, src_mask)
            
        #src = [batch size, src len, hid dim]
            
        return src



class EncoderLayer(nn.Module):
    def __init__(self, 
                 hid_dim, 
                 n_heads, 
                 pf_dim,  
                 dropout, 
                 device):
        super().__init__()
        
        self.layer_norm = nn.LayerNorm(hid_dim)
        self.self_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.positionwise_feedforward = PositionwiseFeedforwardLayer(hid_dim, 
                                                                     pf_dim, 
                                                                     dropout)
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, src, src_mask):
        
        #src = [batch size, src len, hid dim]
        #src_mask = [batch size, src len]
                
        #self attention
        _src, _ = self.self_attention(src, src, src, src_mask)
        
        #dropout, residual connection and layer norm
        src = self.layer_norm(src + self.dropout(_src))
        
        #src = [batch size, src len, hid dim]
        
        #positionwise feedforward
        _src = self.positionwise_feedforward(src)
        
        #dropout, residual and layer norm
        src = self.layer_norm(src + self.dropout(_src))
        
        #src = [batch size, src len, hid dim]
        
        return src


class MultiHeadAttentionLayer(nn.Module):
    def __init__(self, hid_dim, n_heads, dropout, device):
        super().__init__()
        
        assert hid_dim % n_heads == 0
        
        self.hid_dim = hid_dim
        self.n_heads = n_heads
        self.head_dim = hid_dim // n_heads
        
        self.fc_q = nn.Linear(hid_dim, hid_dim)
        self.fc_k = nn.Linear(hid_dim, hid_dim)
        self.fc_v = nn.Linear(hid_dim, hid_dim)
        
        self.fc_o = nn.Linear(hid_dim, hid_dim)
        
        self.dropout = nn.Dropout(dropout)
        
        self.scale = torch.sqrt(torch.FloatTensor([self.head_dim])).to(device)
        
    def forward(self, query, key, value, mask = None):
        
        batch_size = query.shape[0]
        
        #query = [batch size, query len, hid dim]
        #key = [batch size, key len, hid dim]
        #value = [batch size, value len, hid dim]
                
        Q = self.fc_q(query)
        K = self.fc_k(key)
        V = self.fc_v(value)
        
        #Q = [batch size, query len, hid dim]
        #K = [batch size, key len, hid dim]
        #V = [batch size, value len, hid dim]
                
        Q = Q.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
        K = K.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
        V = V.view(batch_size, -1, self.n_heads, self.head_dim).permute(0, 2, 1, 3)
        
        #Q = [batch size, n heads, query len, head dim]
        #K = [batch size, n heads, key len, head dim]
        #V = [batch size, n heads, value len, head dim]
                
        energy = torch.matmul(Q, K.permute(0, 1, 3, 2)) / self.scale
        
        #energy = [batch size, n heads, query len, key len]
        
        if mask is not None:
            energy = energy.masked_fill(mask == 0, -1e10)
        
        attention = torch.softmax(energy, dim = -1)
                
        #attention = [batch size, n heads, query len, key len]
                
        x = torch.matmul(self.dropout(attention), V)
        
        #x = [batch size, n heads, query len, head dim]
        
        x = x.permute(0, 2, 1, 3).contiguous()
        
        #x = [batch size, query len, n heads, head dim]
        
        x = x.view(batch_size, -1, self.hid_dim)
        
        #x = [batch size, query len, hid dim]
        
        x = self.fc_o(x)
        
        #x = [batch size, query len, hid dim]
        
        return x, attention

class PositionwiseFeedforwardLayer(nn.Module):
    def __init__(self, hid_dim, pf_dim, dropout):
        super().__init__()
        
        self.fc_1 = nn.Linear(hid_dim, pf_dim)
        self.fc_2 = nn.Linear(pf_dim, hid_dim)
        
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, x):
        
        #x = [batch size, seq len, hid dim]
        
        x = self.dropout(torch.relu(self.fc_1(x)))
        
        #x = [batch size, seq len, pf dim]
        
        x = self.fc_2(x)
        
        #x = [batch size, seq len, hid dim]
        
        return x


class Decoder(nn.Module):
    def __init__(self, 
                 output_dim, 
                 hid_dim, 
                 n_layers, 
                 n_heads, 
                 pf_dim, 
                 dropout, 
                 device,
                 max_length = 100):
        super().__init__()
        
        self.device = device
        
        self.tok_embedding = nn.Embedding(output_dim, hid_dim)
        self.pos_embedding = nn.Embedding(max_length, hid_dim)
        
        self.layers = nn.ModuleList([DecoderLayer(hid_dim, 
                                                  n_heads, 
                                                  pf_dim, 
                                                  dropout, 
                                                  device)
                                     for _ in range(n_layers)])
        
        self.fc_out = nn.Linear(hid_dim, output_dim)
        
        self.dropout = nn.Dropout(dropout)
        
        self.scale = torch.sqrt(torch.FloatTensor([hid_dim])).to(device)
        
    def forward(self, trg, enc_src, trg_mask, src_mask):
        
        #trg = [batch size, trg len]
        #enc_src = [batch size, src len, hid dim]
        #trg_mask = [batch size, trg len]
        #src_mask = [batch size, src len]
                
        batch_size = trg.shape[0]
        trg_len = trg.shape[1]
        
        pos = torch.arange(0, trg_len).unsqueeze(0).repeat(batch_size, 1).to(self.device)
                            
        #pos = [batch size, trg len]
            
        trg = self.dropout((self.tok_embedding(trg) * self.scale) + self.pos_embedding(pos))
                
        #trg = [batch size, trg len, hid dim]
        
        for layer in self.layers:
            trg, attention = layer(trg, enc_src, trg_mask, src_mask)
        
        #trg = [batch size, trg len, hid dim]
        #attention = [batch size, n heads, trg len, src len]
        
        output = self.fc_out(trg)
        
        #output = [batch size, trg len, output dim]
            
        return output, attention


class DecoderLayer(nn.Module):
    def __init__(self, 
                 hid_dim, 
                 n_heads, 
                 pf_dim, 
                 dropout, 
                 device):
        super().__init__()
        
        self.layer_norm = nn.LayerNorm(hid_dim)
        self.self_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.encoder_attention = MultiHeadAttentionLayer(hid_dim, n_heads, dropout, device)
        self.positionwise_feedforward = PositionwiseFeedforwardLayer(hid_dim, 
                                                                     pf_dim, 
                                                                     dropout)
        self.dropout = nn.Dropout(dropout)
        
    def forward(self, trg, enc_src, trg_mask, src_mask):
        
        #trg = [batch size, trg len, hid dim]
        #enc_src = [batch size, src len, hid dim]
        #trg_mask = [batch size, trg len]
        #src_mask = [batch size, src len]
        
        #self attention
        _trg, _ = self.self_attention(trg, trg, trg, trg_mask)
        
        #dropout, residual connection and layer norm
        trg = self.layer_norm(trg + self.dropout(_trg))
            
        #trg = [batch size, trg len, hid dim]
            
        #encoder attention
        _trg, attention = self.encoder_attention(trg, enc_src, enc_src, src_mask)
        
        #dropout, residual connection and layer norm
        trg = self.layer_norm(trg + self.dropout(_trg))
                    
        #trg = [batch size, trg len, hid dim]
        
        #positionwise feedforward
        _trg = self.positionwise_feedforward(trg)
        
        #dropout, residual and layer norm
        trg = self.layer_norm(trg + self.dropout(_trg))
        
        #trg = [batch size, trg len, hid dim]
        #attention = [batch size, n heads, trg len, src len]
        
        return trg, attention


class Seq2Seq(nn.Module):
    def __init__(self, 
                 encoder, 
                 decoder, 
                 src_pad_idx, 
                 trg_pad_idx, 
                 device):
        super().__init__()
        
        self.encoder = encoder
        self.decoder = decoder
        self.src_pad_idx = src_pad_idx
        self.trg_pad_idx = trg_pad_idx
        self.device = device
        
    def make_src_mask(self, src):
        
        #src = [batch size, src len]
        
        src_mask = (src != self.src_pad_idx).unsqueeze(1).unsqueeze(2)

        #src_mask = [batch size, 1, 1, src len]

        return src_mask
    
    def make_trg_mask(self, trg):
        
        #trg = [batch size, trg len]
        
        trg_pad_mask = (trg != self.trg_pad_idx).unsqueeze(1).unsqueeze(2)
        
        #trg_pad_mask = [batch size, 1, 1, trg len]
        
        trg_len = trg.shape[1]
        
        trg_sub_mask = torch.tril(torch.ones((trg_len, trg_len), device = self.device)).bool()
        
        #trg_sub_mask = [trg len, trg len]
            
        trg_mask = trg_pad_mask & trg_sub_mask
        
        #trg_mask = [batch size, 1, trg len, trg len]
        
        return trg_mask

    def forward(self, src, trg):
        
        #src = [batch size, src len]
        #trg = [batch size, trg len]
                
        src_mask = self.make_src_mask(src)
        trg_mask = self.make_trg_mask(trg)
        
        #src_mask = [batch size, 1, 1, src len]
        #trg_mask = [batch size, 1, trg len, trg len]
        
        enc_src = self.encoder(src, src_mask)
        
        #enc_src = [batch size, src len, hid dim]
                
        output, attention = self.decoder(trg, enc_src, trg_mask, src_mask)
        
        #output = [batch size, trg len, output dim]
        #attention = [batch size, n heads, trg len, src len]
        
        return output, attention

模型訓(xùn)練

INPUT_DIM = len(SRC.vocab)
OUTPUT_DIM = len(TRG.vocab)
HID_DIM = 256
ENC_LAYERS = 3
DEC_LAYERS = 3
ENC_HEADS = 8
DEC_HEADS = 8
ENC_PF_DIM = 512
DEC_PF_DIM = 512
ENC_DROPOUT = 0.1
DEC_DROPOUT = 0.1

enc = Encoder(INPUT_DIM, 
              HID_DIM, 
              ENC_LAYERS, 
              ENC_HEADS, 
              ENC_PF_DIM, 
              ENC_DROPOUT, 
              device)

dec = Decoder(OUTPUT_DIM, 
              HID_DIM, 
              DEC_LAYERS, 
              DEC_HEADS, 
              DEC_PF_DIM, 
              DEC_DROPOUT, 
              device)

SRC_PAD_IDX = SRC.vocab.stoi[SRC.pad_token]
TRG_PAD_IDX = TRG.vocab.stoi[TRG.pad_token]

model = Seq2Seq(enc, dec, SRC_PAD_IDX, TRG_PAD_IDX, device).to(device)

def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

print(f'The model has {count_parameters(model):,} trainable parameters')

def initialize_weights(m):
    if hasattr(m, 'weight') and m.weight.dim() > 1:
        nn.init.xavier_uniform_(m.weight.data)

model.apply(initialize_weights)

LEARNING_RATE = 0.0005

optimizer = torch.optim.Adam(model.parameters(), lr = LEARNING_RATE)

criterion = nn.CrossEntropyLoss(ignore_index = TRG_PAD_IDX)


def train(model, iterator, optimizer, criterion, clip):
    
    model.train()
    
    epoch_loss = 0
    
    for i, batch in enumerate(iterator):
        
        src = batch.src
        trg = batch.trg
        
        optimizer.zero_grad()
        
        output, _ = model(src, trg[:,:-1])
                
        #output = [batch size, trg len - 1, output dim]
        #trg = [batch size, trg len]
            
        output_dim = output.shape[-1]
            
        output = output.contiguous().view(-1, output_dim)
        trg = trg[:,1:].contiguous().view(-1)
                
        #output = [batch size * trg len - 1, output dim]
        #trg = [batch size * trg len - 1]
            
        loss = criterion(output, trg)
        
        loss.backward()
        
        torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
        
        optimizer.step()
        
        epoch_loss += loss.item()
        
    return epoch_loss / len(iterator)

def evaluate(model, iterator, criterion):
    
    model.eval()
    
    epoch_loss = 0
    
    with torch.no_grad():
    
        for i, batch in enumerate(iterator):

            src = batch.src
            trg = batch.trg

            output, _ = model(src, trg[:,:-1])
            
            #output = [batch size, trg len - 1, output dim]
            #trg = [batch size, trg len]
            
            output_dim = output.shape[-1]
            
            output = output.contiguous().view(-1, output_dim)
            trg = trg[:,1:].contiguous().view(-1)
            
            #output = [batch size * trg len - 1, output dim]
            #trg = [batch size * trg len - 1]
            
            loss = criterion(output, trg)

            epoch_loss += loss.item()
        
    return epoch_loss / len(iterator)


def epoch_time(start_time, end_time):
    elapsed_time = end_time - start_time
    elapsed_mins = int(elapsed_time / 60)
    elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
    return elapsed_mins, elapsed_secs

N_EPOCHS = 10
CLIP = 1

best_valid_loss = float('inf')

for epoch in range(N_EPOCHS):
    
    start_time = time.time()
    
    train_loss = train(model, train_iterator, optimizer, criterion, CLIP)
    valid_loss = evaluate(model, valid_iterator, criterion)
    
    end_time = time.time()
    
    epoch_mins, epoch_secs = epoch_time(start_time, end_time)
    
    if valid_loss < best_valid_loss:
        best_valid_loss = valid_loss
        torch.save(model.state_dict(), 'tut6-model.pt')
    
    print(f'Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s')
    print(f'\tTrain Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f}')
    print(f'\t Val. Loss: {valid_loss:.3f} |  Val. PPL: {math.exp(valid_loss):7.3f}')

最后訓(xùn)練輸出:
基于transformer的Seq2Seq機(jī)器翻譯模型訓(xùn)練、預(yù)測(cè)教程文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-437297.html

到了這里,關(guān)于基于transformer的Seq2Seq機(jī)器翻譯模型訓(xùn)練、預(yù)測(cè)教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【深度學(xué)習(xí)】Transformer、GPT、BERT、Seq2Seq什么區(qū)別?

    【深度學(xué)習(xí)】Transformer、GPT、BERT、Seq2Seq什么區(qū)別?

    請(qǐng)看vcr:https://transformers.run/back/transformer/

    2024年02月08日
    瀏覽(22)
  • 深度學(xué)習(xí)筆記之Seq2seq(二)基于Seq2seq注意力機(jī)制的動(dòng)機(jī)

    深度學(xué)習(xí)筆記之Seq2seq(二)基于Seq2seq注意力機(jī)制的動(dòng)機(jī)

    上一節(jié)介紹了 Seq2seq text{Seq2seq} Seq2seq 網(wǎng)絡(luò)常用的基本結(jié)構(gòu)以及在 機(jī)器翻譯 任務(wù)中,關(guān)于 目標(biāo)函數(shù) 與 預(yù)測(cè)概率 的描述。本節(jié)依然以 機(jī)器翻譯 任務(wù)為例,對(duì) Seq2seq text{Seq2seq} Seq2seq 中的 注意力機(jī)制 ( Attention ) (text{Attention}) ( Attention ) 進(jìn)行描述。 關(guān)于 機(jī)器翻譯 任務(wù)的 Seq2

    2024年02月09日
    瀏覽(22)
  • 李宏毅-hw5-translation-有關(guān)transformer、seq2seq的探索

    李宏毅-hw5-translation-有關(guān)transformer、seq2seq的探索

    一、ppt研讀: 1.關(guān)于這個(gè) input Embedding 的內(nèi)容: 2.關(guān)于Positional Encoding: 二、慢慢積累,一點(diǎn)點(diǎn)閱讀代碼: 雖然這次的模塊挺多的,但是,這樣也就意味著,把這個(gè)內(nèi)化為自己的,就可以獲得更大的進(jìn)步了 1.關(guān)于使用git命令獲取到源代碼: 2.Fix Random Seed部分都是相同的,就是為

    2024年02月09日
    瀏覽(12)
  • Transformer通俗筆記:從Word2Vec、Seq2Seq逐步理解到GPT、BERT

    Transformer通俗筆記:從Word2Vec、Seq2Seq逐步理解到GPT、BERT

    我在寫(xiě)上一篇博客《22下半年》時(shí),有讀者在文章下面評(píng)論道:“july大神,請(qǐng)問(wèn)BERT的通俗理解還做嗎?”,我當(dāng)時(shí)給他發(fā)了張俊林老師的BERT文章,所以沒(méi)太在意。 直到今天早上,刷到CSDN上一篇講BERT的文章,號(hào)稱(chēng)一文讀懂,我讀下來(lái)之后,假定我是初學(xué)者,讀不懂。 關(guān)于

    2024年02月02日
    瀏覽(20)
  • 【文本摘要(2)】pytorch之Seq2Seq

    【文本摘要(2)】pytorch之Seq2Seq

    改廢了兩個(gè)代碼后,又找到了一個(gè)文本摘要代碼 終于跑起來(lái)了 改廢的兩個(gè)代碼: 一個(gè)是機(jī)器翻譯改文本摘要,結(jié)果沒(méi)跑起來(lái)。。。 一個(gè)是英文文本摘要改中文文本摘要,預(yù)測(cè)的摘要全是,,,這種 代碼參考: https://github.com/jasoncao11/nlp-notebook/tree/master/4-2.Seq2seq_Att 跪謝大佬

    2024年02月03日
    瀏覽(49)
  • 構(gòu)建seq2seq模型的常見(jiàn)問(wèn)題

    1. seq2seq模型,輸入是一個(gè)詞向量,而不是詞向量列表,對(duì)吧? 是的,對(duì)于seq2seq模型,輸入和輸出都需要被轉(zhuǎn)換成詞向量形式。 對(duì)于輸入來(lái)說(shuō),通常會(huì)將一個(gè)句子轉(zhuǎn)換成一個(gè)詞向量序列。具體地,對(duì)于每個(gè)單詞或者字符,都會(huì)將其對(duì)應(yīng)成一個(gè)詞向量,然后將所有詞向量按照它

    2024年02月06日
    瀏覽(22)
  • Seq2Seq在安全領(lǐng)域的應(yīng)用實(shí)踐

    非常感謝您委托我撰寫(xiě)這篇專(zhuān)業(yè)的技術(shù)博客文章。作為一位世界級(jí)人工智能專(zhuān)家、程序員、軟件架構(gòu)師,我會(huì)遵循您提供的目標(biāo)和約束條件,以專(zhuān)業(yè)的技術(shù)語(yǔ)言,結(jié)合深入的研究和準(zhǔn)確的信息,為您呈現(xiàn)一篇內(nèi)容豐富、結(jié)構(gòu)清晰、實(shí)用價(jià)值高的技術(shù)博客文章。 下面我將開(kāi)始正文的

    2024年04月28日
    瀏覽(36)
  • 自然語(yǔ)言處理: 第四章Seq2Seq

    自然語(yǔ)言處理: 第四章Seq2Seq

    開(kāi)始之前,首先提出一個(gè)問(wèn)題,電腦是怎么識(shí)別人類(lèi)的命令的,首先人們通過(guò)輸入代碼(編碼) ,帶入輸入給計(jì)算機(jī)然后再經(jīng)過(guò)處理(解碼)得到最終的命令。所以可以看到這其實(shí)是一個(gè)編碼 + 解碼的過(guò)程。可以看到首先我們將初始的信息通過(guò)編碼,得到涵蓋全局的信息的特征然

    2024年02月12日
    瀏覽(31)
  • 從零實(shí)現(xiàn)深度學(xué)習(xí)框架——Seq2Seq模型嘗試優(yōu)化

    從零實(shí)現(xiàn)深度學(xué)習(xí)框架——Seq2Seq模型嘗試優(yōu)化

    本著“ 凡我不能創(chuàng)造的,我就不能理解 ”的思想,本系列文章會(huì)基于純Python以及NumPy從零創(chuàng)建自己的深度學(xué)習(xí)框架,該框架類(lèi)似PyTorch能實(shí)現(xiàn)自動(dòng)求導(dǎo)。 ??系列文章完整目錄: ??點(diǎn)此?? 要深入理解深度學(xué)習(xí),從零開(kāi)始創(chuàng)建的經(jīng)驗(yàn)非常重要,從自己可以理解的角度出發(fā),盡

    2024年02月12日
    瀏覽(38)
  • 【深度學(xué)習(xí)-注意力機(jī)制attention 在seq2seq中應(yīng)用】

    【深度學(xué)習(xí)-注意力機(jī)制attention 在seq2seq中應(yīng)用】

    這是一個(gè)普通的seq2seq結(jié)構(gòu),用以實(shí)現(xiàn)機(jī)器對(duì)話(huà),Encoder需要把一個(gè)輸入的一個(gè)句子轉(zhuǎn)化為一個(gè)最終的輸出,上下文context vector,然后在Decoder中使用,但這里有些問(wèn)題: 如果句子很長(zhǎng),這個(gè)向量很難包含sequence中最早輸入的哪些詞的信息,那么decoder的處理必然也缺失了這一部分

    2024年02月09日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包