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
@mohanpedala
mohanpedala / bash_strict_mode.md
Last active August 15, 2025 12:18
set -e, -u, -o, -x pipefail explanation
@wassname
wassname / CausalConv2d.py
Last active July 4, 2025 13:35
pytorch Causal Conv2d
from torch.nn.modules.utils import _pair
class CausalConv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=None, dilation=1, groups=1, bias=True):
kernel_size = _pair(kernel_size)
stride = _pair(stride)
dilation = _pair(dilation)
if padding is None:
padding = [int((kernel_size[i] -1) * dilation[i]) for i in range(len(kernel_size))]
@wassname
wassname / torch_isfinite.py
Last active August 22, 2018 05:53
pytorch isfinite: like numpy.isfinite but for torch tensors
def isfinite(x):
"""
Quick pytorch test that there are no nan's or infs.
note: torch now has torch.isnan
url: https://gist.github.com/wassname/df8bc03e60f81ff081e1895aabe1f519
"""
not_inf = ((x + 1) != x)
not_nan = (x == x)
return not_inf & not_nan
@wassname
wassname / sequence_in_chunk_sampler.py
Last active July 1, 2022 18:05
Pytorch random sampler for bigger than memory arrays like dask, zarr, xarray etc that lets you have randomness with the same speed benefits. It chooses a random location, then takes an ordered batch e.g. [[1,2,3],[9,10,11],[4,5,6]]. This way you get the speed of a sequential read.
"""
Pytorch sampler that samples ordered indices from unordered sequences.
Good for use with dask and RNN's, because
1. Dask will slow down if sampling between chunks, so we must do one chunk at a time
2. RNN's need sequences so we must have seqences e.g. 1,2,3
3. But RNN's train better with batches that are uncorrelated so we want each batch to be sequence from a different part of a chunk.
For example, given each chunk is `range(12)`. Our seq_len is 3. We might end up with these indices:
- [[1,2,3],[9,10,11],[4,5,6]]
@wassname
wassname / numpy_dataset.py
Created May 8, 2018 07:02
NumpyDataset for pytorch (like tensordataset)
import torch.utils.data
class NumpyDataset(torch.utils.data.Dataset):
"""Dataset wrapping arrays.
Each sample will be retrieved by indexing array along the first dimension.
Arguments:
*arrays (numpy.array): arrays that have the same size of the first dimension.
@wassname
wassname / tqdm_dask_progressbar.py
Last active June 1, 2020 10:29
TQDMDaskProgressBar: tqdm for dask 1.2.2
from dask.callbacks import Callback
from tqdm.auto import tqdm
class TQDMDaskProgressBar(Callback, object):
"""
A tqdm progress bar for dask.
Usage:
```
with TQDMDaskProgressBar():
@wassname
wassname / jupyter_logging.py
Last active December 9, 2024 21:49
simple logging for jupyter or python which outputs to stdout (or a console or terminal) and a log file
"""
In jupyter notebook simple logging to console
"""
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# Test
logger = logging.getLogger('LOGGER_NAME')
@wassname
wassname / AdamStepLR.py
Created February 26, 2018 08:38
Combing pytorches adam and scheduled learning rate into one (for when the model doesn't have a callback for the scheduler)
class AdamStepLR(torch.optim.Adam):
"""Combine Adam and lr_scheduler.StepLR so we can use it as a normal optimiser"""
def __init__(self, params, lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, step_size=50000, gamma=0.5):
super().__init__(params, lr, betas, eps, weight_decay)
self.scheduler = torch.optim.lr_scheduler.StepLR(self, step_size, gamma)
def step(self):
self.scheduler.step()
return super().step()
@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
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