部分代码:
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import accuracy_score
import numpy as np
from util import *
# 定义 Transformer 模型
class TransformerModel(nn.Module):
def __init__(self, input_dim, output_dim, hidden_dim, num_heads, num_layers):
super(TransformerModel, self).__init__()
self.transformer_encoder = nn.TransformerEncoder(
nn.TransformerEncoderLayer(input_dim, num_heads, hidden_dim),num_layers
)
self.fc = nn.Linear(input_dim, output_dim)
def forward(self, x):
x = x.unsqueeze(1) #维度扩展 (batch_size, 1, input_dim)
x = self.transformer_encoder(x)
x = x.squeeze(1) #维度压缩 (batch_size, hidden_dim)
x = self.fc(x)
return x
#定义训练函数
def train_model():
# 训练模型
train_losses = []
train_accs = []
for epoch in range(num_epochs):
model.train()
optimizer.zero_grad() #梯度清零
outputs = model(X_train) # 前向传播
loss = criterion(outputs, torch.argmax(y_train, dim=1))
loss.backward() # 反向传播和优化
optimizer.step()
# 计算训练准确率
_, predicted = torch.max(outputs, 1)
train_acc = accuracy_score(torch.argmax(y_train, dim=1).numpy(), predicted.numpy())
# 记录损失和准确率
train_losses.append(loss.item())
train_accs.append(train_acc)
if (epoch + 1) % 10 == 0:
print(f'Epoch {epoch + 1}/{num_epochs}, Loss: {loss.item():.4f}, Train Acc: {train_acc:.4f}')
return train_losses,train_accs
部分数据:
训练损失
预测对比:
混淆矩阵