<s> import cPickle as pickle <EOL> import scipy . io as sio <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> class ModelUtil ( object ) : <EOL> @ staticmethod <EOL> def one_hot_crossentropy ( y_true , y_pred ) : <EOL> if theano . config . floatX == '<STR_LIT>' : <EOL> epsilon = <NUM_LIT> <EOL> else : <EOL> epsilon = <NUM_LIT> <EOL> y_pred = T . clip ( y_pred , epsilon , <NUM_LIT:1.0> - epsilon ) <EOL> y_pred /= y_pred . sum ( axis = - <NUM_LIT:1> , keepdims = True ) <EOL> voca_size = T . shape ( y_pred ) [ - <NUM_LIT:1> ] <EOL> y_pred = y_pred . flatten ( ) <EOL> y_true = y_true . flatten ( ) . astype ( '<STR_LIT>' ) <EOL> ix = T . arange ( y_true . size ) * voca_size + y_true <EOL> cce = - T . log ( y_pred [ ix ] ) <EOL> return cce <EOL> @ staticmethod <EOL> def layer_info ( model ) : <EOL> print '<STR_LIT>' <EOL> ModelUtil . _print_layer_shape ( model ) <EOL> ModelUtil . _print_layer_params ( model ) <EOL> @ staticmethod <EOL> def _shape_to_str ( shape ) : <EOL> str = '<STR_LIT:(>' <EOL> for i , n in enumerate ( shape ) : <EOL> if i > <NUM_LIT:0> : <EOL> str += '<STR_LIT>' . format ( n ) <EOL> str += '<STR_LIT:)>' <EOL> return str <EOL> @ staticmethod <EOL> def _print_layer_shape ( model ) : <EOL> print '<STR_LIT>' <EOL> for i , layer in enumerate ( model . layers ) : <EOL> print '<STR_LIT>' % ( i , layer . __class__ . __name__ , <EOL> ModelUtil . _shape_to_str ( layer . input_shape ) , <EOL> ModelUtil . _shape_to_str ( layer . output_shape ) ) <EOL> @ staticmethod <EOL> def _print_layer_params ( model ) : <EOL> print '<STR_LIT>' <EOL> total_params = <NUM_LIT:0> <EOL> for i , layer in enumerate ( model . layers ) : <EOL> layer_params = layer . count_params ( ) <EOL> total_params += layer_params <EOL> print '<STR_LIT>' % ( i , layer . __class__ . __name__ , layer_params ) <EOL> print '<STR_LIT>' , '<STR_LIT:->' * <NUM_LIT> <EOL> print '<STR_LIT>' % ( '<STR_LIT>' , total_params ) <EOL> @ staticmethod <EOL> def node_info ( graph ) : <EOL> print '<STR_LIT>' <EOL> ModelUtil . _print_node_shape ( graph ) <EOL> ModelUtil . _print_node_params ( graph ) <EOL> @ staticmethod <EOL> def _print_node_shape ( graph ) : <EOL> print '<STR_LIT>' <EOL> for k , node in graph . nodes . iteritems ( ) : <EOL> input_shape = ModelUtil . _shape_to_str ( node . input_shape ) <EOL> print '<STR_LIT>' % ( k , node . __class__ . __name__ , <EOL> input_shape , <EOL> ModelUtil . _shape_to_str ( node . output_shape ) ) <EOL> @ staticmethod <EOL> def _print_node_params ( graph ) : <EOL> print '<STR_LIT>' <EOL> total_params = <NUM_LIT:0> <EOL> for k , node in graph . nodes . iteritems ( ) : <EOL> node_params = node . count_params ( ) <EOL> total_params += node_params <EOL> print '<STR_LIT>' % ( k , node . __class__ . __name__ , node_params ) <EOL> print '<STR_LIT>' , '<STR_LIT:->' * <NUM_LIT> <EOL> print '<STR_LIT>' % ( '<STR_LIT>' , total_params ) <EOL> @ staticmethod <EOL> def save ( path , * var_list ) : <EOL> with open ( path + '<STR_LIT>' , '<STR_LIT:w>' ) as f : <EOL> for v in var_list : <EOL> pickle . dump ( v , f ) <EOL> @ staticmethod <EOL> def load ( path ) : <EOL> def load_iter ( path ) : <EOL> with open ( path + '<STR_LIT>' , '<STR_LIT:r>' ) as f : <EOL> while True : <EOL> try : <EOL> yield pickle . load ( f ) <EOL> except EOFError : <EOL> break <EOL> ret_list = [ ] <EOL> for v in load_iter ( path ) : <EOL> ret_list . append ( v ) <EOL> if len ( ret_list ) == <NUM_LIT:1> : <EOL> return ret_list [ <NUM_LIT:0> ] <EOL> else : <EOL> return tuple ( ret_list ) <EOL> @ staticmethod <EOL> def load_mat ( path , * var_list , ** kwargs ) : <EOL> mat2vec = True <EOL> if '<STR_LIT>' in kwargs : <EOL> mat2vec = kwargs [ '<STR_LIT>' ] <EOL> mat2list = False <EOL> if '<STR_LIT>' in kwargs : <EOL> mat2list = kwargs [ '<STR_LIT>' ] <EOL> contents = sio . loadmat ( path + '<STR_LIT>' ) <EOL> ret_list = [ ] <EOL> for v in var_list : <EOL> try : <EOL> value = contents [ v ] <EOL> if type ( value ) == np . ndarray and value . ndim == <NUM_LIT:2> and ( value . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> or value . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> ) : <EOL> if mat2vec : <EOL> value = value . flatten ( ) <EOL> if mat2list : <EOL> value = list ( value ) <EOL> ret_list . append ( value ) <EOL> except : <EOL> ret_list . append ( None ) <EOL> pass <EOL> if len ( ret_list ) > <NUM_LIT:1> : <EOL> return tuple ( ret_list ) <EOL> else : <EOL> return ret_list [ <NUM_LIT:0> ] <EOL> @ staticmethod <EOL> def save_mat ( path , ** var_list ) : <EOL> sio . savemat ( path + '<STR_LIT>' , var_list ) <EOL> class CodeUtil ( object ) : <EOL> UNICHR_START = <NUM_LIT:10> <EOL> UNICHR_EOS = <NUM_LIT:0> <EOL> UNICHR_OOC = <NUM_LIT:1> <EOL> UNICHR_EMP = <NUM_LIT:2> <EOL> UNICHR_RESERV1 = <NUM_LIT:3> <EOL> UNICHR_RESERV2 = <NUM_LIT:4> <EOL> UNICHR_RESERV3 = <NUM_LIT:5> <EOL> byte_size = <NUM_LIT> <EOL> @ staticmethod <EOL> def str_to_two_hot_vec ( uni_str ) : <EOL> ret = np . zeros ( ( len ( uni_str ) , CodeUtil . byte_size * <NUM_LIT:2> ) , dtype = '<STR_LIT>' ) <EOL> for i , c in enumerate ( uni_str ) : <EOL> code = ord ( c ) <EOL> if CodeUtil . UNICHR_START <= code < <NUM_LIT:2> ** <NUM_LIT:16> : <EOL> ret [ i , int ( code / CodeUtil . byte_size ) + CodeUtil . byte_size ] = <NUM_LIT:1> <EOL> ret [ i , code % CodeUtil . byte_size ] = <NUM_LIT:1> <EOL> else : <EOL> ret [ i , CodeUtil . byte_size ] = <NUM_LIT:1> <EOL> ret [ i , CodeUtil . UNICHR_OOC ] = <NUM_LIT:1> <EOL> return np . array ( ret ) <EOL> @ staticmethod <EOL> def str_to_seq_idx ( uni_str ) : <EOL> ret = np . zeros ( len ( uni_str ) * <NUM_LIT:2> , dtype = '<STR_LIT>' ) <EOL> for i , c in enumerate ( uni_str ) : <EOL> code = ord ( c ) <EOL> if CodeUtil . UNICHR_START <= code < <NUM_LIT:2> ** <NUM_LIT:16> : <EOL> ret [ i * <NUM_LIT:2> ] = int ( code / CodeUtil . byte_size ) <EOL> ret [ i * <NUM_LIT:2> + <NUM_LIT:1> ] = code % CodeUtil . byte_size <EOL> else : <EOL> ret [ i * <NUM_LIT:2> + <NUM_LIT:1> ] = CodeUtil . UNICHR_OOC <EOL> return np . array ( ret ) <EOL> @ staticmethod <EOL> def str_to_seq_vec ( uni_str ) : <EOL> ret = np . zeros ( ( len ( uni_str ) * <NUM_LIT:2> , CodeUtil . byte_size ) , dtype = '<STR_LIT>' ) <EOL> for i , c in enumerate ( uni_str ) : <EOL> code = ord ( c ) <EOL> if CodeUtil . UNICHR_START <= code < <NUM_LIT:2> ** <NUM_LIT:16> : <EOL> ret [ i * <NUM_LIT:2> , int ( code / CodeUtil . byte_size ) ] = <NUM_LIT:1> <EOL> ret [ i * <NUM_LIT:2> + <NUM_LIT:1> , code % CodeUtil . byte_size ] = <NUM_LIT:1> <EOL> else : <EOL> ret [ i * <NUM_LIT:2> + <NUM_LIT:1> , CodeUtil . UNICHR_OOC ] <EOL> return np . array ( ret ) <EOL> @ staticmethod <EOL> def seq_vec_to_str ( vec ) : <EOL> ret = [ ] <EOL> code = <NUM_LIT:0> <EOL> for i in range ( vec . shape [ <NUM_LIT:0> ] ) : <EOL> if i % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> code = ( vec [ i ] . argmax ( ) ) * CodeUtil . byte_size <EOL> else : <EOL> code += vec [ i ] . argmax ( ) <EOL> ret . append ( unichr ( code ) ) <EOL> code = <NUM_LIT:0> <EOL> return '<STR_LIT>' . join ( ret ) <EOL> @ staticmethod <EOL> def idx_to_seq_vec ( idx_mat , n_bytes ) : <EOL> x = idx_mat . flatten ( ) <EOL> idx_list = [ np . mod ( x , CodeUtil . byte_size ** ( i + <NUM_LIT:1> ) ) / CodeUtil . byte_size ** i for i in range ( n_bytes ) ] <EOL> idx_list = idx_list [ : : - <NUM_LIT:1> ] <EOL> n_hot_mat = np . zeros ( ( idx_mat . shape [ <NUM_LIT:0> ] * idx_mat . shape [ <NUM_LIT:1> ] * n_bytes , CodeUtil . byte_size ) , <EOL> dtype = '<STR_LIT>' ) <EOL> for i in range ( len ( idx_list ) ) : <EOL> for j in range ( len ( idx_list [ i ] ) ) : <EOL> n_hot_mat [ j * n_bytes + i , idx_list [ i ] [ j ] ] = <NUM_LIT:1> <EOL> return n_hot_mat . reshape ( idx_mat . shape [ <NUM_LIT:0> ] , idx_mat . shape [ <NUM_LIT:1> ] * n_bytes , CodeUtil . byte_size ) <EOL> @ staticmethod <EOL> def seq_vec_to_idx ( n_hot_mat , n_bytes ) : <EOL> cols = n_hot_mat . shape [ <NUM_LIT:1> ] / n_bytes <EOL> idx_mat = np . zeros ( ( n_hot_mat . shape [ <NUM_LIT:0> ] , cols ) , dtype = '<STR_LIT>' ) <EOL> for i in range ( cols ) : <EOL> for j in range ( n_bytes ) : <EOL> idx_mat [ : , i ] += n_hot_mat [ : , i * n_bytes + j , : ] . argmax ( axis = - <NUM_LIT:1> ) * <NUM_LIT> ** ( n_bytes - j - <NUM_LIT:1> ) <EOL> return idx_mat </s>
