| |
| import os |
| import sys |
| import torch |
| import torch.nn as nn |
| import torch.nn.functional as F |
| import numpy as np |
| import yaml |
| import json |
| from sklearn import metrics |
| import time |
| from tqdm import tqdm |
| import argparse |
|
|
| |
| sys.path.append(os.path.dirname(os.path.abspath(__file__))) |
|
|
| from model import TextCNN as Model, Config |
| from dataset_utils import build_dataset, build_iterator, get_time_dif |
|
|
|
|
| def init_network(model, method='xavier', exclude='embedding', seed=123): |
| """权重初始化,默认xavier""" |
| for name, w in model.named_parameters(): |
| if exclude not in name: |
| if 'weight' in name: |
| if method == 'xavier': |
| nn.init.xavier_normal_(w) |
| elif method == 'kaiming': |
| nn.init.kaiming_normal_(w) |
| else: |
| nn.init.normal_(w) |
| elif 'bias' in name: |
| nn.init.constant_(w, 0) |
|
|
|
|
| def evaluate(model, data_iter, device): |
| """评估函数""" |
| model.eval() |
| loss_total = 0 |
| predict_all = np.array([], dtype=int) |
| labels_all = np.array([], dtype=int) |
| with torch.no_grad(): |
| for texts, labels in data_iter: |
| outputs, _ = model(texts) |
| loss = F.cross_entropy(outputs, labels) |
| loss_total += loss |
| labels = labels.data.cpu().numpy() |
| predic = torch.max(outputs.data, 1)[1].cpu().numpy() |
| labels_all = np.append(labels_all, labels) |
| predict_all = np.append(predict_all, predic) |
|
|
| acc = metrics.accuracy_score(labels_all, predict_all) |
| return acc, loss_total / len(data_iter) |
|
|
|
|
| def save_epoch_data(model, train_data, dev_data, test_data, config, epoch, save_dir, device): |
| """保存每个epoch的模型、特征向量和预测值 |
| 按顺序保存train_data、dev_data、test_data的特征向量 |
| |
| Args: |
| model: 训练好的模型 |
| train_data: 原始训练数据集 |
| dev_data: 原始验证数据集 |
| test_data: 原始测试数据集 |
| config: 配置对象 |
| epoch: 当前epoch数 |
| save_dir: 保存目录 |
| device: 设备 |
| """ |
| epoch_dir = os.path.join(save_dir, f'epoch_{epoch}') |
| os.makedirs(epoch_dir, exist_ok=True) |
| |
| |
| model_path = os.path.join(epoch_dir, 'model.pt') |
| if hasattr(model, 'module'): |
| torch.save(model.module.state_dict(), model_path) |
| else: |
| torch.save(model.state_dict(), model_path) |
| |
| |
| from dataset_utils import build_iterator |
| |
| |
| datasets = [ |
| ('train', train_data), |
| ('dev', dev_data), |
| ('test', test_data) |
| ] |
| |
| model.eval() |
| all_features = [] |
| all_predictions = [] |
| |
| if epoch == 1: |
| |
| index_data = {} |
| start_idx = 0 |
| for name,dataset in datasets: |
| |
| indices = list(range(start_idx, start_idx + len(dataset))) |
| index_data[name] = indices |
| start_idx += len(dataset) |
| |
| with open(os.path.join("../dataset", "index.json"), "w", encoding="utf-8") as f: |
| json.dump(index_data, f, ensure_ascii=False, indent=2) |
| |
| |
| info_data = { |
| "model": "TextCNN", |
| "classes": config.class_list |
| } |
| |
| with open(os.path.join("../dataset", 'info.json'), 'w', encoding='utf-8') as f: |
| json.dump(info_data, f, ensure_ascii=False, indent=2) |
| |
| print(" - ✓ 已保存 index.json 和 info.json") |
| print(" index.json: 包含各数据集的索引映射") |
| print(f" info.json: 模型={info_data['model']}, 类别数={len(info_data['classes'])}") |
|
|
| print(f"正在提取epoch {epoch}的特征向量(按train/dev/test顺序)...") |
| |
| with torch.no_grad(): |
| for dataset_name, dataset in datasets: |
| print(f" 正在处理 {dataset_name} 数据集 ({len(dataset)} 个样本)...") |
| |
| |
| data_iter = build_iterator(dataset, config) |
| |
| dataset_features = [] |
| dataset_predictions = [] |
| |
| for batch_idx, (texts, labels) in enumerate(tqdm(data_iter, desc=f"提取{dataset_name}特征")): |
| |
| if hasattr(model, 'module'): |
| features = model.module.feature(texts) |
| predictions = model.module.get_prediction(texts) |
| else: |
| features = model.feature(texts) |
| predictions = model.get_prediction(texts) |
| |
| dataset_features.append(features) |
| dataset_predictions.append(predictions) |
| |
| |
| if dataset_features: |
| dataset_embeddings = np.vstack(dataset_features) |
| dataset_preds = np.vstack(dataset_predictions) |
| |
| all_features.append(dataset_embeddings) |
| all_predictions.append(dataset_preds) |
| |
| print(f" {dataset_name} 特征形状: {dataset_embeddings.shape}") |
| |
| |
| if all_features: |
| embeddings = np.vstack(all_features) |
| predictions = np.vstack(all_predictions) |
| |
| |
| np.save(os.path.join(epoch_dir, 'embeddings.npy'), embeddings) |
| np.save(os.path.join(epoch_dir, 'predictions.npy'), predictions) |
| |
| print(f"Epoch {epoch} 数据已保存到 {epoch_dir}") |
| print(f" - 特征向量形状: {embeddings.shape}") |
| print(f" - 输出向量形状: {predictions.shape}") |
| else: |
| print("警告:没有提取到任何特征数据") |
|
|
|
|
| def train(config_path): |
| """训练主函数""" |
| |
| with open(config_path, 'r', encoding='utf-8') as f: |
| train_config = yaml.safe_load(f) |
| |
| |
| gpu_ids = train_config.get('gpu_ids', [0]) |
| if not isinstance(gpu_ids, list): |
| gpu_ids = [gpu_ids] |
| |
| |
| if not torch.cuda.is_available(): |
| print("CUDA不可用,使用CPU") |
| device = torch.device('cpu') |
| gpu_ids = [] |
| else: |
| available_gpus = torch.cuda.device_count() |
| print(f"可用GPU数量: {available_gpus}") |
| |
| |
| valid_gpu_ids = [gpu_id for gpu_id in gpu_ids if 0 <= gpu_id < available_gpus] |
| if not valid_gpu_ids: |
| print(f"警告:指定的GPU ID {gpu_ids} 无效,使用GPU 0") |
| valid_gpu_ids = [0] |
| |
| gpu_ids = valid_gpu_ids |
| device = torch.device(f'cuda:{gpu_ids[0]}') |
| |
| print(f"使用设备: {device}") |
| print(f"指定GPU ID: {gpu_ids}") |
| |
| |
| config = Config(train_config['dataset_path'], train_config.get('embedding', 'random')) |
| config.num_epochs = train_config.get('num_epochs', 20) |
| config.batch_size = train_config.get('batch_size', 128) |
| config.learning_rate = train_config.get('learning_rate', 1e-3) |
| |
| |
| print("构建数据集...") |
| vocab, train_data, dev_data, test_data = build_dataset(config, train_config.get('use_word', False)) |
| config.n_vocab = len(vocab) |
| |
| |
| config.device = device |
| |
| |
| train_iter = build_iterator(train_data, config) |
| dev_iter = build_iterator(dev_data, config) |
| test_iter = build_iterator(test_data, config) |
| |
| |
| model = Model(config) |
| |
| |
| if len(gpu_ids) > 1 and torch.cuda.is_available(): |
| try: |
| print(f"尝试使用多GPU训练: {gpu_ids}") |
| model = nn.DataParallel(model, device_ids=gpu_ids) |
| print("✓ 多GPU训练模式已启用") |
| except Exception as e: |
| print(f"⚠️ 多GPU初始化失败: {e}") |
| print("回退到单GPU训练模式") |
| |
| model = model.to(device) |
| elif len(gpu_ids) == 1 and torch.cuda.is_available(): |
| print(f"使用单GPU训练模式: GPU {gpu_ids[0]}") |
| model = model.to(device) |
| else: |
| print("使用CPU训练模式") |
| model = model.to(device) |
| |
| |
| init_network(model) |
| |
| |
| optimizer = torch.optim.Adam(model.parameters(), lr=config.learning_rate) |
| |
| |
| print("开始训练...") |
| start_time = time.time() |
| total_batch = 0 |
| dev_best_loss = float('inf') |
| |
| |
| |
| |
| epochs_dir = train_config.get('epochs_dir', '../epochs') |
| |
| for epoch in range(config.num_epochs): |
| print(f'Epoch [{epoch + 1}/{config.num_epochs}]') |
| model.train() |
| |
| epoch_loss = 0 |
| epoch_acc = 0 |
| batch_count = 0 |
| |
| for i, (trains, labels) in enumerate(tqdm(train_iter, desc=f"训练 Epoch {epoch+1}")): |
| try: |
| outputs, _ = model(trains) |
| model.zero_grad() |
| loss = F.cross_entropy(outputs, labels) |
| loss.backward() |
| optimizer.step() |
| except RuntimeError as e: |
| if "NCCL" in str(e) or "cuda" in str(e).lower(): |
| print(f"\n❌ 多GPU训练错误: {e}") |
| print("建议在配置文件中设置 force_single_gpu: true") |
| return |
| else: |
| raise e |
| |
| epoch_loss += loss.item() |
| |
| |
| true = labels.data.cpu() |
| predic = torch.max(outputs.data, 1)[1].cpu() |
| train_acc = metrics.accuracy_score(true, predic) |
| epoch_acc += train_acc |
| batch_count += 1 |
| |
| if total_batch % 100 == 0: |
| |
| try: |
| dev_acc, dev_loss = evaluate(model, dev_iter, device) |
| except RuntimeError as e: |
| if "NCCL" in str(e) or "cuda" in str(e).lower(): |
| print(f"\n❌ 验证过程多GPU错误: {e}") |
| print("建议在配置文件中设置 force_single_gpu: true") |
| return |
| else: |
| raise e |
| if dev_loss < dev_best_loss: |
| dev_best_loss = dev_loss |
| improve = '*' |
| |
| else: |
| improve = '' |
| |
| time_dif = get_time_dif(start_time) |
| msg = 'Iter: {0:>6}, Train Loss: {1:>5.2}, Train Acc: {2:>6.2%}, Val Loss: {3:>5.2}, Val Acc: {4:>6.2%}, Time: {5} {6}' |
| print(msg.format(total_batch, loss.item(), train_acc, dev_loss, dev_acc, time_dif, improve)) |
| model.train() |
| |
| total_batch += 1 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| print(f"保存 Epoch {epoch+1} 的数据...") |
| try: |
| |
| save_epoch_data(model, train_data, dev_data, test_data, config, epoch+1, epochs_dir, device) |
| except RuntimeError as e: |
| if "NCCL" in str(e) or "cuda" in str(e).lower(): |
| print(f"\n❌ 保存数据时多GPU错误: {e}") |
| print("建议在配置文件中设置 gpu_ids: [0]") |
| return |
| else: |
| raise e |
| |
| |
| avg_loss = epoch_loss / batch_count |
| avg_acc = epoch_acc / batch_count |
| print(f"Epoch {epoch+1} - 平均损失: {avg_loss:.4f}, 平均准确率: {avg_acc:.4f}") |
| |
| |
| print("进行最终测试...") |
| model.eval() |
| test_acc, test_loss = evaluate(model, test_iter, device) |
| print(f"最终测试结果 - 损失: {test_loss:.4f}, 准确率: {test_acc:.4f}") |
| |
| total_time = get_time_dif(start_time) |
| print(f"总训练时间: {total_time}") |
|
|
|
|
| if __name__ == '__main__': |
| parser = argparse.ArgumentParser(description='TextCNN训练脚本') |
| parser.add_argument('--config', type=str, default='train.yaml', help='训练配置文件路径') |
| args = parser.parse_args() |
| |
| train(args.config) |
|
|