<s> import numpy as np <EOL> from . base import BaseEnsemble <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class DynamicallyAveragedNetwork ( BaseEnsemble ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , networks ) : <EOL> super ( DynamicallyAveragedNetwork , self ) . __init__ ( networks ) <EOL> self . weights = None <EOL> for network in networks : <EOL> output_layer_size = network . output_layer . size <EOL> network . verbose = False <EOL> if output_layer_size != <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT:{}>" . format ( self . __class__ . __name__ , output_layer_size ) <EOL> ) <EOL> def train ( self , input_data , target_data , * args , ** kwargs ) : <EOL> for network in self . networks : <EOL> network . train ( input_data , target_data , * args , ** kwargs ) <EOL> def predict_raw ( self , input_data ) : <EOL> number_of_inputs = input_data . shape [ <NUM_LIT:0> ] <EOL> network_certainties = np . zeros ( ( number_of_inputs , len ( self . networks ) ) ) <EOL> network_outputs = network_certainties . copy ( ) <EOL> for i , network in enumerate ( self . networks ) : <EOL> output = network . predict ( input_data ) <EOL> minval , maxval = output . min ( ) , output . max ( ) <EOL> if not ( <NUM_LIT:0> <= minval <= <NUM_LIT:1> and <NUM_LIT:0> <= maxval <= <NUM_LIT:1> ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( minval , maxval ) <EOL> ) <EOL> certainty = np . where ( output > <NUM_LIT:0.5> , output , <NUM_LIT:1> - output ) <EOL> network_certainties [ : , i : i + <NUM_LIT:1> ] = certainty <EOL> network_outputs [ : , i : i + <NUM_LIT:1> ] = output <EOL> total_output_sum = np . reshape ( network_certainties . sum ( axis = <NUM_LIT:1> ) , <EOL> ( number_of_inputs , <NUM_LIT:1> ) ) <EOL> self . weights = network_certainties / total_output_sum <EOL> return ( self . weights * network_outputs ) . sum ( axis = <NUM_LIT:1> ) <EOL> def predict ( self , input_data ) : <EOL> raw_output = self . predict_raw ( input_data ) <EOL> return np . round ( raw_output ) </s>
