示例#1
0
    def _create_one_shooting_block(self,
                                   name,
                                   nr_of_channels,
                                   nr_of_particles,
                                   particle_size,
                                   particle_dimension,
                                   only_pass_through=False):
        if only_pass_through:
            shooting_model = self.shooting_model(
                in_features=nr_of_channels,
                nonlinearity=self.nonlinearity,
                nr_of_particles=nr_of_particles,
                is_pass_through=True,
                **self._shooting_model_kwargs)
        else:
            shooting_model = self.shooting_model(
                in_features=nr_of_channels,
                nonlinearity=self.nonlinearity,
                state_initializer=self._state_initializer,
                costate_initializer=self._costate_initializer,
                nr_of_particles=nr_of_particles,
                particle_size=particle_size,
                particle_dimension=particle_dimension,
                **self._shooting_model_kwargs)

        shooting_block = shooting_blocks.ShootingBlockBase(
            name=name,
            shooting_integrand=shooting_model,
            integrator=self._integrator)

        return shooting_block
示例#2
0
def setup_shooting_block(integrator=None,
                         in_features=20,
                         shooting_model='updown',
                         parameter_weight=1.0,
                         nr_of_particles=10,
                         inflation_factor=2,
                         nonlinearity='relu',
                         use_particle_rnn_mode=False,
                         use_particle_free_rnn_mode=False,
                         optimize_over_data_initial_conditions=False,
                         optimize_over_data_initial_conditions_type='linear'):

    if shooting_model == 'updown':
        smodel = shooting_models.AutoShootingIntegrandModelUpDown(
            in_features=in_features,
            nonlinearity=nonlinearity,
            parameter_weight=parameter_weight,
            inflation_factor=inflation_factor,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features,
            use_analytic_solution=True,
            use_rnn_mode=use_particle_rnn_mode,
            optimize_over_data_initial_conditions=
            optimize_over_data_initial_conditions,
            optimize_over_data_initial_conditions_type=
            optimize_over_data_initial_conditions_type)
    elif shooting_model == 'updown_universal':
        smodel = shooting_models.AutoShootingIntegrandModelUpDownUniversal(
            in_features=in_features,
            nonlinearity=nonlinearity,
            parameter_weight=parameter_weight,
            inflation_factor=inflation_factor,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features,
            use_analytic_solution=True,
            optional_weight=0.1,
            use_rnn_mode=use_particle_rnn_mode,
            optimize_over_data_initial_conditions=
            optimize_over_data_initial_conditions,
            optimize_over_data_initial_conditions_type=
            optimize_over_data_initial_conditions_type)
    elif shooting_model == 'periodic':
        smodel = shooting_models.AutoShootingIntegrandModelUpdownPeriodic(
            in_features=in_features,
            nonlinearity=nonlinearity,
            parameter_weight=parameter_weight,
            inflation_factor=inflation_factor,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features,
            use_analytic_solution=True,
            use_rnn_mode=use_particle_rnn_mode,
            optimize_over_data_initial_conditions=
            optimize_over_data_initial_conditions,
            optimize_over_data_initial_conditions_type=
            optimize_over_data_initial_conditions_type)
    elif shooting_model == 'simple':
        smodel = shooting_models.AutoShootingIntegrandModelSimple(
            in_features=in_features,
            nonlinearity=nonlinearity,
            parameter_weight=parameter_weight,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features,
            use_analytic_solution=True,
            use_rnn_mode=use_particle_rnn_mode)

    print('Using shooting model {}'.format(shooting_model))

    par_initializer = pi.VectorEvolutionParameterInitializer(
        only_random_initialization=True, random_initialization_magnitude=0.5)
    smodel.set_state_initializer(state_initializer=par_initializer)

    shooting_block = shooting_blocks.ShootingBlockBase(
        name='simple',
        shooting_integrand=smodel,
        use_particle_free_rnn_mode=use_particle_free_rnn_mode,
        integrator=integrator)

    return shooting_block
            inflation_factor=inflation_factor,
            use_rnn_mode=use_particle_rnn_mode)
    elif args.shooting_model == 'universal':
        smodel = smodels.AutoShootingIntegrandModelUniversal(
            **shootingintegrand_kwargs,
            use_analytic_solution=use_analytic_solution,
            inflation_factor=inflation_factor,
            use_rnn_mode=use_particle_rnn_mode)

    block_name = 'sblock'

    sblock = sblocks.ShootingBlockBase(
        name=block_name,
        shooting_integrand=smodel,
        integrator_name=args.method,
        use_particle_free_rnn_mode=use_particle_free_rnn_mode,
        use_particle_free_time_dependent_mode=args.
        use_particle_free_time_dependent_mode,
        nr_of_particle_free_time_dependent_steps=args.
        nr_of_particle_free_time_dependent_steps,
        integrator_options={'step_size': args.stepsize})

    use_shooting = False
    integrator = None
    func = None

    if args.use_rnn:
        weight_decay = 0.0001
        print('Using ResNetRNN: weight = {}'.format(weight_decay))
        simple_resnet = sdnn.ResNetRNN(nr_of_layers=args.nr_of_layers,
                                       inflation_factor=inflation_factor)
    elif args.use_double_resnet_rnn:
    elif current_model == 'DEBUG':
        shooting_model = shooting_models.DEBUGAutoShootingIntegrandModelSimple(
            in_features=in_features_size,
            nonlinearity=nonlinearity,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features_size,
            parameter_weight=parameter_weight)
    else:
        raise ValueError('Unknown model to check: {}'.format(current_model))

    use_analytic_solution = True

    shooting_block = shooting_blocks.ShootingBlockBase(
        name='test',
        shooting_integrand=shooting_model,
        integrator_options=integrator_options)
    shooting_model.use_analytic_solution = use_analytic_solution

    print('\n\nChecking model: {}'.format(current_model))
    print('-------------------------------------\n')

    # create some sample data
    sample_data = torch.randn([1, 1, in_features_size])

    # run through the shooting block once to get the necessary parameters
    shooting_block(x=sample_data)

    # create overall time-vector, for simplicity make sure it corresponds to the step-size
    t_np = np.array(range(0, 16)) * stepsize
    t = torch.from_numpy(t_np)
示例#5
0
            parameter_weight=parameter_weight)

    elif current_model == "general":
        shooting_model = shooting_models.AutoShootingIntegrandModelGeneralUpDown(
            in_features=in_features_size,
            nonlinearity=nonlinearity,
            nr_of_particles=nr_of_particles,
            particle_dimension=1,
            particle_size=in_features_size,
            parameter_weight=parameter_weight,
            inflation_factor=5)

    else:
        raise ValueError('Unknown model to check: {}'.format(current_model))

    shooting_block = shooting_blocks.ShootingBlockBase(
        name='test', shooting_integrand=shooting_model)

    print('\n\nChecking model: {}'.format(current_model))
    print('-------------------------------------\n')

    # create some sample data
    sample_data = torch.randn([100, 1, in_features_size])
    sample_data_init = torch.randn([100, 1, in_features_size])

    autodiff_gradient_results = dict()
    analytic_gradient_results = dict()

    for use_analytic_solution in (True, False):
        shooting_model.use_analytic_solution = use_analytic_solution

        # run through the shooting block once to get the necessary parameters