以下是使用PyTorch构建和训练情感分析模型的完整代码:

import torch
import torch.optim as optim
import torch.nn.functional as F
import torch.nn as nn

file_path = r'C:\Users\18105\Desktop\MVSA-multiple\MVSA\biaoqian.txt'

# 读取标签数据
tensor_list = []
with open(file_path, 'r') as file:
    lines = file.readlines()
    for line in lines:
        line = line.strip()
        numbers = line.split()
        tensor = torch.tensor([float(num) for num in numbers])
        tensor_list.append(tensor)

class MyNetwork(nn.Module):
    def __init__(self):
        super(MyNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(312 * 256, 256)  # 添加一个隐藏层
        self.fc2 = nn.Linear(256, 128)  # 添加第二个隐藏层
        self.fc3 = nn.Linear(128, 3)
        self.dropout = nn.Dropout(0.5)  # 添加Dropout层,丢弃率为0.5
        self.l1_regularization = nn.L1Loss()  # L1正则化损失函数

    def forward(self, x):
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.dropout(x)  # 在隐藏层后应用Dropout
        x = torch.relu(x)
        x = self.fc2(x)
        x = self.dropout(x)  # 在隐藏层后应用Dropout
        x = torch.relu(x)
        x = self.fc3(x)
        return x

# 创建增强能力的网络实例
network = MyNetwork()

# 检查是否有可用的GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
network.to(device)
tensor_list = [tensor.to(device) for tensor in tensor_list]

# 将网络参数移动到对应设备
network = network.to(device)

# 指定pt文件的路径和训练数据
pt_file_path = r'C:\Users\18105\PycharmProjects\tuwenqingganfenxi\expanded1.pt'
data = torch.load(pt_file_path)

# 将数据转换为张量
input_tensors = [torch.tensor(tensor) for tensor in data]
train_tensors = input_tensors[0:18000]
val_tensors = input_tensors[18000:]

# 将训练和验证数据移动到对应设备
train_tensors = [tensor.to(device) for tensor in train_tensors]
val_tensors = [tensor.to(device) for tensor in val_tensors]

# 自定义损失函数
def custom_loss(output, label):
    target_similarity = F.cosine_similarity(output, label.unsqueeze(0), dim=1)

    other_similarities = []
    for i, tensor in enumerate(label):
        if i != torch.argmax(label):
            similarity = F.cosine_similarity(output, tensor.unsqueeze(0), dim=1)
            other_similarities.append(similarity)

    other_similarities = torch.cat(other_similarities)
    diff = target_similarity - torch.max(other_similarities)
    loss = -torch.log((1 + diff) / 2)

    return loss

# 定义优化器
optimizer = optim.AdamW(network.parameters(), lr=0.001, weight_decay=0.04)

num_epochs = 50
for epoch in range(num_epochs):
    running_loss = 0.0
    train_correct_total = 0
    train_total = 0

    # 训练阶段
    network.train()

    for i, input_tensor in enumerate(train_tensors):
        optimizer.zero_grad()

        input_tensor = input_tensor.to(device)  # 将输入张量移动到GPU上
        output = network(input_tensor)

        loss = custom_loss(output, tensor_list[i])

        loss.backward()
        optimizer.step()

        # 统计准确率
        target_similarity = F.cosine_similarity(output, tensor_list[i].unsqueeze(0), dim=1)
        label_list = [torch.tensor([1, 0, 0]), torch.tensor([0, 1, 0]), torch.tensor([0, 0, 1])]
        other_list = []
        for label_tensor in label_list:
            if not torch.all(torch.eq(tensor_list[i], label_tensor)):
                other_list.append(label_tensor)

        if target_similarity > torch.max(torch.stack([F.cosine_similarity(output, other.unsqueeze(0), dim=1) for other in other_list]), dim=0).values:
            train_correct_total += 1

        train_total += 1

        running_loss += loss.item()

    # 计算训练集的准确率
    train_accuracy = train_correct_total / train_total

    # 保存网络参数
    if epoch == num_epochs - 1:
        torch.save(network.state_dict(), 'final_model.pt')

    # 打印训练信息
    print('Train Accuracy: %.2f%%' % (100 * train_accuracy))
    print('Epoch: %d, Loss: %.3f' % (epoch + 1, running_loss))

    network.eval()
    val_correct_total = 0
    val_total = 0

    with torch.no_grad():
        for j, val_input_tensor in enumerate(val_tensors):
            val_input_tensor = val_input_tensor.to(device)  # 将输入张量移动到GPU上
            val_output = network(val_input_tensor)

            label_list = [torch.tensor([1, 0, 0]), torch.tensor([0, 1, 0]), torch.tensor([0, 0, 1])]
            val_target_similarity = F.cosine_similarity(val_output, tensor_list[j].unsqueeze(0), dim=1)
            other_list = []
            for label_tensor in label_list:
                if not torch.all(torch.eq(tensor_list[j], label_tensor)):
                    other_list.append(label_tensor)

            if val_target_similarity > torch.max(torch.stack([F.cosine_similarity(val_output, other.unsqueeze(0), dim=1) for other in other_list]), dim=0).values:
                val_correct_total += 1

            val_total += 1

        # 计算验证集的准确率
        val_accuracy = val_correct_total / val_total

        # 打印验证信息
        print('Validation Accuracy: %.2f%%' % (100 * val_accuracy))
        print('Epoch: %d, Loss: %.3f' % (epoch + 1, running_loss))

模型特点:

  • 自定义损失函数: 使用余弦相似度来计算预测结果与目标标签的相似度,并通过对比与其他标签的相似度来计算最终的损失值。
  • Dropout层: 在隐藏层后应用Dropout层,以减少模型的过拟合现象。
  • L1正则化: 使用L1正则化损失函数,鼓励模型参数向零靠近,进一步提高模型的泛化能力。
  • GPU加速: 代码支持GPU加速,提升训练效率。

训练过程:

  1. 读取标签数据和训练数据。
  2. 定义模型并使用AdamW优化器进行训练。
  3. 每训练一个epoch,计算训练集和验证集的准确率,并打印相关信息。
  4. 在训练结束时,保存训练好的模型参数。

适用场景:

  • 情感分析
  • 文本分类
  • 基于相似度的文本匹配

代码使用说明:

  1. 确保安装PyTorch环境。
  2. 将代码中的file_pathpt_file_path替换为实际的标签数据路径和训练数据路径。
  3. 运行代码即可开始训练模型。

注意事项:

  • 训练数据的大小和模型的复杂度会影响训练速度和最终的模型效果。
  • 调整超参数,例如学习率、正则化系数和Dropout率等,可以优化模型性能。
  • 建议在训练过程中使用验证集来监控模型的泛化能力。
  • 在实际应用中,需要对训练好的模型进行评估和测试,以确定其性能是否满足需求。
PyTorch模型训练代码:情感分析模型的构建与训练

原文地址: https://www.cveoy.top/t/topic/cppk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录