Skip to content

Instantly share code, notes, and snippets.

Avatar

Modar M. Alfadly xmodar

View GitHub Profile
@xmodar
xmodar / pytorch_tutorial.ipynb
Last active March 14, 2019 18:36
Basic PyTorch classification tutorial with links and references to useful materials to get started.
View pytorch_tutorial.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xmodar
xmodar / fitting_gaussian.ipynb
Last active August 20, 2021 04:07
Fitting Gaussian to Sampled Data Using PyTorch.
View fitting_gaussian.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xmodar
xmodar / num_bins_in_hist.py
Created October 22, 2018 10:58
If you want to histogram samples of data, use Freedman–Diaconis rule which is the default method to choose the number of bins in Matlab. Here is an implementation in PyTorch:
View num_bins_in_hist.py
import torch
def _indexer_into(x, dim=0, keepdim=False):
'''indexes into x along dim.'''
def indexer(i):
# (e.g., x[:, 2, :] is indexer(2) if dim == 1)
out = x[[slice(None, None)] * dim + [i, ...]]
return out.unsqueeze(dim) if keepdim and x.dim() != out.dim() else out
return indexer
@xmodar
xmodar / rng.py
Last active November 20, 2021 03:44
A more flexible context manager than `torch.random.fork_rng()` to preserve the state of the random number generator in PyTorch for the desired devices.
View rng.py
import torch
class RNG():
"""Preserve the state of the random number generators of torch
https://gist.github.com/ModarTensai/2328b13bdb11c6309ba449195a6b551a
Inspired by torch.random.fork_rng().
Seeding random number generators (RNGs):
@xmodar
xmodar / histogram_summary.py
Created October 23, 2018 12:19
If you want to dump histograms in tensorboard while using pytorch, following [this tutorial](https://nbviewer.jupyter.org/gist/ModarTensai/b081dcf6c87f9134f29abe3a77be1ab5), you can use this.
View histogram_summary.py
import torch
import tensorflow as tf
def histogram_summary(name, array):
if not hasattr(histogram_summary, 'session'):
histogram_summary.placeholder = tf.placeholder(tf.float32)
histogram_summary.session = tf.Session()
histogram_summary.histograms = {}
if name not in histogram_summary.histograms:
@xmodar
xmodar / sfbvc
Created January 24, 2019 04:42
A shell script for single file branch version control (sfbvc)
View sfbvc
# sfbvc stands for single file branch version control.
#
# By Modar Alfadly <https://modar.me> on the 24th Jan 2019
#
# sfbvc is a convention to create an orphan branch for each file in a repository
# and the master branch will be the merge of all the other branches.
# This is a niche convention which is trying to simulate file history
# in applications similar to cloud storages like Google Drive and Dropbox.
# We are using git under the hood and assuming that it is installed and in PATH.
#
@xmodar
xmodar / python_cheat_sheet.py
Last active February 4, 2019 15:48
A cheat sheet for Python I made a while ago.
View python_cheat_sheet.py
#Python Cheat Sheet
#Any thing preceded by a '#' is a comment and won't be executed like this line here
#Variables are names defined by the programmer that hold values
WonderfulVariableByMe = 5
#There are some reserved words like: for, if, def, while, break, ... that cannot be used to name a variable
#A variable name must not contain spaces and must never start with a number
#Variable names are case-sensitive: modar does not equal Modar
@xmodar
xmodar / running_meters.py
Last active February 10, 2023 17:49
Seamless running stats for (native python, numpy.ndarray, torch.tensor). Also see: https://gist.github.com/davidbau/00a9b6763a260be8274f6ba22df9a145
View running_meters.py
"""Seamless running stats for (native python, numpy.ndarray, torch.tensor)."""
from collections import namedtuple
class MeanMeter:
"""Estimate the mean for a stream of values."""
def __init__(self):
"""Initialize the meter."""
@xmodar
xmodar / denormalize_conv2d.py
Created May 22, 2019 05:26
Change the weights of a conv2d in pytorch to incorporate the mean and std and allow the input range to be in [0, 1]
View denormalize_conv2d.py
from torch.nn import functional as F
from torchvision.transforms.functional import normalize
def denormalize_conv2d(weight, bias, mean, std):
weight, bias = weight.data, bias.data
std = torch.as_tensor(std).data.view(1, -1, 1, 1)
mean = torch.as_tensor(mean).data.view(1, -1, 1, 1)
w = weight / std
b = bias - (w * mean).flatten(1).sum(1)
@xmodar
xmodar / polynomial.py
Created September 10, 2019 01:40
Compute polynomials efficiently with numpy and pytorch (differentiable).
View polynomial.py
import torch
def polynomial(coefficients, x):
"""Evaluate polynomials using Horner method.
The coefficients are from highest to lowest order.
Args:
coefficients: Tensor of size (N, *K).
K is any broadcastable size to `x.size()`.