<s> import numpy <EOL> import tempfile <EOL> import theano <EOL> from blocks . initialization import IsotropicGaussian , Orthogonal , Constant <EOL> from blocks . model import Model <EOL> from machine_translation . model import BidirectionalEncoder , Decoder <EOL> from machine_translation . stream import get_tr_stream , get_dev_stream <EOL> from numpy . testing import assert_allclose <EOL> def test_search_model ( ) : <EOL> floatX = theano . config . floatX <EOL> source_sentence = theano . tensor . lmatrix ( '<STR_LIT:source>' ) <EOL> source_sentence_mask = theano . tensor . matrix ( '<STR_LIT>' , dtype = floatX ) <EOL> target_sentence = theano . tensor . lmatrix ( '<STR_LIT:target>' ) <EOL> target_sentence_mask = theano . tensor . matrix ( '<STR_LIT>' , dtype = floatX ) <EOL> encoder = BidirectionalEncoder ( <EOL> vocab_size = <NUM_LIT:10> , embedding_dim = <NUM_LIT:5> , state_dim = <NUM_LIT:8> ) <EOL> decoder = Decoder ( <EOL> vocab_size = <NUM_LIT:12> , embedding_dim = <NUM_LIT:6> , state_dim = <NUM_LIT:8> , representation_dim = <NUM_LIT:16> ) <EOL> cost = decoder . cost ( <EOL> encoder . apply ( source_sentence , source_sentence_mask ) , <EOL> source_sentence_mask , target_sentence , target_sentence_mask ) <EOL> f_cost = theano . function ( <EOL> inputs = [ source_sentence , source_sentence_mask , <EOL> target_sentence , target_sentence_mask ] , <EOL> outputs = cost ) <EOL> numpy . random . seed ( <NUM_LIT> ) <EOL> x = numpy . random . randint ( <NUM_LIT:0> , <NUM_LIT:10> , size = ( <NUM_LIT> , <NUM_LIT:4> ) ) <EOL> y = numpy . random . randint ( <NUM_LIT:0> , <NUM_LIT:12> , size = ( <NUM_LIT> , <NUM_LIT:6> ) ) <EOL> x_mask = numpy . ones_like ( x ) . astype ( floatX ) <EOL> y_mask = numpy . ones_like ( y ) . astype ( floatX ) <EOL> encoder . weights_init = decoder . weights_init = IsotropicGaussian ( <EOL> <NUM_LIT> ) <EOL> encoder . biases_init = decoder . biases_init = Constant ( <NUM_LIT:0> ) <EOL> encoder . push_initialization_config ( ) <EOL> decoder . push_initialization_config ( ) <EOL> encoder . bidir . prototype . weights_init = Orthogonal ( ) <EOL> decoder . transition . weights_init = Orthogonal ( ) <EOL> encoder . initialize ( ) <EOL> decoder . initialize ( ) <EOL> cost_ = f_cost ( x , x_mask , y , y_mask ) <EOL> assert_allclose ( cost_ , <NUM_LIT> ) <EOL> def test_stream ( ) : <EOL> vocab = { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> } <EOL> with tempfile . NamedTemporaryFile ( ) as src_data : <EOL> with tempfile . NamedTemporaryFile ( ) as trg_data : <EOL> get_tr_stream ( <EOL> src_vocab = vocab , trg_vocab = vocab , src_data = src_data . name , <EOL> trg_data = trg_data . name ) <EOL> with tempfile . NamedTemporaryFile ( ) as val_set : <EOL> get_dev_stream ( val_set = val_set . name , src_vocab = vocab ) <EOL> def test_sampling ( ) : <EOL> sampling_input = theano . tensor . lmatrix ( '<STR_LIT:input>' ) <EOL> encoder = BidirectionalEncoder ( <EOL> vocab_size = <NUM_LIT:10> , embedding_dim = <NUM_LIT:5> , state_dim = <NUM_LIT:8> ) <EOL> decoder = Decoder ( <EOL> vocab_size = <NUM_LIT:12> , embedding_dim = <NUM_LIT:6> , state_dim = <NUM_LIT:8> , representation_dim = <NUM_LIT:16> , <EOL> theano_seed = <NUM_LIT> ) <EOL> sampling_representation = encoder . apply ( <EOL> sampling_input , theano . tensor . ones ( sampling_input . shape ) ) <EOL> generateds = decoder . generate ( sampling_input , sampling_representation ) <EOL> model = Model ( generateds [ <NUM_LIT:1> ] ) <EOL> encoder . weights_init = decoder . weights_init = IsotropicGaussian ( <EOL> <NUM_LIT> ) <EOL> encoder . biases_init = decoder . biases_init = Constant ( <NUM_LIT:0> ) <EOL> encoder . push_initialization_config ( ) <EOL> decoder . push_initialization_config ( ) <EOL> encoder . bidir . prototype . weights_init = Orthogonal ( ) <EOL> decoder . transition . weights_init = Orthogonal ( ) <EOL> encoder . initialize ( ) <EOL> decoder . initialize ( ) <EOL> sampling_fn = model . get_theano_function ( ) <EOL> numpy . random . seed ( <NUM_LIT> ) <EOL> x = numpy . random . randint ( <NUM_LIT:0> , <NUM_LIT:10> , size = ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> generated_step = sampling_fn ( x ) <EOL> assert len ( generated_step [ <NUM_LIT:0> ] . flatten ( ) ) == <NUM_LIT:4> </s>
