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


Python fluid.default_main_program方法代码示例

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


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

示例1: infer

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [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

示例2: _build_sync_target_network

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [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

示例3: __init__

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def __init__(self, model):
        super(StaticGraphAdapter, self).__init__()
        self.model = model
        # with `_build_once` gone, parameters are now created in `__init__`
        # so we need to keep track of the parameters already created
        self._startup_prog = fluid.default_startup_program()
        self._orig_prog = fluid.default_main_program()

        self._label_vars = {}  # label variables
        self._input_vars = {}  # label variables
        self._endpoints = {}
        self._loss_endpoint = None
        self._executor = None
        self._progs = {}
        self._compiled_progs = {}

        self._merge_count = {
            'eval_total': 0,
            'test_total': 0,
            'eval_batch': 0,
            'test_batch': 0
        }

        self._nranks = ParallelEnv().nranks
        self._local_rank = ParallelEnv().local_rank 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:27,代码来源:model.py

示例4: fit

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def fit(self, dynamic):
        fluid.enable_dygraph(self.device) if dynamic else None
        seed = 333
        fluid.default_startup_program().random_seed = seed
        fluid.default_main_program().random_seed = seed

        model = LeNet()
        optim_new = fluid.optimizer.Adam(
            learning_rate=0.001, parameter_list=model.parameters())
        model.prepare(
            optim_new,
            loss_function=CrossEntropy(average=False),
            metrics=Accuracy(),
            inputs=self.inputs,
            labels=self.labels)
        model.fit(self.train_dataset, batch_size=64, shuffle=False)

        result = model.evaluate(self.val_dataset, batch_size=64)
        np.testing.assert_allclose(result['acc'], self.acc1)
        fluid.disable_dygraph() if dynamic else None 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:22,代码来源:test_model.py

示例5: train

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def train(save_dirname):
    x = fluid.layers.data(name='x', shape=[13], dtype='float32')
    y = fluid.layers.data(name='y', shape=[1], dtype='float32')
    y_predict, avg_cost = net(x, y)
    sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
    sgd_optimizer.minimize(avg_cost)
    train_reader = paddle.batch(
        paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500),
        batch_size=20)
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)

    def train_loop(main_program):
        feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
        exe.run(fluid.default_startup_program())

        PASS_NUM = 1000
        for pass_id in range(PASS_NUM):
            total_loss_pass = 0
            for data in train_reader():
                avg_loss_value, = exe.run(
                    main_program, feed=feeder.feed(data), fetch_list=[avg_cost])
                total_loss_pass += avg_loss_value
                if avg_loss_value < 5.0:
                    if save_dirname is not None:
                        fluid.io.save_inference_model(
                            save_dirname, ['x'], [y_predict], exe)
                    return
            print("Pass %d, total avg cost = %f" % (pass_id, total_loss_pass))

    train_loop(fluid.default_main_program())


# Infer by using provided test data. 
开发者ID:yeyupiaoling,项目名称:LearnPaddle2,代码行数:36,代码来源:test_paddle.py

示例6: linear_warmup_decay

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def linear_warmup_decay(learning_rate, warmup_steps, num_train_steps):
    """ Applies linear warmup of learning rate from 0 and decay to 0."""
    with fluid.default_main_program()._lr_schedule_guard():
        lr = fluid.layers.tensor.create_global_var(
            shape=[1],
            value=0.0,
            dtype='float32',
            persistable=True,
            name="scheduled_learning_rate")

        global_step = fluid.layers.learning_rate_scheduler._decay_step_counter(
        )

        with fluid.layers.control_flow.Switch() as switch:
            with switch.case(global_step < warmup_steps):
                warmup_lr = learning_rate * (global_step / warmup_steps)
                fluid.layers.tensor.assign(warmup_lr, lr)
            with switch.default():
                decayed_lr = fluid.layers.learning_rate_scheduler.polynomial_decay(
                    learning_rate=learning_rate,
                    decay_steps=num_train_steps,
                    end_learning_rate=0.0,
                    power=1.0,
                    cycle=False)
                fluid.layers.tensor.assign(decayed_lr, lr)

        return lr 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:29,代码来源:static_optimization.py

示例7: setUpClass

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def setUpClass(cls):
        cls.device = set_device('gpu')
        fluid.enable_dygraph(cls.device)

        sp_num = 1280
        cls.train_dataset = MnistDataset(mode='train', sample_num=sp_num)
        cls.val_dataset = MnistDataset(mode='test', sample_num=sp_num)
        cls.test_dataset = MnistDataset(
            mode='test', return_label=False, sample_num=sp_num)

        cls.train_loader = fluid.io.DataLoader(
            cls.train_dataset, places=cls.device, batch_size=64)
        cls.val_loader = fluid.io.DataLoader(
            cls.val_dataset, places=cls.device, batch_size=64)
        cls.test_loader = fluid.io.DataLoader(
            cls.test_dataset, places=cls.device, batch_size=64)

        seed = 333
        fluid.default_startup_program().random_seed = seed
        fluid.default_main_program().random_seed = seed

        dy_lenet = LeNetDygraph()
        cls.init_param = dy_lenet.state_dict()
        dynamic_train(dy_lenet, cls.train_loader)

        cls.acc1 = dynamic_evaluate(dy_lenet, cls.val_loader)

        cls.inputs = [Input([-1, 1, 28, 28], 'float32', name='image')]
        cls.labels = [Input([None, 1], 'int64', name='label')]

        cls.save_dir = tempfile.mkdtemp()
        cls.weight_path = os.path.join(cls.save_dir, 'lenet')
        fluid.dygraph.save_dygraph(dy_lenet.state_dict(), cls.weight_path)

        fluid.disable_dygraph() 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:37,代码来源:test_model.py

示例8: set_seed

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def set_seed(self, seed=1024):
        fluid.default_startup_program().random_seed = seed
        fluid.default_main_program().random_seed = seed 
开发者ID:PaddlePaddle,项目名称:hapi,代码行数:5,代码来源:test_model.py

示例9: create_tmp_var

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def create_tmp_var(name, dtype, shape, program=None):
    """
        Create variable which is used to store the py_func result
    """
    if program is None:
        return fluid.default_main_program().current_block().create_var(
                name=fluid.unique_name.generate(name),
                dtype=dtype, shape=shape)
    else:
        return program.current_block().create_var(
                name=fluid.unique_name.generate(name),
                dtype=dtype, shape=shape) 
开发者ID:PaddlePaddle,项目名称:AutoDL,代码行数:14,代码来源:distribute_generator.py

示例10: save_program

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def save_program():
    dataset = fluid.DatasetFactory().create_dataset()
    dataset.set_use_var(sparse_input_ids + [label])
    pipe_command = "python criteo_dataset.py {}".format(sys.argv[1])
    dataset.set_pipe_command(pipe_command)
    dataset.set_batch_size(32)
    dataset.set_thread(10)
    optimizer = fluid.optimizer.SGD(0.0001)
    optimizer.minimize(avg_cost)
    exe = fluid.Executor(fluid.CPUPlace())

    input_folder = "hdfs:"
    output = sp.check_output( "hadoop fs -D hadoop.job.ugi=" + hdfs_ugi
                              + " -D fs.defaultFS=" + hdfs_address +" -ls " + os.path.join(dataset_prefix, current_date_hr) + "/ | awk '{if(NR>1) print $8}'", shell=True)
    train_filelist = ["{}{}".format(input_folder, f) for f in output.decode('ascii').strip().split('\n')]
    train_filelist.remove('hdfs:' + os.path.join(dataset_prefix, current_date_hr, 'donefile'))
    train_filelist = [train_filelist[0]] 
    print(train_filelist)

    exe.run(fluid.default_startup_program())
    print("startup save program done.")
    dataset.set_filelist(train_filelist)
    exe.train_from_dataset(
        program=fluid.default_main_program(),
        dataset=dataset,
        fetch_list=[auc_var],
        fetch_info=["auc"],
        debug=False,)
        #print_period=10000)
    # save model here
    fetch_list = fluid.io.save_inference_model(inference_path, [x.name for x in inference_feed_vars], [predict], exe) 
开发者ID:PaddlePaddle,项目名称:ElasticCTR,代码行数:33,代码来源:save_program.py

示例11: test

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def test(model, args, im_shape):

    test_py_reader, prob, acc_1, acc_5 = build_program(args, False, model,
                                                       im_shape)

    test_prog = fluid.default_main_program().clone(for_test=True)

    place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    # yapf: disable
    if args.pretrained_model:
        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model, var.name))
        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)

    # yapf: enable

    exec_strategy = fluid.ExecutionStrategy()
    exec_strategy.num_threads = 1
    compile_program = fluid.compiler.CompiledProgram(
        test_prog).with_data_parallel(exec_strategy=exec_strategy)
    test_reader = reader.test10(args)
    test_py_reader.decorate_paddle_reader(test_reader)

    test_fetch_list = [prob, acc_1, acc_5]
    prob = []
    top1 = utils.AvgrageMeter()
    top5 = utils.AvgrageMeter()
    test_py_reader.start()
    test_start_time = time.time()
    step_id = 0
    try:
        while True:
            prev_test_start_time = test_start_time
            test_start_time = time.time()
            prob_v, acc_1_v, acc_5_v = exe.run(compile_program,
                                               test_prog,
                                               fetch_list=test_fetch_list)
            prob.append(list(np.array(prob_v)))
            top1.update(np.array(acc_1_v), np.array(prob_v).shape[0])
            top5.update(np.array(acc_5_v), np.array(prob_v).shape[0])
            if step_id % args.report_freq == 0:
                print('prob shape:', np.array(prob_v).shape)
                print("Step {}, acc_1 {}, acc_5 {}, time {}".format(
                    step_id,
                    np.array(acc_1_v),
                    np.array(acc_5_v), test_start_time - prev_test_start_time))
            step_id += 1
    except fluid.core.EOFException:
        test_py_reader.reset()
    np.concatenate(prob).dump(args.dump_path)
    print("top1 {0}, top5 {1}".format(top1.avg, top5.avg)) 
开发者ID:PaddlePaddle,项目名称:AutoDL,代码行数:56,代码来源:test_mixup.py

示例12: pgd_loss

# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import default_main_program [as 别名]
def pgd_loss(ernie, labels, loss, task_fc_fn, epsilon=0.25):
    """ refer code from
    https://github.com/tensorflow/models/blob/master/research/adversarial_text/adversarial_losses.py#L145
    but we didn't use the vat loss for now
    """

    #TODO any difference with fleet_main_program or ParallelProgram or TrainProgram?
    program = fluid.default_main_program()

    param_grads = fluid.backward.append_backward(loss, parameter_list=[ernie._word_emb_name])

    # in the VAT paper code, the d is draw from a norm distribution, what's the advantage? why not use the
    # gradient of the emb directly?
    # d = fluid.layers.random_normal(shape=emb.shape)
    d = filter(lambda p: p[0].name == ernie._word_emb_name, param_grads)[0][1]
    emb = program.block(0).var(ernie._word_emb_name)

    #for _ in range(args.K_iteration):
    K_iteration = 8
    small_constant_for_finite_diff = 1e-5
    emb_hat = emb

    d = fluid.layers.gaussian_random(emb.shape)

    # it seems it can be implemented by the while loop
    for _ in range(K_iteration):
        #d = xi * utils_tf.l2_batch_normalize(d)
        d = scale_l2(d, small_constant_for_finite_diff)
        #logits_d = model.get_logits(x + d)
        #kl = utils_tf.kl_with_logits(logits, logits_d)

        emb_hat = emb_hat + d
        ernie._build_model(emb=emb_hat)
        graph_vars = task_fc_fn(ernie, labels)

        gradient = filter(lambda p: p[0].name == ernie._word_emb_name, param_grads)[0][1]
        gradient.stop_gradient = True
        d = gradient
        #Hd = tf.gradients(kl, d)[0]
        #d = tf.stop_gradient(Hd)

    d = scale_l2(d, small_constant_for_finite_diff)
    emb_hat = emb_hat + d
    ernie._build_model(emb=emb_hat)
    graph_vars = task_fc_fn(ernie, labels)

    return graph_vars['loss'] 
开发者ID:baidu,项目名称:Senta,代码行数:49,代码来源:glue_eval.py


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