<s> import warnings <EOL> import tarfile <EOL> from pickle import PicklingError <EOL> from tempfile import NamedTemporaryFile <EOL> import numpy <EOL> import theano <EOL> from numpy . testing import assert_allclose , assert_raises <EOL> from blocks . config import config <EOL> from theano import tensor , shared <EOL> from blocks . bricks import MLP , Linear <EOL> from blocks . initialization import Constant <EOL> from blocks . serialization import ( load , dump , secure_dump , load_parameters , <EOL> _Renamer , add_to_dump ) <EOL> def test_renamer ( ) : <EOL> x = tensor . matrix ( '<STR_LIT>' ) <EOL> layer = Linear ( <NUM_LIT:10> , <NUM_LIT:10> ) <EOL> y = layer . apply ( x ) <EOL> named = shared ( name = '<STR_LIT>' , value = numpy . zeros ( <NUM_LIT:2> ) ) <EOL> tag_named = shared ( value = numpy . zeros ( <NUM_LIT:2> ) ) <EOL> tag_named . tag . name = '<STR_LIT>' <EOL> unnamed = shared ( value = numpy . zeros ( <NUM_LIT:2> ) ) <EOL> variables = [ layer . W , named , tag_named , unnamed , unnamed , unnamed ] <EOL> renamer = _Renamer ( ) <EOL> names = [ renamer ( n ) for n in variables ] <EOL> true_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert set ( names ) == set ( true_names ) <EOL> def foo ( ) : <EOL> pass <EOL> def test_serialization ( ) : <EOL> mlp = MLP ( activations = [ None , None ] , dims = [ <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ] , <EOL> weights_init = Constant ( <NUM_LIT:1.> ) , use_bias = False ) <EOL> mlp . initialize ( ) <EOL> W = mlp . linear_transformations [ <NUM_LIT:1> ] . W <EOL> W . set_value ( W . get_value ( ) * <NUM_LIT:2> ) <EOL> foo . __module__ = '<STR_LIT:__main__>' <EOL> import __main__ <EOL> __main__ . __dict__ [ '<STR_LIT:foo>' ] = foo <EOL> mlp . foo = foo <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> dump ( mlp . foo , f ) <EOL> assert len ( w ) == <NUM_LIT:1> <EOL> assert '<STR_LIT:__main__>' in str ( w [ - <NUM_LIT:1> ] . message ) <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> dump ( mlp , f , parameters = [ mlp . children [ <NUM_LIT:0> ] . W , mlp . children [ <NUM_LIT:1> ] . W ] ) <EOL> with open ( f . name , '<STR_LIT:rb>' ) as ff : <EOL> numpy_data = load_parameters ( ff ) <EOL> assert set ( numpy_data . keys ( ) ) == set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert_allclose ( numpy_data [ '<STR_LIT>' ] , numpy . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) <EOL> assert numpy_data [ '<STR_LIT>' ] . dtype == theano . config . floatX <EOL> with open ( f . name , '<STR_LIT:rb>' ) as ff : <EOL> mlp = load ( ff ) <EOL> assert_allclose ( mlp . linear_transformations [ <NUM_LIT:1> ] . W . get_value ( ) , <EOL> numpy . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) * <NUM_LIT:2> ) <EOL> for child in mlp . children : <EOL> child . name = '<STR_LIT>' <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> dump ( mlp , f , parameters = [ mlp . children [ <NUM_LIT:0> ] . W , mlp . children [ <NUM_LIT:1> ] . W ] ) <EOL> with open ( f . name , '<STR_LIT:rb>' ) as ff : <EOL> numpy_data = load_parameters ( ff ) <EOL> assert set ( numpy_data . keys ( ) ) == set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> dump ( None , f , parameters = [ mlp . children [ <NUM_LIT:0> ] . W , mlp . children [ <NUM_LIT:1> ] . W ] ) <EOL> with tarfile . open ( f . name , '<STR_LIT:r>' ) as tarball : <EOL> assert set ( tarball . getnames ( ) ) == set ( [ '<STR_LIT>' ] ) <EOL> def test_add_to_dump ( ) : <EOL> mlp = MLP ( activations = [ None , None ] , dims = [ <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ] , <EOL> weights_init = Constant ( <NUM_LIT:1.> ) , use_bias = False ) <EOL> mlp . initialize ( ) <EOL> W = mlp . linear_transformations [ <NUM_LIT:1> ] . W <EOL> W . set_value ( W . get_value ( ) * <NUM_LIT:2> ) <EOL> mlp2 = MLP ( activations = [ None , None ] , dims = [ <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ] , <EOL> weights_init = Constant ( <NUM_LIT:1.> ) , use_bias = False , <EOL> name = '<STR_LIT>' ) <EOL> mlp2 . initialize ( ) <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> dump ( mlp , f , parameters = [ mlp . children [ <NUM_LIT:0> ] . W , mlp . children [ <NUM_LIT:1> ] . W ] ) <EOL> with open ( f . name , '<STR_LIT>' ) as ff : <EOL> add_to_dump ( mlp . children [ <NUM_LIT:0> ] , ff , '<STR_LIT>' , <EOL> parameters = [ mlp . children [ <NUM_LIT:0> ] . W ] ) <EOL> add_to_dump ( mlp . children [ <NUM_LIT:1> ] , ff , '<STR_LIT>' ) <EOL> with tarfile . open ( f . name , '<STR_LIT:r>' ) as tarball : <EOL> assert set ( tarball . getnames ( ) ) == set ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> with open ( f . name , '<STR_LIT:rb>' ) as ff : <EOL> saved_children_0 = load ( ff , '<STR_LIT>' ) <EOL> saved_children_1 = load ( ff , '<STR_LIT>' ) <EOL> assert_allclose ( saved_children_0 . W . get_value ( ) , <EOL> numpy . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) ) <EOL> assert_allclose ( saved_children_1 . W . get_value ( ) , <EOL> numpy . ones ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) * <NUM_LIT:2> ) <EOL> with open ( f . name , '<STR_LIT>' ) as ff : <EOL> assert_raises ( ValueError , add_to_dump , * [ mlp . children [ <NUM_LIT:0> ] , ff , '<STR_LIT>' ] ) <EOL> with open ( f . name , '<STR_LIT>' ) as ff : <EOL> assert_raises ( ValueError , add_to_dump , * [ mlp2 , ff , '<STR_LIT>' ] , <EOL> ** dict ( parameters = [ mlp2 . children [ <NUM_LIT:0> ] . W , <EOL> mlp2 . children [ <NUM_LIT:1> ] . W ] ) ) <EOL> with NamedTemporaryFile ( delete = False ) as f : <EOL> dump ( mlp , f ) <EOL> with open ( f . name , '<STR_LIT>' ) as ff : <EOL> assert_raises ( ValueError , add_to_dump , * [ mlp2 , ff , '<STR_LIT>' ] , <EOL> ** dict ( parameters = [ mlp2 . children [ <NUM_LIT:0> ] . W , <EOL> mlp2 . children [ <NUM_LIT:1> ] . W ] ) ) <EOL> def test_secure_dump ( ) : <EOL> foo = object ( ) <EOL> bar = lambda : None <EOL> with NamedTemporaryFile ( delete = False , dir = config . temp_dir ) as f : <EOL> secure_dump ( foo , f . name ) <EOL> assert_raises ( PicklingError , secure_dump , bar , f . name ) <EOL> with open ( f . name , '<STR_LIT:rb>' ) as f : <EOL> assert type ( load ( f ) ) is object </s>
