trainer_result_t batch_trainer_t::train( const task_t& task, const fold_t& fold, const loss_t& loss, size_t nthreads, const string_t& criterion, model_t& model) const { if (fold.second != protocol::train) { log_error() << "batch trainer: can only train models with training samples!"; return trainer_result_t(); } // initialize the model model.resize(task, true); model.random_params(); // prune training & validation data sampler_t tsampler(task); tsampler.setup(fold).setup(sampler_t::atype::annotated); sampler_t vsampler(task); tsampler.split(80, vsampler); if (tsampler.empty() || vsampler.empty()) { log_error() << "batch trainer: no annotated training samples!"; return trainer_result_t(); } // parameters const size_t iterations = math::clamp(text::from_params<size_t>(configuration(), "iters", 1024), 4, 4096); const scalar_t epsilon = math::clamp(text::from_params<scalar_t>(configuration(), "eps", 1e-4), 1e-8, 1e-3); const optim::batch_optimizer optimizer = text::from_string<optim::batch_optimizer> (text::from_params<string_t>(configuration(), "opt", "lbfgs")); // train the model const trainer_result_t result = ncv::batch_train( model, task, tsampler, vsampler, nthreads, loss, criterion, optimizer, iterations, epsilon); const trainer_state_t state = result.optimum_state(); log_info() << "optimum [train = " << state.m_tvalue << "/" << state.m_terror_avg << ", valid = " << state.m_vvalue << "/" << state.m_verror_avg << ", epoch = " << result.optimum_epoch() << ", config = " << text::concatenate(result.optimum_config(), "/") << "]."; // OK if (result.valid()) { model.load_params(result.optimum_params()); } return result; }
// advection void Fluid2::fluidAdvection( const float dt ) { // ink { Array2<float> inkcopy( ink ); CellSampler inksampler( grid, inkcopy ); const Index2& size = ink.getSize(); for( unsigned int i = 0; i < size.x; ++i ) for( unsigned int j = 0; j < size.y; ++j ) { const Index2 id( i, j ); const Vec2 pos( grid.getCellPos( id ) ); const Vec2 vel( ( velocityX[ id ] + velocityX[ Index2( i+1, j ) ] ) * 0.5f, ( velocityY[ id ] + velocityY[ Index2( i, j+1 ) ] ) * 0.5f ); const Vec2 endpos( pos - dt * vel ); ink[ id ] = inksampler.getValue( endpos );; } } // velocity { Array2< float > ucopy( velocityX ); Array2< float > vcopy( velocityY ); FaceSampler usampler( grid, ucopy, 0 ); FaceSampler vsampler( grid, vcopy, 1 ); const Index2& sizeu = velocityX.getSize(); const Index2& sizev = velocityY.getSize(); for( unsigned int i = 0; i < sizeu.x; ++i ) for( unsigned int j = 0; j < sizeu.y; ++j ) { const Index2 id( i, j ); const Index2 idv1( clamp( i-1, 0, sizev.x-1 ), clamp( j , 0, sizev.y-1 ) ); const Index2 idv2( clamp( i , 0, sizev.x-1 ), clamp( j , 0, sizev.y-1 ) ); const Index2 idv3( clamp( i-1, 0, sizev.x-1 ), clamp( j+1, 0, sizev.y-1 ) ); const Index2 idv4( clamp( i , 0, sizev.x-1 ), clamp( j+1, 0, sizev.y-1 ) ); const Vec2 pos( grid.getFaceXPos( id ) ); const Vec2 vel( ucopy[ id ], ( vcopy[ idv1 ] + vcopy[ idv2 ] + vcopy[ idv3 ] + vcopy[ idv4 ] ) * 0.25f ); const Vec2 endpos( pos - dt * vel ); velocityX[ id ] = usampler.getValue( endpos ); } for( unsigned int i = 0; i < sizev.x; ++i ) for( unsigned int j = 0; j < sizev.y; ++j ) { const Index2 id( i, j ); const Index2 idu1( clamp( i , 0, sizeu.x-1 ), clamp( j-1, 0, sizeu.y-1 ) ); const Index2 idu2( clamp( i , 0, sizeu.x-1 ), clamp( j , 0, sizeu.y-1 ) ); const Index2 idu3( clamp( i+1, 0, sizeu.x-1 ), clamp( j-1, 0, sizeu.y-1 ) ); const Index2 idu4( clamp( i+1, 0, sizeu.x-1 ), clamp( j , 0, sizeu.y-1 ) ); const Vec2 pos( grid.getFaceYPos( id ) ); const Vec2 vel( ( ucopy[ idu1 ] + ucopy[ idu2 ] + ucopy[ idu3 ] + ucopy[ idu4 ] ) * 0.25f, vcopy[ id ] ); const Vec2 endpos( pos - dt * vel ); velocityY[ id ] = vsampler.getValue( endpos ); } } }