<s> import numpy as np <EOL> import scipy <EOL> import scipy . signal <EOL> import theano . tensor . nnet <EOL> import theano . tensor as TT <EOL> import theano . tensor . extra_ops <EOL> from collections import OrderedDict <EOL> def weighted_sample ( weights , objects ) : <EOL> """<STR_LIT>""" <EOL> cs = np . cumsum ( weights ) <EOL> idx = sum ( cs < np . random . rand ( ) ) <EOL> return objects [ idx ] <EOL> def softmax ( x ) : <EOL> shifted = x - np . max ( x , axis = <NUM_LIT:1> , keepdims = True ) <EOL> expx = np . exp ( shifted ) <EOL> return expx / np . sum ( expx , axis = <NUM_LIT:1> , keepdims = True ) <EOL> def softmax_sym ( x ) : <EOL> return theano . tensor . nnet . softmax ( x ) <EOL> def cat_entropy ( x ) : <EOL> return - np . sum ( x * np . log ( x ) , axis = <NUM_LIT:1> ) <EOL> def cat_perplexity ( x ) : <EOL> return np . exp ( cat_entropy ( x ) ) <EOL> def explained_variance_1d ( ypred , y ) : <EOL> assert y . ndim == <NUM_LIT:1> and ypred . ndim == <NUM_LIT:1> <EOL> vary = np . var ( y ) <EOL> if np . isclose ( vary , <NUM_LIT:0> ) : <EOL> if np . var ( ypred ) > <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> if abs ( <NUM_LIT:1> - np . var ( y - ypred ) / ( vary + <NUM_LIT> ) ) > <NUM_LIT> : <EOL> import ipdb ; ipdb . set_trace ( ) <EOL> return <NUM_LIT:1> - np . var ( y - ypred ) / ( vary + <NUM_LIT> ) <EOL> def to_onehot ( ind , dim ) : <EOL> ret = np . zeros ( dim ) <EOL> ret [ ind ] = <NUM_LIT:1> <EOL> return ret <EOL> def to_onehot_n ( inds , dim ) : <EOL> ret = np . zeros ( ( len ( inds ) , dim ) ) <EOL> ret [ np . arange ( len ( inds ) ) , inds ] = <NUM_LIT:1> <EOL> return ret <EOL> def to_onehot_sym ( ind , dim ) : <EOL> assert ind . ndim == <NUM_LIT:1> <EOL> return theano . tensor . extra_ops . to_one_hot ( ind , dim ) <EOL> def from_onehot ( v ) : <EOL> return np . nonzero ( v ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> def from_onehot_n ( v ) : <EOL> return np . nonzero ( v ) [ <NUM_LIT:1> ] <EOL> def normalize_updates ( old_mean , old_std , new_mean , new_std , old_W , old_b ) : <EOL> """<STR_LIT>""" <EOL> new_W = old_W * old_std [ <NUM_LIT:0> ] / ( new_std [ <NUM_LIT:0> ] + <NUM_LIT> ) <EOL> new_b = ( old_b * old_std [ <NUM_LIT:0> ] + old_mean [ <NUM_LIT:0> ] - new_mean [ <NUM_LIT:0> ] ) / ( new_std [ <NUM_LIT:0> ] + <NUM_LIT> ) <EOL> return OrderedDict ( [ <EOL> ( old_W , TT . cast ( new_W , old_W . dtype ) ) , <EOL> ( old_b , TT . cast ( new_b , old_b . dtype ) ) , <EOL> ( old_mean , new_mean ) , <EOL> ( old_std , new_std ) , <EOL> ] ) <EOL> def discount_cumsum ( x , discount ) : <EOL> return scipy . signal . lfilter ( [ <NUM_LIT:1> ] , [ <NUM_LIT:1> , - discount ] , x [ : : - <NUM_LIT:1> ] , axis = <NUM_LIT:0> ) [ : : - <NUM_LIT:1> ] <EOL> def discount_return ( x , discount ) : <EOL> return np . sum ( x * ( discount ** np . arange ( len ( x ) ) ) ) <EOL> def rk4 ( derivs , y0 , t , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> Ny = len ( y0 ) <EOL> except TypeError : <EOL> yout = np . zeros ( ( len ( t ) , ) , np . float_ ) <EOL> else : <EOL> yout = np . zeros ( ( len ( t ) , Ny ) , np . float_ ) <EOL> yout [ <NUM_LIT:0> ] = y0 <EOL> i = <NUM_LIT:0> <EOL> for i in np . arange ( len ( t ) - <NUM_LIT:1> ) : <EOL> thist = t [ i ] <EOL> dt = t [ i + <NUM_LIT:1> ] - thist <EOL> dt2 = dt / <NUM_LIT> <EOL> y0 = yout [ i ] <EOL> k1 = np . asarray ( derivs ( y0 , thist , * args , ** kwargs ) ) <EOL> k2 = np . asarray ( derivs ( y0 + dt2 * k1 , thist + dt2 , * args , ** kwargs ) ) <EOL> k3 = np . asarray ( derivs ( y0 + dt2 * k2 , thist + dt2 , * args , ** kwargs ) ) <EOL> k4 = np . asarray ( derivs ( y0 + dt * k3 , thist + dt , * args , ** kwargs ) ) <EOL> yout [ i + <NUM_LIT:1> ] = y0 + dt / <NUM_LIT> * ( k1 + <NUM_LIT:2> * k2 + <NUM_LIT:2> * k3 + k4 ) <EOL> return yout </s>
