<s> import numpy <EOL> from six . moves import xrange <EOL> import theano <EOL> import scipy . sparse <EOL> from theano import gof , tensor <EOL> from theano . tensor . opt import register_specialize <EOL> from theano . sparse . basic import ( <EOL> as_sparse_variable , SparseType , add_s_s , neg , <EOL> mul_s_s , mul_s_d , dot , <EOL> CSMProperties , CSM , <EOL> _is_sparse_variable , _is_dense_variable , CSC , CSR , <EOL> csm_properties , csm_data , csm_indices , csm_indptr , csm_shape , <EOL> _is_sparse , <EOL> Remove0 , remove0 , <EOL> Cast , bcast , wcast , icast , lcast , fcast , dcast , ccast , zcast , <EOL> HStack , hstack , VStack , vstack , <EOL> AddSSData , add_s_s_data , <EOL> MulSV , mul_s_v , <EOL> structured_monoid , <EOL> structured_sigmoid , structured_exp , structured_log , structured_pow , <EOL> structured_minimum , structured_maximum , structured_add , <EOL> StructuredAddSV , structured_add_s_v , <EOL> SamplingDot , sampling_dot ) <EOL> from theano . sparse . opt import ( <EOL> MulSDCSC , mul_s_d_csc , MulSDCSR , mul_s_d_csr , <EOL> MulSVCSR , mul_s_v_csr , <EOL> StructuredAddSVCSR , structured_add_s_v_csr , <EOL> SamplingDotCSR , sampling_dot_csr , <EOL> local_mul_s_d , local_mul_s_v , <EOL> local_structured_add_s_v , local_sampling_dot_csr ) <EOL> EliminateZeros = Remove0 <EOL> eliminate_zeros = remove0 <EOL> class Poisson ( gof . op . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def make_node ( self , x ) : <EOL> x = as_sparse_variable ( x ) <EOL> return gof . Apply ( self , [ x ] , [ x . type ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> ( x , ) = inputs <EOL> ( out , ) = outputs <EOL> assert _is_sparse ( x ) <EOL> assert x . format in [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> out [ <NUM_LIT:0> ] = x . copy ( ) <EOL> out [ <NUM_LIT:0> ] . data = numpy . asarray ( numpy . random . poisson ( out [ <NUM_LIT:0> ] . data ) , <EOL> dtype = x . dtype ) <EOL> out [ <NUM_LIT:0> ] . eliminate_zeros ( ) <EOL> def grad ( self , inputs , outputs_gradients ) : <EOL> comment = "<STR_LIT>" <EOL> return [ theano . gradient . grad_undefined ( op = self , x_pos = <NUM_LIT:0> , x = inputs [ <NUM_LIT:0> ] , <EOL> comment = comment ) ] <EOL> def infer_shape ( self , node , ins_shapes ) : <EOL> return ins_shapes <EOL> poisson = Poisson ( ) <EOL> class Binomial ( gof . op . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __init__ ( self , format , dtype ) : <EOL> self . format = format <EOL> self . dtype = dtype <EOL> def make_node ( self , n , p , shape ) : <EOL> n = tensor . as_tensor_variable ( n ) <EOL> p = tensor . as_tensor_variable ( p ) <EOL> shape = tensor . as_tensor_variable ( shape ) <EOL> return gof . Apply ( self , [ n , p , shape ] , <EOL> [ SparseType ( dtype = self . dtype , <EOL> format = self . format ) ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> ( n , p , shape ) = inputs <EOL> ( out , ) = outputs <EOL> binomial = numpy . random . binomial ( n , p , size = shape ) <EOL> csx_matrix = getattr ( scipy . sparse , self . format + '<STR_LIT>' ) <EOL> out [ <NUM_LIT:0> ] = csx_matrix ( binomial , dtype = self . dtype ) <EOL> def connection_pattern ( self , node ) : <EOL> return [ [ True ] , [ True ] , [ False ] ] <EOL> def grad ( self , inputs , gout ) : <EOL> ( n , p , shape ) = inputs <EOL> ( gz , ) = gout <EOL> comment_n = "<STR_LIT>" <EOL> comment_p = "<STR_LIT>" <EOL> return [ theano . gradient . grad_undefined ( op = self , x_pos = <NUM_LIT:0> , x = n , <EOL> comment = comment_n ) , <EOL> theano . gradient . grad_undefined ( op = self , x_pos = <NUM_LIT:1> , x = p , <EOL> comment = comment_p ) , <EOL> theano . gradient . disconnected_type ( ) ] <EOL> def infer_shape ( self , node , ins_shapes ) : <EOL> return [ ( node . inputs [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] , node . inputs [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) ] <EOL> csr_fbinomial = Binomial ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> csc_fbinomial = Binomial ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> csr_dbinomial = Binomial ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> csc_dbinomial = Binomial ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Multinomial ( gof . op . Op ) : <EOL> """<STR_LIT>""" <EOL> __props__ = ( ) <EOL> def make_node ( self , n , p ) : <EOL> n = tensor . as_tensor_variable ( n ) <EOL> p = as_sparse_variable ( p ) <EOL> assert p . format in [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> return gof . Apply ( self , [ n , p ] , [ p . type ( ) ] ) <EOL> def perform ( self , node , inputs , outputs ) : <EOL> ( n , p ) = inputs <EOL> ( out , ) = outputs <EOL> assert _is_sparse ( p ) <EOL> if p . format != '<STR_LIT>' : <EOL> raise NotImplemented ( ) <EOL> out [ <NUM_LIT:0> ] = p . copy ( ) <EOL> if n . ndim == <NUM_LIT:0> : <EOL> for i in xrange ( p . shape [ <NUM_LIT:0> ] ) : <EOL> k , l = p . indptr [ i ] , p . indptr [ i + <NUM_LIT:1> ] <EOL> out [ <NUM_LIT:0> ] . data [ k : l ] = numpy . random . multinomial ( n , p . data [ k : l ] ) <EOL> elif n . ndim == <NUM_LIT:1> : <EOL> if n . shape [ <NUM_LIT:0> ] != p . shape [ <NUM_LIT:0> ] : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> for i in xrange ( p . shape [ <NUM_LIT:0> ] ) : <EOL> k , l = p . indptr [ i ] , p . indptr [ i + <NUM_LIT:1> ] <EOL> out [ <NUM_LIT:0> ] . data [ k : l ] = numpy . random . multinomial ( n [ i ] , p . data [ k : l ] ) <EOL> def grad ( self , inputs , outputs_gradients ) : <EOL> comment_n = "<STR_LIT>" <EOL> comment_p = "<STR_LIT>" <EOL> return [ theano . gradient . grad_undefined ( op = self , x_pos = <NUM_LIT:0> , x = inputs [ <NUM_LIT:0> ] , <EOL> comment = comment_n ) , <EOL> theano . gradient . grad_undefined ( op = self , x_pos = <NUM_LIT:1> , x = inputs [ <NUM_LIT:1> ] , <EOL> comment = comment_p ) ] <EOL> def infer_shape ( self , node , ins_shapes ) : <EOL> return [ ins_shapes [ <NUM_LIT:1> ] ] <EOL> multinomial = Multinomial ( ) </s>
