<s> from __future__ import absolute_import , print_function , division <EOL> import numpy <EOL> import theano <EOL> from theano import config , function , tensor <EOL> from . . multinomial import GPUAMultinomialFromUniform <EOL> import theano . tests . unittest_tools as utt <EOL> from . config import mode_with_gpu , mode_without_gpu <EOL> def get_mode ( gpu ) : <EOL> mode = mode_without_gpu <EOL> if gpu : <EOL> mode = mode_with_gpu <EOL> return mode <EOL> def run_with_c ( f , gpu = False ) : <EOL> mode = get_mode ( gpu ) <EOL> f ( mode , gpu ) <EOL> def test_multinomial_0 ( ) : <EOL> p = tensor . fmatrix ( ) <EOL> u = tensor . fvector ( ) <EOL> m = theano . sandbox . multinomial . MultinomialFromUniform ( '<STR_LIT>' ) ( p , u ) <EOL> def body ( mode , gpu ) : <EOL> f = function ( [ p , u ] , m * <NUM_LIT:2> , allow_input_downcast = True , mode = mode ) <EOL> if gpu : <EOL> assert any ( [ type ( node . op ) is GPUAMultinomialFromUniform <EOL> for node in f . maker . fgraph . toposort ( ) ] ) <EOL> utt . assert_allclose ( f ( [ [ <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , [ <NUM_LIT> , <NUM_LIT> ] ) , <EOL> [ [ <NUM_LIT:2> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:2> ] ] ) <EOL> r = f ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> utt . assert_allclose ( r , [ [ <NUM_LIT:0> , <NUM_LIT:2> ] , [ <NUM_LIT:0> , <NUM_LIT:2> ] ] ) <EOL> r = f ( [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> utt . assert_allclose ( r , [ [ <NUM_LIT:0> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] ) <EOL> r = f ( [ [ <NUM_LIT> , <NUM_LIT> ] ] , [ <NUM_LIT> ] ) <EOL> utt . assert_allclose ( r , [ [ <NUM_LIT:0> , <NUM_LIT:2> ] ] ) <EOL> run_with_c ( body ) <EOL> run_with_c ( body , True ) <EOL> def test_multinomial_large ( ) : <EOL> def body ( mode , gpu ) : <EOL> p = tensor . fmatrix ( ) <EOL> u = tensor . fvector ( ) <EOL> m = theano . sandbox . multinomial . MultinomialFromUniform ( '<STR_LIT>' ) ( p , u ) <EOL> f = function ( [ p , u ] , m * <NUM_LIT:2> , allow_input_downcast = True , mode = mode ) <EOL> if gpu : <EOL> assert any ( [ type ( node . op ) is GPUAMultinomialFromUniform <EOL> for node in f . maker . fgraph . toposort ( ) ] ) <EOL> pval = numpy . arange ( <NUM_LIT> * <NUM_LIT:4> , <EOL> dtype = '<STR_LIT>' ) . reshape ( ( <NUM_LIT> , <NUM_LIT:4> ) ) + <NUM_LIT:0.1> <EOL> pval = pval / pval . sum ( axis = <NUM_LIT:1> ) [ : , None ] <EOL> uval = numpy . ones_like ( pval [ : , <NUM_LIT:0> ] ) * <NUM_LIT:0.5> <EOL> mval = f ( pval , uval ) <EOL> assert mval . shape == pval . shape <EOL> if config . cast_policy == '<STR_LIT>' : <EOL> assert mval . dtype == pval . dtype <EOL> elif config . cast_policy == '<STR_LIT>' : <EOL> assert mval . dtype == config . floatX <EOL> elif config . cast_policy == '<STR_LIT>' : <EOL> assert mval . dtype == '<STR_LIT>' <EOL> else : <EOL> raise NotImplementedError ( config . cast_policy ) <EOL> utt . assert_allclose ( mval . sum ( axis = <NUM_LIT:1> ) , <NUM_LIT:2> ) <EOL> asdf = numpy . asarray ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:0> ] ) + <NUM_LIT:0> * pval <EOL> utt . assert_allclose ( mval , asdf ) <EOL> run_with_c ( body ) <EOL> run_with_c ( body , True ) <EOL> def test_gpu_opt ( ) : <EOL> p = tensor . fmatrix ( ) <EOL> u = tensor . fvector ( ) <EOL> m = theano . sandbox . multinomial . MultinomialFromUniform ( '<STR_LIT>' ) ( p , u ) <EOL> assert m . dtype == '<STR_LIT>' , m . dtype <EOL> f = function ( [ p , u ] , m , allow_input_downcast = True , mode = get_mode ( True ) ) <EOL> assert any ( [ type ( node . op ) is GPUAMultinomialFromUniform <EOL> for node in f . maker . fgraph . toposort ( ) ] ) <EOL> pval = numpy . arange ( <NUM_LIT> * <NUM_LIT:4> , dtype = '<STR_LIT>' ) . reshape ( ( <NUM_LIT> , <NUM_LIT:4> ) ) + <NUM_LIT:0.1> <EOL> pval = pval / pval . sum ( axis = <NUM_LIT:1> ) [ : , None ] <EOL> uval = numpy . ones_like ( pval [ : , <NUM_LIT:0> ] ) * <NUM_LIT:0.5> <EOL> f ( pval , uval ) <EOL> r = tensor . frow ( ) <EOL> m = theano . sandbox . multinomial . MultinomialFromUniform ( '<STR_LIT>' ) ( r , u ) <EOL> assert m . dtype == '<STR_LIT>' , m . dtype <EOL> f = function ( [ r , u ] , m , allow_input_downcast = True , mode = get_mode ( True ) ) <EOL> assert any ( [ type ( node . op ) is GPUAMultinomialFromUniform <EOL> for node in f . maker . fgraph . toposort ( ) ] ) <EOL> pval = numpy . arange ( <NUM_LIT:1> * <NUM_LIT:4> , dtype = '<STR_LIT>' ) . reshape ( ( <NUM_LIT:1> , <NUM_LIT:4> ) ) + <NUM_LIT:0.1> <EOL> pval = pval / pval . sum ( axis = <NUM_LIT:1> ) [ : , None ] <EOL> uval = numpy . ones_like ( pval [ : , <NUM_LIT:0> ] ) * <NUM_LIT:0.5> <EOL> f ( pval , uval ) </s>
