WeiNote

yyrcd

web

TA

FEB

19

CUDA

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
if torch.cuda.is_available(): print(torch.cuda.get_device_name()) 

FEB

15

VS Code - Common setting

{
    "python.pythonPath": "/Users/richard/Program/anaconda3/envs/ml/bin/python",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120", 
        // "--ignore=None"
    ]

}

FEB

11

python assert

assert torch.cuda.is_available(), "CUDA is not avaliable"

FEB

10

Mac build Pytorch with CUDA

All versions | CUDA Toolkit Archive | NVIDIA Developer

PyTorch (1.0), CUDA & cuDNN on MacOS / Data Scientist from Chile Installing-Pytorch-Cuda-on-Macbook

Magma

Magma Bitbucket - Magma Webpage RuntimeError: inverse: MAGMA library not found in compilation. Please rebuild with MAGMA.

# environment
brew install openblas
brew install libomp
brew install lapack

export CUDADIR=/Developer/NVIDIA/CUDA-10.1
export LAPACKDIR="/usr/local/opt/lapacke"
export OPENBLASDIR="/usr/local/opt/openblas"
export OpenBLAS_LIB="/usr/local/opt/openblas/lib"
export OpenBLAS_INCLUDE_DIR="/usr/local/opt/openblas/include"

# magma
wget http://icl.utk.edu/projectsfiles/magma/downloads/magma-2.5.2.tar.gz
tar xvf magma-2.5.2.tar.gz
cd magma
vim make.inc
sudo mkdir /usr/local/magma
sudo chown richard:admin /usr/local/magma
make install prefix=/usr/local/magma -j 4

# to build pytorch, add magma/lib to dyld path
export DYLD_LIBRARY_PATH=/usr/local/magma/lib:$DYLD_LIBRARY_PATH

Build pytorch wheel for mac

pytorch-osx-build/BuildInstractions-1.0.1.md

FEB

05

3D Plot

Code on Gist

FEB

03

copy.copy() and copy.deepcopy()

>>> a = [1, 2, [3]]
>>> c = copy.deepcopy(a)
>>> b = copy.copy(a)
>>> a[-1][0] = 4
>>> a
[1, 2, [4]]
>>> b
[1, 2, [4]]
>>> c
[1, 2, [3]]

is and ==

a = 1
b = 1
a == b  # True  | Value
a is b  # False | Object

sort

intervals = [[1, 2], [2, 3], [5, 8], [0, 1]]
intervals.sort(key=lambda x: x[0])

dict

dict.values()
dict.keys()
dict.items()

max(dict, key=dict.get)
max(dict.keys(), key=lambda k: dict[k])

JAN

09

Counting parameters in a model

sum(p.numel() for p in model.parameters())

JAN

06

h5ls

$ h5ls data/ANI-1x-hf-mo/6-31gd/ANI_1x_HF_631gd_MO_e_lt71.v5.train.h5 
MO_energies              Dataset {4173863}
coordinates              Dataset {4173863}
energies                 Dataset {4173863, 1}
id                       Dataset {4173863}
name                     Dataset {4173863}
no                       Dataset {4173863, 1}
num_e                    Dataset {4173863, 1}
runtime                  Dataset {4173863, 1}
species                  Dataset {4173863}

JAN

05

byte string (ASCII) UTF-8 string

>>> a =  b"Hello"
>>> a
b'Hello'
>>> a.decode()
'Hello'

JAN

04

pandas

df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in data.items()]))

2019

DEC

12

Print without new line

import sys
sys.stdout.write('.')
sys.stdout.flush()

DEC

09

Polymorphism - 多态

继承 inheritence 重载 overload 一个函数名对应多个函数(根据参数类型,个数,顺序) 重写 override 子类override父类的方法 模板 genericity 类型type作为一个parameter

More info: Java 多态 | 菜鸟教程

DEC

08

Python Internal

  1. Book Inside The Python Virtual Machine pdf | Read online Python源码剖析 pdf

  2. 谈谈 Python 程序的运行原理 | 淡水网志

  3. CPython的Global Interprate Lock(GIL): Preventing multiple threads from executing Python bytecodes at once.

    • 大家听说过对 CPython的GIL的抱怨不?经常听到对不对? 有多少一般 Python用户知道吐槽GIL其实真的在吐槽的就是 CPython的引用计数及C API实现? -- 知乎

    • Python的全局解释器锁(GIL) - 简书

      Done is better than perfect

SEP

25

Save CSV file and load from http

Save

import pandas as pd
df = pd.DataFrame(dict([(k, pd.Series(v)) for k, v in data.items()]))
data = {'wavenumber': wavenumber, 'absorbance': absorbance}
df.to_csv('wave_abs.csv', mode='a', index=False)

Load from http

data = pd.read_csv('https://yyrcd-1256568788.cos.na-siliconvalley.myqcloud.com/yyrcd/2019-09-25-wave_abs.csv')
wavenumber = data['wavenumber'].values
absorbance = data['absorbance'].values

AUG

24

python global

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

AUG

23

pathlib is better than os.path

AUG

16

python import a package

# ~/.bashrc
export $PYTHONPATH=/path/to/package:$PYTHONPATH

then the path will be shown in sys.path, then you can import package Another way is

import sys
sys.path.append(/path/to/package)

AUG

14

python str.format()

'CPU  {: 4d}%'.format(100)
'CPU  {: 4d}%'.format(10)
'CPU  {: 4d}%'.format(0)
CPU   100%
CPU    10%
CPU     0%
'CPU  {:04d}%'.format(100)
'CPU  {:04d}%'.format(10)
'CPU  {:04d}%'.format(0)
CPU  0100%
CPU  0010%
CPU  0000%

AUG

10

JUL

31

pytorch two test molecule

model = ANI_D.model.ANI_MO_Pair_Model(device=device)

coordinates = torch.tensor(
    [[[0.03192167, 0.00638559, 0.01301679],
      [-0.83140486, 0.39370209, -0.26395324],
      [-0.66518241, -0.84461308, 0.20759389],
      [0.45554739, 0.54289633, 0.81170881],
      [0.66091919, -0.16799635, -0.91037834]],
     [[-4.1862600, 0.0575700, -0.0381200], 
      [-3.1689400, 0.0523700, 0.0200000],
      [-4.4978600, 0.8211300, 0.5604100], 
      [-4.4978700, -0.8000100, 0.4155600],
      [0.00000000, -0.00000000, -0.00000000]]],
    requires_grad=True,
    device=device)

species = torch.tensor([[1, 0, 0, 0, 0], [2, 0, 0, 0, -1]], device=device)

pred, _, MO_num = model([(species, coordinates)])

JUL

29

Note that calling my_tensor.to(device) returns a new copy of my_tensor on GPU. It does NOT overwrite my_tensor. Therefore, remember to manually overwrite tensors: my_tensor = my_tensor.to(torch.device('cuda')).

JUL

02

TensorFlow-nightly anaconda setup

# base
conda update -n base -c defaults conda
conda update conda-build
# tf
conda create -n tf python=3.6 anaconda
conda update setuptools #tf needs setuptools>=41.0.0
pip install tf-nightly-gpu==1.14.1.dev20190620
conda install -c conda-forge jupytext
conda install -c conda-forge pysnooper
conda install -c conda-forge matplotlib
conda install -c anaconda jupyter 

JUN

20

Transformer Model Bert XLNet

JUN

18

TensorFlow: Could not load dynamic library 'libcupti.so.10.0'

conda install cudatoolkit=10.0 -c pytorch

JUN

18

TensorFlow get operation by name

op = tf.get_default_graph().get_operation_by_name("training_2/SGD/gradients/loss_1/output_1_loss/Mean_grad/DynamicStitch")
op.inputs[1]

JUN

18

JUN

18

JUN

18

JUN

10

2 numpy molecule

import numpy as np
coor = np.array([[[0.03192167,  0.00638559,  0.01301679],
[-0.83140486,  0.39370209, -0.26395324],
[-0.66518241, -0.84461308,  0.20759389],
[0.45554739,   0.54289633,  0.81170881],
[0.66091919,  -0.16799635, -0.91037834]],
[[-1.46909, 2.22911, 0.00000],
[-0.44909, 2.22911, 0.00000],
[-1.80908, 1.69374, 0.79886],
[-1.80909, 1.80497, -0.86308],
[-1.80909, 1.80497, -0.86308]]])
spe = np.array([[1, 0, 0, 0, 0], [1, 0, 0, 0, 0]])

JUN

10

trainable weights

homo_model.animodel.layers[0].trainable
homo_model.weights

JUN

10

print all numpy values

import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)

JUN

07

tensorflow gpu set

# 1
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)) as sess:

# 2
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.enable_eager_execution(config=config)
with tf.Session(config=config) as sess:

JUN

07

inspect

import inspect
print(inspect.getsource(tf.keras.metrics.RootMeanSquaredError))
print(inspect.getsource(tf.keras.metrics.mean_squared_error))
print(inspect.getmro(tf.keras.metrics.RootMeanSquaredError))