Skip to content

Instantly share code, notes, and snippets.

View wassname's full-sized avatar
🙃

Michael J Clark wassname

🙃
View GitHub Profile
@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 / kdtree_scaling_scipy.ipynb
Last active April 30, 2018 03:33
kdtree_scaling_scipy
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wassname
wassname / jupyter_logging.py
Last active November 11, 2022 10:11
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 / pytorch_window_stack.py
Created February 7, 2018 04:32
pytorch stack widow in timeseries
def window_stack(x, window=4, pad=True):
"""
Stack along a moving window of a pytorch timeseries
Inputs:
tensor of dims (batches/time, channels)
pad: if true the left side will be padded to let the output match
Outputs:
if pad=True: a tensor of size (batches, channels, window)
else: tensor of size (batches-window, channels, window)
@wassname
wassname / layer_norm_conv2d.py
Created February 5, 2018 02:00
pytorch layer norm for conv2d
import torch
import torch.nn as nn
class LayerNormConv2d(nn.Module):
"""
Layer norm the just works on the channel axis for a Conv2d
Ref:
- code modified from https://github.com/Scitator/Run-Skeleton-Run/blob/master/common/modules/LayerNorm.py
- paper: https://arxiv.org/abs/1607.06450
@wassname
wassname / batchrenorm2d.py
Last active April 5, 2018 17:29
pytorch implementation of BatchRenorm2d (for Conv2d)
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd as autograd
from torch.nn.parameter import Parameter
from torch.autograd import Variable
def r_d_max_func(itr):
"Default max r and d provider as recommended in paper."
@wassname
wassname / hydrosaver.ipynb
Last active February 13, 2018 03:28
starter colab jupyter notebook for the hydrosaver competition
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wassname
wassname / pandas_profiling_hydrosaver.html
Created January 24, 2018 07:42
pandas profiling for unerathed hydrosaver
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Profile report</title>
<meta name="description" content="Profile report generated by pandas-profiling. See GitHub.">
<meta name="author" content="pandas-profiling">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>