import pandas as pd
train_df = pd.read_csv('train.csv')
test_df = pd.read_csv('testB.csv')
train_df.info()
res = []
for i in range(len(train_df)):
paper_item = train_df.loc[i]
tmp = {
"instruction": "Please judge whether it is a medical field paper according to the given paper title and abstract, output 1 or 0, the following is the paper title, author and abstract -->",
"input": f"title:{paper_item[1]},abstract:{paper_item[3]}",
"output": str(paper_item[5])
}
res.append(tmp)
import json
with open('paper_label.json', mode='w', encoding='utf-8') as f:
json.dump(res, f, ensure_ascii=False, indent=4)
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Device:", device)
torch.cuda.empty_cache()
from peft import PeftModel
from transformers import AutoTokenizer, AutoModel, GenerationConfig, AutoModelForCausalLM
model_path = "THUDM/chatglm2-6b"
model = AutoModel.from_pretrained(model_path, trust_remote_code=True).half().cuda()
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# 加载 label lora权重
model = PeftModel.from_pretrained(model, 'huanhuan-chat/output/label_xfg').half()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
def predict(text):
response, history = model.chat(tokenizer, f"Please judge whether it is a medical field paper according to the given paper title and abstract, output 1 or 0, the following is the paper title, author and abstract -->{text}", history=[],
temperature=0.01)
return response
predict('title:Seizure Detection and Prediction by Parallel Memristive Convolutional Neural Networks,author:Li, Chenqi; Lammie, Corey; Dong, Xuening; Amirsoleimani, Amirali; Azghadi, Mostafa Rahimi; Genov, Roman,abstract:During the past two decades, epileptic seizure detection and prediction algorithms have evolved rapidly. However, despite significant performance improvements, their hardware implementation using conventional technologies, such as Complementary Metal-Oxide-Semiconductor (CMOS), in power and areaconstrained settings remains a challenging task; especially when many recording channels are used. In this paper, we propose a novel low-latency parallel Convolutional Neural Network (CNN) architecture that has between 2-2,800x fewer network parameters compared to State-Of-The-Art (SOTA) CNN architectures and achieves 5-fold cross validation accuracy of 99.84% for epileptic seizure detection, and 99.01% and 97.54% for epileptic seizure prediction, when evaluated using the University of Bonn Electroencephalogram (EEG), CHB-MIT and SWEC-ETHZ seizure datasets, respectively. We subsequently implement our network onto analog crossbar arrays comprising Resistive Random-Access Memory (RRAM) devices, and provide a comprehensive benchmark by simulating, laying out, and determining hardware requirements of theCNNcomponent of our system. We parallelize the execution of convolution layer kernels on separate analog crossbars to enable 2 orders of magnitude reduction in latency compared to SOTA hybrid Memristive-CMOS Deep Learning (DL) accelerators. Furthermore, we investigate the effects of non-idealities on our system and investigate Quantization Aware Training (QAT) to mitigate the performance degradation due to lowAnalog-to-Digital Converter (ADC)/Digital-to-Analog Converter (DAC) resolution. Finally, we propose a stuck weight offsetting methodology to mitigate performance degradation due to stuck RON/ROFF memristor weights, recovering up to 32% accuracy, without requiring retraining. The CNN component of our platform is estimated to consume approximately 2.791Wof power while occupying an area of 31.255 mm(2) in a 22 nm FDSOI CMOS process.')
# 预测测试集
from tqdm import tqdm
label = []
for i in tqdm(range(len(test_df))):
test_item = test_df.loc[i]
test_input = f"title:{test_item[1]},author:{test_item[2]},abstract:{test_item[3]}"
label.append(int(predict(test_input)))
test_df['label'] = label
submit = test_df[['uuid', 'Keywords', 'label']]
submit.to_csv('submit.csv', index=False)
1-2. 导入 pandas 库并读取 'train.csv' 和 'testB.csv' 这两个 CSV 文件。这些文件应该包含了训练和测试的数据。
-
查看训练数据的信息,包括每个特征的类型和非空值的数量等。
-
初始化一个空列表,用于存储转换后的训练数据。
5-14. 遍历训练数据,对每一行数据进行处理。首先获取这一行的数据,然后将数据转换为一个字典,字典的 "instruction" 键对应的值是一个固定的字符串,"input" 键对应的值是由标题和摘要组成的字符串,"output" 键对应的值是这篇文章是否属于医学领域(1 代表是,0 代表不是)。然后将这个字典添加到之前创建的列表中。
15-16. 导入 json 库,用于将数据转换为 JSON 格式。
17-19. 将处理后的数据转换为 JSON 格式并保存到 'paper_label.json' 文件中。
- 导入 torch 库,用于后续的深度学习模型的加载和预测。
21-22. 检查是否有可用的 CUDA 设备,如果有,则使用 CUDA 设备,否则使用 CPU。
- 清空 CUDA 缓存,这可以释放未使用的显存,但是不能释放已使用的显存。
24-26. 从 'peft' 库中导入 'PeftModel' 类,这是一个自定义的深度学习模型。
27-29. 从 'transformers' 库中导入一些类,这些类用于加载预训练的深度学习模型和分词器。
30-33. 加载预训练的深度学习模型和分词器。模型和分词器的路径是 "THUDM/chatglm2-6b",如果这个路径不存在,代码将无法加载模型和分词器。
34-35. 加载标签权重。权重的路径是 'huanhuan-chat/output/label_xfg',如果这个路径不存在,代码将无法加载权重。
36-37. 将模型设置为评估模式,这意味着模型在进行预测时不会改变其内部状态。
38-39. 使用模型和分词器进行一次预测,输入是 "你好",输出是模型的预测结果。
40-49. 定义一个函数,这个函数的输入是一篇文章的标题和摘要,输出是模型对这篇文章是否属于医学领域的预测结果。
-
调用之前定义的函数进行一次预测,输入是一篇文章的标题和摘要,输出是模型的预测结果。
-
导入 'tqdm' 库,这个库可以生成进度条,用于显示代码的运行进度。
53-54. 初始化一个空列表,用于存储模型对测试数据的预测结果。
55-60. 遍历测试数据,对每一行数据进行处理。首先获取这一行的数据,然后将数据转换为模型需要的输入格式,调用之前定义的函数进行预测,然后将预测结果添加到之前创建的列表中。
- 将模型的预测结果添加到测试数据中,形成一个新的列。
63-65. 从测试数据中选择需要的列,然后将结果保存到 'submit.csv' 文件中。