Skip to content

Instantly share code, notes, and snippets.

View nasimrahaman's full-sized avatar

Nasim Rahaman nasimrahaman

View GitHub Profile
@nasimrahaman
nasimrahaman / linreg.py
Created January 22, 2016 16:52
Basic Linear Regression with Theano
# (CC-NC-SA) Nasim Rahaman
import theano as th
import theano.tensor as T
import numpy as np
import time
# Weights
W = th.shared(value=np.random.uniform(size=(3, 3)))
# Input
@nasimrahaman
nasimrahaman / betterTheanoFunction.py
Last active August 15, 2016 08:20
A dictionary-ready wrapper for theano functions.
__author__ = "nasim.rahaman at iwr.uni-heidelberg.de"
__doc__ = """A few bells and whistles for the theano function callable.
Examples:
import theano.tensor as T
x = T.scalar()
y = T.scalar()
f1 = function(inputs={'x': x, 'y': y}, outputs={'z1': x + y, 'z2': x + 2*y})
f1(x=2, y=3)
import yaml
import numpy as np
import os
from theano import config
class relay(object):
def __init__(self, switches, ymlfile, callevery=1):
@nasimrahaman
nasimrahaman / continoulli_with_logits.py
Created July 24, 2019 20:28
Continoulli with Logits
import torch
import torch.nn as nn
class ContinoulliWithLogitsLoss(nn.BCEWithLogitsLoss):
"""
Numerically stable implementation of the objective function defined in [1].
[1] https://arxiv.org/abs/1907.06845
"""
@nasimrahaman
nasimrahaman / weighted_cross_entropy.py
Last active November 16, 2023 04:54
Pytorch instance-wise weighted cross-entropy loss
import torch
import torch.nn as nn
def log_sum_exp(x):
# See implementation detail in
# http://timvieira.github.io/blog/post/2014/02/11/exp-normalize-trick/
# b is a shift factor. see link.
# x.size() = [N, C]:
b, _ = torch.max(x, 1)
@nasimrahaman
nasimrahaman / elastic_transform.py
Last active January 16, 2024 12:53
Random elastic transformations for data augmentation
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter
# Elastic transform
def elastic_transformations(alpha, sigma, rng=np.random.RandomState(42),
interpolation_order=1):
"""Returns a function to elastically transform multiple images."""
# Good values for:
# alpha: 2000