山东大学2019级软件工程应用与实践——基于人工智能的多肽药物分析问题(十二)"/>
山东大学2019级软件工程应用与实践——基于人工智能的多肽药物分析问题(十二)
2021SC@SDUSC
基于人工智能的多肽药物分析问题
主题:蛋白质预训练模型(6)
代码分析
Benchmark Section
ProtTrans/Benchmark/ProtAlbert.ipynb
加载必要的库,包括 huggingface transformer
import torch
from transformers import AlbertModel
import time
from datetime import timedelta
import os
import requests
from tqdm.auto import tqdm
设置 ProtAlbert 和 vocabulary 文件的 url
modelUrl = '.bin?dl=1'
configUrl = '.json?dl=1'
tokenizerUrl = '.model?dl=1'
下载 ProtAlbert 模型和 vocabulary 文件
downloadFolderPath = 'models/ProtAlbert/'modelFolderPath = downloadFolderPathmodelFilePath = os.path.join(modelFolderPath, 'pytorch_model.bin')configFilePath = os.path.join(modelFolderPath, 'config.json')tokenizerFilePath = os.path.join(modelFolderPath, 'spm_model.model')
定义 download_file 函数
if not os.path.exists(modelFolderPath):os.makedirs(modelFolderPath)def download_file(url, filename):response = requests.get(url, stream=True)with tqdm.wrapattr(open(filename, "wb"), "write", miniters=1,total=int(response.headers.get('content-length', 0)),desc=filename) as fout:for chunk in response.iter_content(chunk_size=4096):fout.write(chunk)
下载
if not os.path.exists(modelFilePath):download_file(modelUrl, modelFilePath)if not os.path.exists(configFilePath):download_file(configUrl, configFilePath)if not os.path.exists(tokenizerFilePath):download_file(tokenizerUrl, tokenizerFilePath)
载入 ProtAlbert 模型
model = AlbertModel.from_pretrained(modelFolderPath)
若GPU可用则将模型载入GPU,切换至推理模式
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')model = model.to(device)
model = model.eval()
Benchmark Configuration
min_batch_size = 8
max_batch_size = 32
inc_batch_size = 8min_sequence_length = 64
max_sequence_length = 512
inc_sequence_length = 64iterations = 10
Start Benchmarking
device_name = torch.cuda.get_device_name(device.index) if device.type == 'cuda' else 'CPU'with torch.no_grad():print((' Benchmarking using ' + device_name + ' ').center(80, '*'))print(' Start '.center(80, '*'))for sequence_length in range(min_sequence_length,max_sequence_length+1,inc_sequence_length):for batch_size in range(min_batch_size,max_batch_size+1,inc_batch_size):start = time.time()for i in range(iterations):input_ids = torch.randint(1, 20, (batch_size,sequence_length)).to(device)results = model(input_ids)[0].cpu().numpy()end = time.time()ms_per_protein = (end-start)/(iterations*batch_size)print('Sequence Length: %4d \t Batch Size: %4d \t Ms per protein %4.2f' %(sequence_length,batch_size,ms_per_protein))print(' Done '.center(80, '*'))print(' Finished '.center(80, '*'))
论文学习
文章中的 2.5 Step 2: Transfer learning of supervised models 使用到了迁移学习,因此我们对迁移学习进行初步的学习。
迁移学习 Transfer Learning
迁移学习(Transfer learning) 就是就是把已学训练好的模型参数迁移到新的模型来帮助新模型训练。考虑到大部分数据或任务是存在相关性的,所以通过迁移学习我们可以将已经学到的模型参数(也可理解为模型学到的知识)通过某种方式来分享给新模型从而加快并优化模型的学习效率,不用像大多数网络那样从零学习(starting from scratch,tabula rasa)。
Model Fine-tuning是处理这种问题的一种常用方法,即在已有的数据集的基础上,使用新的数据集重新训练训练一个模型,新的数据集只会对模型进行微调(防止出现过拟合)。
常常用的方法是conservative training,这是用一种保守的方法对模型进行训练,具体的操作方法是将神经网络中的某些层frozen。
-
在做语音识别任务时,通常对神经网络的后几层进行frozen,一种解释是说,在处理语音信号时,神经网络的前几层会根据语音的不同表现进行不同的处理,经过前几层后,不管是谁说的什么话,都被处理成相似的东西,再交由后几层以一种范式处理信息;
-
在做图像分类时,通常对网络的前几层进行frozen,因为研究表明,神经网络的前几层通常是对图像的几何特性进行提取分割,即使是不同的图像,分割提取的手法也近乎相近。
参考资料:
更多推荐
山东大学2019级软件工程应用与实践——基于人工智能的多肽药物分析问题(十二)
发布评论