Skip to content

Instantly share code, notes, and snippets.

"""A context manager for enforcing (integer) timeouts of a code block."""
import contextlib
@contextlib.contextmanager
def Timeout(timeout: Optional[int]) -> ContextManager[None]:
"""Raises TimeoutError() if block isn't completed within `timeout` seconds.
Args:
timeout: Seconds. If None, there is no timeout.
@cfperez
cfperez / jupyter_gym_render.md
Created August 30, 2018 17:45 — forked from andrewschreiber/jupyter_gym_render.md
How to stream OpenAI Gym environment rendering within a Jupyter Notebook

Open jupyter with

$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

In Jupyter

import matplotlib.pyplot as plt
%matplotlib inline

After each step

def show_state(env, step=0):
@cfperez
cfperez / legend.py
Created August 14, 2018 21:50
Ways to combine legends in matplotlib
def combined_legend(*axes):
"""Creates combines legends from provided axes.
Usually used for ax and ax.twinx()
"""
lines, labels = [], []
for ax in axes:
line, label = ax.get_legend_handles_labels()
lines.extend(line)
labels.extend(label)
@cfperez
cfperez / cuda_install.sh
Last active June 28, 2018 21:58
CUDA 9.2 + cuDNN 7.1 install
#!/bin/bash
CUDNN_FILE="cudnn-9.2-linux-x64-v7.1.tgz"
[ -e $CUDNN_FILE ] || echo "Please download CUDNN 7.1 from https://developer.nvidia.com/rdp/cudnn-download" && exit 1
sudo apt-get install linux-headers-$(uname -r)
# get CUDA 9.0 toolkit
PKG="cuda-repo-ubuntu1604_9.2.88-1_amd64.deb"
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/$PKG
@cfperez
cfperez / keras-metrics.py
Last active September 13, 2017 18:38
Some metrics for Keras training
# From https://github.com/fchollet/keras/issues/5400#issuecomment-314747992
from keras import backend as K
def mcor(y_true, y_pred):
#matthews_correlation
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
#!/bin/bash
ENV=${1:-"$CONDA_DEFAULT_ENV"}
if [ -z "$ENV" ]; then
echo Must specify conda environment
exit 1
fi
DISPLAY_NAME=${2:-$ENV}
echo Installing kernel for enviroment $ENV using display name \"$DISPLAY_NAME\"
@cfperez
cfperez / waya-dl-setup.sh
Last active September 11, 2017 23:33 — forked from mjdietzx/waya-dl-setup.sh
Install CUDA Toolkit v8.0 and cuDNN v6.0 on Ubuntu 16.04
#!/bin/bash
# install CUDA Toolkit v8.0
# instructions from https://developer.nvidia.com/cuda-downloads (linux -> x86_64 -> Ubuntu -> 16.04 -> deb (network))
CUDA_REPO_PKG="cuda-repo-ubuntu1604_8.0.61-1_amd64.deb"
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/${CUDA_REPO_PKG}
sudo dpkg -i ${CUDA_REPO_PKG}
sudo apt-get update
sudo apt-get -y install cuda
@cfperez
cfperez / attach
Created July 11, 2017 17:30
Attach to an existing tmux session or create a new one
#!/bin/bash
ssh $@ -t -- 'tmux a || tmux new-session'
@cfperez
cfperez / lr_schedule.py
Created July 10, 2017 18:39
Learning rate scheduler
from keras.callbacks import LearningRateScheduler
def scheduler(schedule):
def lr_schedule(epoch):
epoch += 1
for ep,lr in sorted(schedule, reverse=True):
if epoch >= ep:
return lr
raise ValueError()
return lr_schedule
@cfperez
cfperez / .vimrc
Created April 13, 2017 22:04
vim-tmux-navigator work with zsh
" If using YADR: ~/.vim/settings/path.vim
" Or else in .vimrc
set shell=/bin/zsh\ -l