機械学習メモ

化学を機械学習で何か

WSL2上でUbuntuにamberをコンパイルする

WSL上のubuntuにamber23をインストールした時のメモ

ある段階でcudatoolkitのバージョンが最新すぎると、コンパイルが通らない問題がある。
それを回避するために、gcc-10, g++-10, gfortran-10を導入し、cutatoolkit-11.1を導入した。
homedir直下にlocalフォルダを作成して、そこにインストールすることを想定しているので
適宜変えてください。

導入方法
sudo apt -y update
sudo apt -y install tcsh make \
gcc gfortran g++ \
flex bison patch bc wget \
xorg-dev libz-dev libbz2-dev

sudo apt-get -y install gcc-10 g++-10 gfortran-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11

sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

Selection Path Priority Status

                                                                                                                      • -

0 /usr/bin/gcc-11 11 auto mode
1 /usr/bin/gcc-10 10 manual mode
2 /usr/bin/gcc-11 11 manual mode
1を選んでおく

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/g++-10 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/g++-11 11
sudo update-alternatives --config g++

これも同じく

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gfortran-10 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gfortran-11 11
sudo update-alternatives --config gfortran

これも同じく

次に、nvidiaのサイトでcudatoolkit-11.1をダウンロードする
developer.nvidia.com

これは、サイトの指示に従って導入する。


.bashrcに下記を追加する(/usr/local/cuda-11.1を指定してもOK)
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

設定した環境変数を読み込む
sourrce ~/.bashrc

minicondaを導入し、環境を構築する
conda create -n amber23 python=3.8
codna activate amber23
conda install -y mpi4py numpy pandas

amber23.tar.bzとambertools22.tar.bzを展開し、build内のrun_cmakeを書き換える
#!/bin/bash

# This file gives some sample cmake invocations. You may wish to
# edit some options that are chosen here.

# For information on how to get cmake, visit this page:
# https://ambermd.org/pmwiki/pmwiki.php/Main/CMake-Quick-Start

# For information on common options for cmake, visit this page:
# http://ambermd.org/pmwiki/pmwiki.php/Main/CMake-Common-Options

# (Note that you can change the value of CMAKE_INSTALL_PREFIX from what
# is suggested below, but it cannot coincide with the amber22_src
# folder.)

#AMBER_PREFIX=$(dirname $(dirname `pwd`))

AMBER_PREFIX="/home/hoge/local/"
if [ `uname -s|awk '{print $1}'` = "Darwin" ]; then

# For macOS:

if [ -x /Applications/CMake.app/Contents/bin/cmake ]; then
cmake=/Applications/CMake.app/Contents/bin/cmake
else
cmake=cmake
fi

$cmake $AMBER_PREFIX/amber22_src \
-DCMAKE_INSTALL_PREFIX=$AMBER_PREFIX/amber22 \
-DCOMPILER=CLANG -DBLA_VENDOR=Apple \
-DMPI=FALSE -DCUDA=FALSE -DINSTALL_TESTS=TRUE \
-DDOWNLOAD_MINICONDA=TRUE \
2>&1 | tee cmake.log

else

# Assume this is Linux:

cmake /home/hoge/src/amber22_src/ \
-DCMAKE_INSTALL_PREFIX=$AMBER_PREFIX/amber23 \
-DCOMPILER=GNU \
-DMPI=FALSE -DCUDA=FALSE -DINSTALL_TESTS=TRUE \
-DDOWNLOAD_MINICONDA=FALSE \
-DPYTHON_EXECUTABLE=/home/hoge/miniconda3/envs/amber23/bin/python \
2>&1 | tee cmake.log

fi

if [ ! -s cmake.log ]; then
echo ""
echo "Error: No cmake.log file created: you may need to edit run_cmake"
exit 1
fi

echo ""
echo "If errors are reported, search for 'CMake Error' in the cmake.log file."
echo ""
echo "If the cmake build report looks OK, you should now do the following:"
echo ""
echo " make install"
echo " source $AMBER_PREFIX/amber22/amber.sh"
echo ""
echo "Consider adding the last line to your login startup script, e.g. ~/.bashrc"
echo ""

コンパイルする
./run_cmake
make
make install
source ~/local/amber23/amber.sh

その後に、Ambertools/src内にmpichi-4.1.2.tar.gzを展開し、
cd ../AmberTools/src/
configure_mpich gnu

でmpi用のコンパイラを作成

cd ../../build
make clean
./run_cmake # DMPI=TRUEにする
make
make install
./run_cmake #さらに DCUDA=TRUEにする
make
make install

WSL2上でUbuntuのGUIを使う

WSL2上でUbuntuGUIを利用する手段として、Windowsリモートデスクトップ機能を用いることができる。Windowsのストアより、Ubuntuをインストールしたのちに、Ubuntuを起動させてから、下記のコマンドを順次入力していく

sudo apt-mark hold acpid acpi-support
sudo apt install -y ubuntu-desktop
sudo apt install -y xrdp
# change the Linux RDP port, because already used by Windows 
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini    
sudo systemctl restart xrdp

次に、ホームディレクトリの.xsessionrcを作成して、下記の内容を入れる

export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share:/usr/share:/var/lib/snapd/desktop
export WAYLAND_DISPLAY=
export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg

あとは、日本語化したり、Nvidiaのcudaを入れる。キーボードレイアウトがかなり面倒だった(setxkbmap jpを.bashrcにいれるだけ)。以下があっているかどうかわからないけど。

sudo apt -y install language-pack-ja
sudo update-locale LANG=ja_JP.UTF8
# WSLを再起動 wsl --shutdown
sudo dpkg-reconfigure tzdata
sudo apt -y install manpages-ja manpages-ja-dev
sudo apt-get -y install ibus-mozc
sudo apt-get -y install console-common console-data unicode-data

qiita.com
qiita.com
qiita.com
www.kkaneko.jp

ようやく、下記みたいにLinuxGUIを使えるようになった。仮想デスクトップ機能を使えば、linuxwindowsをショートカットで行き来できるようになる。すごい時代が来たものだ。

リモートデスクトップ画面

土日ライターって辛いね

平日は仕事をしながら、ネタを考えて記事をアップするのはなかなか難しいですね。いや、ネタはいっぱいあるんだけど、ネタを吸収するための土壌を得るには、三日くらいの連休がないと、エンジンがかからないです。平日本気だしすぎている証拠だな。

同じ化学構造かどうかを調べるツール

データベースの中に、同じ化学構造が入っていて、あーーってことあります。自分が作っていないと、余計に気になります。なので、確かめるプログラムを作っています。

import pandas as pd
from rdkit import Chem, DataStructs
from rdkit.Chem import AllChem
csv="iQSPR_sample_data.csv"
data=pd.read_csv(csv)
data=data[ data["SMILES"] != "FBr(F)(F)(F)F"  ]
smi=data["SMILES"]
mols=[Chem.MolFromSmiles(m) for m in smi]
morgan_fps = [AllChem.GetMorganFingerprintAsBitVect(mol, 2, 2048) for mol in mols]
sameOne=0
for i in range(len(smi)):
    morgan = DataStructs.BulkTanimotoSimilarity(morgan_fps[i], morgan_fps)
    for j in range(len(smi)):
        if i != j :
            if morgan[j] > 0.9:
                a=Chem.MolToSmiles(mols[i])
                b=Chem.MolToSmiles(mols[j])
                if a==b:
                    print("Tanimo coefficient and SMILES test",i,j,a,list(data["E"])[i],list(data["E"])[j])
                    sameOne+=1
print(len(smi),sameOne)

実行してみると、
16673 814
という答えがあるので、814/2個分だけ余計なデータが入っていたようです。

そこまで多くはないので、学習モデルには影響していないのかもしれない。おなじ化学構造でエネルギー計算の値が0.8kJ/molほど違っていたので、その差はそんなにないのか。

計算化学のデータだからと言って、鵜呑みにせず、調べてみることが必要かもしれないですね。
(自分でデータ作るか、検算はしておこうということですね)

Graph Convolutional Networkやってみた

Graph Convolutional Network

XenonPyのiQSPRデモで用いた量子化学計算データを使って、学習可能かどうか試してみた。

二つの値を同時に予測するモデルを作成し、50ステップほどやってみました。エネルギーは簡単に学習されますが、バンドギャップはもう少し学習させると飽和しそうです。エネルギーが学習されてから、バンドギャップの学習性能が向上しているようにも見えますね。

見かけの予測精度は高いけれど、ノード数を増やして、解像度を上げないとiQSPRに持っていけないな。転移学習させる際にも、ノード数を増やさないとダメだろうな。

構造改変に対して、予測値がどのように変化しているかを調べる仕組みを作らないといけないな。やり方は複数種類あるだろうし、そこまで難しくないな。

気づいたこととしては、learning rateをどんどん小さくしていかないと、片方の予測精度を上げることが優先されてしまう。ゆっくり学習させる必要があります。

Graph Convolutional Networkは、こちらを参考にしました。
qiita.com

論文メモ

 

あとで読もうかなという論文たち

https://www.sciencedirect.com/science/article/abs/pii/S0927025619305026

https://proceedings.mlr.press/v162/stark22a.html

https://pubs.acs.org/doi/10.1021/acssuschemeng.2c05225

https://arxiv.org/abs/2303.08272#

https://jcheminf.biomedcentral.com/articles/10.1186/s13321-020-00479-8

https://www.nature.com/articles/s43246-022-00315-6

https://arxiv.org/abs/2303.12188

 

GcNN関連

Make Graph convolution model with geometric deep learning extension library for PyTorch #RDKit #chemoinformatics #pytorch – Is life worth living? (wordpress.com)

HannesStark/3DInfomax: Making self-supervised learning work on molecules by using their 3D geometry to pre-train GNNs. Implemented in DGL and Pytorch Geometric. (github.com)

MolecularAI/GraphINVENT: Graph neural networks for molecular design. (github.com)

Graph Convolutional Network による溶解度予測 (回帰) - Qiita

biomed-AI/MolRep: MolRep: A Deep Representation Learning Library for Molecular Property Prediction (github.com)

DG

GitHub - akensert/molgraph: Graph neural networks for molecular machine learning. Implemented and compatible with TensorFlow and Keras.

GitHub - NU-CUCIS/CheMixNet: Mixed DNN Architectures for Predicting Properties using Multiple Molecular Representations

GitHub - rnepal2/Solubility-Prediction-with-Graph-Neural-Networks: GNN, GCN, Molecular Solubility, RDKit, Cheminformatics

qiitaの方がコード貼り付けやすいかもな。