

AI4S Cup-IRMI tutorial | 基于红外光谱的分子识别
报名地址: https://nb.bohrium.dp.tech/competitions/detail/3473441128?tab=introduce
比赛内容
基于红外光谱预测的分子预测:在本次竞赛中,我们希望参赛者实现基于分子的红外光谱反推分子结构(SMILES表达)的机器学习算法。
数据介绍
本次赛题针对光谱结构反演,这也是目前学界和业界非常关注的课题之一。
- 数据量约13万分子。
- training set/test set = 8:2
- 输入是红外光谱数据
- 需要预测:按照置信度排序给出10个和这个光谱匹配的分子SMILES表达式
提交格式
需提交可运行的notebook推理代码,其中需要运行成功生成submission.csv文件在指定的路径。
使用镜像
ai4s-cup-0.1
推荐GPU
16G V00
baselines
simple_baseline Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com Requirement already satisfied: lmdb in /opt/conda/lib/python3.8/site-packages (1.3.0) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
比赛数据
此次比赛使用lmdb文件来存储数据,下面提供了一个dataset类来帮助选手们顺利读取训练数据。
/opt/conda/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm
每一条训练数据是一个包含两个key “smi” 和 “ir” 的dict,其中,“ir”是我们作为输入的红外光谱信息,而 "smi" 是需要预测的分子信息,它是个字符串。
下面我们会给一个例子,打印出数据集的第一条训练数据,并且把红外光谱以曲线图的形式画出来,给大家一个直观的理解。

训练模型
下面,我们将会抛砖引玉,构建一个简单的小改过的Transformer模型用于训练。
自定义collator
定义一些模型基础层
进行模型定义
直觉来说,一个transfomrer结构,输入ir输出SMILES就可以基本的解决这个问题。
但是由于ir是一个长度为3,000的tensor,所以需要使用一些MLP层,把ir变成encoder embedding
进行训练
因为只是示范,为了不浪费算力,只在small数据集上进行 20 steps 训练
/opt/conda/lib/python3.8/site-packages/transformers/optimization.py:411: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning warnings.warn( <ipython-input-4-deda29c3cfdf>:29: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at ../torch/csrc/utils/tensor_new.cpp:201.) input["ir"] = torch.tensor(ir, dtype=torch.float32).cuda() Epoch 0001 | Step 0001/0025 | Loss 5.8578 | Time 2.0328 Learning rate = 4.75e-05 Epoch 0001 | Step 0002/0025 | Loss 4.8940 | Time 2.4123 Learning rate = 4.5e-05 Epoch 0001 | Step 0003/0025 | Loss 4.4027 | Time 2.7918 Learning rate = 4.25e-05 Epoch 0001 | Step 0004/0025 | Loss 4.0437 | Time 3.1705 Learning rate = 4e-05 Epoch 0001 | Step 0005/0025 | Loss 3.8260 | Time 3.5467 Learning rate = 3.7500000000000003e-05 Epoch 0001 | Step 0006/0025 | Loss 3.6969 | Time 3.9215 Learning rate = 3.5e-05 Epoch 0001 | Step 0007/0025 | Loss 3.5683 | Time 4.3173 Learning rate = 3.2500000000000004e-05 Epoch 0001 | Step 0008/0025 | Loss 3.5376 | Time 4.7043 Learning rate = 3e-05 Epoch 0001 | Step 0009/0025 | Loss 3.4501 | Time 5.0899 Learning rate = 2.7500000000000004e-05 Epoch 0001 | Step 0010/0025 | Loss 3.3715 | Time 5.4703 Learning rate = 2.5e-05 Epoch 0001 | Step 0011/0025 | Loss 3.3231 | Time 5.7715 Learning rate = 2.25e-05 Epoch 0001 | Step 0012/0025 | Loss 3.3309 | Time 6.1494 Learning rate = 2e-05 Epoch 0001 | Step 0013/0025 | Loss 3.2512 | Time 6.4937 Learning rate = 1.75e-05 Epoch 0001 | Step 0014/0025 | Loss 3.1804 | Time 6.8662 Learning rate = 1.5e-05 Epoch 0001 | Step 0015/0025 | Loss 3.1801 | Time 7.2430 Learning rate = 1.25e-05 Epoch 0001 | Step 0016/0025 | Loss 3.1528 | Time 7.6188 Learning rate = 1e-05 Epoch 0001 | Step 0017/0025 | Loss 3.1445 | Time 7.9922 Learning rate = 7.5e-06 Epoch 0001 | Step 0018/0025 | Loss 3.1076 | Time 8.3664 Learning rate = 5e-06 Epoch 0001 | Step 0019/0025 | Loss 3.0716 | Time 8.7404 Learning rate = 2.5e-06
输出submission文件
输出submission.csv, 格式:
第一行是:
index,rank1,rank2,rank3,rank4,rank5,rank6,rank7,rank8,rank9,rank10
后面每行写一条结果,index从0开始计数
输出内容类似如下:
index,rank1,rank2,rank3,rank4,rank5,rank6,rank7,rank8,rank9,rank10
0,C,C,C,C,C,C,C,C,C,C
1,C,C,C,C,C,C,C,C,C,C
...
使用以上述代码在真正的数据集上训练20 epoch后保存的模型进行分子生成的结果:/bohr/AI4SCUP-IRMI-baseline-shdv/v2/simple_baseline/output/submission.csv
生成输出结果所需要的代码如下,为了快捷,以下代码,只在small数据集上跑2个batch:
2it [01:14, 37.37s/it]
curve_plot.png final_0.pt simple_baseline submission.csv
评测指标
本次赛题采用top-k指标,指对于每一个红外线光谱,按rank给予k个候选smiles字符串,这个k在本次比赛中设置为10。
在所有test集合的所有结果中,ground-truth smiles在top-1/top-3/top-5/top-10个candidates中命中的百分比是多少。
(加权为 0.4 * top-1 + 0.1 * top-3 + 0.1 * top-5 + 0.4 * top-10)
评测代码
下面是判断两个生成的SMILES是否为同一个分子的代码,我们将用以下标准,进行模型评测





