Skip to content

Instantly share code, notes, and snippets.

View jiangnanhugo's full-sized avatar
🎯
Focusing

Nan Jiang jiangnanhugo

🎯
Focusing
View GitHub Profile
@jiangnanhugo
jiangnanhugo / gist:3cdd13861ffeafe437d36f269f42ca29
Created August 18, 2021 02:21 — forked from debasishg/gist:8172796
A collection of links for streaming algorithms and data structures
  1. General Background and Overview
@jiangnanhugo
jiangnanhugo / sparsemax_loss_theano.ipynb
Created October 12, 2016 04:11 — forked from vene/sparsemax_loss_theano.ipynb
sparsemax loss for Theano
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import theano as T
def nce_binary_conditional_likelihood(p_unnormalized,y,y_flat,noise_samples,noise_dist,k):
p_unnormalized_exp=T.exp(p_unnormalized)
p_unnormalized_flat=p_unnormalized_exp.flatten()
unnorm_y=p_unnormalized_flat[y_flat]
noise_y=noise_dist[y]
pos_cost=T.log(unnorm_y/(unnorm_y+k*noise_y))
neg_cost=T.log(k*noise_dist/(p_unnormalized_exp+k*noise_dist))
return -T.mean(pos_cost+ T.sum(neg_cost*noise_samples,axis=1),dtype=theano.config.floatX)
@jiangnanhugo
jiangnanhugo / cg.py
Created May 27, 2016 13:11 — forked from neosatrapahereje/cg.py
Conjugate gradient optimization for Lasagne
from collections import OrderedDict
import numpy as np
import theano
import theano.tensor as T
from theano.ifelse import ifelse
def cg(loss, params, x0=None, max_iters=100, precondition=None, tol=1e-3,
conv_crit='cg'):
"""(Preconditioned) Conjugate Gradient (CG) updates
@jiangnanhugo
jiangnanhugo / fast_jac.py
Created May 17, 2016 14:21 — forked from aam-at/fast_jac.py
Fast Jacobian computation with scan chunking
# See for details and discussion: https://github.com/Theano/Theano/issues/4087
import theano
from theano import tensor as T, ifelse
from theano.gof import Variable
def fast_jacobian(expr, wrt, chunk_size=16, func=None):
assert isinstance(expr, Variable), \
"tensor.jacobian expects a Variable as `expr`"
assert expr.ndim < 2, \