Skip to content

Instantly share code, notes, and snippets.

@rkaplan
rkaplan / collate.py
Last active June 4, 2020 00:54
PyTorch example of a custom collate function that uses shared memory when appropriate
import functools
def my_collate(batch, use_shared_memory=False):
r"""Puts each data field into a tensor with outer dimension batch size"""
error_msg = "batch must contain tensors, numbers, dicts or lists; found {}"
elem_type = type(batch[0])
if isinstance(batch[0], torch.Tensor):
out = None
if use_shared_memory:
@rkaplan
rkaplan / large_blobs.sh
Created October 15, 2018 21:01
Find largest git blobs in a project
#!/bin/bash
#set -x
# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see https://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs
#
# Found on https://stackoverflow.com/questions/10622179/how-to-find-identify-large-files-commits-in-git-history
import random
import torch
class DynamicNet(torch.nn.Module):
def __init__(self, D_in, H, D_out):
super(DynamicNet, self).__init__()
self.backbone = torch.nn.Linear(D_in, H)
self.head1 = torch.nn.Linear(H, D_out)
self.head2 = torch.nn.Linear(H, D_out)
# Generates aliases for .bashrc that allow easy toggling of CUDA_VISIBLE_DEVICES
# The printf is to name the tmux pane with which GPUs are available
ALIAS_STR = 'alias {name}=\'printf "\\033]2;%s\\033\\\\" "GPU: {gpu_list}"; export CUDA_VISIBLE_DEVICES="{gpu_list}"\''
ngpu = 8
for i in range(ngpu):
print(ALIAS_STR.format(name="g" + str(i), gpu_list=str(i)))
for i in range(ngpu-1):
@rkaplan
rkaplan / gist:042198c5b56e37871dd8
Created April 11, 2015 04:16
MetaMind Sentiment Classification: Java
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.metamind.io/language/classify")
httppost.setHeader("Authorization: Basic", "YOUR API KEY");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("classifier_id", 155));