更改requirements.txt文件的torch==1.12.0
Browse files- app.py +2 -2
- saved_models/config.json +46 -0
- train.py +29 -8
app.py
CHANGED
|
@@ -3,8 +3,8 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# 加载模型和tokenizer
|
| 6 |
-
tokenizer = AutoTokenizer.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify")
|
| 7 |
-
model = AutoModelForSequenceClassification.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify", num_labels=8)
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
def predict(text):
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
# 加载模型和tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify/model_20240302-214915_lr1e-05_optAdamW_lossBCEWithLogitsLoss_batch16_epoch10.pt")
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained("Tokymin/Mood_Anxiety_Disorder_Classify/model_20240302-214915_lr1e-05_optAdamW_lossBCEWithLogitsLoss_batch16_epoch10.pt", num_labels=8)
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
def predict(text):
|
saved_models/config.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "emilyalsentzer/Bio_ClinicalBERT",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"BertForSequenceClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"classifier_dropout": null,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.1,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"id2label": {
|
| 12 |
+
"0": "Anxiety Normal",
|
| 13 |
+
"1": "Anxiety Mild",
|
| 14 |
+
"2": "Anxiety Moderate",
|
| 15 |
+
"3": "Anxiety Major",
|
| 16 |
+
"4": "Depression Normal",
|
| 17 |
+
"5": "Depression Mild",
|
| 18 |
+
"6": "Depression Moderate",
|
| 19 |
+
"7": "Depression Major"
|
| 20 |
+
},
|
| 21 |
+
"initializer_range": 0.02,
|
| 22 |
+
"intermediate_size": 3072,
|
| 23 |
+
"label2id": {
|
| 24 |
+
"Anxiety Normal": 0,
|
| 25 |
+
"Anxiety Mild": 1,
|
| 26 |
+
"Anxiety Moderate": 2,
|
| 27 |
+
"Anxiety Major": 3,
|
| 28 |
+
"Depression Normal": 4,
|
| 29 |
+
"Depression Mild": 5,
|
| 30 |
+
"Depression Moderate": 6,
|
| 31 |
+
"Depression Major": 7
|
| 32 |
+
},
|
| 33 |
+
"layer_norm_eps": 1e-12,
|
| 34 |
+
"max_position_embeddings": 512,
|
| 35 |
+
"model_type": "bert",
|
| 36 |
+
"num_attention_heads": 12,
|
| 37 |
+
"num_hidden_layers": 12,
|
| 38 |
+
"pad_token_id": 0,
|
| 39 |
+
"position_embedding_type": "absolute",
|
| 40 |
+
"problem_type": "single_label_classification",
|
| 41 |
+
"torch_dtype": "float32",
|
| 42 |
+
"transformers_version": "4.25.1",
|
| 43 |
+
"type_vocab_size": 2,
|
| 44 |
+
"use_cache": true,
|
| 45 |
+
"vocab_size": 28996
|
| 46 |
+
}
|
train.py
CHANGED
|
@@ -107,14 +107,35 @@ for epoch in range(epochs): # 迭代多个epoch
|
|
| 107 |
loss = loss_fn(logits, b_labels)
|
| 108 |
total_eval_loss += loss.item() # 累加批次损失到总损失
|
| 109 |
# 使用sigmoid函数将logits转换为概率值
|
| 110 |
-
probs = torch.sigmoid(logits)
|
| 111 |
-
# 将概率高于0.5的预测为正类(1),低于0.5的预测为负类(0)
|
| 112 |
-
predictions = (probs > 0.5).int()
|
| 113 |
-
# 比较预测和真实标签
|
| 114 |
-
correct_predictions = (predictions == b_labels.int()).float() # 确保标签也是整数类型
|
| 115 |
-
# 计算每个样本的正确预测的平均数,然后计算整个批次的平均值
|
| 116 |
-
accuracy_per_sample = correct_predictions.mean(dim=1)
|
| 117 |
-
accuracy = accuracy_per_sample.mean().item()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
total_eval_accuracy += accuracy
|
| 119 |
# 更新进度条
|
| 120 |
eval_progress_bar.set_postfix({'accuracy': f"{accuracy:.2f}"})
|
|
|
|
| 107 |
loss = loss_fn(logits, b_labels)
|
| 108 |
total_eval_loss += loss.item() # 累加批次损失到总损失
|
| 109 |
# 使用sigmoid函数将logits转换为概率值
|
| 110 |
+
# probs = torch.sigmoid(logits)
|
| 111 |
+
# # 将概率高于0.5的预测为正类(1),低于0.5的预测为负类(0)
|
| 112 |
+
# predictions = (probs > 0.5).int()
|
| 113 |
+
# # 比较预测和真实标签
|
| 114 |
+
# correct_predictions = (predictions == b_labels.int()).float() # 确保标签也是整数类型
|
| 115 |
+
# # 计算每个样本的正确预测的平均数,然后计算整个批次的平均值
|
| 116 |
+
# accuracy_per_sample = correct_predictions.mean(dim=1)
|
| 117 |
+
# accuracy = accuracy_per_sample.mean().item()
|
| 118 |
+
|
| 119 |
+
logits_sas = logits[:, :4] # SAS_Class的4个输出
|
| 120 |
+
logits_sds = logits[:, 4:] # SDS_Class的4个输出
|
| 121 |
+
# 应用softmax来获取概率分布
|
| 122 |
+
probs_sas = torch.softmax(logits_sas, dim=1)
|
| 123 |
+
probs_sds = torch.softmax(logits_sds, dim=1)
|
| 124 |
+
|
| 125 |
+
# 选择概率最高的类别作为预测结果
|
| 126 |
+
_, predictions_sas = torch.max(probs_sas, dim=1)
|
| 127 |
+
_, predictions_sds = torch.max(probs_sds, dim=1)
|
| 128 |
+
|
| 129 |
+
# 真实的标签
|
| 130 |
+
true_sas = b_labels[:, 0].long() # 确保是长整型
|
| 131 |
+
true_sds = b_labels[:, 1].long() # 确保是长整型
|
| 132 |
+
|
| 133 |
+
# 计算准确性
|
| 134 |
+
accuracy_sas = (predictions_sas == true_sas).float().mean()
|
| 135 |
+
accuracy_sds = (predictions_sds == true_sds).float().mean()
|
| 136 |
+
|
| 137 |
+
# 综合两个准确性得分
|
| 138 |
+
accuracy = (accuracy_sas + accuracy_sds) / 2
|
| 139 |
total_eval_accuracy += accuracy
|
| 140 |
# 更新进度条
|
| 141 |
eval_progress_bar.set_postfix({'accuracy': f"{accuracy:.2f}"})
|