本文整理汇总了Python中paddle.fluid.Program方法的典型用法代码示例。如果您正苦于以下问题:Python fluid.Program方法的具体用法?Python fluid.Program怎么用?Python fluid.Program使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paddle.fluid
的用法示例。
在下文中一共展示了fluid.Program方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_weights
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [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
示例2: test_main
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [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
示例3: prepare_distributed_context
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [as 别名]
def prepare_distributed_context(place=None):
if place is None:
place = fluid.CUDAPlace(ParallelEnv().dev_id) if ParallelEnv().nranks > 1 \
else fluid.CUDAPlace(0)
strategy = ParallelStrategy()
strategy.nranks = ParallelEnv().nranks
strategy.local_rank = ParallelEnv().local_rank
strategy.trainer_endpoints = ParallelEnv().trainer_endpoints
strategy.current_endpoint = ParallelEnv().current_endpoint
if strategy.nranks < 2:
return
global _parallel_context_initialized
if not _parallel_context_initialized and isinstance(place,
fluid.CUDAPlace):
def _init_context():
communicator_prog = fluid.Program()
init_communicator(communicator_prog, strategy.local_rank,
strategy.nranks, True, strategy.current_endpoint,
strategy.trainer_endpoints)
exe = fluid.Executor(place)
exe.run(communicator_prog)
if fluid.in_dygraph_mode():
fluid.disable_dygraph()
_init_context()
fluid.enable_dygraph(place)
else:
_init_context()
else:
assert ("Only support CUDAPlace for now.")
_parallel_context_initialized = True
return strategy
示例4: build_program
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [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])
示例5: main
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [as 别名]
def main(_):
"""
main
"""
image_shape = [3, image_size, image_size]
files = os.listdir(FLAGS.data_path)
names = [each_item for each_item in files]
np.random.shuffle(names)
train_list = names[:9]
test_list = names[-1]
tokens, adjvec = utils.load_action(FLAGS.mid)
model = CIFARModel(tokens, adjvec, image_shape)
place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
startup = fluid.Program()
train_prog = fluid.Program()
test_prog = fluid.Program()
train_vars = model.build_program(train_prog, startup, True)
test_vars = model.build_program(test_prog, startup, False)
exe.run(startup)
train_accuracy, epoch_id = train(model, FLAGS.early_stop,
train_prog, train_vars, exe, train_list)
if epoch_id < FLAGS.early_stop:
utils.dump_reward(FLAGS.mid, train_accuracy)
else:
test_accuracy = test(model, test_prog, test_vars, exe, [test_list])
utils.dump_reward(FLAGS.mid, test_accuracy)
示例6: init_program
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [as 别名]
def init_program(self):
"""
:return:
"""
param_dict = OrderedDict()
self.startup_program = fluid.Program()
if self.random_seed is not None:
self.startup_program.random_seed = self.random_seed
self.train_program = fluid.Program()
self.test_program = fluid.Program()
self.evaluate_program = fluid.Program()
self.save_inference_program = fluid.Program()
示例7: append_cast_op
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [as 别名]
def append_cast_op(i, o, prog):
"""
Append a cast op in a given Program to cast input `i` to data type `o.dtype`.
Args:
i (Variable): The input Variable.
o (Variable): The output Variable.
prog (Program): The Program to append cast op.
"""
prog.global_block().append_op(
type="cast",
inputs={"X": i},
outputs={"Out": o},
attrs={"in_dtype": i.dtype,
"out_dtype": o.dtype})
示例8: evaluate
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [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)))
示例9: predict
# 需要导入模块: from paddle import fluid [as 别名]
# 或者: from paddle.fluid import Program [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)