Пример #1
0
tanh_input = np.array(tanh_input, dtype=np.float32)
sig_adjoint_label = np.array(sig_adjoint_label, dtype=np.float32)
tanh_adjoint_label = np.array(tanh_adjoint_label, dtype=np.float32)
adjoint_input = np.ones((len(sig_input), 1), dtype=np.float32)
# create db if needed
db_name = 'adjoint_pinn.db'
if os.path.isfile(db_name):
    os.remove(db_name)
write_db('minidb', db_name, [
    sig_input, tanh_input, adjoint_input, sig_adjoint_label, tanh_adjoint_label
])
(sig_input, tanh_input, adjoint_input, sig_adjoint_label,
 tanh_adjoint_label) = build_input_reader(
     model,
     db_name,
     'minidb', [
         'sig_input', 'tanh_input', 'adjoint_input', 'sig_adjoint_label',
         'tanh_adjoint_label'
     ],
     batch_size=100)
model.input_feature_schema.sig_input.set_value(sig_input.get(), unsafe=True)
model.input_feature_schema.tanh_input.set_value(tanh_input.get(), unsafe=True)
model.input_feature_schema.adjoint_input.set_value(adjoint_input.get(),
                                                   unsafe=True)
model.trainer_extra_schema.sig_loss_record.label.set_value(
    sig_adjoint_label.get(), unsafe=True)
model.trainer_extra_schema.tanh_loss_record.label.set_value(
    tanh_adjoint_label.get(), unsafe=True)
# Build model
(origin_pred, sig_adjoint_pred, tanh_adjoint_pred,
 loss) = build_adjoint_pinn(model,
                            sig_net_dim=[10, 1],
Пример #2
0
    def build_nets(
        self,
        hidden_sig_dims,
        hidden_tanh_dims,
        train_batch_size=1,
        eval_batch_size=1,
        weight_optim_method='AdaGrad',
        weight_optim_param={
            'alpha': 0.01,
            'epsilon': 1e-4
        },
        bias_optim_method='AdaGrad',
        bias_optim_param={
            'alpha': 0.01,
            'epsilon': 1e-4
        },
        loss_function='scaled_l1',
        max_loss_scale=1e6,
    ):
        assert len(self.input_data_store) > 0, 'Input data store is empty.'
        assert 'train' in self.input_data_store, 'Missing training data.'
        self.batch_size = train_batch_size
        # Build the date reader net for train net
        input_data_train = data_reader.build_input_reader(
            self.model,
            self.input_data_store['train'][0],
            'minidb',
            ['sig_input', 'tanh_input', 'label'],
            batch_size=train_batch_size,
            data_type='train',
        )

        if 'eval' in self.input_data_store:
            # Build the data reader net for eval net
            input_data_eval = data_reader.build_input_reader(
                self.model,
                self.input_data_store['eval'][0],
                'minidb',
                ['eval_sig_input', 'eval_tanh_input', 'eval_label'],
                batch_size=eval_batch_size,
                data_type='eval',
            )

        # Build the computational nets
        # Create train net
        self.model.input_feature_schema.sig_input.set_value(
            input_data_train[0].get(), unsafe=True)
        self.model.input_feature_schema.tanh_input.set_value(
            input_data_train[1].get(), unsafe=True)
        self.model.trainer_extra_schema.label.set_value(
            input_data_train[2].get(), unsafe=True)

        self.pred, self.loss = build_pinn(
            self.model,
            sig_net_dim=hidden_sig_dims,
            tanh_net_dim=hidden_tanh_dims,
            weight_optim=_build_optimizer(weight_optim_method,
                                          weight_optim_param),
            bias_optim=_build_optimizer(bias_optim_method, bias_optim_param),
            loss_function=loss_function,
            max_loss_scale=max_loss_scale)

        train_init_net, train_net = instantiator.generate_training_nets(
            self.model)
        workspace.RunNetOnce(train_init_net)
        workspace.CreateNet(train_net)
        self.net_store['train_net'] = train_net

        pred_net = instantiator.generate_predict_net(self.model)
        workspace.CreateNet(pred_net)
        self.net_store['pred_net'] = pred_net

        if 'eval' in self.input_data_store:
            # Create eval net
            self.model.input_feature_schema.sig_input.set_value(
                input_data_eval[0].get(), unsafe=True)
            self.model.input_feature_schema.tanh_input.set_value(
                input_data_eval[1].get(), unsafe=True)
            self.model.trainer_extra_schema.label.set_value(
                input_data_eval[2].get(), unsafe=True)
            eval_net = instantiator.generate_eval_net(self.model)
            workspace.CreateNet(eval_net)
            self.net_store['eval_net'] = eval_net
Пример #3
0
    def build_nets(
        self,
        hidden_sig_dims,
        hidden_tanh_dims,
        train_batch_size=1,
        eval_batch_size=1,
        weight_optim_method='AdaGrad',
        weight_optim_param={
            'alpha': 0.01,
            'epsilon': 1e-4
        },
        bias_optim_method='AdaGrad',
        bias_optim_param={
            'alpha': 0.01,
            'epsilon': 1e-4
        },
        loss_function='scaled_l1',
        max_loss_scale=1.,
    ):
        assert len(self.input_data_store) > 0, 'Input data store is empty.'
        assert 'train' in self.input_data_store, 'Missing training data.'
        self.batch_size = train_batch_size

        # Build the date reader net for train net
        if self.train_target == TrainTarget.ORIGIN:
            input_data_train = data_reader.build_input_reader(
                self.model,
                self.input_data_store['train'][0],
                'minidb',
                ['sig_input', 'tanh_input', 'label'],
                batch_size=train_batch_size,
                data_type='train',
            )
            if 'eval' in self.input_data_store:
                # Build the data reader net for eval net
                input_data_eval = data_reader.build_input_reader(
                    self.model,
                    self.input_data_store['eval'][0],
                    'minidb',
                    ['eval_sig_input', 'eval_tanh_input', 'eval_label'],
                    batch_size=eval_batch_size,
                    data_type='eval',
                )

            if self.net_builder == TrainTarget.ADJOINT:  # Use Adjoint net so output adjoint net
                # for training origin, use origin_loss_record
                self.model.trainer_extra_schema.origin_loss_record.label.set_value(
                    input_data_train[2].get(), unsafe=True)
            elif self.net_builder == TrainTarget.ORIGIN:
                self.model.trainer_extra_schema.label.set_value(
                    input_data_train[2].get(), unsafe=True)

        if self.train_target == TrainTarget.ADJOINT:
            raise Exception('Not Implemented')

        # Build the computational nets
        # Create train net
        self.model.input_feature_schema.sig_input.set_value(
            input_data_train[0].get(), unsafe=True)
        self.model.input_feature_schema.tanh_input.set_value(
            input_data_train[1].get(), unsafe=True)

        if self.net_builder == TrainTarget.ADJOINT:
            (self.pred, self.sig_adjoint_pred, self.tanh_adjoint_pred,
             self.loss) = build_adjoint_pinn(
                 self.model,
                 sig_input_dim=self.sig_input_dim,
                 tanh_input_dim=self.tanh_input_dim,
                 sig_net_dim=hidden_sig_dims,
                 tanh_net_dim=hidden_tanh_dims,
                 weight_optim=_build_optimizer(weight_optim_method,
                                               weight_optim_param),
                 bias_optim=_build_optimizer(bias_optim_method,
                                             bias_optim_param),
                 adjoint_tag=self.adjoint_tag,
                 train_target=self.train_target,
                 loss_function=loss_function,
                 max_loss_scale=max_loss_scale,
             )
        elif self.net_builder == TrainTarget.ORIGIN:
            self.pred, self.loss = build_pinn(
                self.model,
                sig_net_dim=hidden_sig_dims,
                tanh_net_dim=hidden_tanh_dims,
                weight_optim=_build_optimizer(weight_optim_method,
                                              weight_optim_param),
                bias_optim=_build_optimizer(bias_optim_method,
                                            bias_optim_param),
                loss_function=loss_function,
                max_loss_scale=max_loss_scale,
            )

        train_init_net, train_net = instantiator.generate_training_nets(
            self.model)
        workspace.RunNetOnce(train_init_net)
        workspace.CreateNet(train_net)
        self.net_store['train_net'] = train_net

        pred_net = instantiator.generate_predict_net(self.model)
        workspace.CreateNet(pred_net)
        self.net_store['pred_net'] = pred_net

        if 'eval' in self.input_data_store:
            # Create eval net
            self.model.input_feature_schema.sig_input.set_value(
                input_data_eval[0].get(), unsafe=True)
            self.model.input_feature_schema.tanh_input.set_value(
                input_data_eval[1].get(), unsafe=True)

            if self.train_target == TrainTarget.ORIGIN:
                if self.net_builder == TrainTarget.ADJOINT:
                    self.model.trainer_extra_schema.origin_loss_record.label.set_value(
                        input_data_eval[2].get(), unsafe=True)
                elif self.net_builder == TrainTarget.ORIGIN:
                    self.model.trainer_extra_schema.label.set_value(
                        input_data_eval[2].get(), unsafe=True)

            if self.train_target == TrainTarget.ADJOINT:
                raise Exception('Not Implemented')

            eval_net = instantiator.generate_eval_net(self.model)
            workspace.CreateNet(eval_net)
            self.net_store['eval_net'] = eval_net
Пример #4
0
origin_input = np.array([[x] for x in x_array], dtype = np.float32)
adjoint_label = np.array(
	[[(1 - np.exp(10*x)) / (1 + np.exp(10*x))] for x in x_array], 
	dtype = np.float32
)
adjoint_input = np.ones((100,1), dtype = np.float32)
# create db if needed
db_name = 'adjoint_nn.db'
if not os.path.isfile(db_name):
	print(">>> Create a new database...")
	write_db('minidb', db_name, 
		[origin_input, adjoint_input, adjoint_label])
else:
	print(">>> The database with the same name already existed.")
origin_input, adjoint_input, label = build_input_reader(
	model, db_name, 'minidb', ['origin_input', 'adjoint_input', 'label'], 
	batch_size=100
)
model.input_feature_schema.origin_input.set_value(
	origin_input.get(), unsafe=True)
model.input_feature_schema.adjoint_input.set_value(
	adjoint_input.get(), unsafe=True)
model.trainer_extra_schema.label.set_value(
	label.get(), unsafe=True)
# Build model
origin_pred, adjoint_pred, loss = build_adjoint_mlp(
	model, 
	input_dim=input_dim,
	hidden_dims=hidden_dims,
	output_dim=output_dim,
	optim=optimizer.AdagradOptimizer(alpha=0.01, epsilon=1e-4,))
Пример #5
0
    def build_nets(
            self,
            hidden_sig_dims,
            hidden_tanh_dims,
            train_batch_size=1,
            eval_batch_size=1,
            weight_optim_method='AdaGrad',
            weight_optim_param={
                'alpha': 0.01,
                'epsilon': 1e-4
            },
            bias_optim_method='AdaGrad',
            bias_optim_param={
                'alpha': 0.01,
                'epsilon': 1e-4
            },
            loss_function='scaled_l1',
            max_loss_scale=1.,  # used to scale up the loss signal for small input
            neg_grad_penalty=None,  # whether and how to apply neg_grad_penalty
            init_model=None,  # do postfix matching i.e. adjoint/<blob_nanme> == <blob_nanme>
    ):
        assert len(self.input_data_store) > 0, 'Input data store is empty.'
        assert 'train' in self.input_data_store, 'Missing training data.'
        assert (neg_grad_penalty is None or
                (neg_grad_penalty and self.train_target == TrainTarget.ORIGIN
                 and self.net_builder == TrainTarget.ADJOINT)
                ), '''When set neg_grad_penalty, train target should be ORIGIN,
            but net builder should be ADJOINT'''
        self.has_neg_grad_penalty = True if neg_grad_penalty else False
        self.batch_size = train_batch_size

        # Build the date reader net for train net
        if self.train_target == TrainTarget.ORIGIN:
            input_data_train = data_reader.build_input_reader(
                self.model,
                self.input_data_store['train'][0],
                'minidb',
                ['sig_input', 'tanh_input', 'label'],
                batch_size=train_batch_size,
                data_type='train',
            )
            if 'eval' in self.input_data_store:
                # Build the data reader net for eval net
                input_data_eval = data_reader.build_input_reader(
                    self.model,
                    self.input_data_store['eval'][0],
                    'minidb',
                    ['eval_sig_input', 'eval_tanh_input', 'eval_label'],
                    batch_size=eval_batch_size,
                    data_type='eval',
                )

            if self.net_builder == TrainTarget.ADJOINT:  # Use Adjoint net so output adjoint net
                # for training origin, use origin_loss_record
                self.model.trainer_extra_schema.origin_loss_record.label.set_value(
                    input_data_train[2].get(), unsafe=True)
            elif self.net_builder == TrainTarget.ORIGIN:
                self.model.trainer_extra_schema.label.set_value(
                    input_data_train[2].get(), unsafe=True)

        if self.train_target == TrainTarget.ADJOINT:
            raise Exception('Not Implemented')

        # Build the computational nets
        # Create train net
        self.model.input_feature_schema.sig_input.set_value(
            input_data_train[0].get(), unsafe=True)
        self.model.input_feature_schema.tanh_input.set_value(
            input_data_train[1].get(), unsafe=True)

        if self.net_builder == TrainTarget.ADJOINT:
            # decide adjoint tag
            adjoint_tag = 'no_tag'
            if self.train_target == TrainTarget.ORIGIN and neg_grad_penalty is None:
                adjoint_tag = Tags.PREDICTION_ONLY

            (self.pred, self.sig_adjoint_pred, self.tanh_adjoint_pred,
             self.loss) = build_adjoint_pinn(
                 self.model,
                 sig_input_dim=self.sig_input_dim,
                 tanh_input_dim=self.tanh_input_dim,
                 sig_net_dim=hidden_sig_dims,
                 tanh_net_dim=hidden_tanh_dims,
                 weight_optim=_build_optimizer(weight_optim_method,
                                               weight_optim_param),
                 bias_optim=_build_optimizer(bias_optim_method,
                                             bias_optim_param),
                 adjoint_tag=adjoint_tag,
                 train_target=self.train_target,
                 loss_function=loss_function,
                 max_loss_scale=max_loss_scale,
                 neg_grad_penalty=neg_grad_penalty,
             )
        elif self.net_builder == TrainTarget.ORIGIN:
            self.pred, self.loss = build_pinn(
                self.model,
                sig_net_dim=hidden_sig_dims,
                tanh_net_dim=hidden_tanh_dims,
                weight_optim=_build_optimizer(weight_optim_method,
                                              weight_optim_param),
                bias_optim=_build_optimizer(bias_optim_method,
                                            bias_optim_param),
                loss_function=loss_function,
                max_loss_scale=max_loss_scale,
            )

        train_init_net, train_net = instantiator.generate_training_nets(
            self.model)
        workspace.RunNetOnce(train_init_net)

        if init_model:
            model_name = init_model['name']
            print('[INFO] Init params from ' + model_name)
            given_init_net = exporter.load_init_net(model_name)
            if 'prefix' in init_model.keys():
                print('[INFO] Append ' + init_model['prefix'] +
                      ' to all blob names.')
                for op in given_init_net.op:
                    op.output[0] = init_model['prefix'] + op.output[0]
                workspace.RunNetOnce(given_init_net)

        workspace.CreateNet(train_net)
        self.net_store['train_net'] = train_net

        pred_net = instantiator.generate_predict_net(self.model)
        workspace.CreateNet(pred_net)
        self.net_store['pred_net'] = pred_net

        if 'eval' in self.input_data_store:
            # Create eval net
            self.model.input_feature_schema.sig_input.set_value(
                input_data_eval[0].get(), unsafe=True)
            self.model.input_feature_schema.tanh_input.set_value(
                input_data_eval[1].get(), unsafe=True)

            if self.train_target == TrainTarget.ORIGIN:
                if self.net_builder == TrainTarget.ADJOINT:
                    self.model.trainer_extra_schema.origin_loss_record.label.set_value(
                        input_data_eval[2].get(), unsafe=True)
                elif self.net_builder == TrainTarget.ORIGIN:
                    self.model.trainer_extra_schema.label.set_value(
                        input_data_eval[2].get(), unsafe=True)

            if self.train_target == TrainTarget.ADJOINT:
                raise Exception('Not Implemented')

            eval_net = instantiator.generate_eval_net(self.model)
            workspace.CreateNet(eval_net)
            self.net_store['eval_net'] = eval_net
Пример #6
0
    def build_nets(
        self,
        hidden_dims,
        batch_size=1,
        optim_method='AdaGrad',
        optim_param={
            'alpha': 0.01,
            'epsilon': 1e-4
        },
    ):
        assert len(self.input_data_store) > 0, 'Input data store is empty.'
        assert 'train' in self.input_data_store, 'Missing training data.'
        self.batch_size = batch_size
        # Build the date reader net for train net
        input_data_train = data_reader.build_input_reader(
            self.model,
            self.input_data_store['train'][0],
            'minidb',
            ['origin_input', 'adjoint_input', 'label'],
            batch_size=batch_size,
            data_type='train',
        )

        if 'eval' in self.input_data_store:
            # Build the data reader net for eval net
            input_data_eval = data_reader.build_input_reader(
                self.model,
                self.input_data_store['eval'][0],
                'minidb',
                ['origin_input', 'adjoint_input', 'label'],
                batch_size=batch_size,
                data_type='eval',
            )

        # Build the computational nets
        # Create train net
        self.model.input_feature_schema.origin_input.set_value(
            input_data_train[0].get(), unsafe=True)
        self.model.input_feature_schema.adjoint_input.set_value(
            input_data_train[1].get(), unsafe=True)
        self.model.trainer_extra_schema.label.set_value(
            input_data_train[2].get(), unsafe=True)

        self.origin_pred, self.adjoint_pred, self.loss = build_adjoint_mlp(
            self.model,
            input_dim=self.input_dim,
            hidden_dims=hidden_dims,
            output_dim=self.output_dim,
            optim=_build_optimizer(optim_method, optim_param),
        )

        train_init_net, train_net = instantiator.generate_training_nets(
            self.model)
        workspace.RunNetOnce(train_init_net)
        workspace.CreateNet(train_net)
        self.net_store['train_net'] = train_net

        pred_net = instantiator.generate_predict_net(self.model)
        workspace.CreateNet(pred_net)
        self.net_store['pred_net'] = pred_net

        if 'eval' in self.input_data_store:
            # Create eval net
            self.model.input_feature_schema.origin_input.set_value(
                input_data_eval[0].get(), unsafe=True)
            self.model.input_feature_schema.adjoint_input.set_value(
                input_data_eval[1].get(), unsafe=True)
            self.model.trainer_extra_schema.label.set_value(
                input_data_eval[2].get(), unsafe=True)
            eval_net = instantiator.generate_eval_net(self.model)
            workspace.CreateNet(eval_net)
            self.net_store['eval_net'] = eval_net