Skip to content

Instantly share code, notes, and snippets.

View dwf's full-sized avatar

David Warde-Farley dwf

View GitHub Profile
@dwf
dwf / lib.py
Last active December 20, 2016 00:45
Minimal examples of using the Blocks MainLoop and checkpointing machinery.
# It's important not to define any classes you want serialized in
# the script you're running as pickle doesn't like that (if you pass
# save_main_loop=False to Checkpoint it's fine, though).
from theano import tensor
import numpy
import theano
from picklable_itertools import imap, izip, repeat
# Your algorithm object just needs two required methods: initialize()
@dwf
dwf / segmented_epoch_stream.py
Created September 28, 2016 17:04
Fuel DataStream that segments the epochs of another DataStream.
"""Fuel DataStream that segments the epochs of another DataStream."""
__license__ = """
Copyright (c) 2016 Universite de Montreal
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
@dwf
dwf / gist:c659fdd50285b5a4885c10acc34b77e7
Created April 20, 2016 20:16
traceback of IPython caught in infinite corrupt DB handling loop
Traceback (most recent call last):
File "/u/wardefar/miniconda/lib/python3.4/site-packages/IPython/core/history.py", line 85, in catch_corrupt_db
return f(self, *a, **kw)
File "/u/wardefar/miniconda/lib/python3.4/site-packages/IPython/core/history.py", line 227, in init_db
end timestamp, num_cmds integer, remark text)""")
sqlite3.OperationalError: disk I/O error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
@dwf
dwf / binary_cross_entropy.tex
Created February 17, 2016 22:35
Simplification of binary cross-entropy in terms of logits, yielding the familiar gradient.
\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{eqnarray*}
CE(a, t) & = & -t\log\left(\sigma(a)\right) - (1 - t)\log\left(1 - \sigma(a)\right) \\
& = & -t \log \left(\frac{1}{1 + \exp(a)}\right) - (1 - t)\log\left(1 - \frac{1}{1 + \exp(a)}\right) \\
& = & -t \log \left(\frac{1}{1 + \exp(a)}\right) - (1 - t)\log\left(\frac{\exp(a)}{1 + \exp(a)}\right) \\
& = & t \log \left(1 + \exp(a)\right) - \left[(1 - t)\log(\exp(a)) - \log(1 + \exp(a))\right] \\
&= & t \log \left(1 + \exp(a)\right) - (1 - t)\log(\exp(a)) + (1 - t)\log(1 + \exp(a)) \\
@dwf
dwf / rpn.py
Created February 5, 2016 07:25
A reverse Polish notation calculator in 50 lines.
"""A reverse Polish notation calculator. Supports float and int literals."""
from __future__ import print_function
import operator
import sys
OPERATORS = {'+': operator.add, '-': operator.sub, '%': operator.mod, '/':
operator.truediv, '//': operator.floordiv, '*': operator.mul,
'**': operator.pow}
@dwf
dwf / partial_last.py
Created January 28, 2016 21:48
functools.partial workalike where provided positionals are appended, not prepended.
import functools
class partial_last(functools.partial):
"""Like functools.partial, but fill positionals from the end.
Useful for builtin C functions where you *can't* pass later args as
keywords because CPython is stupid about that.
Examples
--------
@dwf
dwf / wtf.py
Last active January 11, 2016 15:12
Spooky bug in Theano and/or Blocks.
import io
from blocks.bricks import Rectifier, MLP, Sequence, Logistic
from blocks.select import Selector
from theano import tensor
from theano.printing import debugprint
def wtf(call_b):
sub_mlp = MLP([Rectifier(), Rectifier()], [5, 4, 3])
a = Sequence([sub_mlp.apply, Logistic().apply])
"""Very simple PUSH-PULL reusable producer-consumer with ZeroMQ."""
# By David Warde-Farley. Released under the 3-clause BSD license.
import time
from multiprocessing import Process
import zmq
def _producer_wrapper(f, port, addr='tcp://127.0.0.1'):
@dwf
dwf / mobile-shell-mousefix.rb
Created May 25, 2015 21:17
Homebrew formula for a version of mosh with xterm mouse reporting fixed (i.e. works more than once per tmux session)
class MobileShellMousefix < Formula
homepage "http://mosh.mit.edu/"
revision 2
head do
url "https://github.com/lpkruger/mosh.git", :revision => "d5f75ec54a"
depends_on "autoconf" => :build
depends_on "automake" => :build
end
@dwf
dwf / gist:83f79c0533a26f56d09a
Last active August 29, 2015 14:19
Proof of concept using a logging handler and LogRecord "extras" attribute to manage a progress bar.
# Copyright (c) 2015 David Warde-Farley.
#
# Permission is granted to use this code under the MIT license:
# http://opensource.org/licenses/mit-license.php
import logging
import progressbar
import time