Skip to content

Instantly share code, notes, and snippets.

View rmurphy2718's full-sized avatar

Ryan L. Murphy rmurphy2718

View GitHub Profile
@rmurphy2718
rmurphy2718 / sklearn_grid_search_save_states.md
Created September 8, 2021 23:40
A very simple strategy for mitigating a possible problem with `GridSearchCV` in `sklearn`: when the grid search crashes prematurely.

This post documents, for quick reference, a very simple strategy for mitigating a possible problem with GridSearchCV in sklearn. What happens if your process crashes in the middle of your grid search? You can lose everything, even hours of tuning. This post documents a very simple solution that can be set up in under a minute.

The idea, suggested on this StackOverflow post is to

  1. Use a verbose option, so that sklearn prints the performance of each model after a fold finishes
  2. Direct that output to a persistent file on disk.

This solution does not save all the information that is returned after fit() completes. Moreover, it does not provide a way to jump right back into the grid search where the program crashed, and relies in setting up a new search to complete the grid. However, I think we can agree it is much better than losing hours of compute, and has tremendous value for the minimal amount of work

"""
A self-contained script in PyTorch that generates a simple dataset from a
linear model and fits a neural network.
Can be used as a starting script for trying out different PyTorch functionalities
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
# Function to create a random permutation matrix of some size
import numpy as np
def r_pmatrix(dim):
"""
Random Permutation Matrix (r_pmatrix)
Create a random permutation matrix P such that PM shuffles the rows of M, for some matrix M
:param dim: dimension of (square) permutation matrix
:return: P, permutation matrix
@rmurphy2718
rmurphy2718 / DEG.py
Last active June 10, 2019 19:48
Simple Implementation of DEG in Heath and Parikh
#########################################################################################
# Ryan L Murphy
# Toy illustrative implementation of DEG in
# Heath & Parikh 2011, Generating random graphs with tunable clustering coefficients
# -----------------------------------------------------------------------------------
# The algorithm may get stuck for a while, this toy implementation just exits if
# there is no progress for some number of iterations
#########################################################################################
import networkx as nx
@rmurphy2718
rmurphy2718 / GraphsSubfiguresCaptions.tex
Last active June 10, 2019 20:06
Latex: Graphs, Subfigures, and Captions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ryan Murphy
%
% This latex shows:
% (1) How to make graphs using tikz
% (2) How to align them as subfigures in a grid
% (3) Use subfigure-level and figure-level captions
% (4) Include a description
% (5) Reference the figure
%
@rmurphy2718
rmurphy2718 / AllBinaryLists.py
Last active June 10, 2019 20:01
All binary lists of size NN
##########################################
# Ryan Murphy
# Create all binary lists of length N
# E.g.: [[0, 0], [0, 1], [1, 0], [1, 1]]
##########################################
def all_binary_lists(num):
superset = []