Skip to content

Instantly share code, notes, and snippets.

View arunmallya's full-sized avatar

Arun Mallya arunmallya

View GitHub Profile
@arunmallya
arunmallya / masked_conv.py
Created June 20, 2017 17:51
Performs conv only on valid input images.
# Since convolution takes the most time, only do it on images with
# mask = 1. Note that masks.data.nonzero() is of size (N, 1).
# As a result, when expanding to 4 dims, we need to unsqueeze it twice.
selected_idx = Variable(masks.data.nonzero().unsqueeze(2).unsqueeze(3).repeat(
1, images.size(1), images.size(2), images.size(3)))
selected_images = torch.gather(images, 0, selected_idx)
# Get image features from CNN and linear layer rnn_emb.
some_im_feats = self.rnn_emb(self.cnn(selected_images).squeeze())
@arunmallya
arunmallya / bug.py
Created June 20, 2017 20:46
Exposes bug with DataParallel when using dicts as input
import torch
import torch.nn as nn
from torch.autograd import Variable
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.net = nn.Linear(10, 2)
def forward(self, inputs):
import torch.nn as nn
def myModule(nn.Module):
def __init__(self):
# Init stuff here
self.X = nn.Sequential(
nn.Linear(num_input_genes, num_tfs),
nn.ReLU(),
nn.BatchNorm1d(num_tfs)
)
@arunmallya
arunmallya / ReadCSVToDatum.txt
Last active August 10, 2017 10:55
A function to load CSV data into a caffe datum
bool ReadCSVToDatum(const string& filename, Datum* datum) {
// read in the CSV file into a vector
fstream file(filename.c_str(), ios::in);
vector<vector<int> > label;
std::string line;
while (std::getline(file, line)) {
// replace commas with spaces
for (int i = 0; i < line.length(); i++) {
if (line[i] == ',')
line[i] = ' ';
from __future__ import print_function
import torch
A = torch.rand(4)
idx = torch.LongTensor([0, 3])
An = A.numpy()
idxn = idx.numpy()
# Numpy indexing.
from __future__ import print_function
import torch
A = torch.rand(4, 4)
An = A.numpy()
idx = torch.ByteTensor([1, 0, 0, 1])
idxn = [True, False, False, True]
# Numpy indexing.
@arunmallya
arunmallya / binarizer.py
Created February 20, 2018 20:41
Autograd snippet for Binarizer
DEFAULT_THRESHOLD = 5e-3
class Binarizer(torch.autograd.Function):
"""Binarizes {0, 1} a real valued tensor."""
def __init__(self, threshold=DEFAULT_THRESHOLD):
super(Binarizer, self).__init__()
self.threshold = threshold
def forward(self, inputs):
@arunmallya
arunmallya / modconv.py
Created February 20, 2018 20:43
Convolution with masking support.
class ElementWiseConv2d(nn.Module):
"""Modified conv. Do we need mask for biases too?"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True,
mask_init='1s', mask_scale=1e-2,
threshold_fn='binarizer', threshold=None):
super(ElementWiseConv2d, self).__init__()
kernel_size = _pair(kernel_size)
stride = _pair(stride)
@arunmallya
arunmallya / test.ipynb
Created August 17, 2018 22:30
Inplace normal_( ) bug
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@arunmallya
arunmallya / test-Copy1.ipynb
Created August 17, 2018 22:38
Normal operation on small tensor
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.