Skip to content

Instantly share code, notes, and snippets.

View MathiasGruber's full-sized avatar

Mathias Gruber MathiasGruber

View GitHub Profile
@MathiasGruber
MathiasGruber / ordinal_prediction_convert.py
Created July 10, 2021 07:13
Conversion from ordinal predictions to class labels
def prediction2label(pred: np.ndarray):
"""Convert ordinal predictions to class labels, e.g.
[0.9, 0.1, 0.1, 0.1] -> 0
[0.9, 0.9, 0.1, 0.1] -> 1
[0.9, 0.9, 0.9, 0.1] -> 2
etc.
"""
return (pred > 0.5).cumprod(axis=1).sum(axis=1) - 1
@MathiasGruber
MathiasGruber / ordinal_regression_pytorch.py
Created July 2, 2021 06:38
Ordinal regression loss function for
def ordinal_regression(predictions: List[List[float]], targets: List[float]):
"""Ordinal regression with encoding as in https://arxiv.org/pdf/0704.1028.pdf"""
# Create out modified target with [batch_size, num_labels] shape
modified_target = torch.zeros_like(predictions)
# Fill in ordinal target function, i.e. 0 -> [1,0,0,...]
for i, target in enumerate(targets):
modified_target[i, 0:target+1] = 1
@MathiasGruber
MathiasGruber / crossentropy_wrapper.py
Last active July 2, 2021 04:56
Small wrapper function for the torch cross-entropy loss
def cross_entropy(predictions: List[List[float]], targets: List[float]):
return nn.CrossEntropyLoss(reduction='none')(predictions, target)
@MathiasGruber
MathiasGruber / train_multiclass_chemprop.sh
Last active July 1, 2021 13:19
Sample command for calling multiclass chemprop model training
python train.py \
--data_path data/solubility_dataset.csv \
--dataset_type multiclass \
--save_dir solubility_checkpoints/ \
--ensemble 1 --num_folds 5 --epochs 50 \
--split_type scaffold_balanced \
--multiclass_num_classes 5 \
--save_smiles_splits --save_preds
@MathiasGruber
MathiasGruber / bayes_ensemble_grad.sh
Created June 23, 2021 08:06
Training solubility ensemble model
# Train a logSolubility model
python train.py \
--data_path data/delaney.csv \
--dataset_type regression \
--save_dir delaney_checkpoints \
--ensemble 3 --num_folds 10 --epochs 50
# Get bayes ensemble grad results
python interpret_local.py \
--test_path data/delaney_subset.csv \
@MathiasGruber
MathiasGruber / chemprop_prediction.sh
Last active June 23, 2021 09:27
Sample snippet for predicting chemical properties with trained chemprop model
chemprop_predict \
--test_path data/clintox_test.csv \
--checkpoint_dir model_checkpoint \
--features_generator rdkit_2d_normalized --no_features_scaling \
--preds_path data/predictions.csv
@MathiasGruber
MathiasGruber / chemprop_train.sh
Last active June 23, 2021 09:02
Sample training of chemprop model
chemprop_train \
--data_path data/clintox_train.csv \
--config_path data/config.json \
--dataset_type classification \
--save_dir model_checkpoint \
--num_folds 5 \
--ensemble_size 3 \
--features_generator rdkit_2d_normalized --no_features_scaling \
--split_type scaffold_balanced
@MathiasGruber
MathiasGruber / tune_chemprop.sh
Last active June 23, 2021 09:02
Sample command for tuning chemprop
chemprop_hyperopt \
--data_path data/clintox_train.csv \
--dataset_type classification \
--num_iters 50 \
--features_generator rdkit_2d_normalized --no_features_scaling \
--config_save_path data/config.json
@MathiasGruber
MathiasGruber / bentoml_deploy_template.yml
Created June 20, 2021 18:50
Super simple example for deploying model into Kubernetes
apiVersion: v1
kind: Service
metadata:
labels:
app: [VAR_MODULE_NAME]
name: [VAR_MODULE_NAME]
spec:
ports:
- name: predict
port: 5000
@MathiasGruber
MathiasGruber / sync_bentoml_k8s.py
Created June 20, 2021 18:50
Super simple sample for deploying BentoML images into Kubernetes
import os
def deploy_bentoml(model_name):
"""Deploys a BentoML from dockerhub into Kubernetes cluster"""
# Open a template for BentoML deployments
with open('bentoml_deploy.tpl', 'r') as fi:
# Substitute the name of the model into template & save
yaml_file = fi.read()