<s> import os <EOL> import json <EOL> import theano <EOL> import theano . tensor as T <EOL> import numpy as np <EOL> def sharedX ( value , name = None , borrow = True , keep_on_cpu = False ) : <EOL> """<STR_LIT>""" <EOL> if keep_on_cpu : <EOL> return T . _shared ( theano . _asarray ( value , dtype = theano . config . floatX ) , <EOL> name = name , <EOL> borrow = borrow ) <EOL> return theano . shared ( theano . _asarray ( value , dtype = theano . config . floatX ) , <EOL> name = name , <EOL> borrow = borrow ) <EOL> class NumpyEncoder ( json . JSONEncoder ) : <EOL> def default ( self , obj ) : <EOL> if isinstance ( obj , np . ndarray ) : <EOL> return { "<STR_LIT>" : obj . tolist ( ) } <EOL> return json . JSONEncoder ( self , obj ) <EOL> def json_numpy_obj_hook ( dct ) : <EOL> if isinstance ( dct , dict ) and '<STR_LIT>' in dct : <EOL> return np . array ( dct [ '<STR_LIT>' ] ) <EOL> return dct <EOL> def save_dict_to_json_file ( path , dictionary ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , "<STR_LIT:w>" ) as json_file : <EOL> json_file . write ( json . dumps ( dictionary , indent = <NUM_LIT:4> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) , cls = NumpyEncoder ) ) <EOL> def load_dict_from_json_file ( path ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , "<STR_LIT:r>" ) as json_file : <EOL> return json . loads ( json_file . read ( ) , object_hook = json_numpy_obj_hook ) <EOL> def create_folder ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . makedirs ( path ) <EOL> except : <EOL> pass <EOL> return path <EOL> def split_dataset ( dataset , proportions ) : <EOL> """<STR_LIT>""" <EOL> from . interfaces . dataset import Dataset <EOL> indices = np . cumsum ( np . ceil ( np . array ( proportions ) / np . sum ( proportions ) * len ( dataset ) ) ) <EOL> indices [ - <NUM_LIT:1> ] = len ( dataset ) <EOL> dsets = [ ] <EOL> for s , f in zip ( [ <NUM_LIT:0> ] + list ( indices ) , indices ) : <EOL> s , f = int ( s ) , int ( f ) <EOL> covars = dataset . inputs . get_value ( ) [ s : f ] <EOL> targets = dataset . targets . get_value ( ) [ s : f ] if dataset . has_targets else None <EOL> dset = Dataset ( covars , targets , dataset . name + '<STR_LIT:_>' + str ( s ) + '<STR_LIT:to>' + str ( f ) ) <EOL> dset . symb_inputs = dataset . symb_inputs <EOL> dset . symb_targets = dataset . symb_targets <EOL> dsets . append ( dset ) <EOL> return dsets <EOL> def kfold ( dataset , k ) : <EOL> return split_dataset ( dataset , np . ones ( ( k , ) ) ) <EOL> def growing_sequential_kfold ( dataset , k ) : <EOL> """<STR_LIT>""" <EOL> if k < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> folds = [ ] <EOL> for i in range ( <NUM_LIT:1> , k + <NUM_LIT:1> ) : <EOL> proportions = [ i , <NUM_LIT:1> , <NUM_LIT:1> , k - i ] <EOL> folds . append ( split_dataset ( dataset , proportions ) [ : <NUM_LIT:3> ] ) <EOL> return folds </s>
