能否生成transformer的torch代码
时间: 2023-08-13 21:54:11 浏览: 40
当然可以!以下是一个简单的Transformer模型的PyTorch代码示例:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
class Transformer(nn.Module):
def __init__(self, input_dim, hidden_dim, num_heads, num_layers):
super(Transformer, self).__init__()
self.embedding = nn.Embedding(input_dim, hidden_dim)
self.positional_encoding = PositionalEncoding(hidden_dim)
self.encoder = Encoder(hidden_dim, num_heads, num_layers)
self.fc = nn.Linear(hidden_dim, input_dim)
def forward(self, x):
x = self.embedding(x)
x = self.positional_encoding(x)
x = self.encoder(x)
x = self.fc(x)
return F.log_softmax(x, dim=-1)
class PositionalEncoding(nn.Module):
def __init__(self, hidden_dim, max_length=1000):
super(PositionalEncoding, self).__init__()
pe = torch.zeros(max_length, hidden_dim)
position = torch.arange(0, max_length, dtype=torch.float32).unsqueeze(1)
div_term = torch.exp(torch.arange(0, hidden_dim, 2).float() * (-math.log(10000.0) / hidden_dim))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:, :x.size(1)]
return x
class Encoder(nn.Module):
def __init__(self, hidden_dim, num_heads, num_layers):
super(Encoder, self).__init__()
self.layers = nn.ModuleList([EncoderLayer(hidden_dim, num_heads) for _ in range(num_layers)])
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class EncoderLayer(nn.Module):
def __init__(self, hidden_dim, num_heads):
super(EncoderLayer, self).__init__()
self.multihead_attention = MultiheadAttention(hidden_dim, num_heads)
self.feed_forward = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim * 4),
nn.ReLU(),
nn.Linear(hidden_dim * 4, hidden_dim)
)
self.layer_norm = nn.LayerNorm(hidden_dim)
def forward(self, x):
residual = x
x = self.multihead_attention(x)
x = x + residual
x = self.layer_norm(x)
residual = x
x = self.feed_forward(x)
x = x + residual
x = self.layer_norm(x)
return x
class MultiheadAttention(nn.Module):
def __init__(self, hidden_dim, num_heads):
super(MultiheadAttention, self).__init__()
self.num_heads = num_heads
self.hidden_dim = hidden_dim
self.head_dim = hidden_dim // num_heads
self.query = nn.Linear(hidden_dim, hidden_dim)
self.key = nn.Linear(hidden_dim, hidden_dim)
self.value = nn.Linear(hidden_dim, hidden_dim)
self.fc = nn.Linear(hidden_dim, hidden_dim)
def forward(self, x):
batch_size, seq_len, hidden_dim = x.size()
query = self.query(x).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
key = self.key(x).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
value = self.value(x).view(batch_size, seq_len, self.num_heads, self.head_dim).transpose(1, 2)
attention_scores = torch.matmul(query, key.transpose(-2, -1))
attention_scores = attention_scores / math.sqrt(self.head_dim)
attention_probs = F.softmax(attention_scores, dim=-1)
context = torch.matmul(attention_probs, value).transpose(1, 2).contiguous().view(batch_size, seq_len, -1)
x = self.fc(context)
return x
```
这是一个简化的Transformer模型,包含了Encoder、EncoderLayer、MultiheadAttention和PositionalEncoding等组件。你可以根据自己的需求进行修改和扩展。