<s> from __future__ import division , print_function , absolute_import <EOL> import six <EOL> import string <EOL> import random <EOL> try : <EOL> import h5py <EOL> H5PY_SUPPORTED = True <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" ) <EOL> H5PY_SUPPORTED = False <EOL> import numpy as np <EOL> import tensorflow as tf <EOL> import tflearn . variables as vs <EOL> def get_from_module ( identifier , module_params , module_name , instantiate = False , kwargs = None ) : <EOL> if isinstance ( identifier , six . string_types ) : <EOL> res = module_params . get ( identifier ) <EOL> if not res : <EOL> res = module_params . get ( identifier . lower ( ) ) <EOL> if not res : <EOL> raise Exception ( '<STR_LIT>' + str ( module_name ) + '<STR_LIT>' + str ( identifier ) ) <EOL> if instantiate and not kwargs : <EOL> return res ( ) <EOL> elif instantiate and kwargs : <EOL> return res ( ** kwargs ) <EOL> else : <EOL> return res <EOL> return identifier <EOL> def get_incoming_shape ( incoming ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( incoming , tf . Tensor ) : <EOL> return incoming . get_shape ( ) . as_list ( ) <EOL> elif type ( incoming ) in [ np . array , list , tuple ] : <EOL> return np . shape ( incoming ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def get_tensor_parents_placeholders ( tensor ) : <EOL> """<STR_LIT>""" <EOL> placeholders_list = [ ] <EOL> if tensor . op . type == '<STR_LIT>' : <EOL> placeholders_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . inputs : <EOL> if not '<STR_LIT>' in t . name : <EOL> placeholders_list += get_tensor_parents_placeholders ( t ) <EOL> return list ( set ( placeholders_list ) ) <EOL> def get_tensor_parents ( tensor ) : <EOL> """<STR_LIT>""" <EOL> parents_list = [ ] <EOL> parents_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . inputs : <EOL> if not '<STR_LIT>' in t . name : <EOL> parents_list += get_tensor_parents ( t ) <EOL> return parents_list <EOL> def get_all_tensor_parents ( tensor ) : <EOL> """<STR_LIT>""" <EOL> parents_list = [ ] <EOL> parents_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . inputs : <EOL> parents_list += get_tensor_parents ( t ) <EOL> return list ( set ( parents_list ) ) <EOL> def get_tensor_children_placeholders ( tensor ) : <EOL> """<STR_LIT>""" <EOL> placeholders_list = [ ] <EOL> if tensor . op . type == '<STR_LIT>' : <EOL> placeholders_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . outputs : <EOL> if not '<STR_LIT>' in t . name : <EOL> placeholders_list += get_tensor_children_placeholders ( t ) <EOL> return list ( set ( placeholders_list ) ) <EOL> def get_tensor_children ( tensor ) : <EOL> """<STR_LIT>""" <EOL> children_list = [ ] <EOL> children_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . outputs : <EOL> if not '<STR_LIT>' in t . name : <EOL> children_list += get_tensor_children ( t ) <EOL> return list ( set ( children_list ) ) <EOL> def get_all_tensor_children ( tensor ) : <EOL> """<STR_LIT>""" <EOL> children_list = [ ] <EOL> children_list . append ( tensor ) <EOL> if tensor . op : <EOL> for t in tensor . op . outputs : <EOL> children_list += get_all_tensor_children ( t ) <EOL> return list ( set ( children_list ) ) <EOL> def to_list ( data ) : <EOL> if data is None : return None <EOL> if type ( data ) in [ tuple , list ] : <EOL> return data <EOL> return [ data ] <EOL> def standarize_data ( data ) : <EOL> if data is None : return None <EOL> if type ( data ) in [ tuple , list ] : <EOL> return [ np . asarray ( x ) for x in data ] <EOL> if type ( data ) is dict : <EOL> return data <EOL> return [ np . asarray ( data ) ] <EOL> def standarize_dict ( d ) : <EOL> for key in d : <EOL> if isinstance ( d [ key ] , list ) : <EOL> d [ key ] = np . asarray ( d [ key ] ) <EOL> def del_duplicated ( l ) : <EOL> res = [ ] <EOL> for e in l : <EOL> if e not in res : <EOL> res . append ( e ) <EOL> return res <EOL> def make_batches ( samples_size , batch_size ) : <EOL> nb_batch = int ( np . ceil ( samples_size / float ( batch_size ) ) ) <EOL> return [ ( i * batch_size , min ( samples_size , ( i + <NUM_LIT:1> ) * batch_size ) ) for i in range ( <NUM_LIT:0> , nb_batch ) ] <EOL> def slice_array ( X , start = None , stop = None ) : <EOL> if type ( X ) == list : <EOL> if hasattr ( start , '<STR_LIT>' ) : <EOL> return [ x [ start ] for x in X ] <EOL> else : <EOL> return [ x [ start : stop ] for x in X ] <EOL> if H5PY_SUPPORTED : <EOL> if type ( X ) == h5py . Dataset : <EOL> return [ X [ i ] for i in start ] <EOL> if hasattr ( start , '<STR_LIT>' ) : <EOL> return X [ start ] <EOL> else : <EOL> return X [ start : stop ] <EOL> def get_dict_first_element ( input_dict ) : <EOL> for key in input_dict : <EOL> return input_dict [ key ] <EOL> def get_tensor_with_parent_name ( tensor ) : <EOL> """<STR_LIT>""" <EOL> tensor_name = tensor . name <EOL> if tensor . op . inputs [ <NUM_LIT:0> ] . name is not None : <EOL> return tensor . op . inputs [ <NUM_LIT:0> ] . name + "<STR_LIT:_>" + tensor_name <EOL> return tensor_name <EOL> def format_scope_name ( scope_name , prefix , suffix ) : <EOL> """<STR_LIT>""" <EOL> if prefix is not "<STR_LIT>" : <EOL> if not prefix [ - <NUM_LIT:1> ] == "<STR_LIT:/>" : <EOL> prefix += "<STR_LIT:/>" <EOL> if suffix is not "<STR_LIT>" : <EOL> if not suffix [ <NUM_LIT:0> ] == "<STR_LIT:/>" : <EOL> suffix = "<STR_LIT:/>" + suffix <EOL> return prefix + scope_name + suffix <EOL> def check_scope_path ( scope_name ) : <EOL> scope_name = scope_name . replace ( "<STR_LIT>" , "<STR_LIT:/>" ) <EOL> return scope_name <EOL> def feed_dict_builder ( X , Y , net_inputs , net_targets ) : <EOL> """<STR_LIT>""" <EOL> feed_dict = { } <EOL> if not ( is_none ( X ) or is_none ( net_inputs ) ) : <EOL> if not isinstance ( X , dict ) : <EOL> if isinstance ( X , float ) : X = [ X for _i in net_inputs ] <EOL> elif len ( net_inputs ) > <NUM_LIT:1> : <EOL> if np . ndim ( X ) < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif len ( X ) != len ( net_inputs ) : <EOL> raise Exception ( str ( len ( X ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" + str ( len ( net_inputs ) ) + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> X = [ X ] <EOL> for i , x in enumerate ( X ) : <EOL> feed_dict [ net_inputs [ i ] ] = x <EOL> else : <EOL> for key , val in X . items ( ) : <EOL> if isinstance ( key , tf . Tensor ) : <EOL> continue <EOL> else : <EOL> feed_dict [ vs . get_inputs_placeholder_by_name ( key ) ] = val <EOL> if not ( is_none ( Y ) or is_none ( net_targets ) ) : <EOL> if not isinstance ( Y , dict ) : <EOL> if len ( net_targets ) == <NUM_LIT:0> : <EOL> return feed_dict <EOL> if isinstance ( Y , float ) : <EOL> Y = [ Y for _t in net_targets ] <EOL> elif len ( net_targets ) > <NUM_LIT:1> : <EOL> if np . ndim ( Y ) < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> elif len ( Y ) != len ( net_targets ) : <EOL> raise Exception ( str ( len ( Y ) ) + "<STR_LIT>" <EOL> "<STR_LIT>" + str ( len ( net_targets ) ) ) <EOL> else : Y = [ Y ] <EOL> for i , y in enumerate ( Y ) : <EOL> feed_dict [ net_targets [ i ] ] = y <EOL> else : <EOL> for key , val in Y . items ( ) : <EOL> if isinstance ( key , tf . Tensor ) : <EOL> continue <EOL> else : <EOL> feed_dict [ vs . get_targets_placeholder_by_name ( key ) ] = val <EOL> return feed_dict <EOL> def is_none ( val ) : <EOL> if type ( val ) is np . array : <EOL> return False <EOL> else : <EOL> return val is None <EOL> def id_generator ( size = <NUM_LIT:6> , chars = string . ascii_uppercase + string . digits ) : <EOL> return '<STR_LIT>' . join ( random . choice ( chars ) for _ in range ( size ) ) <EOL> def check_dir_name ( dir_path ) : <EOL> if isinstance ( dir_path , str ) : <EOL> if len ( dir_path ) > <NUM_LIT:0> : <EOL> if dir_path [ - <NUM_LIT:1> ] != "<STR_LIT:/>" : <EOL> dir_path += "<STR_LIT:/>" <EOL> return dir_path <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def autoformat_kernel_2d ( strides ) : <EOL> if type ( strides ) is int : <EOL> return [ <NUM_LIT:1> , strides , strides , <NUM_LIT:1> ] <EOL> elif type ( strides ) in [ tuple , list ] : <EOL> if len ( strides ) == <NUM_LIT:2> : <EOL> return [ <NUM_LIT:1> , strides [ <NUM_LIT:0> ] , strides [ <NUM_LIT:1> ] , <NUM_LIT:1> ] <EOL> elif len ( strides ) == <NUM_LIT:4> : <EOL> return [ strides [ <NUM_LIT:0> ] , strides [ <NUM_LIT:1> ] , strides [ <NUM_LIT:2> ] , strides [ <NUM_LIT:3> ] ] <EOL> else : <EOL> if type ( strides ) in [ tuple , list ] : <EOL> raise Exception ( "<STR_LIT>" + str ( len ( strides ) ) <EOL> + "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" + str ( type ( strides ) ) ) <EOL> def autoformat_filter_conv2d ( fsize , in_depth , out_depth ) : <EOL> if type ( fsize ) is int : <EOL> return [ fsize , fsize , in_depth , out_depth ] <EOL> elif type ( fsize ) in [ tuple , list ] : <EOL> if len ( fsize ) == <NUM_LIT:2> : <EOL> return [ fsize [ <NUM_LIT:0> ] , fsize [ <NUM_LIT:1> ] , in_depth , out_depth ] <EOL> else : <EOL> if type ( fsize ) in [ tuple , list ] : <EOL> raise Exception ( "<STR_LIT>" + str ( len ( fsize ) ) <EOL> + "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" + str ( type ( fsize ) ) ) <EOL> def autoformat_padding ( padding ) : <EOL> if padding in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return str . upper ( padding ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) </s>
