private Model buildModel ( final MP params , Grid < MP > grid , int paramsIdx , String protoModelKey ) { // Make sure that the model is not yet built (can be case of duplicated hyper parameters). // We first look in the grid _models cache, then we look in the DKV. // FIXME: get checksum here since model builder will modify instance of params!!! final long checksum = params . checksum ( ) ; Key < Model > key = grid . getModelKey ( checksum ) ; if ( key != null ) { if ( DKV . get ( key ) == null ) { // We know about a model that's been removed; rebuild. Log . info ( "GridSearch.buildModel(): model with these parameters was built but removed, rebuilding; checksum: " + checksum ) ; } else { Log . info ( "GridSearch.buildModel(): model with these parameters already exists, skipping; checksum: " + checksum ) ; return key . get ( ) ; } } // Is there a model with the same params in the DKV? @ SuppressWarnings ( "unchecked" ) final Key < Model > [ ] modelKeys = KeySnapshot . globalSnapshot ( ) . filter ( new KeySnapshot . KVFilter ( ) { @ Override public boolean filter ( KeySnapshot . KeyInfo k ) { if ( ! Value . isSubclassOf ( k . _type , Model . class ) ) return false ; Model m = ( ( Model ) k . _key . get ( ) ) ; if ( ( m == null ) || ( m . _parms == null ) ) return false ; try { return m . _parms . checksum ( ) == checksum ; } catch ( H2OConcurrentModificationException e ) { // We are inspecting model parameters that doesn't belong to us - they might be modified (or deleted) while // checksum is being calculated: we skip them (see PUBDEV-5286) Log . warn ( "GridSearch encountered concurrent modification while searching DKV" , e ) ; return false ; } catch ( final RuntimeException e ) { Throwable ex = e ; boolean concurrentModification = false ; while ( ex . getCause ( ) != null ) { ex = ex . getCause ( ) ; if ( ex instanceof H2OConcurrentModificationException ) { concurrentModification = true ; break ; } } if ( ! concurrentModification ) throw e ; Log . warn ( "GridSearch encountered concurrent modification while searching DKV" , e ) ; return false ; } } } ) . keys ( ) ; if ( modelKeys . length > 0 ) { grid . putModel ( checksum , modelKeys [ 0 ] ) ; return modelKeys [ 0 ] . get ( ) ; } // Modify model key to have nice version with counter // Note: Cannot create it before checking the cache since checksum would differ for each model Key < Model > result = Key . make ( protoModelKey + paramsIdx ) ; // Build a new model // THIS IS BLOCKING call since we do not have enough information about free resources // FIXME: we should allow here any launching strategy (not only sequential) assert grid . getModel ( params ) == null ; Model m = ModelBuilder . trainModelNested ( _job , result , params , null ) ; grid . putModel ( checksum , result ) ; return m ; }
Build a model based on specified parameters and save it to resulting Grid object .