Transformer and Self-Attention Pytorch代码
时间: 2024-05-14 21:18:01 浏览: 227
下面是一个使用 PyTorch 实现 Transformer 和 Self-Attention 的示例代码:
首先,我们需要导入所需的库:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
```
然后,我们定义一个 Self-Attention 类:
```python
class SelfAttention(nn.Module):
def __init__(self, embed_size, heads):
super(SelfAttention, self).__init__()
self.embed_size = embed_size
self.heads = heads
self.head_dim = embed_size // heads
assert (self.head_dim * heads == embed_size), "Embed size needs to be divisible by heads"
self.values = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.keys = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.queries = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.fc_out = nn.Linear(heads*self.head_dim, embed_size)
def forward(self, values, keys, query, mask):
N = query.shape[0]
value_len, key_len, query_len = values.shape[1], keys.shape[1], query.shape[1]
# Split embedding into self.heads pieces
values = values.reshape(N, value_len, self.heads, self.head_dim)
keys = keys.reshape(N, key_len, self.heads, self.head_dim)
query = query.reshape(N, query_len, self.heads, self.head_dim)
values = self.values(values) # (N, value_len, heads, head_dim)
keys = self.keys(keys) # (N, key_len, heads, head_dim)
query = self.queries(query) # (N, query_len, heads, heads_dim)
# Compute dot product attention
energy = torch.einsum("nqhd,nkhd->nhqk", [query, keys])
# energy shape: (N, heads, query_len, key_len)
if mask is not None:
energy = energy.masked_fill(mask == 0, float("-1e20"))
attention = torch.softmax(energy / (self.embed_size ** (1/2)), dim=3)
# Compute attention output
out = torch.einsum("nhql,nlhd->nqhd", [attention, values]).reshape(
N, query_len, self.heads*self.head_dim
)
out = self.fc_out(out)
return out
```
接下来,我们定义一个 TransformerBlock 类:
```python
class TransformerBlock(nn.Module):
def __init__(self, embed_size, heads, dropout, forward_expansion):
super(TransformerBlock, self).__init__()
self.attention = SelfAttention(embed_size, heads)
self.norm1 = nn.LayerNorm(embed_size)
self.norm2 = nn.LayerNorm(embed_size)
self.feed_forward = nn.Sequential(
nn.Linear(embed_size, forward_expansion*embed_size),
nn.ReLU(),
nn.Linear(forward_expansion*embed_size, embed_size),
)
self.dropout = nn.Dropout(dropout)
def forward(self, value, key, query, mask):
attention = self.attention(value, key, query, mask)
x = self.dropout(self.norm1(attention + query))
forward = self.feed_forward(x)
out = self.dropout(self.norm2(forward + x))
return out
```
最后,我们定义一个 TransformerEncoder 类:
```python
class TransformerEncoder(nn.Module):
def __init__(self, embed_size, heads, dropout, forward_expansion, num_layers):
super(TransformerEncoder, self).__init__()
self.layers = nn.ModuleList(
[
TransformerBlock(embed_size, heads, dropout, forward_expansion)
for _ in range(num_layers)
]
)
def forward(self, x, mask):
for layer in self.layers:
x = layer(x, x, x, mask)
return x
```
现在,我们已经定义了一个完整的 Transformer 模型,可以在实际应用中使用。
阅读全文