当前位置: 首页>>代码示例>>Python>>正文


Python fluid.program_guard方法代码示例

本文整理汇总了Python中paddle.fluid.program_guard方法的典型用法代码示例。如果您正苦于以下问题:Python fluid.program_guard方法的具体用法?Python fluid.program_guard怎么用?Python fluid.program_guard使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在paddle.fluid的用法示例。


在下文中一共展示了fluid.program_guard方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: build_program

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def build_program(main_prog, startup_prog, args, is_train, model, im_shape,
                  steps_one_epoch):
    out = []
    with fluid.program_guard(main_prog, startup_prog):
        py_reader = model.build_input(im_shape, is_train)
        if is_train:
            with fluid.unique_name.guard():
                loss = model.train_model(py_reader, args.init_channels,
                                         args.auxiliary, args.auxiliary_weight,
                                         args.lrc_loss_lambda)
                optimizer = fluid.optimizer.Momentum(
                    learning_rate=cosine_decay(args.learning_rate, args.epochs,
                                               steps_one_epoch),
                    regularization=fluid.regularizer.L2Decay(args.weight_decay),
                    momentum=args.momentum)
                optimizer.minimize(loss)
                out = [py_reader, loss]
        else:
            with fluid.unique_name.guard():
                prob, acc_1, acc_5 = model.test_model(py_reader,
                                                      args.init_channels)
                out = [py_reader, prob, acc_1, acc_5]
    return out 
开发者ID:PaddlePaddle,项目名称:AutoDL,代码行数:25,代码来源:train_mixup.py

示例2: infer

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def infer(self, main_program=None):
        if main_program is None:
            test_program = fluid.default_main_program().clone(for_test=True)
        else:
            test_program = main_program.clone(for_test=True)
        with fluid.program_guard(test_program):
            face_nmsed_out = fluid.layers.detection_output(
                self.face_mbox_loc,
                self.face_mbox_conf,
                self.prior_boxes,
                self.box_vars,
                nms_threshold=0.3,
                nms_top_k=5000,
                keep_top_k=750,
                score_threshold=0.01)
        return test_program, face_nmsed_out 
开发者ID:deepinsight,项目名称:insightface,代码行数:18,代码来源:pyramidbox.py

示例3: _build_sync_target_network

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def _build_sync_target_network():
    # 获取所有的参数
    vars = list(fluid.default_main_program().list_vars())
    # 把两个网络的参数分别过滤出来
    policy_vars = list(filter(lambda x: 'GRAD' not in x.name and 'policy' in x.name, vars))
    target_vars = list(filter(lambda x: 'GRAD' not in x.name and 'target' in x.name, vars))
    policy_vars.sort(key=lambda x: x.name)
    target_vars.sort(key=lambda x: x.name)

    # 从主程序中克隆一个程序用于更新参数
    sync_program = fluid.default_main_program().clone()
    with fluid.program_guard(sync_program):
        sync_ops = []
        for i, var in enumerate(policy_vars):
            sync_op = fluid.layers.assign(policy_vars[i], target_vars[i])
            sync_ops.append(sync_op)
    # 修剪第二个玩了个的参数,完成更新参数
    sync_program = sync_program._prune(sync_ops)
    return sync_program


# 定义输入数据 
开发者ID:yeyupiaoling,项目名称:LearnPaddle2,代码行数:24,代码来源:DQN.py

示例4: build_program

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def build_program(self, main_prog, startup_prog, is_train):
        """
            build_program
        """
        out = []
        with fluid.program_guard(main_prog, startup_prog):
            py_reader = self.build_input(self.im_shape, is_train)
            if is_train:
                with fluid.unique_name.guard():
                    loss, accuracy = self.fn_model(py_reader)
                    optimizer = fluid.optimizer.Momentum(
                        learning_rate=self.lr_strategy(),
                        momentum=0.9,
                        use_nesterov=True)
                    optimizer.minimize(loss)
                    out = [py_reader, loss, accuracy]
            else:
                with fluid.unique_name.guard():
                    loss, accuracy = self.fn_model(py_reader)
                    out = [py_reader, loss, accuracy]
        return out 
开发者ID:PaddlePaddle,项目名称:AutoDL,代码行数:23,代码来源:nn.py

示例5: init_train_net

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def init_train_net(self):
        """
        训练网络初始化,前向+后向
        :return:
        """
        with fluid.program_guard(self.train_program, self.startup_program):
            with fluid.unique_name.guard():
                self.data_set_reader.train_reader.create_reader()
                fields_dict = self.data_set_reader.train_reader.instance_fields_dict()
                self.forward_train_output = self.model_class.forward(fields_dict, phase=InstanceName.TRAINING)

                self.optimizer_output_dict = self.model_class.optimizer(self.forward_train_output[InstanceName.LOSS],
                                                                        self.is_fleet)
                if isinstance(self.optimizer_output_dict, dict):
                    if "use_ernie_opt" in self.optimizer_output_dict:
                        opt_args = self.optimizer_output_dict["opt_args"]
                        self.optimizer_output_dict = optimization(train_program=self.train_program,
                                                                  startup_prog=self.startup_program,
                                                                  **opt_args)
                else:
                    self.optimizer_output_dict = {}
                self.forward_train_output.update(self.optimizer_output_dict)
                self.fetch_list_train = list(self.forward_train_output.values())
                self.fetch_list_train_key = list(self.forward_train_output.keys()) 
开发者ID:baidu,项目名称:Senta,代码行数:26,代码来源:base_trainer.py

示例6: init_save_inference_net

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def init_save_inference_net(self):
        """初始化用来保存inference model的网络,只有前向,且是裁切过后的网络。
        :return:
        """
        if self.data_set_reader.predict_reader:
            with fluid.program_guard(self.save_inference_program, self.startup_program):
                with fluid.unique_name.guard():
                    self.data_set_reader.predict_reader.create_reader()
                    fields_dict = self.data_set_reader.predict_reader.instance_fields_dict()
                    forward_output_dict = self.model_class.forward(fields_dict, phase=InstanceName.SAVE_INFERENCE)
                    target_feed_list = forward_output_dict[InstanceName.TARGET_FEED_NAMES]
                    self.infer_dict = self.get_infer_data_meta(target_feed_list, fields_dict)
                    self.feed_target_names = target_feed_list

                    logging.info('...infer dict...')
                    logging.info(self.infer_dict)
                    logging.info('...feed target names...')
                    logging.info(self.feed_target_names)
                    self.inference_output = forward_output_dict[InstanceName.TARGET_PREDICTS]
            self.save_inference_program = self.save_inference_program.clone(for_test=True) 
开发者ID:baidu,项目名称:Senta,代码行数:22,代码来源:base_trainer.py

示例7: extract_weights

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def extract_weights(args):
    # add ERNIR to environment
    print('extract weights start'.center(60, '='))
    startup_prog = fluid.Program()
    test_prog = fluid.Program()
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(startup_prog)
    args.max_seq_len = 512
    args.use_fp16 = False
    args.num_labels = 2
    args.loss_scaling = 1.0
    ernie_config = ErnieConfig(args.ernie_config_path)
    ernie_config.print_config()
    with fluid.program_guard(test_prog, startup_prog):
        with fluid.unique_name.guard():
            create_model(
                args,
                pyreader_name='train',
                ernie_config=ernie_config)
    fluid.io.load_vars(exe, args.init_pretraining_params, main_program=test_prog, predicate=if_exist)
    state_dict = collections.OrderedDict()
    weight_map = build_weight_map()
    for ernie_name, pytorch_name in weight_map.items():
        fluid_tensor = fluid.global_scope().find_var(ernie_name).get_tensor()
        fluid_array = np.array(fluid_tensor, dtype=np.float32)
        if 'w_0' in ernie_name:
            fluid_array = fluid_array.transpose()
        state_dict[pytorch_name] = fluid_array
        print(f'{ernie_name} -> {pytorch_name} {fluid_array.shape}')
    print('extract weights done!'.center(60, '='))
    return state_dict 
开发者ID:nghuyong,项目名称:ERNIE-Pytorch,代码行数:34,代码来源:convert_ernie_to_pytorch.py

示例8: test_main

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def test_main(self):
        main_prog = fluid.Program()
        startup_prog = fluid.Program()
        with fluid.program_guard(main_prog, startup_prog):
            pred = fluid.data(name='pred', shape=[None, self.class_num], dtype='float32')
            label = fluid.data(name='label', shape=[None, 1], dtype='int64')
            acc = Accuracy(topk=self.topk, name=self.name)
            state = acc.add_metric_op(pred, label)

        exe = fluid.Executor(fluid.CPUPlace())
        compiled_main_prog = fluid.CompiledProgram(main_prog)

        for i in range(10):
            label, pred = self.random_pred_label()
            state_ret = exe.run(compiled_main_prog,
                                feed={'pred': pred, 'label': label},
                                fetch_list=[s.name for s in to_list(state)],
                                return_numpy=True)
            acc.update(*state_ret)
            res_m = acc.accumulate()
            res_f = accuracy(pred, label, self.topk)
            assert np.all(np.isclose(np.array(res_m), np.array(res_f), rtol=1e-3)), \
                    "Accuracy precision error: {} != {}".format(res_m, res_f)
            acc.reset()
            assert np.sum(acc.total) == 0
            assert np.sum(acc.count) == 0 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:28,代码来源:test_metrics.py

示例9: build_program

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def build_program(self):
        """
            build program
        """
        self.predict_program = fluid.Program()
        self.train_program = fluid.Program()
        with fluid.program_guard(self.predict_program):
            self.predict_inputs = layers.data(
                    name='input',
                    append_batch_size=False,
                    shape=[self.batch_size, 1],
                    dtype='int32')
            self.predict_tokens, self.predict_adjvec = self.alg.define_predict(
                    self.predict_inputs)

        with fluid.program_guard(self.train_program):
            self.train_inputs = layers.data(
                    name='input',
                    append_batch_size=False,
                    shape=[self.batch_size, 1],
                    dtype='int32')
            self.actions_to = layers.data(
                    name='actions_to',
                    append_batch_size=False,
                    shape=[self.batch_size,
                           self.num_nodes * 2],
                    dtype='int32')
            self.actions_ad = layers.data(
                    name='actions_ad',
                    append_batch_size=False,
                    shape=[self.batch_size,
                           self.num_nodes * (self.num_nodes - 1)],
                    dtype='int32')
            self.rewards = layers.data(
                name='rewards',
                append_batch_size=False,
                shape=[self.batch_size],
                dtype='float32')
            self.cost = self.alg.define_learn(
                    obs=self.train_inputs, reward=self.rewards,
                    action=[self.actions_to, self.actions_ad]) 
开发者ID:PaddlePaddle,项目名称:AutoDL,代码行数:43,代码来源:autodl_agent.py

示例10: init_evaluate_net

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def init_evaluate_net(self, reader, program):
        """初始化评估过程的网络,网络只有前向
        :return:
        """
        if reader:
            with fluid.program_guard(program, self.startup_program):
                with fluid.unique_name.guard():
                    reader.create_reader()
                    fields_dict = reader.instance_fields_dict()
                    self.forward_evaluate_output = self.model_class.forward(fields_dict, phase=InstanceName.EVALUATE)
                    self.fetch_list_evaluate = list(self.forward_evaluate_output.values())
                    self.fetch_list_evaluate_key = list(self.forward_evaluate_output.keys())
            program = program.clone(for_test=True)
        return program 
开发者ID:baidu,项目名称:Senta,代码行数:16,代码来源:base_trainer.py

示例11: evaluate

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def evaluate(logger, args):
    """evaluate a specific model using devset"""
    logger.info('Load data_set and vocab...')
    with open(os.path.join(args.vocab_dir, 'vocab.data'), 'rb') as fin:
        vocab = pickle.load(fin)
        logger.info('vocab size is {} and embed dim is {}'.format(vocab.size(
        ), vocab.embed_dim))
    brc_data = BRCDataset(
        args.max_p_num, args.max_p_len, args.max_q_len, dev_files=args.devset)
    logger.info('Converting text into ids...')
    brc_data.convert_to_ids(vocab)
    logger.info('Initialize the model...')

    # build model
    main_program = fluid.Program()
    startup_prog = fluid.Program()
    with fluid.program_guard(main_program, startup_prog):
        with fluid.unique_name.guard():
            avg_cost, s_probs, e_probs, match, feed_order = rc_model.rc_model(
                args.hidden_size, vocab, args)
            # initialize parameters
            if not args.use_gpu:
                place = fluid.CPUPlace()
                dev_count = int(
                    os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
            else:
                place = fluid.CUDAPlace(0)
                dev_count = fluid.core.get_cuda_device_count()

            exe = Executor(place)
            if args.load_dir:
                logger.info('load from {}'.format(args.load_dir))
                fluid.io.load_persistables(
                    exe, args.load_dir, main_program=main_program)
            else:
                logger.error('No model file to load ...')
                return

            inference_program = main_program.clone(for_test=True)
            eval_loss, bleu_rouge = validation(
                inference_program, avg_cost, s_probs, e_probs, match, feed_order,
                place, dev_count, vocab, brc_data, logger, args)
            logger.info('Dev eval result: {}'.format(bleu_rouge))
            logger.info('Predicted answers are saved to {}'.format(
                os.path.join(args.result_dir))) 
开发者ID:baidu,项目名称:DuReader,代码行数:47,代码来源:run.py

示例12: predict

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import program_guard [as 别名]
def predict(logger, args):
    """do inference on the test dataset """
    logger.info('Load data_set and vocab...')
    with open(os.path.join(args.vocab_dir, 'vocab.data'), 'rb') as fin:
        vocab = pickle.load(fin)
        logger.info('vocab size is {} and embed dim is {}'.format(vocab.size(
        ), vocab.embed_dim))
    brc_data = BRCDataset(
        args.max_p_num, args.max_p_len, args.max_q_len, dev_files=args.testset)
    logger.info('Converting text into ids...')
    brc_data.convert_to_ids(vocab)
    logger.info('Initialize the model...')

    # build model
    main_program = fluid.Program()
    startup_prog = fluid.Program()
    with fluid.program_guard(main_program, startup_prog):
        with fluid.unique_name.guard():
            avg_cost, s_probs, e_probs, match, feed_order = rc_model.rc_model(
                args.hidden_size, vocab, args)
            # initialize parameters
            if not args.use_gpu:
                place = fluid.CPUPlace()
                dev_count = int(
                    os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
            else:
                place = fluid.CUDAPlace(0)
                dev_count = fluid.core.get_cuda_device_count()

            exe = Executor(place)
            if args.load_dir:
                logger.info('load from {}'.format(args.load_dir))
                fluid.io.load_persistables(
                    exe, args.load_dir, main_program=main_program)
            else:
                logger.error('No model file to load ...')
                return

            inference_program = main_program.clone(for_test=True)
            eval_loss, bleu_rouge = validation(
                inference_program, avg_cost, s_probs, e_probs, match,
                feed_order, place, dev_count, vocab, brc_data, logger, args) 
开发者ID:baidu,项目名称:DuReader,代码行数:44,代码来源:run.py


注:本文中的paddle.fluid.program_guard方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。