Views
No views yet
merged_models/
├── config.json # BERT模型配置文件
├── merge_models.py # 模型合并脚本
├── predict.py # 预测功能实现
├── pytorch_model.bin # 模型权重文件
├── tokenizer_config.json # 分词器配置
└── vocab.txt # 词表文件pip install torch transformers numpy scikit-learn1import torch
2from transformers import BertTokenizer
3from merge_models import SimpleRatingPredictor
4
5# 设置设备
6device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
7
8# 加载模型和分词器
9model_path = './merged_models' # 指向模型文件夹的路径
10tokenizer = BertTokenizer.from_pretrained(model_path)
11model = SimpleRatingPredictor(model_path)
12
13# 加载模型权重
14state_dict = torch.load(f'{model_path}/pytorch_model.bin', map_location=device)
15model.load_state_dict(state_dict)
16model.to(device)1from predict import predict_rating
2
3# 示例文本
4text = "这是一个需要评分的文本"
5
6# 进行预测
7rating = predict_rating(text, model, tokenizer, device)
8print(f"预测评分: {rating:.1f}")1from predict import load_test_data, evaluate_model
2
3# 加载测试数据
4test_file = 'test.jsonl'
5texts, true_points = load_test_data(test_file)
6
7# 进行预测
8predictions = []
9for i, text in enumerate(texts, 1):
10 rating = predict_rating(text, model, tokenizer, device)
11 predictions.append(rating)
12 if i % 10 == 0:
13 print(f"已完成 {i}/{len(texts)} 条预测")
14
15# 评估模型性能
16metrics = evaluate_model(predictions, true_points)
17print("\n模型评估结果:")
18for metric_name, value in metrics.items():
19 print(f"{metric_name}: {value:.4f}")