Skip to content

Instantly share code, notes, and snippets.

View nasimrahaman's full-sized avatar

Nasim Rahaman nasimrahaman

View GitHub Profile
nasimrahaman /
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].
nasimrahaman /
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
# b is a shift factor. see link.
# x.size() = [N, C]:
b, _ = torch.max(x, 1)
nasimrahaman /
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),
"""Returns a function to elastically transform multiple images."""
# Good values for:
# alpha: 2000
import yaml
import numpy as np
import os
from theano import config
class relay(object):
def __init__(self, switches, ymlfile, callevery=1):
nasimrahaman /
Last active August 15, 2016 08:20
A dictionary-ready wrapper for theano functions.
__author__ = "nasim.rahaman at"
__doc__ = """A few bells and whistles for the theano function callable.
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 /
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