WeiNote

yyrcd

web

TA

pl

2019

Jul 29

(2019)

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')).

2020

Apr 16

Python print colord text to terminal

print('\033[91m', "hello", '\33[0m')  # red
print('\033[32m', "hello", '\33[0m')  # green

2020

Mar 16

2020

Mar 16

Avoid use np.append for big array in for loop

In [1]: import numpy as np                                                                                                                                                  

In [2]: %%timeit 
   ...: a = np.array(1) 
   ...: for i in range(5000): 
   ...:     a = np.append(a, 1) 
   ...:                                                                                                                                                                     
31.8 ms ± 615 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [3]: %%timeit 
   ...: a = [1] 
   ...: for i in range(5000): 
   ...:     a.append(1) 
   ...:                                                                                                                                                                     
380 µs ± 9.29 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

2020

Mar 14

Python tips and tricks

List Comprehensions

# [ expression for item in list if conditional ]
def square(x):
    return x**2
a = [square(i) for i in range(10) if (i % 2 == 0)]
# [0, 4, 16, 36, 64]

dataclass, Python 3.7 中 dataclass

from dataclasses import dataclass
@dataclass
class Card:
    rank: str
    suit: str
card = Card("Q", "hearts")
print(card == card)
# True

Slicing a list

a[start:stop:step], default is a[0:-1:1]

"abcdefgh"[::2]
# 'aceg'
"abcd"[::-1]
# 'dcba'

map

def upper(s):
    return s.upper()
mylist = list(map(upper, ['sentence', 'fragment']))
# ['SENTENCE', 'FRAGMENT']

Ternary Operator For Conditional Assignment

x = "Success!" if (y == 2) else "Failed!"

Integer division

Python 3
5 / 2 = 2.5
5 // 2 = 2

Reference:

30 Python Best Practices, Tips, And Tricks - Towards Data Science

2020

Mar 10

Jupyter style

<div class="alert alert-success"> 
    <p><b>alert-success</b></p>
</div>

<div class="alert alert-info"> 
    <p><b>alert-info</b></p>
</div>

<div class="alert alert-warning"> 
    <p><b>alert-warning</b></p>
</div>

<div class="alert alert-danger"> 
    <p><b>alert-danger</b></p>
</div>

2020

Mar 02

程序的编译与解释有什么区别? - 知乎

  1. 编译型语言在编译过程中生成目标平台的指令,解释型语言在运行过程中才生成目标平台的指令。
  2. 虚拟机的任务是在运行过程中将中间代码翻译成目标平台的指令。

也就是

  1. 一个是翻译成machine code,machine去解释
  2. 一个是翻译成bytecode,virtual machine去解释

2020

Feb 27

pip release

~/.pypirc file

[distutils]
  index-servers = pypi

[pypi]
  repository = https://upload.pypi.org/legacy/
  username = __token__
  password = API_Token
pip install setuptools-scm
pip install twine
git commit -m "update"
git push origin maseter 
git tag v0.4
git push origin v0.4
python setup.py sdist bdist_wheel # creating pkbar-0.4
twine upload dist/*

2020

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()) 

2020

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"
    ]

}

2020

Feb 11

python assert

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

2020

Feb 10

Pytorch

Common

pytorch/CONTRIBUTING.md update

git pull --rebase
git submodule sync --recursive
git submodule update --init --recursive
# or 
git pull --rebase; git submodule sync --recursive; git submodule update --init --recursive

Mac

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
MACOSX_DEPLOYMENT_TARGET=10.13 CC=clang CXX=clang++ python setup.py install

# build wheel
MACOSX_DEPLOYMENT_TARGET=10.13 CC=clang CXX=clang++ TORCH_CUDA_ARCH_LIST="2.0;3.0;3.5;5.2;6.1;7.0" python setup.py bdist_wheel

Build pytorch wheel for mac

pytorch-osx-build/BuildInstractions-1.0.1.md

2020

Feb 05

3D Plot

Code on Gist

2020

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

  • 1
intervals = [[1, 2], [2, 3], [5, 8], [0, 1]]
intervals.sort(key=lambda x: x[0])
from functools import cmp_to_key
def cmp_xy(x, y):
    spe_dict = {'C':1, 'H':0, 'N':1, 'O':2}
    x = spe_dict[x]
    y = spe_dict[y]

    if x > y:
        return 1
    elif x < y:
        return -1
    else:
        return 0

a = ['C', 'H', 'N', 'O']
a.sort(key=cmp_to_key(cmp_xy))
print(a)  # ['H', 'C', 'N', 'O']
  • 3
a = ['C', 'H', 'N', 'O']
spe_dict = {'C':1, 'H':0, 'N':1, 'O':2}
a.sort(key=lambda x: spe_dict[x])
print(a) # ['H', 'C', 'N', 'O']

dict

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

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

2020

Jan 09

Counting parameters in a model

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

2020

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}

2020

Jan 05

byte string (ASCII) UTF-8 string

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

2020

Jan 04

pandas

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

2019

2019

Dec 12

Print without new line

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

2019

Dec 09

Polymorphism - 多态

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

More info: Java 多态 | 菜鸟教程

2019

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

2019

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

2019

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

2019

Aug 23

pathlib is better than os.path

2019

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)

2019

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%

If there is {} in string, change it to {{}}

title1 = r'$ \text {{Wavefunction for }} k = {:0} \; Å^{{-1}}$'.format(k)

2019

Aug 10

2019

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)])

2019

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 

2019

Jun 20

Transformer Model Bert XLNet

2019

Jun 18

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

conda install cudatoolkit=10.0 -c pytorch

2019

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]

2019

Jun 18

2019

Jun 18

2019

Jun 18

2019

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]])

2019

Jun 10

trainable weights

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

2019

Jun 10

print all numpy values

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

2019

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:

2019

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))