Skip to content

Instantly share code, notes, and snippets.

Avatar

Nasim Rahaman nasimrahaman

View GitHub Profile
View continoulli_with_logits.py
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 Jun 2, 2021
Pytorch instance-wise weighted cross-entropy loss
View weighted_cross_entropy.py
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 Mar 6, 2021
Random elastic transformations for data augmentation
View elastic_transform.py
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
View relay.py
import yaml
import numpy as np
import os
from theano import config
class relay(object):
def __init__(self, switches, ymlfile, callevery=1):
@nasimrahaman
nasimrahaman / betterTheanoFunction.py
Last active Aug 15, 2016
A dictionary-ready wrapper for theano functions.
View betterTheanoFunction.py
__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)
@nasimrahaman
nasimrahaman / linreg.py
Created Jan 22, 2016
Basic Linear Regression with Theano
View linreg.py
# (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