transformer代码
时间: 2023-09-03 08:12:21 浏览: 64
以下是一个简单的Transformer模型的示例代码,使用PyTorch实现:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, num_heads):
super(MultiHeadAttention, self).__init__()
assert d_model % num_heads == 0
self.d_model = d_model
self.num_heads = num_heads
self.depth = d_model // num_heads
self.q_linear = nn.Linear(d_model, d_model)
self.v_linear = nn.Linear(d_model, d_model)
self.k_linear = nn.Linear(d_model, d_model)
self.out = nn.Linear(d_model, d_model)
def forward(self, q, k, v, mask=None):
bs = q.size(0)
# perform linear operation and split into h heads
k = self.k_linear(k).view(bs, -1, self.num_heads, self.depth)
q = self.q_linear(q).view(bs, -1, self.num_heads, self.depth)
v = self.v_linear(v).view(bs, -1, self.num_heads, self.depth)
# transpose to get dimensions bs * h * sl * d_model
k = k.transpose(1,2)
q = q.transpose(1,2)
v = v.transpose(1,2)
# calculate attention using function we will define next
scores = self.attention(q, k, v, self.depth, mask)
# concatenate heads and put through final linear layer
concat = scores.transpose(1,2).contiguous().view(bs, -1, self.d_model)
output = self.out(concat)
return output
def attention(self, q, k, v, depth, mask=None):
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(depth)
if mask is not None:
mask = mask.unsqueeze(1)
scores = scores.masked_fill(mask == 0, -1e9)
scores = F.softmax(scores, dim=-1)
output = torch.matmul(scores, v)
return output
class FeedForward(nn.Module):
def __init__(self, d_model, d_ff=2048, dropout=0.1):
super(FeedForward, self).__init__()
self.linear_1 = nn.Linear(d_model, d_ff)
self.dropout = nn.Dropout(p=dropout)
self.linear_2 = nn.Linear(d_ff, d_model)
def forward(self, x):
x = self.dropout(F.relu(self.linear_1(x)))
x = self.linear_2(x)
return x
class TransformerBlock(nn.Module):
def __init__(self, d_model, num_heads, dropout=0.1):
super(TransformerBlock, self).__init__()
self.attention = MultiHeadAttention(d_model, num_heads)
self.norm_1 = nn.LayerNorm(d_model)
self.ff = FeedForward(d_model)
self.norm_2 = nn.LayerNorm(d_model)
self.dropout_1 = nn.Dropout(dropout)
self.dropout_2 = nn.Dropout(dropout)
def forward(self, x, mask):
x2 = self.norm_1(x)
x = x + self.dropout_1(self.attention(x2, x2, x2, mask))
x2 = self.norm_2(x)
x = x + self.dropout_2(self.ff(x2))
return x
class Transformer(nn.Module):
def __init__(self, input_dim, output_dim, d_model, num_heads, num_layers, dropout=0.1):
super(Transformer, self).__init__()
self.d_model = d_model
self.num_layers = num_layers
self.num_heads = num_heads
self.embedding = nn.Linear(input_dim, d_model)
self.pos_enc = PositionalEncoding(d_model, dropout)
self.layers = nn.ModuleList([TransformerBlock(d_model, num_heads, dropout) for _ in range(num_layers)])
self.out = nn.Linear(d_model, output_dim)
def forward(self, x, mask):
x = self.embedding(x) * math.sqrt(self.d_model)
x = self.pos_enc(x)
for i in range(self.num_layers):
x = self.layers[i](x, mask)
x = self.out(x[:, 0])
return F.log_softmax(x, dim=-1)
```
这个代码片段实现了一个标准的Transformer模型,其中包括位置编码(Positional Encoding)、多头自注意力(Multi-Head Attention)、前馈网络(FeedForward)、层归一化(Layer Normalization)和残差连接(Residual Connection)等组件。在实现自己的Transformer模型时,可以根据具体任务的要求进行相应的修改和调整。
阅读全文