Skip to content

Instantly share code, notes, and snippets.

View xmodar's full-sized avatar

Modar M. Alfadly xmodar

View GitHub Profile
@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:
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 / 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.
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)
# 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.
#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 / pytorch_tutorial.ipynb
Last active March 14, 2019 18:36
Basic PyTorch classification tutorial with links and references to useful materials to get started.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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]
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).
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()`.
@xmodar
xmodar / named_tuples.py
Created September 18, 2019 22:11
Create namedtuple types for function outputs only once with nice type name.
from collections import namedtuple
from types import MethodType, FunctionType
# using neither typing.NamedTuple nor dataclasses.dataclass
def named_tuple(function, field_names, *args, name=None, **kwargs):
"""Memoize namedtuple types for function outputs."""
if isinstance(function, MethodType):
function = function.__func__
assert isinstance(function, FunctionType)
@xmodar
xmodar / pca.py
Last active September 19, 2019 00:21
Principal Component Analysis (PCA) with pytorch and numpy
from collections import namedtuple
import numpy as np
class PCA:
def __init__(self, features, variance=None):
if variance is None:
pca = self.fit(features).pca
features = pca.projection_matrix
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.