Skip to content

Instantly share code, notes, and snippets.

to_bool = {'T': True, 'F': False}
def matches(what, whom):
return (whom == '?') or (what == to_bool[whom])
def simplify(answers):
if len(answers) > 1:
return '?'
return ['F', 'T'][answers.pop()]
@dniku
dniku / tiled_matrix_multiplication.cu
Last active August 29, 2015 14:14
Tiled matrix multiplication with CUDA, the algorithm is from https://www.coursera.org/course/hetero
#define TILE_WIDTH 16
// NOTE: Since TILE_WIDTH is a constant, I cannot use the more readable idiom
// ceil(whatever/16.0) for calculating the number of tiles. Instead I use
// (whatever + TILE_WIDTH - 1) / TILE_WIDTH, which is equivalent. Trust me.
// Compute C = A * B
__global__ void matrixMultiplyShared(float *A, float *B, float *C, int numARows,
int numAColumns, int numBRows,
int numBColumns, int numCRows,
@dniku
dniku / pylearn2_convnet_loader.py
Last active August 29, 2015 14:14
Loading external digit images for pylearn2 convnet example
import cv2, numpy as np
import theano
from pylearn2.utils import serial
def load_pl2_recognizer(model_path):
model = serial.load(model_path)
X = model.get_input_space().make_theano_batch()
Y = model.fprop(X)
@dniku
dniku / setup_pylearn2_aws.sh
Last active August 29, 2015 14:15
To launch, do "chmod +x setup_pylearn2_aws.sh" first. An updated version of http://www.kurtsp.com/deep-learning-in-python-with-pylearn2-and-amazon-ec2.html
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt-get -y install git make python-dev python-setuptools libblas-dev gfortran g++ python-pip python-numpy python-scipy liblapack-dev
sudo pip install ipython nose
sudo apt-get install screen
sudo pip install --upgrade git+git://github.com/Theano/Theano.git
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_6.5-14_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1404_6.5-14_amd64.deb
sudo apt-get update
@dniku
dniku / amazon_cuda_log.txt
Last active August 29, 2015 14:15
Shell output on an Amazon g2.2xlarge instance for: wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_6.5-14_amd64.deb && sudo dpkg -i cuda-repo-ubuntu1404_6.5-14_amd64.deb && sudo apt-get update && sudo apt-get install cuda
--2015-02-09 16:39:38-- http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_6.5-14_amd64.deb
Resolving developer.download.nvidia.com (developer.download.nvidia.com)... 92.123.72.158, 92.123.72.176
Connecting to developer.download.nvidia.com (developer.download.nvidia.com)|92.123.72.158|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2138 (2.1K) [application/x-deb]
Saving to: ‘cuda-repo-ubuntu1404_6.5-14_amd64.deb’
100%[==================================================================================================>] 2,138 --.-K/s in 0s
2015-02-09 16:39:39 (406 MB/s) - ‘cuda-repo-ubuntu1404_6.5-14_amd64.deb’ saved [2138/2138]
ubuntu@ip-172-31-44-36:~/pylearn2/pylearn2/scripts/papers/maxout$ THEANO_FLAGS="device=gpu,floatX=float32" ~/pylearn2/pylearn2/scripts/train.py mnist.yaml
Using gpu device 0: GRID K520
(train.py:4883): Gdk-CRITICAL **: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
Traceback (most recent call last):
File "/home/ubuntu/pylearn2/pylearn2/scripts/train.py", line 252, in <module>
args.verbose_logging, args.debug)
File "/home/ubuntu/pylearn2/pylearn2/scripts/train.py", line 197, in train
train_obj = serial.load_train_file(config)
File "/home/ubuntu/pylearn2/pylearn2/utils/serial.py", line 524, in load_train_file
Using gpu device 0: GRID K520
error: XDG_RUNTIME_DIR not set in the environment.
error: XDG_RUNTIME_DIR not set in the environment.
(train.py:2434): Gdk-CRITICAL **: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
/home/ubuntu/pylearn2/pylearn2/utils/image.py:16: UserWarning: Unable to import matplotlib. Some features unavailable. Original exception: constructor returned NULL
"Original exception: " + str(matplotlib_exception))
(train.py:2434): Gdk-CRITICAL **: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
/home/ubuntu/pylearn2/pylearn2/sandbox/cuda_convnet/__init__.py:66: UserWarning: You are using probably a too old Theano version. That will cause compilation crash. If so, update Theano.
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Sandbox</title>
</head>
<body>
<script src="sandbox.js"></script>
</body>
@dniku
dniku / expansions.py
Created March 19, 2015 17:54
Expansions of a natural number into sums
def get_expansions(num, nterms):
result = []
def run(cur_sum, cur_vals, remain):
if remain == 0:
if cur_sum == num:
result.append(tuple(cur_vals))
return
elif remain == 1:
cur_vals.append(num - cur_sum)
from __future__ import division, print_function
from pprint import pprint
import glob
import caffe
MODEL_FILE = '../models/lenet.prototxt'
PRETRAINED_FILE = '../models/lenet_pretrained.caffemodel'
IMAGE_DIR = 'test'