上传文件至 /

This commit is contained in:
2026-04-30 15:53:27 +08:00
parent 2fc549d284
commit 58da0d6314
5 changed files with 7934 additions and 360 deletions

94
main.py
View File

@@ -1,34 +1,60 @@
# -*- coding: utf-8 -*-
"""
主程序入口
使用方式:
1. 运行单个模型(默认):
python main.py
修改 config.py 中的 MODEL_TYPE 和 VECTORIZER_TYPE 来切换配置
2. 运行对比实验:
修改 config.py 中 RUN_COMPARISON = True
这会依次运行:
- 实验1: BoW vs TF-IDF (固定LR模型)
- 实验2: LR vs MLP (固定TF-IDF)
- 实验3: 不同学习率对比
- 实验4: 不同隐藏层大小对比
最后输出汇总报告
"""
from train import main
if __name__ == '__main__':
print("\n" + "=" * 70)
print("文本分类实验 - 纯NumPy实现")
print("数据集: ChnSentiCorp (中文酒店评论)")
print("模型: Logistic Regression / MLP")
print("向量化: BoW / TF-IDF")
print("=" * 70 + "\n")
main()
import numpy as np
from dataset import load_data, BoWVectorizer, TFIDFVectorizer
from train import train
import config as cfg
import pickle
import time
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
texts, labels = load_data()
labels = np.array(labels)
# <20><><EFBFBD><EFBFBD>ѵ<EFBFBD><D1B5><EFBFBD><EFBFBD>/<2F><><EFBFBD>Լ<EFBFBD>
np.random.seed(42)
indices = np.random.permutation(len(texts))
split = int(0.8 * len(texts))
train_idx, test_idx = indices[:split], indices[split:]
train_texts = [texts[i] for i in train_idx]
test_texts = [texts[i] for i in test_idx]
y_train, y_test = labels[train_idx], labels[test_idx]
# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if cfg.VECTORIZER_TYPE == "bow":
vec = BoWVectorizer(cfg.MAX_FEATURES)
else:
vec = TFIDFVectorizer(cfg.MAX_FEATURES)
vec.fit(train_texts)
X_train = np.array([vec.transform(t) for t in train_texts])
X_test = np.array([vec.transform(t) for t in test_texts])
# ѵ<><D1B5>
print("="*50)
print(f"ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n ģ<><C4A3>: {cfg.MODEL_TYPE}\n <20><><EFBFBD><EFBFBD>: {cfg.VECTORIZER_TYPE}\n ѧϰ<D1A7><CFB0>: {cfg.LEARNING_RATE}")
print("="*50)
model, t = train(
X_train, y_train, X_test, y_test,
model_type=cfg.MODEL_TYPE,
lr=cfg.LEARNING_RATE,
epochs=cfg.NUM_EPOCHS,
use_weight=cfg.USE_CLASS_WEIGHT
)
# <20><><EFBFBD><EFBFBD>
ts = time.strftime("%m%d_%H%M%S")
name = f"model_{cfg.MODEL_TYPE}_{cfg.VECTORIZER_TYPE}_{'weighted' if cfg.USE_CLASS_WEIGHT else 'raw'}_{ts}"
if cfg.MODEL_TYPE == "lr":
np.save(f"{name}_W.npy", model.W)
np.save(f"{name}_b.npy", model.b)
else:
np.save(f"{name}_W1.npy", model.W1)
np.save(f"{name}_b1.npy", model.b1)
np.save(f"{name}_W2.npy", model.W2)
np.save(f"{name}_b2.npy", model.b2)
with open(f"{name}_vec.pkl", "wb") as f:
pickle.dump(vec, f)
print(f"\nģ<EFBFBD><EFBFBD><EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD>: {name}_*.npy/*.pkl")