Skip to content

Instantly share code, notes, and snippets.

View wassname's full-sized avatar
🙃

wassname (Michael J Clark) wassname

🙃
  • I'm just a guy who likes to machine learn
  • IAU #90377 (Sedna), IAU #137924 (2000 BD19), IAU #85770 (1998 UP1), IAU #66063 (1998 RO1), and minor planet 1992 TY₁. from 2025 Feb 25.
  • X @wassname
  • LinkedIn in/mclark52
View GitHub Profile
@wassname
wassname / find_best_dummy_classification.py
Last active August 27, 2017 05:56
Try all dummy models in sklearn
# from https://gist.github.com/wassname/00b94dc7eb7220c136685fce782be3a4
from io import StringIO
import pandas as pd
import numpy as np
from sklearn import metrics
import sklearn
def parse_classification_report(classification_report):
"""Parse a sklearn classification report to a dict."""
return pd.read_fwf(
@shivakar
shivakar / RangeHTTPServer.py
Created May 24, 2017 05:11
Python's SimpleHTTPServer extended to handle HTTP/1.1 Range requests
import os
import SimpleHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
class RangeHTTPRequestHandler(SimpleHTTPRequestHandler):
"""RangeHTTPRequestHandler is a SimpleHTTPRequestHandler
with HTTP 'Range' support"""
def send_head(self):
"""Common code for GET and HEAD commands.
@wassname
wassname / torch_summarize_with_df.py
Last active May 22, 2020 06:09
summarize a torch model like in keras, showing parameters and output shape
# summarize model
from collections import OrderedDict
import pandas as pd
import torch
from torch import nn
from torch.autograd import Variable
class TorchSummarizeDf(object):
def __init__(self, model, weights=False, input_shape=True, nb_trainable=False, debug=False):
@wassname
wassname / augumented_hdf5_matrix.py
Last active October 11, 2020 23:32
How to do data augmentation on a keras HDF5Matrix
"""Another way, note this one will load the whole array into memory ."""
from keras.preprocessing.image import ImageDataGenerator
import h5py
from keras.utils.io_utils import HDF5Matrix
seed=0
batch_size=32
# we create two instances with the same arguments
data_gen_args = dict(
rotation_range=90.,
@wassname
wassname / jaccard_coef_loss.py
Last active January 30, 2024 15:45
jaccard_coef_loss for keras. This loss is usefull when you have unbalanced classes within a sample such as segmenting each pixel of an image. For example you are trying to predict if each pixel is cat, dog, or background. You may have 80% background, 10% dog, and 10% cat. Should a model that predicts 100% background be 80% right, or 30%? Categor…
from keras import backend as K
def jaccard_distance_loss(y_true, y_pred, smooth=100):
"""
Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|)
= sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|))
The jaccard distance loss is usefull for unbalanced datasets. This has been
shifted so it converges on 0 and is smoothed to avoid exploding or disapearing
gradient.
@nitrag
nitrag / convert_m4b.sh
Last active July 9, 2025 01:54
Audibook convert m4b to mp3. This will split into chaptered mp3 files and automatically reconfigure proper ID3v2 tags.
#!/bin/bash
#
# sudo apt-get install id3v2 ffmpeg
#
# USAGE:
# cd /book title/
# bash ~/this_script_path.sh
# rm *.m4b (you need to manually remove the original in case something goes wrong)
#
#
@ihsgnef
ihsgnef / colorize_text.py
Last active July 12, 2025 06:46
Visualize attention over text with background colors
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def colorize(words, color_array):
# words is a list of words
# color_array is an array of numbers between 0 and 1 of length equal to words
cmap = matplotlib.cm.get_cmap('RdBu')
template = '<span class="barcode"; style="color: black; background-color: {}">{}</span>'
colored_string = ''
@rewida17
rewida17 / termux
Last active March 20, 2025 10:27
Run termux env via eg adb shell
#!/system/xbin/bash
#Based on https://github.com/termux/termux-app/issues/77
export PREFIX='/data/data/com.termux/files/usr'
export HOME='/data/data/com.termux/files/home'
export LD_LIBRARY_PATH='/data/data/com.termux/files/usr/lib'
export PATH="/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:$PATH"
export LANG='en_US.UTF-8'
export SHELL='/data/data/com.termux/files/usr/bin/bash'
export BIN='/data/data/com.termux/files/usr/bin'
export TERM=vt220
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 4.
Book Id,Title,Author,Author l-f,Additional Authors,ISBN,ISBN13,My Rating,Average Rating,Publisher,Binding,Number of Pages,Year Published,Original Publication Year,Date Read,Date Added,Bookshelves,Bookshelves with positions,Exclusive Shelf,My Review,Spoiler,Private Notes,Read Count,Recommended For,Recommended By,Owned Copies,Original Purchase Date,Original Purchase Location,Condition,Condition Description,BCID
13278990,The Housing Monster,prole.info,"prole.info, prole.info",,"=""160486530X""","=""9781604865301""",0,3.77,PM Press,Paperback,160,2012,2011,,2017/12/07,currently-reading,currently-reading (#3),currently-reading,,,,1,,,0,,,,,
7805,Pale Fire,Vladimir Nabokov,"Nabokov, Vladimir",,"=""0141185260""","=""9780141185262""",0,4.19,Penguin Books Ltd,Paperback,246,2000,1962,,2013/10/09,to-read,to-read (#26),to-read,,,,0,,,0,,,,,
34220725,Never Use Futura,Doug Thomas,"Thomas, Doug",Ellen Lupton,"=""1616895721""","=""9781616895723""",4,4.29,Princeton Architectural Press,Paperback,208,2017,,,2017/11/12,,,read,"Pr
@wassname
wassname / NoisyConv2d.py
Last active March 2, 2022 17:25
pytorch noisy conv2d
"""
A noisy convolution 2d for pytorch
Adapted from:
- https://raw.githubusercontent.com/Scitator/Run-Skeleton-Run/master/common/modules/NoisyLinear.py
- https://github.com/pytorch/pytorch/pull/2103/files#diff-531f4c06f42260d699f43dabdf741b6d
More details can be found in the paper `Noisy Networks for Exploration`
Original: https://gist.github.com/wassname/001aff274c7c8196055fabfc06cf80c5
"""
import math