<s> import os <EOL> import tempfile <EOL> import unittest <EOL> import mock <EOL> import numpy <EOL> from chainer import cuda <EOL> from chainer import link <EOL> from chainer import links <EOL> from chainer import optimizers <EOL> from chainer . serializers import npz <EOL> from chainer import testing <EOL> from chainer . testing import attr <EOL> class TestDictionarySerializer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . serializer = npz . DictionarySerializer ( { } ) <EOL> self . data = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) . astype ( numpy . float32 ) <EOL> def test_get_item ( self ) : <EOL> child = self . serializer [ '<STR_LIT:x>' ] <EOL> self . assertIsInstance ( child , npz . DictionarySerializer ) <EOL> self . assertEqual ( child . path , '<STR_LIT>' ) <EOL> def test_get_item_strip_slashes ( self ) : <EOL> child = self . serializer [ '<STR_LIT>' ] <EOL> self . assertEqual ( child . path , '<STR_LIT>' ) <EOL> def check_serialize ( self , data , query ) : <EOL> ret = self . serializer ( query , data ) <EOL> dset = self . serializer . target [ '<STR_LIT:w>' ] <EOL> self . assertIsInstance ( dset , numpy . ndarray ) <EOL> self . assertEqual ( dset . shape , data . shape ) <EOL> self . assertEqual ( dset . size , data . size ) <EOL> self . assertEqual ( dset . dtype , data . dtype ) <EOL> numpy . testing . assert_array_equal ( dset , cuda . to_cpu ( data ) ) <EOL> self . assertIs ( ret , data ) <EOL> def test_serialize_cpu ( self ) : <EOL> self . check_serialize ( self . data , '<STR_LIT:w>' ) <EOL> @ attr . gpu <EOL> def test_serialize_gpu ( self ) : <EOL> self . check_serialize ( cuda . to_gpu ( self . data ) , '<STR_LIT:w>' ) <EOL> def test_serialize_cpu_strip_slashes ( self ) : <EOL> self . check_serialize ( self . data , '<STR_LIT>' ) <EOL> @ attr . gpu <EOL> def test_serialize_gpu_strip_slashes ( self ) : <EOL> self . check_serialize ( cuda . to_gpu ( self . data ) , '<STR_LIT>' ) <EOL> def test_serialize_scalar ( self ) : <EOL> ret = self . serializer ( '<STR_LIT:x>' , <NUM_LIT:10> ) <EOL> dset = self . serializer . target [ '<STR_LIT:x>' ] <EOL> self . assertIsInstance ( dset , numpy . ndarray ) <EOL> self . assertEqual ( dset . shape , ( ) ) <EOL> self . assertEqual ( dset . size , <NUM_LIT:1> ) <EOL> self . assertEqual ( dset . dtype , int ) <EOL> self . assertEqual ( dset [ ( ) ] , <NUM_LIT:10> ) <EOL> self . assertIs ( ret , <NUM_LIT:10> ) <EOL> @ testing . parameterize ( * testing . product ( { '<STR_LIT>' : [ False , True ] } ) ) <EOL> class TestNpzDeserializer ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . data = numpy . random . uniform ( - <NUM_LIT:1> , <NUM_LIT:1> , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) . astype ( numpy . float32 ) <EOL> fd , path = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> self . temp_file_path = path <EOL> with open ( path , '<STR_LIT:wb>' ) as f : <EOL> savez = numpy . savez_compressed if self . compress else numpy . savez <EOL> savez ( <EOL> f , ** { '<STR_LIT>' : None , '<STR_LIT:y>' : self . data , '<STR_LIT:z>' : numpy . asarray ( <NUM_LIT:10> ) } ) <EOL> self . npzfile = numpy . load ( path ) <EOL> self . deserializer = npz . NpzDeserializer ( self . npzfile ) <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . npzfile . close ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> os . remove ( self . temp_file_path ) <EOL> def test_get_item ( self ) : <EOL> child = self . deserializer [ '<STR_LIT:x>' ] <EOL> self . assertIsInstance ( child , npz . NpzDeserializer ) <EOL> self . assertEqual ( child . path [ - <NUM_LIT:2> : ] , '<STR_LIT>' ) <EOL> def test_get_item_strip_slashes ( self ) : <EOL> child = self . deserializer [ '<STR_LIT>' ] <EOL> self . assertEqual ( child . path , '<STR_LIT>' ) <EOL> def check_deserialize ( self , y , query ) : <EOL> ret = self . deserializer ( query , y ) <EOL> numpy . testing . assert_array_equal ( cuda . to_cpu ( y ) , self . data ) <EOL> self . assertIs ( ret , y ) <EOL> def test_deserialize_cpu ( self ) : <EOL> y = numpy . empty ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = numpy . float32 ) <EOL> self . check_deserialize ( y , '<STR_LIT:y>' ) <EOL> @ attr . gpu <EOL> def test_deserialize_gpu ( self ) : <EOL> y = numpy . empty ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = numpy . float32 ) <EOL> self . check_deserialize ( cuda . to_gpu ( y ) , '<STR_LIT:y>' ) <EOL> def test_deserialize_cpu_strip_slashes ( self ) : <EOL> y = numpy . empty ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = numpy . float32 ) <EOL> self . check_deserialize ( y , '<STR_LIT>' ) <EOL> @ attr . gpu <EOL> def test_deserialize_gpu_strip_slashes ( self ) : <EOL> y = numpy . empty ( ( <NUM_LIT:2> , <NUM_LIT:3> ) , dtype = numpy . float32 ) <EOL> self . check_deserialize ( cuda . to_gpu ( y ) , '<STR_LIT>' ) <EOL> def test_deserialize_scalar ( self ) : <EOL> z = <NUM_LIT:5> <EOL> ret = self . deserializer ( '<STR_LIT:z>' , z ) <EOL> self . assertEqual ( ret , <NUM_LIT:10> ) <EOL> @ testing . parameterize ( * testing . product ( { '<STR_LIT>' : [ False , True ] } ) ) <EOL> class TestSaveNpz ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> fd , path = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> self . temp_file_path = path <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> os . remove ( self . temp_file_path ) <EOL> def test_save ( self ) : <EOL> obj = mock . MagicMock ( ) <EOL> npz . save_npz ( self . temp_file_path , obj , self . compress ) <EOL> self . assertEqual ( obj . serialize . call_count , <NUM_LIT:1> ) <EOL> ( serializer , ) , _ = obj . serialize . call_args <EOL> self . assertIsInstance ( serializer , npz . DictionarySerializer ) <EOL> @ testing . parameterize ( * testing . product ( { '<STR_LIT>' : [ False , True ] } ) ) <EOL> class TestLoadNpz ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> fd , path = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> self . temp_file_path = path <EOL> with open ( path , '<STR_LIT:wb>' ) as f : <EOL> savez = numpy . savez_compressed if self . compress else numpy . savez <EOL> savez ( f , None ) <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> os . remove ( self . temp_file_path ) <EOL> def test_load ( self ) : <EOL> obj = mock . MagicMock ( ) <EOL> npz . load_npz ( self . temp_file_path , obj ) <EOL> self . assertEqual ( obj . serialize . call_count , <NUM_LIT:1> ) <EOL> ( serializer , ) , _ = obj . serialize . call_args <EOL> self . assertIsInstance ( serializer , npz . NpzDeserializer ) <EOL> @ testing . parameterize ( * testing . product ( { '<STR_LIT>' : [ False , True ] } ) ) <EOL> class TestGroupHierachy ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> fd , path = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> self . temp_file_path = path <EOL> child = link . Chain ( linear = links . Linear ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> child . add_param ( '<STR_LIT>' , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . parent = link . Chain ( child = child ) <EOL> self . parent . add_param ( '<STR_LIT>' , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> self . optimizer = optimizers . AdaDelta ( ) <EOL> self . optimizer . setup ( self . parent ) <EOL> self . savez = numpy . savez_compressed if self . compress else numpy . savez <EOL> def _save ( self , target , obj , name ) : <EOL> serializer = npz . DictionarySerializer ( target , name ) <EOL> serializer . save ( obj ) <EOL> def tearDown ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> os . remove ( self . temp_file_path ) <EOL> def _check_chain_group ( self , file , state , prefix = '<STR_LIT>' ) : <EOL> keys = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) + state <EOL> self . assertSetEqual ( set ( file . keys ( ) ) , { prefix + x for x in keys } ) <EOL> def _check_optimizer_group ( self , file , state , prefix = '<STR_LIT>' ) : <EOL> keys = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) + state <EOL> self . assertSetEqual ( set ( file . keys ( ) ) , <EOL> { prefix + x for x in keys } ) <EOL> def test_save_chain ( self ) : <EOL> d = { } <EOL> self . _save ( d , self . parent , '<STR_LIT>' ) <EOL> with open ( self . temp_file_path , '<STR_LIT:wb>' ) as f : <EOL> self . savez ( f , ** d ) <EOL> with numpy . load ( self . temp_file_path ) as f : <EOL> self . _check_chain_group ( f , ( '<STR_LIT>' , ) , '<STR_LIT>' ) <EOL> def test_save_optimizer ( self ) : <EOL> d = { } <EOL> self . _save ( d , self . optimizer , '<STR_LIT>' ) <EOL> with open ( self . temp_file_path , '<STR_LIT:wb>' ) as f : <EOL> self . savez ( f , ** d ) <EOL> with numpy . load ( self . temp_file_path ) as f : <EOL> self . _check_optimizer_group ( <EOL> f , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:t>' ) , '<STR_LIT>' ) <EOL> def test_save_chain2 ( self ) : <EOL> npz . save_npz ( self . temp_file_path , self . parent , self . compress ) <EOL> with numpy . load ( self . temp_file_path ) as f : <EOL> self . _check_chain_group ( f , ( '<STR_LIT>' , ) ) <EOL> def test_save_optimizer2 ( self ) : <EOL> npz . save_npz ( self . temp_file_path , self . optimizer , self . compress ) <EOL> with numpy . load ( self . temp_file_path ) as f : <EOL> self . _check_optimizer_group ( f , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:t>' ) ) <EOL> def test_load_optimizer ( self ) : <EOL> for param in self . parent . params ( ) : <EOL> param . data . fill ( <NUM_LIT:1> ) <EOL> npz . save_npz ( self . temp_file_path , self . parent , self . compress ) <EOL> for param in self . parent . params ( ) : <EOL> param . data . fill ( <NUM_LIT:0> ) <EOL> npz . load_npz ( self . temp_file_path , self . parent ) <EOL> for param in self . parent . params ( ) : <EOL> self . assertTrue ( ( param . data == <NUM_LIT:1> ) . all ( ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
