未验证 提交 51d4afaa 编写于 作者: Y Yu Yang 提交者: GitHub

Rename program->main_program, init_program->startup_program (#5360)

上级 e745bcfc
...@@ -550,5 +550,5 @@ class Parameter(Variable): ...@@ -550,5 +550,5 @@ class Parameter(Variable):
# program is a global instance. # program is a global instance.
g_program = Program() g_main_program = Program()
g_init_program = Program() g_startup_program = Program()
import os import os
import cPickle as pickle import cPickle as pickle
from paddle.v2.framework.framework import Program, Parameter, g_program, \ from paddle.v2.framework.framework import Program, Parameter, g_main_program, \
Variable Variable
__all__ = [ __all__ = [
...@@ -29,13 +29,13 @@ def _clone_var_in_block_(block, var): ...@@ -29,13 +29,13 @@ def _clone_var_in_block_(block, var):
persistable=True) persistable=True)
def save_vars(executor, dirname, program=None, vars=None, predicate=None): def save_vars(executor, dirname, main_program=None, vars=None, predicate=None):
""" """
Save variables to directory by executor. Save variables to directory by executor.
:param executor: executor that save variable :param executor: executor that save variable
:param dirname: directory path :param dirname: directory path
:param program: program. If vars is None, then filter all variables in this :param main_program: program. If vars is None, then filter all variables in this
program which fit `predicate`. Default g_program. program which fit `predicate`. Default g_program.
:param predicate: The Predicate describes a callable that returns a variable :param predicate: The Predicate describes a callable that returns a variable
as a bool. If it returns true, the variables will be saved. as a bool. If it returns true, the variables will be saved.
...@@ -44,15 +44,15 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None): ...@@ -44,15 +44,15 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
:return: None :return: None
""" """
if vars is None: if vars is None:
if program is None: if main_program is None:
program = g_program main_program = g_main_program
if not isinstance(program, Program): if not isinstance(main_program, Program):
raise TypeError("program should be as Program type or None") raise TypeError("program should be as Program type or None")
save_vars( save_vars(
executor, executor,
dirname=dirname, dirname=dirname,
vars=filter(predicate, program.list_vars())) vars=filter(predicate, main_program.list_vars()))
else: else:
save_program = Program() save_program = Program()
save_block = save_program.global_block() save_block = save_program.global_block()
...@@ -66,37 +66,37 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None): ...@@ -66,37 +66,37 @@ def save_vars(executor, dirname, program=None, vars=None, predicate=None):
executor.run(save_program) executor.run(save_program)
def save_params(executor, dirname, program=None): def save_params(executor, dirname, main_program=None):
""" """
Save all parameters to directory with executor. Save all parameters to directory with executor.
""" """
save_vars( save_vars(
executor, executor,
dirname=dirname, dirname=dirname,
program=program, main_program=main_program,
vars=None, vars=None,
predicate=is_parameter) predicate=is_parameter)
def save_persistables(executor, dirname, program=None): def save_persistables(executor, dirname, main_program=None):
""" """
Save all persistables to directory with executor. Save all persistables to directory with executor.
""" """
save_vars( save_vars(
executor, executor,
dirname=dirname, dirname=dirname,
program=program, main_program=main_program,
vars=None, vars=None,
predicate=is_persistable) predicate=is_persistable)
def load_vars(executor, dirname, program=None, vars=None, predicate=None): def load_vars(executor, dirname, main_program=None, vars=None, predicate=None):
""" """
Load variables from directory by executor. Load variables from directory by executor.
:param executor: executor that save variable :param executor: executor that save variable
:param dirname: directory path :param dirname: directory path
:param program: program. If vars is None, then filter all variables in this :param main_program: program. If vars is None, then filter all variables in this
program which fit `predicate`. Default g_program. program which fit `predicate`. Default g_program.
:param predicate: The Predicate describes a callable that returns a variable :param predicate: The Predicate describes a callable that returns a variable
as a bool. If it returns true, the variables will be loaded. as a bool. If it returns true, the variables will be loaded.
...@@ -105,15 +105,15 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None): ...@@ -105,15 +105,15 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
:return: None :return: None
""" """
if vars is None: if vars is None:
if program is None: if main_program is None:
program = g_program main_program = g_main_program
if not isinstance(program, Program): if not isinstance(main_program, Program):
raise TypeError("program's type should be Program") raise TypeError("program's type should be Program")
load_vars( load_vars(
executor, executor,
dirname=dirname, dirname=dirname,
vars=filter(predicate, program.list_vars())) vars=filter(predicate, main_program.list_vars()))
else: else:
load_prog = Program() load_prog = Program()
load_block = load_prog.global_block() load_block = load_prog.global_block()
...@@ -129,27 +129,33 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None): ...@@ -129,27 +129,33 @@ def load_vars(executor, dirname, program=None, vars=None, predicate=None):
executor.run(load_prog) executor.run(load_prog)
def load_params(executor, dirname, program=None): def load_params(executor, dirname, main_program=None):
""" """
load all parameters from directory by executor. load all parameters from directory by executor.
""" """
load_vars( load_vars(
executor, dirname=dirname, program=program, predicate=is_parameter) executor,
dirname=dirname,
main_program=main_program,
predicate=is_parameter)
def load_persistables(executor, dirname, program=None): def load_persistables(executor, dirname, main_program=None):
""" """
load all persistables from directory by executor. load all persistables from directory by executor.
""" """
load_vars( load_vars(
executor, dirname=dirname, program=program, predicate=is_persistable) executor,
dirname=dirname,
main_program=main_program,
predicate=is_persistable)
def save_inference_model(dirname, def save_inference_model(dirname,
feeded_var_names, feeded_var_names,
target_vars, target_vars,
executor, executor,
program=None): main_program=None):
""" """
Build a model especially for inference, Build a model especially for inference,
and save it to directory by the executor. and save it to directory by the executor.
...@@ -158,20 +164,20 @@ def save_inference_model(dirname, ...@@ -158,20 +164,20 @@ def save_inference_model(dirname,
:param feeded_var_names: Names of variables that need to be feeded data during inference :param feeded_var_names: Names of variables that need to be feeded data during inference
:param target_vars: Variables from which we can get inference results. :param target_vars: Variables from which we can get inference results.
:param executor: executor that save inference model :param executor: executor that save inference model
:param program: original program, which will be pruned to build the inference model. :param main_program: original program, which will be pruned to build the inference model.
Default g_program. Default g_program.
:return: None :return: None
""" """
if program is None: if main_program is None:
program = g_program main_program = g_main_program
if not isinstance(target_vars, list): if not isinstance(target_vars, list):
target_vars = [target_vars] target_vars = [target_vars]
if not os.path.isdir(dirname): if not os.path.isdir(dirname):
os.makedirs(dirname) os.makedirs(dirname)
pruned_program = program.prune(target_vars) pruned_program = main_program.prune(target_vars)
fetch_var_names = [v.name for v in target_vars] fetch_var_names = [v.name for v in target_vars]
model_file_name = dirname + "/__model__" model_file_name = dirname + "/__model__"
...@@ -182,10 +188,10 @@ def save_inference_model(dirname, ...@@ -182,10 +188,10 @@ def save_inference_model(dirname,
"fetch_var_names": fetch_var_names "fetch_var_names": fetch_var_names
}, f, -1) }, f, -1)
save_params(executor, dirname, program) save_params(executor, dirname, main_program)
def load_persistables_if_exist(executor, dirname, program=None): def load_persistables_if_exist(executor, dirname, main_program=None):
filenames = next(os.walk(dirname))[2] filenames = next(os.walk(dirname))[2]
filenames = set(filenames) filenames = set(filenames)
...@@ -198,7 +204,7 @@ def load_persistables_if_exist(executor, dirname, program=None): ...@@ -198,7 +204,7 @@ def load_persistables_if_exist(executor, dirname, program=None):
load_vars( load_vars(
executor, executor,
dirname, dirname,
program=program, main_program=main_program,
vars=None, vars=None,
predicate=_is_presistable_and_exist_) predicate=_is_presistable_and_exist_)
......
import copy import copy
import itertools import itertools
from paddle.v2.framework.framework import Variable, g_program, \ from paddle.v2.framework.framework import Variable, g_main_program, \
g_init_program, unique_name, Program g_startup_program, unique_name, Program
from paddle.v2.framework.initializer import ConstantInitializer, \ from paddle.v2.framework.initializer import ConstantInitializer, \
UniformInitializer UniformInitializer
...@@ -20,23 +20,23 @@ class LayerHelper(object): ...@@ -20,23 +20,23 @@ class LayerHelper(object):
return self.kwargs['name'] return self.kwargs['name']
@property @property
def program(self): def main_program(self):
prog = self.kwargs.get('program', None) prog = self.kwargs.get('main_program', None)
if prog is None: if prog is None:
return g_program return g_main_program
else: else:
return prog return prog
@property @property
def init_program(self): def startup_program(self):
prog = self.kwargs.get('init_program', None) prog = self.kwargs.get('startup_program', None)
if prog is None: if prog is None:
return g_init_program return g_startup_program
else: else:
return prog return prog
def append_op(self, *args, **kwargs): def append_op(self, *args, **kwargs):
return self.program.current_block().append_op(*args, **kwargs) return self.main_program.current_block().append_op(*args, **kwargs)
def multiple_input(self, input_param_name='input'): def multiple_input(self, input_param_name='input'):
inputs = self.kwargs.get(input_param_name, []) inputs = self.kwargs.get(input_param_name, [])
...@@ -120,27 +120,27 @@ class LayerHelper(object): ...@@ -120,27 +120,27 @@ class LayerHelper(object):
attr_copy['initializer'] = initializer attr_copy['initializer'] = initializer
if attr_copy['name'] is None: if attr_copy['name'] is None:
attr_copy['name'] = unique_name(".".join([self.name, suffix])) attr_copy['name'] = unique_name(".".join([self.name, suffix]))
self.init_program.global_block().create_parameter( self.startup_program.global_block().create_parameter(
dtype=dtype, shape=shape, **attr_copy) dtype=dtype, shape=shape, **attr_copy)
return self.program.global_block().create_parameter( return self.main_program.global_block().create_parameter(
name=attr_copy['name'], dtype=dtype, shape=shape) name=attr_copy['name'], dtype=dtype, shape=shape)
def create_tmp_variable(self, dtype): def create_tmp_variable(self, dtype):
return self.program.current_block().create_var( return self.main_program.current_block().create_var(
name=unique_name(".".join([self.name, 'tmp'])), name=unique_name(".".join([self.name, 'tmp'])),
dtype=dtype, dtype=dtype,
persistable=False) persistable=False)
def create_variable(self, *args, **kwargs): def create_variable(self, *args, **kwargs):
return self.program.current_block().create_var(*args, **kwargs) return self.main_program.current_block().create_var(*args, **kwargs)
def create_global_variable(self, persistable=False, *args, **kwargs): def create_global_variable(self, persistable=False, *args, **kwargs):
return self.program.global_block().create_var( return self.main_program.global_block().create_var(
*args, persistable=persistable, **kwargs) *args, persistable=persistable, **kwargs)
def set_variable_initializer(self, var, initializer): def set_variable_initializer(self, var, initializer):
assert isinstance(var, Variable) assert isinstance(var, Variable)
self.init_program.global_block().create_var( self.startup_program.global_block().create_var(
name=var.name, name=var.name,
type=var.type, type=var.type,
dtype=var.data_type, dtype=var.data_type,
......
...@@ -18,8 +18,8 @@ def fc(input, ...@@ -18,8 +18,8 @@ def fc(input,
name=None, name=None,
act=None, act=None,
num_flatten_dims=1, num_flatten_dims=1,
program=None, main_program=None,
init_program=None): startup_program=None):
# create helper # create helper
helper = LayerHelper('fc', **locals()) helper = LayerHelper('fc', **locals())
...@@ -64,8 +64,8 @@ def embedding(input, ...@@ -64,8 +64,8 @@ def embedding(input,
data_type='float32', data_type='float32',
is_sparse=False, is_sparse=False,
param_attr=None, param_attr=None,
program=None, main_program=None,
init_program=None): startup_program=None):
helper = LayerHelper('embedding', **locals()) helper = LayerHelper('embedding', **locals())
w = helper.create_parameter( w = helper.create_parameter(
attr=helper.param_attr, shape=size, dtype=data_type) attr=helper.param_attr, shape=size, dtype=data_type)
...@@ -84,8 +84,8 @@ def data(name, ...@@ -84,8 +84,8 @@ def data(name,
data_type='float32', data_type='float32',
type=core.VarDesc.VarType.LOD_TENSOR, type=core.VarDesc.VarType.LOD_TENSOR,
append_batch_size=True, append_batch_size=True,
program=None, main_program=None,
init_program=None): startup_program=None):
helper = LayerHelper('data', **locals()) helper = LayerHelper('data', **locals())
shape = list(shape) shape = list(shape)
for i in xrange(len(shape)): for i in xrange(len(shape)):
...@@ -178,7 +178,7 @@ _create_op_func_('sigmoid') ...@@ -178,7 +178,7 @@ _create_op_func_('sigmoid')
_create_op_func_('scale') _create_op_func_('scale')
def cast(x, data_type, program=None): def cast(x, data_type, main_program=None):
helper = LayerHelper('cast', **locals()) helper = LayerHelper('cast', **locals())
out = helper.create_tmp_variable(dtype=data_type) out = helper.create_tmp_variable(dtype=data_type)
helper.append_op( helper.append_op(
...@@ -190,7 +190,7 @@ def cast(x, data_type, program=None): ...@@ -190,7 +190,7 @@ def cast(x, data_type, program=None):
return out return out
def concat(input, axis, program=None, init_program=None): def concat(input, axis, main_program=None, startup_program=None):
helper = LayerHelper('concat', **locals()) helper = LayerHelper('concat', **locals())
out = helper.create_tmp_variable(dtype=helper.input_dtype()) out = helper.create_tmp_variable(dtype=helper.input_dtype())
helper.append_op( helper.append_op(
...@@ -201,7 +201,7 @@ def concat(input, axis, program=None, init_program=None): ...@@ -201,7 +201,7 @@ def concat(input, axis, program=None, init_program=None):
return out return out
def sums(input, program=None, init_program=None): def sums(input, main_program=None, startup_program=None):
helper = LayerHelper('sum', **locals()) helper = LayerHelper('sum', **locals())
out = helper.create_tmp_variable(dtype=helper.input_dtype()) out = helper.create_tmp_variable(dtype=helper.input_dtype())
helper.append_op(type='sum', inputs={'X': input}, outputs={'Out': out}) helper.append_op(type='sum', inputs={'X': input}, outputs={'Out': out})
...@@ -281,8 +281,8 @@ def sequence_conv(input, ...@@ -281,8 +281,8 @@ def sequence_conv(input,
padding=None, padding=None,
bias_attr=None, bias_attr=None,
param_attr=None, param_attr=None,
program=None, main_program=None,
init_program=None): startup_program=None):
# FIXME(dzh) : want to unify the argument of python layer # FIXME(dzh) : want to unify the argument of python layer
# function. So we ignore some unecessary attributes. # function. So we ignore some unecessary attributes.
# such as, padding_trainable, context_start. # such as, padding_trainable, context_start.
...@@ -321,8 +321,8 @@ def conv2d(input, ...@@ -321,8 +321,8 @@ def conv2d(input,
padding=None, padding=None,
bias_attr=None, bias_attr=None,
param_attr=None, param_attr=None,
program=None, main_program=None,
init_program=None): startup_program=None):
helper = LayerHelper('conv2d', **locals()) helper = LayerHelper('conv2d', **locals())
dtype = helper.input_dtype() dtype = helper.input_dtype()
...@@ -388,8 +388,8 @@ def pool2d(input, ...@@ -388,8 +388,8 @@ def pool2d(input,
pool_stride=[1, 1], pool_stride=[1, 1],
pool_padding=[0, 0], pool_padding=[0, 0],
global_pooling=False, global_pooling=False,
program=None, main_program=None,
init_program=None): startup_program=None):
if pool_type not in ["max", "avg"]: if pool_type not in ["max", "avg"]:
raise ValueError( raise ValueError(
"Unknown pool_type: '%s'. It can only be 'max' or 'avg'.", "Unknown pool_type: '%s'. It can only be 'max' or 'avg'.",
...@@ -428,8 +428,8 @@ def batch_norm(input, ...@@ -428,8 +428,8 @@ def batch_norm(input,
param_attr=None, param_attr=None,
bias_attr=None, bias_attr=None,
data_layout='NCHW', data_layout='NCHW',
program=None, main_program=None,
init_program=None): startup_program=None):
helper = LayerHelper('batch_norm', **locals()) helper = LayerHelper('batch_norm', **locals())
dtype = helper.input_dtype() dtype = helper.input_dtype()
...@@ -505,16 +505,16 @@ class BlockGuard(object): ...@@ -505,16 +505,16 @@ class BlockGuard(object):
keyword. keyword.
""" """
def __init__(self, program): def __init__(self, main_program):
if not isinstance(program, Program): if not isinstance(main_program, Program):
raise TypeError("BlockGuard takes a program") raise TypeError("BlockGuard takes a program")
self.program = program self.main_program = main_program
def __enter__(self): def __enter__(self):
self.program.create_block() self.main_program.create_block()
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
self.program.rollback() self.main_program.rollback()
if exc_type is not None: if exc_type is not None:
return False # re-raise exception return False # re-raise exception
return True return True
...@@ -524,7 +524,7 @@ class StaticRNNGuard(BlockGuard): ...@@ -524,7 +524,7 @@ class StaticRNNGuard(BlockGuard):
def __init__(self, rnn): def __init__(self, rnn):
if not isinstance(rnn, StaticRNN): if not isinstance(rnn, StaticRNN):
raise TypeError("StaticRNNGuard takes an StaticRNN") raise TypeError("StaticRNNGuard takes an StaticRNN")
super(StaticRNNGuard, self).__init__(rnn.helper.program) super(StaticRNNGuard, self).__init__(rnn.helper.main_program)
self.rnn = rnn self.rnn = rnn
def __enter__(self): def __enter__(self):
...@@ -560,8 +560,9 @@ class StaticRNN(object): ...@@ -560,8 +560,9 @@ class StaticRNN(object):
IN_RNN_BLOCK = 1 IN_RNN_BLOCK = 1
AFTER_RNN_BLOCK = 2 AFTER_RNN_BLOCK = 2
def __init__(self, name=None, program=None): def __init__(self, name=None, main_program=None):
self.helper = LayerHelper("static_rnn", name=name, program=program) self.helper = LayerHelper(
"static_rnn", name=name, main_program=main_program)
self.memories = {} # memory map, from pre_mem.name --> MemoryLink self.memories = {} # memory map, from pre_mem.name --> MemoryLink
self.inputs = [] # input variable list in current block self.inputs = [] # input variable list in current block
self.outputs = [] # output variable list in parent block self.outputs = [] # output variable list in parent block
...@@ -653,7 +654,7 @@ class StaticRNN(object): ...@@ -653,7 +654,7 @@ class StaticRNN(object):
self.memories[mem.name].mem = var self.memories[mem.name].mem = var
def parent_block(self): def parent_block(self):
prog = self.helper.program prog = self.helper.main_program
parent_idx = prog.current_block().parent_idx parent_idx = prog.current_block().parent_idx
assert parent_idx >= 0 assert parent_idx >= 0
parent_block = prog.block(parent_idx) parent_block = prog.block(parent_idx)
...@@ -670,8 +671,8 @@ class StaticRNN(object): ...@@ -670,8 +671,8 @@ class StaticRNN(object):
return self.outputs return self.outputs
def complete_rnn_op(self): def complete_rnn_op(self):
program = self.helper.program main_program = self.helper.main_program
rnn_block = program.current_block() rnn_block = main_program.current_block()
parent_block = self.parent_block() parent_block = self.parent_block()
local_inputs = set() local_inputs = set()
...@@ -737,7 +738,7 @@ class StaticRNN(object): ...@@ -737,7 +738,7 @@ class StaticRNN(object):
}) })
def lod_rank_table(x, level=0, program=None): def lod_rank_table(x, level=0, main_program=None):
helper = LayerHelper("lod_rank_table", **locals()) helper = LayerHelper("lod_rank_table", **locals())
table = helper.create_variable( table = helper.create_variable(
type=core.VarDesc.VarType.LOD_RANK_TABLE, type=core.VarDesc.VarType.LOD_RANK_TABLE,
......
...@@ -80,7 +80,7 @@ def parse_graph(program, graph, var_dict, **kwargs): ...@@ -80,7 +80,7 @@ def parse_graph(program, graph, var_dict, **kwargs):
graph.edge(**draw_edge(var_dict, op, e, arg)) graph.edge(**draw_edge(var_dict, op, e, arg))
def draw_graph(init_program, program, **kwargs): def draw_graph(startup_program, main_program, **kwargs):
if kwargs.has_key("graph_attr"): if kwargs.has_key("graph_attr"):
GRAPH_STYLE.update(kwargs[graph_attr]) GRAPH_STYLE.update(kwargs[graph_attr])
if kwargs.has_key("node_attr"): if kwargs.has_key("node_attr"):
...@@ -101,8 +101,8 @@ def draw_graph(init_program, program, **kwargs): ...@@ -101,8 +101,8 @@ def draw_graph(init_program, program, **kwargs):
**kwargs) **kwargs)
var_dict = {} var_dict = {}
parse_graph(init_program, g, var_dict) parse_graph(startup_program, g, var_dict)
parse_graph(program, g, var_dict) parse_graph(main_program, g, var_dict)
if filename != None: if filename != None:
g.save() g.save()
......
...@@ -10,23 +10,23 @@ def simple_img_conv_pool(input, ...@@ -10,23 +10,23 @@ def simple_img_conv_pool(input,
pool_stride, pool_stride,
act, act,
pool_type='max', pool_type='max',
program=None, main_program=None,
init_program=None): startup_program=None):
conv_out = layers.conv2d( conv_out = layers.conv2d(
input=input, input=input,
num_filters=num_filters, num_filters=num_filters,
filter_size=filter_size, filter_size=filter_size,
act=act, act=act,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
pool_out = layers.pool2d( pool_out = layers.pool2d(
input=conv_out, input=conv_out,
pool_size=pool_size, pool_size=pool_size,
pool_type=pool_type, pool_type=pool_type,
pool_stride=pool_stride, pool_stride=pool_stride,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return pool_out return pool_out
...@@ -40,8 +40,8 @@ def img_conv_group(input, ...@@ -40,8 +40,8 @@ def img_conv_group(input,
conv_batchnorm_drop_rate=None, conv_batchnorm_drop_rate=None,
pool_stride=1, pool_stride=1,
pool_type=None, pool_type=None,
program=None, main_program=None,
init_program=None): startup_program=None):
""" """
Image Convolution Group, Used for vgg net. Image Convolution Group, Used for vgg net.
""" """
...@@ -71,30 +71,30 @@ def img_conv_group(input, ...@@ -71,30 +71,30 @@ def img_conv_group(input,
filter_size=conv_filter_size[i], filter_size=conv_filter_size[i],
padding=conv_padding[i], padding=conv_padding[i],
act=local_conv_act, act=local_conv_act,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
if conv_with_batchnorm[i]: if conv_with_batchnorm[i]:
tmp = layers.batch_norm( tmp = layers.batch_norm(
input=tmp, input=tmp,
act=conv_act, act=conv_act,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
drop_rate = conv_batchnorm_drop_rate[i] drop_rate = conv_batchnorm_drop_rate[i]
if abs(drop_rate) > 1e-5: if abs(drop_rate) > 1e-5:
tmp = layers.dropout( tmp = layers.dropout(
x=tmp, x=tmp,
dropout_prob=drop_rate, dropout_prob=drop_rate,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
pool_out = layers.pool2d( pool_out = layers.pool2d(
input=tmp, input=tmp,
pool_size=pool_size, pool_size=pool_size,
pool_type=pool_type, pool_type=pool_type,
pool_stride=pool_stride, pool_stride=pool_stride,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return pool_out return pool_out
...@@ -103,19 +103,19 @@ def sequence_conv_pool(input, ...@@ -103,19 +103,19 @@ def sequence_conv_pool(input,
filter_size, filter_size,
act="sigmoid", act="sigmoid",
pool_type="max", pool_type="max",
program=None, main_program=None,
init_program=None): startup_program=None):
conv_out = layers.sequence_conv( conv_out = layers.sequence_conv(
input=input, input=input,
num_filters=num_filters, num_filters=num_filters,
filter_size=filter_size, filter_size=filter_size,
act=act, act=act,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
pool_out = layers.sequence_pool( pool_out = layers.sequence_pool(
input=conv_out, input=conv_out,
pool_type=pool_type, pool_type=pool_type,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return pool_out return pool_out
...@@ -132,7 +132,7 @@ class Optimizer(object): ...@@ -132,7 +132,7 @@ class Optimizer(object):
def create_optimization_pass(self, def create_optimization_pass(self,
parameters_and_grads, parameters_and_grads,
loss, loss,
init_program=None): startup_program=None):
"""Add optimization operators to update gradients to variables. """Add optimization operators to update gradients to variables.
Args: Args:
...@@ -144,7 +144,7 @@ class Optimizer(object): ...@@ -144,7 +144,7 @@ class Optimizer(object):
optimization. This will include parameter update ops, global step optimization. This will include parameter update ops, global step
update ops and any other custom ops required by subclasses to manage update ops and any other custom ops required by subclasses to manage
their internal state. their internal state.
:param init_program: :param startup_program:
""" """
# This is a default implementation of create_optimization_pass that # This is a default implementation of create_optimization_pass that
# can be shared by most optimizers. This implementation assumes that # can be shared by most optimizers. This implementation assumes that
...@@ -156,7 +156,9 @@ class Optimizer(object): ...@@ -156,7 +156,9 @@ class Optimizer(object):
# Create any accumulators # Create any accumulators
program = loss.block.program program = loss.block.program
self.helper = LayerHelper( self.helper = LayerHelper(
self.__class__.__name__, program=program, init_program=init_program) self.__class__.__name__,
main_program=program,
startup_program=startup_program)
self._create_accumulators(loss.block, self._create_accumulators(loss.block,
[p[0] for p in parameters_and_grads]) [p[0] for p in parameters_and_grads])
# Create any necessary tensors # Create any necessary tensors
...@@ -185,7 +187,7 @@ class Optimizer(object): ...@@ -185,7 +187,7 @@ class Optimizer(object):
def minimize(self, def minimize(self,
loss, loss,
init_program=None, startup_program=None,
parameter_list=None, parameter_list=None,
no_grad_set=None): no_grad_set=None):
"""Add operations to minimize `loss` by updating `parameter_list`. """Add operations to minimize `loss` by updating `parameter_list`.
...@@ -198,7 +200,7 @@ class Optimizer(object): ...@@ -198,7 +200,7 @@ class Optimizer(object):
# Add regularization if any # Add regularization if any
params_grads = append_regularization_ops(params_grads) params_grads = append_regularization_ops(params_grads)
optimize_ops = self.create_optimization_pass(params_grads, loss, optimize_ops = self.create_optimization_pass(params_grads, loss,
init_program) startup_program)
return optimize_ops return optimize_ops
......
...@@ -2,7 +2,7 @@ import unittest ...@@ -2,7 +2,7 @@ import unittest
from paddle.v2.framework.layers import mul, data from paddle.v2.framework.layers import mul, data
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
from paddle.v2.framework.framework import g_program from paddle.v2.framework.framework import g_main_program
import numpy import numpy
...@@ -23,7 +23,7 @@ class TestExecutor(unittest.TestCase): ...@@ -23,7 +23,7 @@ class TestExecutor(unittest.TestCase):
tensor_b = core.LoDTensor() tensor_b = core.LoDTensor()
tensor_b.set(b_np, place) tensor_b.set(b_np, place)
exe = Executor(place) exe = Executor(place)
outs = exe.run(g_program, outs = exe.run(g_main_program,
feed={'a': tensor_a, feed={'a': tensor_a,
'b': tensor_b}, 'b': tensor_b},
fetch_list=[out]) fetch_list=[out])
......
...@@ -3,40 +3,44 @@ import paddle.v2.framework.layers as layers ...@@ -3,40 +3,44 @@ import paddle.v2.framework.layers as layers
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
from paddle.v2.framework.io import save_persistables, load_persistables from paddle.v2.framework.io import save_persistables, load_persistables
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
import numpy as np import numpy as np
init_program = Program() startup_program = Program()
program = Program() main_program = Program()
x = layers.data( x = layers.data(
name='x', name='x',
shape=[13], shape=[13],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
y_predict = layers.fc(input=x, y_predict = layers.fc(input=x,
size=1, size=1,
act=None, act=None,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
y = layers.data( y = layers.data(
name='y', name='y',
shape=[1], shape=[1],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
cost = layers.square_error_cost( cost = layers.square_error_cost(
input=y_predict, label=y, program=program, init_program=init_program) input=y_predict,
avg_cost = layers.mean(x=cost, program=program, init_program=init_program) label=y,
main_program=main_program,
startup_program=startup_program)
avg_cost = layers.mean(
x=cost, main_program=main_program, startup_program=startup_program)
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost, init_program) opts = sgd_optimizer.minimize(avg_cost, startup_program)
BATCH_SIZE = 20 BATCH_SIZE = 20
...@@ -48,12 +52,12 @@ train_reader = paddle.batch( ...@@ -48,12 +52,12 @@ train_reader = paddle.batch(
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(init_program, feed={}, fetch_list=[]) exe.run(startup_program, feed={}, fetch_list=[])
PASS_NUM = 100 PASS_NUM = 100
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
save_persistables(exe, "./fit_a_line.model/", program=program) save_persistables(exe, "./fit_a_line.model/", main_program=main_program)
load_persistables(exe, "./fit_a_line.model/", program=program) load_persistables(exe, "./fit_a_line.model/", main_program=main_program)
for data in train_reader(): for data in train_reader():
x_data = np.array(map(lambda x: x[0], data)).astype("float32") x_data = np.array(map(lambda x: x[0], data)).astype("float32")
y_data = np.array(map(lambda x: x[1], data)).astype("float32") y_data = np.array(map(lambda x: x[1], data)).astype("float32")
...@@ -65,7 +69,7 @@ for pass_id in range(PASS_NUM): ...@@ -65,7 +69,7 @@ for pass_id in range(PASS_NUM):
tensor_y = core.LoDTensor() tensor_y = core.LoDTensor()
tensor_y.set(y_data, place) tensor_y.set(y_data, place)
# print tensor_y.get_dims() # print tensor_y.get_dims()
outs = exe.run(program, outs = exe.run(main_program,
feed={'x': tensor_x, feed={'x': tensor_x,
'y': tensor_y}, 'y': tensor_y},
fetch_list=[avg_cost]) fetch_list=[avg_cost])
......
...@@ -9,8 +9,8 @@ def conv_block(input, ...@@ -9,8 +9,8 @@ def conv_block(input,
num_filter, num_filter,
groups, groups,
dropouts, dropouts,
program=None, main_program=None,
init_program=None): startup_program=None):
return nets.img_conv_group( return nets.img_conv_group(
input=input, input=input,
pool_size=2, pool_size=2,
...@@ -21,77 +21,81 @@ def conv_block(input, ...@@ -21,77 +21,81 @@ def conv_block(input,
conv_with_batchnorm=True, conv_with_batchnorm=True,
conv_batchnorm_drop_rate=dropouts, conv_batchnorm_drop_rate=dropouts,
pool_type='max', pool_type='max',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
class TestLayer(unittest.TestCase): class TestLayer(unittest.TestCase):
def test_batch_norm_layer(self): def test_batch_norm_layer(self):
program = Program() main_program = Program()
init_program = Program() startup_program = Program()
images = layers.data( images = layers.data(
name='pixel', name='pixel',
shape=[3, 48, 48], shape=[3, 48, 48],
data_type='float32', data_type='float32',
program=program) main_program=main_program)
layers.batch_norm( layers.batch_norm(
input=images, program=program, init_program=init_program) input=images,
main_program=main_program,
startup_program=startup_program)
# print str(program) # print str(main_program)
def test_dropout_layer(self): def test_dropout_layer(self):
program = Program() main_program = Program()
init_program = Program() startup_program = Program()
images = layers.data( images = layers.data(
name='pixel', name='pixel',
shape=[3, 48, 48], shape=[3, 48, 48],
data_type='float32', data_type='float32',
program=program) main_program=main_program)
layers.dropout( layers.dropout(
x=images, x=images,
dropout_prob=0.5, dropout_prob=0.5,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
# print str(program) # print str(main_program)
def test_img_conv_group(self): def test_img_conv_group(self):
program = Program() main_program = Program()
init_program = Program() startup_program = Program()
images = layers.data( images = layers.data(
name='pixel', name='pixel',
shape=[3, 48, 48], shape=[3, 48, 48],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
conv1 = conv_block(images, 64, 2, [0.3, 0], program, init_program) conv1 = conv_block(images, 64, 2, [0.3, 0], main_program,
conv2 = conv_block(conv1, 256, 3, [0.4, 0.4, 0], program, init_program) startup_program)
conv2 = conv_block(conv1, 256, 3, [0.4, 0.4, 0], main_program,
startup_program)
# print str(program) # print str(main_program)
def test_elementwise_add_with_act(self): def test_elementwise_add_with_act(self):
program = Program() main_program = Program()
init_program = Program() startup_program = Program()
image1 = layers.data( image1 = layers.data(
name='pixel1', name='pixel1',
shape=[3, 48, 48], shape=[3, 48, 48],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
image2 = layers.data( image2 = layers.data(
name='pixel2', name='pixel2',
shape=[3, 48, 48], shape=[3, 48, 48],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
out = layers.elementwise_add( out = layers.elementwise_add(
x=image1, x=image1,
y=image2, y=image2,
act='relu', act='relu',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
# print(program) # print(main_program)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -5,19 +5,19 @@ import paddle.v2.framework.layers as layers ...@@ -5,19 +5,19 @@ import paddle.v2.framework.layers as layers
import paddle.v2.framework.nets as nets import paddle.v2.framework.nets as nets
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
from paddle.v2.framework.framework import g_init_program, g_program from paddle.v2.framework.framework import g_startup_program, g_main_program
from paddle.v2.framework.initializer import XavierInitializer from paddle.v2.framework.initializer import XavierInitializer
def resnet_cifar10(input, depth=32, program=None, init_program=None): def resnet_cifar10(input, depth=32, main_program=None, startup_program=None):
def conv_bn_layer(input, def conv_bn_layer(input,
ch_out, ch_out,
filter_size, filter_size,
stride, stride,
padding, padding,
act='relu', act='relu',
program=None, main_program=None,
init_program=None): startup_program=None):
tmp = layers.conv2d( tmp = layers.conv2d(
input=input, input=input,
filter_size=filter_size, filter_size=filter_size,
...@@ -26,10 +26,13 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -26,10 +26,13 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
padding=padding, padding=padding,
act=None, act=None,
bias_attr=False, bias_attr=False,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return layers.batch_norm( return layers.batch_norm(
input=tmp, act=act, program=program, init_program=init_program) input=tmp,
act=act,
main_program=main_program,
startup_program=startup_program)
def shortcut(input, ch_in, ch_out, stride, program, init_program): def shortcut(input, ch_in, ch_out, stride, program, init_program):
if ch_in != ch_out: if ch_in != ch_out:
...@@ -42,16 +45,16 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -42,16 +45,16 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
ch_in, ch_in,
ch_out, ch_out,
stride, stride,
program=program, main_program=main_program,
init_program=init_program): startup_program=startup_program):
tmp = conv_bn_layer( tmp = conv_bn_layer(
input, input,
ch_out, ch_out,
3, 3,
stride, stride,
1, 1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
tmp = conv_bn_layer( tmp = conv_bn_layer(
tmp, tmp,
ch_out, ch_out,
...@@ -59,21 +62,22 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -59,21 +62,22 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
1, 1,
1, 1,
act=None, act=None,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
short = shortcut(input, ch_in, ch_out, stride, program, init_program) short = shortcut(input, ch_in, ch_out, stride, main_program,
startup_program)
return layers.elementwise_add( return layers.elementwise_add(
x=tmp, x=tmp,
y=short, y=short,
act='relu', act='relu',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
def layer_warp(block_func, input, ch_in, ch_out, count, stride, program, def layer_warp(block_func, input, ch_in, ch_out, count, stride, program,
init_program): startup_program):
tmp = block_func(input, ch_in, ch_out, stride, program, init_program) tmp = block_func(input, ch_in, ch_out, stride, program, startup_program)
for i in range(1, count): for i in range(1, count):
tmp = block_func(tmp, ch_out, ch_out, 1, program, init_program) tmp = block_func(tmp, ch_out, ch_out, 1, program, startup_program)
return tmp return tmp
assert (depth - 2) % 6 == 0 assert (depth - 2) % 6 == 0
...@@ -84,8 +88,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -84,8 +88,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
filter_size=3, filter_size=3,
stride=1, stride=1,
padding=1, padding=1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
res1 = layer_warp( res1 = layer_warp(
basicblock, basicblock,
conv1, conv1,
...@@ -93,8 +97,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -93,8 +97,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
16, 16,
n, n,
1, 1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
res2 = layer_warp( res2 = layer_warp(
basicblock, basicblock,
res1, res1,
...@@ -102,8 +106,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -102,8 +106,8 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
32, 32,
n, n,
2, 2,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
res3 = layer_warp( res3 = layer_warp(
basicblock, basicblock,
res2, res2,
...@@ -111,25 +115,25 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None): ...@@ -111,25 +115,25 @@ def resnet_cifar10(input, depth=32, program=None, init_program=None):
64, 64,
n, n,
2, 2,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
pool = layers.pool2d( pool = layers.pool2d(
input=res3, input=res3,
pool_size=8, pool_size=8,
pool_type='avg', pool_type='avg',
pool_stride=1, pool_stride=1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return pool return pool
def vgg16_bn_drop(input, program=None, init_program=None): def vgg16_bn_drop(input, main_program=None, startup_program=None):
def conv_block(input, def conv_block(input,
num_filter, num_filter,
groups, groups,
dropouts, dropouts,
program=None, main_program=None,
init_program=None): startup_program=None):
return nets.img_conv_group( return nets.img_conv_group(
input=input, input=input,
pool_size=2, pool_size=2,
...@@ -140,38 +144,50 @@ def vgg16_bn_drop(input, program=None, init_program=None): ...@@ -140,38 +144,50 @@ def vgg16_bn_drop(input, program=None, init_program=None):
conv_with_batchnorm=True, conv_with_batchnorm=True,
conv_batchnorm_drop_rate=dropouts, conv_batchnorm_drop_rate=dropouts,
pool_type='max', pool_type='max',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
conv1 = conv_block(input, 64, 2, [0.3, 0], program, init_program) conv1 = conv_block(input, 64, 2, [0.3, 0], main_program, startup_program)
conv2 = conv_block(conv1, 128, 2, [0.4, 0], program, init_program) conv2 = conv_block(conv1, 128, 2, [0.4, 0], main_program, startup_program)
conv3 = conv_block(conv2, 256, 3, [0.4, 0.4, 0], program, init_program) conv3 = conv_block(conv2, 256, 3, [0.4, 0.4, 0], main_program,
conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0], program, init_program) startup_program)
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0], program, init_program) conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0], main_program,
startup_program)
conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0], main_program,
startup_program)
drop = layers.dropout( drop = layers.dropout(
x=conv5, dropout_prob=0.5, program=program, init_program=init_program) x=conv5,
dropout_prob=0.5,
main_program=main_program,
startup_program=startup_program)
fc1 = layers.fc(input=drop, fc1 = layers.fc(input=drop,
size=512, size=512,
act=None, act=None,
param_attr={"initializer": XavierInitializer()}, param_attr={"initializer": XavierInitializer()},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
reshape1 = layers.reshape( reshape1 = layers.reshape(
x=fc1, x=fc1,
shape=list(fc1.shape + (1, 1)), shape=list(fc1.shape + (1, 1)),
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
bn = layers.batch_norm( bn = layers.batch_norm(
input=reshape1, act='relu', program=program, init_program=init_program) input=reshape1,
act='relu',
main_program=main_program,
startup_program=startup_program)
drop2 = layers.dropout( drop2 = layers.dropout(
x=bn, dropout_prob=0.5, program=program, init_program=init_program) x=bn,
dropout_prob=0.5,
main_program=main_program,
startup_program=startup_program)
fc2 = layers.fc(input=drop2, fc2 = layers.fc(input=drop2,
size=512, size=512,
act=None, act=None,
param_attr={"initializer": XavierInitializer()}, param_attr={"initializer": XavierInitializer()},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return fc2 return fc2
...@@ -209,7 +225,7 @@ train_reader = paddle.batch( ...@@ -209,7 +225,7 @@ train_reader = paddle.batch(
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(g_init_program, feed={}, fetch_list=[]) exe.run(g_startup_program, feed={}, fetch_list=[])
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
batch_id = 0 batch_id = 0
...@@ -227,7 +243,7 @@ for pass_id in range(PASS_NUM): ...@@ -227,7 +243,7 @@ for pass_id in range(PASS_NUM):
tensor_img.set(img_data, place) tensor_img.set(img_data, place)
tensor_y.set(y_data, place) tensor_y.set(y_data, place)
outs = exe.run(g_program, outs = exe.run(g_main_program,
feed={"pixel": tensor_img, feed={"pixel": tensor_img,
"label": tensor_y}, "label": tensor_y},
fetch_list=[avg_cost, accuracy]) fetch_list=[avg_cost, accuracy])
......
...@@ -3,7 +3,7 @@ import paddle.v2.framework.layers as layers ...@@ -3,7 +3,7 @@ import paddle.v2.framework.layers as layers
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
from paddle.v2.framework.io import save_inference_model, load_inference_model from paddle.v2.framework.io import save_inference_model, load_inference_model
import paddle.v2.framework.executor as executor import paddle.v2.framework.executor as executor
import unittest import unittest
...@@ -20,28 +20,28 @@ class TestBook(unittest.TestCase): ...@@ -20,28 +20,28 @@ class TestBook(unittest.TestCase):
name='x', name='x',
shape=[2], shape=[2],
data_type='float32', data_type='float32',
program=program, main_program=program,
init_program=init_program) startup_program=init_program)
y = layers.data( y = layers.data(
name='y', name='y',
shape=[1], shape=[1],
data_type='float32', data_type='float32',
program=program, main_program=program,
init_program=init_program) startup_program=init_program)
y_predict = layers.fc(input=x, y_predict = layers.fc(input=x,
size=1, size=1,
act=None, act=None,
program=program, main_program=program,
init_program=init_program) startup_program=init_program)
cost = layers.square_error_cost( cost = layers.square_error_cost(
input=y_predict, input=y_predict,
label=y, label=y,
program=program, main_program=program,
init_program=init_program) startup_program=init_program)
avg_cost = layers.mean( avg_cost = layers.mean(
x=cost, program=program, init_program=init_program) x=cost, main_program=program, startup_program=init_program)
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost, init_program) opts = sgd_optimizer.minimize(avg_cost, init_program)
......
import paddle.v2.framework.layers as layers import paddle.v2.framework.layers as layers
import paddle.v2.framework.nets as nets import paddle.v2.framework.nets as nets
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import unittest import unittest
...@@ -9,15 +9,15 @@ class TestBook(unittest.TestCase): ...@@ -9,15 +9,15 @@ class TestBook(unittest.TestCase):
def test_fit_a_line(self): def test_fit_a_line(self):
program = Program() program = Program()
x = layers.data( x = layers.data(
name='x', shape=[13], data_type='float32', program=program) name='x', shape=[13], data_type='float32', main_program=program)
y_predict = layers.fc(input=x, size=1, act=None, program=program) y_predict = layers.fc(input=x, size=1, act=None, main_program=program)
y = layers.data( y = layers.data(
name='y', shape=[1], data_type='float32', program=program) name='y', shape=[1], data_type='float32', main_program=program)
cost = layers.square_error_cost( cost = layers.square_error_cost(
input=y_predict, label=y, program=program) input=y_predict, label=y, main_program=program)
avg_cost = layers.mean(x=cost, program=program) avg_cost = layers.mean(x=cost, main_program=program)
self.assertIsNotNone(avg_cost) self.assertIsNotNone(avg_cost)
program.append_backward(avg_cost) program.append_backward(avg_cost)
print str(program) print str(program)
...@@ -27,26 +27,42 @@ class TestBook(unittest.TestCase): ...@@ -27,26 +27,42 @@ class TestBook(unittest.TestCase):
# Change g_program, so the rest layers use `g_program` # Change g_program, so the rest layers use `g_program`
images = layers.data( images = layers.data(
name='pixel', shape=[784], data_type='float32', program=program) name='pixel',
shape=[784],
data_type='float32',
main_program=program)
label = layers.data( label = layers.data(
name='label', shape=[1], data_type='int32', program=program) name='label', shape=[1], data_type='int32', main_program=program)
hidden1 = layers.fc(input=images, size=128, act='relu', program=program) hidden1 = layers.fc(input=images,
hidden2 = layers.fc(input=hidden1, size=64, act='relu', program=program) size=128,
act='relu',
main_program=program)
hidden2 = layers.fc(input=hidden1,
size=64,
act='relu',
main_program=program)
predict = layers.fc(input=hidden2, predict = layers.fc(input=hidden2,
size=10, size=10,
act='softmax', act='softmax',
program=program) main_program=program)
cost = layers.cross_entropy(input=predict, label=label, program=program) cost = layers.cross_entropy(
avg_cost = layers.mean(x=cost, program=program) input=predict, label=label, main_program=program)
avg_cost = layers.mean(x=cost, main_program=program)
self.assertIsNotNone(avg_cost) self.assertIsNotNone(avg_cost)
print str(program) print str(program)
def test_simple_conv2d(self): def test_simple_conv2d(self):
program = Program() program = Program()
images = layers.data( images = layers.data(
name='pixel', shape=[3, 48, 48], data_type='int32', program=program) name='pixel',
shape=[3, 48, 48],
data_type='int32',
main_program=program)
layers.conv2d( layers.conv2d(
input=images, num_filters=3, filter_size=[4, 4], program=program) input=images,
num_filters=3,
filter_size=[4, 4],
main_program=program)
print str(program) print str(program)
...@@ -57,9 +73,9 @@ class TestBook(unittest.TestCase): ...@@ -57,9 +73,9 @@ class TestBook(unittest.TestCase):
name='pixel', name='pixel',
shape=[1, 28, 28], shape=[1, 28, 28],
data_type='float32', data_type='float32',
program=program) main_program=program)
label = layers.data( label = layers.data(
name='label', shape=[1], data_type='int32', program=program) name='label', shape=[1], data_type='int32', main_program=program)
conv_pool_1 = nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=images, input=images,
filter_size=5, filter_size=5,
...@@ -67,7 +83,7 @@ class TestBook(unittest.TestCase): ...@@ -67,7 +83,7 @@ class TestBook(unittest.TestCase):
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
act="relu", act="relu",
program=program) main_program=program)
conv_pool_2 = nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
...@@ -75,14 +91,15 @@ class TestBook(unittest.TestCase): ...@@ -75,14 +91,15 @@ class TestBook(unittest.TestCase):
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
act="relu", act="relu",
program=program) main_program=program)
predict = layers.fc(input=conv_pool_2, predict = layers.fc(input=conv_pool_2,
size=10, size=10,
act="softmax", act="softmax",
program=program) main_program=program)
cost = layers.cross_entropy(input=predict, label=label, program=program) cost = layers.cross_entropy(
avg_cost = layers.mean(x=cost, program=program) input=predict, label=label, main_program=program)
avg_cost = layers.mean(x=cost, main_program=program)
program.append_backward(avg_cost) program.append_backward(avg_cost)
...@@ -93,58 +110,58 @@ class TestBook(unittest.TestCase): ...@@ -93,58 +110,58 @@ class TestBook(unittest.TestCase):
dict_size = 10000 dict_size = 10000
embed_size = 32 embed_size = 32
first_word = layers.data( first_word = layers.data(
name='firstw', shape=[1], data_type='int64', program=program) name='firstw', shape=[1], data_type='int64', main_program=program)
second_word = layers.data( second_word = layers.data(
name='secondw', shape=[1], data_type='int64', program=program) name='secondw', shape=[1], data_type='int64', main_program=program)
third_word = layers.data( third_word = layers.data(
name='thirdw', shape=[1], data_type='int64', program=program) name='thirdw', shape=[1], data_type='int64', main_program=program)
forth_word = layers.data( forth_word = layers.data(
name='forthw', shape=[1], data_type='int64', program=program) name='forthw', shape=[1], data_type='int64', main_program=program)
next_word = layers.data( next_word = layers.data(
name='nextw', shape=[1], data_type='int64', program=program) name='nextw', shape=[1], data_type='int64', main_program=program)
embed_first = layers.embedding( embed_first = layers.embedding(
input=first_word, input=first_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program) main_program=program)
embed_second = layers.embedding( embed_second = layers.embedding(
input=second_word, input=second_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program) main_program=program)
embed_third = layers.embedding( embed_third = layers.embedding(
input=third_word, input=third_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program) main_program=program)
embed_forth = layers.embedding( embed_forth = layers.embedding(
input=forth_word, input=forth_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program) main_program=program)
concat_embed = layers.concat( concat_embed = layers.concat(
input=[embed_first, embed_second, embed_third, embed_forth], input=[embed_first, embed_second, embed_third, embed_forth],
axis=1, axis=1,
program=program) main_program=program)
hidden1 = layers.fc(input=concat_embed, hidden1 = layers.fc(input=concat_embed,
size=256, size=256,
act='sigmoid', act='sigmoid',
program=program) main_program=program)
predict_word = layers.fc(input=hidden1, predict_word = layers.fc(input=hidden1,
size=dict_size, size=dict_size,
act='softmax', act='softmax',
program=program) main_program=program)
cost = layers.cross_entropy( cost = layers.cross_entropy(
input=predict_word, label=next_word, program=program) input=predict_word, label=next_word, main_program=program)
avg_cost = layers.mean(x=cost, program=program) avg_cost = layers.mean(x=cost, main_program=program)
self.assertIsNotNone(avg_cost) self.assertIsNotNone(avg_cost)
print str(program) print str(program)
......
from paddle.v2.framework.layers import lod_rank_table, data from paddle.v2.framework.layers import lod_rank_table, data
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
from paddle.v2.framework.framework import g_program from paddle.v2.framework.framework import g_main_program
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import numpy import numpy
import unittest import unittest
...@@ -19,7 +19,7 @@ class TestLoDRankTable(unittest.TestCase): ...@@ -19,7 +19,7 @@ class TestLoDRankTable(unittest.TestCase):
tensor.set(numpy.random.random(size=(17, 100)), cpu) tensor.set(numpy.random.random(size=(17, 100)), cpu)
tensor.set_lod([[0, 1, 3], [0, 5, 6, 7], [0, 3, 4, 9, 10, 13, 16, 17]]) tensor.set_lod([[0, 1, 3], [0, 5, 6, 7], [0, 3, 4, 9, 10, 13, 16, 17]])
exe.run(g_program, scope=scope, feed={'x': tensor}) exe.run(g_main_program, scope=scope, feed={'x': tensor})
var = scope.find_var(rank_table.name) var = scope.find_var(rank_table.name)
table = var.get_lod_rank_table() table = var.get_lod_rank_table()
self.assertEqual([(0, 5), (1, 1), (2, 1)], table.items()) self.assertEqual([(0, 5), (1, 1), (2, 1)], table.items())
......
import unittest import unittest
from paddle.v2.framework.framework import Variable, Program, g_program from paddle.v2.framework.framework import Variable, Program, g_main_program
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
class TestOperator(unittest.TestCase): class TestOperator(unittest.TestCase):
def test_error_type(self): def test_error_type(self):
block = g_program.create_block() block = g_main_program.create_block()
try: try:
block.append_op() block.append_op()
self.assertFail() self.assertFail()
......
import unittest import unittest
from paddle.v2.framework.framework import g_program from paddle.v2.framework.framework import g_main_program
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
class TestParameter(unittest.TestCase): class TestParameter(unittest.TestCase):
def test_param(self): def test_param(self):
b = g_program.create_block() b = g_main_program.create_block()
param = b.create_parameter( param = b.create_parameter(
name='fc.w', name='fc.w',
shape=[784, 100], shape=[784, 100],
......
...@@ -2,35 +2,35 @@ import unittest ...@@ -2,35 +2,35 @@ import unittest
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
from paddle.v2.framework.framework import Program from paddle.v2.framework.framework import Program
from paddle.v2.framework.framework import g_program from paddle.v2.framework.framework import g_main_program
class TestProgram(unittest.TestCase): class TestProgram(unittest.TestCase):
def test_program(self): def test_program(self):
b = g_program.current_block() b = g_main_program.current_block()
self.assertEqual(-1, b.parent_idx) self.assertEqual(-1, b.parent_idx)
self.assertEqual(0, b.idx) self.assertEqual(0, b.idx)
b = g_program.create_block() b = g_main_program.create_block()
self.assertEqual(1, b.idx) self.assertEqual(1, b.idx)
self.assertEqual(0, b.parent_idx) self.assertEqual(0, b.parent_idx)
b = g_program.create_block() b = g_main_program.create_block()
self.assertEqual(2, b.idx) self.assertEqual(2, b.idx)
self.assertEqual(1, b.parent_idx) self.assertEqual(1, b.parent_idx)
g_program.rollback() g_main_program.rollback()
b = g_program.current_block() b = g_main_program.current_block()
self.assertEqual(1, b.idx) self.assertEqual(1, b.idx)
self.assertEqual(0, b.parent_idx) self.assertEqual(0, b.parent_idx)
b = g_program.create_block() b = g_main_program.create_block()
self.assertEqual(3, b.idx) self.assertEqual(3, b.idx)
self.assertEqual(1, b.parent_idx) self.assertEqual(1, b.parent_idx)
g_program.rollback() g_main_program.rollback()
b = g_program.current_block() b = g_main_program.current_block()
self.assertEqual(1, b.idx) self.assertEqual(1, b.idx)
self.assertEqual(0, b.parent_idx) self.assertEqual(0, b.parent_idx)
......
...@@ -4,26 +4,26 @@ import paddle.v2.framework.nets as nets ...@@ -4,26 +4,26 @@ import paddle.v2.framework.nets as nets
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
import numpy as np import numpy as np
init_program = Program() startup_program = Program()
program = Program() main_program = Program()
images = layers.data( images = layers.data(
name='pixel', name='pixel',
shape=[1, 28, 28], shape=[1, 28, 28],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
label = layers.data( label = layers.data(
name='label', name='label',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
conv_pool_1 = nets.simple_img_conv_pool( conv_pool_1 = nets.simple_img_conv_pool(
input=images, input=images,
filter_size=5, filter_size=5,
...@@ -31,8 +31,8 @@ conv_pool_1 = nets.simple_img_conv_pool( ...@@ -31,8 +31,8 @@ conv_pool_1 = nets.simple_img_conv_pool(
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
act="relu", act="relu",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
conv_pool_2 = nets.simple_img_conv_pool( conv_pool_2 = nets.simple_img_conv_pool(
input=conv_pool_1, input=conv_pool_1,
filter_size=5, filter_size=5,
...@@ -40,24 +40,30 @@ conv_pool_2 = nets.simple_img_conv_pool( ...@@ -40,24 +40,30 @@ conv_pool_2 = nets.simple_img_conv_pool(
pool_size=2, pool_size=2,
pool_stride=2, pool_stride=2,
act="relu", act="relu",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
predict = layers.fc(input=conv_pool_2, predict = layers.fc(input=conv_pool_2,
size=10, size=10,
act="softmax", act="softmax",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
cost = layers.cross_entropy( cost = layers.cross_entropy(
input=predict, label=label, program=program, init_program=init_program) input=predict,
avg_cost = layers.mean(x=cost, program=program) label=label,
main_program=main_program,
startup_program=startup_program)
avg_cost = layers.mean(x=cost, main_program=main_program)
accuracy = layers.accuracy( accuracy = layers.accuracy(
input=predict, label=label, program=program, init_program=init_program) input=predict,
label=label,
main_program=main_program,
startup_program=startup_program)
# optimizer = optimizer.MomentumOptimizer(learning_rate=0.1 / 128.0, # optimizer = optimizer.MomentumOptimizer(learning_rate=0.1 / 128.0,
# momentum=0.9) # momentum=0.9)
optimizer = optimizer.AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999) optimizer = optimizer.AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999)
opts = optimizer.minimize(avg_cost, init_program) opts = optimizer.minimize(avg_cost, startup_program)
BATCH_SIZE = 50 BATCH_SIZE = 50
PASS_NUM = 3 PASS_NUM = 3
...@@ -69,7 +75,7 @@ train_reader = paddle.batch( ...@@ -69,7 +75,7 @@ train_reader = paddle.batch(
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(init_program, feed={}, fetch_list=[]) exe.run(startup_program, feed={}, fetch_list=[])
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
count = 0 count = 0
...@@ -84,7 +90,7 @@ for pass_id in range(PASS_NUM): ...@@ -84,7 +90,7 @@ for pass_id in range(PASS_NUM):
tensor_img.set(img_data, place) tensor_img.set(img_data, place)
tensor_y.set(y_data, place) tensor_y.set(y_data, place)
outs = exe.run(program, outs = exe.run(main_program,
feed={"pixel": tensor_img, feed={"pixel": tensor_img,
"label": tensor_y}, "label": tensor_y},
fetch_list=[avg_cost, accuracy]) fetch_list=[avg_cost, accuracy])
......
...@@ -11,14 +11,14 @@ from paddle.v2.framework.initializer import UniformInitializer ...@@ -11,14 +11,14 @@ from paddle.v2.framework.initializer import UniformInitializer
import numpy as np import numpy as np
BATCH_SIZE = 128 BATCH_SIZE = 128
init_program = Program() startup_program = Program()
program = Program() main_program = Program()
image = layers.data( image = layers.data(
name='x', name='x',
shape=[784], shape=[784],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
param_attr = { param_attr = {
'name': None, 'name': None,
...@@ -30,38 +30,45 @@ param_attr = { ...@@ -30,38 +30,45 @@ param_attr = {
hidden1 = layers.fc(input=image, hidden1 = layers.fc(input=image,
size=128, size=128,
act='relu', act='relu',
program=program, main_program=main_program,
init_program=init_program, startup_program=startup_program,
param_attr=param_attr) param_attr=param_attr)
hidden2 = layers.fc(input=hidden1, hidden2 = layers.fc(input=hidden1,
size=64, size=64,
act='relu', act='relu',
program=program, main_program=main_program,
init_program=init_program, startup_program=startup_program,
param_attr=param_attr) param_attr=param_attr)
predict = layers.fc(input=hidden2, predict = layers.fc(input=hidden2,
size=10, size=10,
act='softmax', act='softmax',
program=program, main_program=main_program,
init_program=init_program, startup_program=startup_program,
param_attr=param_attr) param_attr=param_attr)
label = layers.data( label = layers.data(
name='y', name='y',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
cost = layers.cross_entropy( cost = layers.cross_entropy(
input=predict, label=label, program=program, init_program=init_program) input=predict,
avg_cost = layers.mean(x=cost, program=program, init_program=init_program) label=label,
main_program=main_program,
startup_program=startup_program)
avg_cost = layers.mean(
x=cost, main_program=main_program, startup_program=startup_program)
accuracy = layers.accuracy( accuracy = layers.accuracy(
input=predict, label=label, program=program, init_program=init_program) input=predict,
label=label,
main_program=main_program,
startup_program=startup_program)
optimizer = optimizer.MomentumOptimizer(learning_rate=0.001, momentum=0.9) optimizer = optimizer.MomentumOptimizer(learning_rate=0.001, momentum=0.9)
opts = optimizer.minimize(avg_cost, init_program) opts = optimizer.minimize(avg_cost, startup_program)
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -71,7 +78,7 @@ train_reader = paddle.batch( ...@@ -71,7 +78,7 @@ train_reader = paddle.batch(
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(init_program, feed={}, fetch_list=[]) exe.run(startup_program, feed={}, fetch_list=[])
PASS_NUM = 100 PASS_NUM = 100
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
...@@ -86,7 +93,7 @@ for pass_id in range(PASS_NUM): ...@@ -86,7 +93,7 @@ for pass_id in range(PASS_NUM):
tensor_y = core.LoDTensor() tensor_y = core.LoDTensor()
tensor_y.set(y_data, place) tensor_y.set(y_data, place)
outs = exe.run(program, outs = exe.run(main_program,
feed={'x': tensor_x, feed={'x': tensor_x,
'y': tensor_y}, 'y': tensor_y},
fetch_list=[avg_cost, accuracy]) fetch_list=[avg_cost, accuracy])
......
...@@ -4,13 +4,13 @@ import paddle.v2.framework.nets as nets ...@@ -4,13 +4,13 @@ import paddle.v2.framework.nets as nets
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
import numpy as np import numpy as np
init_program = Program() startup_program = Program()
program = Program() main_program = Program()
is_sparse = True is_sparse = True
use_gpu = False use_gpu = False
BATCH_SIZE = 256 BATCH_SIZE = 256
...@@ -26,8 +26,8 @@ def get_usr_combined_features(): ...@@ -26,8 +26,8 @@ def get_usr_combined_features():
name='user_id', name='user_id',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_emb = layers.embedding( usr_emb = layers.embedding(
input=uid, input=uid,
...@@ -35,13 +35,13 @@ def get_usr_combined_features(): ...@@ -35,13 +35,13 @@ def get_usr_combined_features():
size=[USR_DICT_SIZE, 32], size=[USR_DICT_SIZE, 32],
param_attr={'name': 'user_table'}, param_attr={'name': 'user_table'},
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_fc = layers.fc(input=usr_emb, usr_fc = layers.fc(input=usr_emb,
size=32, size=32,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
USR_GENDER_DICT_SIZE = 2 USR_GENDER_DICT_SIZE = 2
...@@ -49,75 +49,75 @@ def get_usr_combined_features(): ...@@ -49,75 +49,75 @@ def get_usr_combined_features():
name='gender_id', name='gender_id',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_gender_emb = layers.embedding( usr_gender_emb = layers.embedding(
input=usr_gender_id, input=usr_gender_id,
size=[USR_GENDER_DICT_SIZE, 16], size=[USR_GENDER_DICT_SIZE, 16],
param_attr={'name': 'gender_table'}, param_attr={'name': 'gender_table'},
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_gender_fc = layers.fc(input=usr_gender_emb, usr_gender_fc = layers.fc(input=usr_gender_emb,
size=16, size=16,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
USR_AGE_DICT_SIZE = len(paddle.dataset.movielens.age_table) USR_AGE_DICT_SIZE = len(paddle.dataset.movielens.age_table)
usr_age_id = layers.data( usr_age_id = layers.data(
name='age_id', name='age_id',
shape=[1], shape=[1],
data_type="int64", data_type="int64",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_age_emb = layers.embedding( usr_age_emb = layers.embedding(
input=usr_age_id, input=usr_age_id,
size=[USR_AGE_DICT_SIZE, 16], size=[USR_AGE_DICT_SIZE, 16],
is_sparse=is_sparse, is_sparse=is_sparse,
param_attr={'name': 'age_table'}, param_attr={'name': 'age_table'},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_age_fc = layers.fc(input=usr_age_emb, usr_age_fc = layers.fc(input=usr_age_emb,
size=16, size=16,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
USR_JOB_DICT_SIZE = paddle.dataset.movielens.max_job_id() + 1 USR_JOB_DICT_SIZE = paddle.dataset.movielens.max_job_id() + 1
usr_job_id = layers.data( usr_job_id = layers.data(
name='job_id', name='job_id',
shape=[1], shape=[1],
data_type="int64", data_type="int64",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_job_emb = layers.embedding( usr_job_emb = layers.embedding(
input=usr_job_id, input=usr_job_id,
size=[USR_JOB_DICT_SIZE, 16], size=[USR_JOB_DICT_SIZE, 16],
param_attr={'name': 'job_table'}, param_attr={'name': 'job_table'},
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_job_fc = layers.fc(input=usr_job_emb, usr_job_fc = layers.fc(input=usr_job_emb,
size=16, size=16,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
concat_embed = layers.concat( concat_embed = layers.concat(
input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc], input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc],
axis=1, axis=1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
usr_combined_features = layers.fc(input=concat_embed, usr_combined_features = layers.fc(input=concat_embed,
size=200, size=200,
act="tanh", act="tanh",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return usr_combined_features return usr_combined_features
...@@ -130,8 +130,8 @@ def get_mov_combined_features(): ...@@ -130,8 +130,8 @@ def get_mov_combined_features():
name='movie_id', name='movie_id',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_emb = layers.embedding( mov_emb = layers.embedding(
input=mov_id, input=mov_id,
...@@ -139,13 +139,13 @@ def get_mov_combined_features(): ...@@ -139,13 +139,13 @@ def get_mov_combined_features():
size=[MOV_DICT_SIZE, 32], size=[MOV_DICT_SIZE, 32],
param_attr={'name': 'movie_table'}, param_attr={'name': 'movie_table'},
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_fc = layers.fc(input=mov_emb, mov_fc = layers.fc(input=mov_emb,
size=32, size=32,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
CATEGORY_DICT_SIZE = len(paddle.dataset.movielens.movie_categories()) CATEGORY_DICT_SIZE = len(paddle.dataset.movielens.movie_categories())
...@@ -153,21 +153,21 @@ def get_mov_combined_features(): ...@@ -153,21 +153,21 @@ def get_mov_combined_features():
name='category_id', name='category_id',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_categories_emb = layers.embedding( mov_categories_emb = layers.embedding(
input=category_id, input=category_id,
size=[CATEGORY_DICT_SIZE, 32], size=[CATEGORY_DICT_SIZE, 32],
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_categories_hidden = layers.sequence_pool( mov_categories_hidden = layers.sequence_pool(
input=mov_categories_emb, input=mov_categories_emb,
pool_type="sum", pool_type="sum",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
MOV_TITLE_DICT_SIZE = len(paddle.dataset.movielens.get_movie_title_dict()) MOV_TITLE_DICT_SIZE = len(paddle.dataset.movielens.get_movie_title_dict())
...@@ -175,15 +175,15 @@ def get_mov_combined_features(): ...@@ -175,15 +175,15 @@ def get_mov_combined_features():
name='movie_title', name='movie_title',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_title_emb = layers.embedding( mov_title_emb = layers.embedding(
input=mov_title_id, input=mov_title_id,
size=[MOV_TITLE_DICT_SIZE, 32], size=[MOV_TITLE_DICT_SIZE, 32],
is_sparse=is_sparse, is_sparse=is_sparse,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
mov_title_conv = nets.sequence_conv_pool( mov_title_conv = nets.sequence_conv_pool(
input=mov_title_emb, input=mov_title_emb,
...@@ -191,21 +191,21 @@ def get_mov_combined_features(): ...@@ -191,21 +191,21 @@ def get_mov_combined_features():
filter_size=3, filter_size=3,
act="tanh", act="tanh",
pool_type="sum", pool_type="sum",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
concat_embed = layers.concat( concat_embed = layers.concat(
input=[mov_fc, mov_categories_hidden, mov_title_conv], input=[mov_fc, mov_categories_hidden, mov_title_conv],
axis=1, axis=1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
# FIXME(dzh) : need tanh operator # FIXME(dzh) : need tanh operator
mov_combined_features = layers.fc(input=concat_embed, mov_combined_features = layers.fc(input=concat_embed,
size=200, size=200,
act="tanh", act="tanh",
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
return mov_combined_features return mov_combined_features
...@@ -218,24 +218,26 @@ def model(): ...@@ -218,24 +218,26 @@ def model():
inference = layers.cos_sim( inference = layers.cos_sim(
X=usr_combined_features, X=usr_combined_features,
Y=mov_combined_features, Y=mov_combined_features,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
label = layers.data( label = layers.data(
name='score', name='score',
shape=[1], shape=[1],
data_type='float32', data_type='float32',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
square_cost = layers.square_error_cost( square_cost = layers.square_error_cost(
input=inference, input=inference,
label=label, label=label,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
avg_cost = layers.mean( avg_cost = layers.mean(
x=square_cost, program=program, init_program=init_program) x=square_cost,
main_program=main_program,
startup_program=startup_program)
return avg_cost return avg_cost
...@@ -243,8 +245,8 @@ def model(): ...@@ -243,8 +245,8 @@ def model():
def main(): def main():
cost = model() cost = model()
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.2) sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.2)
opts = sgd_optimizer.minimize(cost, init_program=init_program) opts = sgd_optimizer.minimize(cost, startup_program=startup_program)
block = program.block(0) block = main_program.block(0)
if use_gpu: if use_gpu:
place = core.GPUPlace(0) place = core.GPUPlace(0)
...@@ -252,7 +254,7 @@ def main(): ...@@ -252,7 +254,7 @@ def main():
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(init_program, feed={}, fetch_list=[]) exe.run(startup_program, feed={}, fetch_list=[])
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
...@@ -301,7 +303,7 @@ def main(): ...@@ -301,7 +303,7 @@ def main():
PASS_NUM = 100 PASS_NUM = 100
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
for data in train_reader(): for data in train_reader():
outs = exe.run(program, outs = exe.run(main_program,
feed=func_feed(feeding, data), feed=func_feed(feeding, data),
fetch_list=[cost]) fetch_list=[cost])
out = np.array(outs[0]) out = np.array(outs[0])
......
...@@ -99,17 +99,17 @@ class RecurrentOpTest1(unittest.TestCase): ...@@ -99,17 +99,17 @@ class RecurrentOpTest1(unittest.TestCase):
batch_size = 1 batch_size = 1
sent_len = 1 sent_len = 1
def init_program(self): def setup_program(self):
self.program = Program() self.main_program = Program()
self.init_program = Program() self.startup_program = Program()
self.p_info = { self.p_info = {
"program": self.program, "main_program": self.main_program,
"init_program": self.init_program "startup_program": self.startup_program
} }
self.place = core.CPUPlace() self.place = core.CPUPlace()
def setUp(self): def setUp(self):
self.init_program() self.setup_program()
self.data_field = {"x", "h_boot"} self.data_field = {"x", "h_boot"}
self.input_shape = (self.sent_len, self.batch_size, self.input_dim) self.input_shape = (self.sent_len, self.batch_size, self.input_dim)
...@@ -131,7 +131,7 @@ class RecurrentOpTest1(unittest.TestCase): ...@@ -131,7 +131,7 @@ class RecurrentOpTest1(unittest.TestCase):
name='h_boot', name='h_boot',
**self.p_info) **self.p_info)
rnn = StaticRNN(program=self.program) rnn = StaticRNN(main_program=self.main_program)
with rnn.step(): with rnn.step():
h_pre = rnn.memory(init=h_boot) h_pre = rnn.memory(init=h_boot)
x_t = rnn.step_input(x) x_t = rnn.step_input(x)
...@@ -153,7 +153,7 @@ class RecurrentOpTest1(unittest.TestCase): ...@@ -153,7 +153,7 @@ class RecurrentOpTest1(unittest.TestCase):
for x in self.data_field for x in self.data_field
} }
exe = Executor(self.place) exe = Executor(self.place)
out = exe.run(self.program, out = exe.run(self.main_program,
feed=self.feed_map, feed=self.feed_map,
fetch_list=[self.output]) fetch_list=[self.output])
...@@ -165,12 +165,14 @@ class RecurrentOpTest1(unittest.TestCase): ...@@ -165,12 +165,14 @@ class RecurrentOpTest1(unittest.TestCase):
for x in self.data_field for x in self.data_field
} }
fetch_list = [ fetch_list = [
self.program.global_block().var(x + "@GRAD") self.main_program.global_block().var(x + "@GRAD")
for x in self.data_field for x in self.data_field
] ]
exe = Executor(self.place) exe = Executor(self.place)
return exe.run(self.program, feed=self.feed_map, fetch_list=fetch_list) return exe.run(self.main_program,
feed=self.feed_map,
fetch_list=fetch_list)
def test_backward(self): def test_backward(self):
self.check_forward() self.check_forward()
...@@ -237,7 +239,7 @@ class RecurrentOpTest2(RecurrentOpTest1): ...@@ -237,7 +239,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
sent_len = 2 sent_len = 2
def setUp(self): def setUp(self):
self.init_program() self.setup_program()
self.data_field = {"x", "h_boot", "W", "U"} self.data_field = {"x", "h_boot", "W", "U"}
...@@ -260,7 +262,7 @@ class RecurrentOpTest2(RecurrentOpTest1): ...@@ -260,7 +262,7 @@ class RecurrentOpTest2(RecurrentOpTest1):
name='h_boot', name='h_boot',
**self.p_info) **self.p_info)
rnn = StaticRNN(program=self.program) rnn = StaticRNN(main_program=self.main_program)
with rnn.step(): with rnn.step():
h_pre = rnn.memory(init=h_boot) h_pre = rnn.memory(init=h_boot)
x_t = rnn.step_input(x) x_t = rnn.step_input(x)
...@@ -333,7 +335,7 @@ class RecurrentOpTest3(RecurrentOpTest1): ...@@ -333,7 +335,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
sent_len = 2 sent_len = 2
def setUp(self): def setUp(self):
self.init_program() self.setup_program()
self.data_field = {"x", "h_boot1", "h_boot2"} self.data_field = {"x", "h_boot1", "h_boot2"}
...@@ -364,7 +366,7 @@ class RecurrentOpTest3(RecurrentOpTest1): ...@@ -364,7 +366,7 @@ class RecurrentOpTest3(RecurrentOpTest1):
append_batch_size=False, append_batch_size=False,
**self.p_info) **self.p_info)
rnn = StaticRNN(program=self.program) rnn = StaticRNN(main_program=self.main_program)
with rnn.step(): with rnn.step():
h_pre1 = rnn.memory(init=h_boot1) h_pre1 = rnn.memory(init=h_boot1)
h_pre2 = rnn.memory(init=h_boot2) h_pre2 = rnn.memory(init=h_boot2)
......
...@@ -4,7 +4,7 @@ import paddle.v2.framework.nets as nets ...@@ -4,7 +4,7 @@ import paddle.v2.framework.nets as nets
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program, g_init_program from paddle.v2.framework.framework import Program, g_main_program, g_startup_program
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
import numpy as np import numpy as np
...@@ -70,7 +70,7 @@ def main(): ...@@ -70,7 +70,7 @@ def main():
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(g_init_program) exe.run(g_startup_program)
for pass_id in xrange(PASS_NUM): for pass_id in xrange(PASS_NUM):
for data in train_data(): for data in train_data():
...@@ -82,7 +82,7 @@ def main(): ...@@ -82,7 +82,7 @@ def main():
tensor_label = core.LoDTensor() tensor_label = core.LoDTensor()
tensor_label.set(label, place) tensor_label.set(label, place)
outs = exe.run(g_program, outs = exe.run(g_main_program,
feed={"words": tensor_words, feed={"words": tensor_words,
"label": tensor_label}, "label": tensor_label},
fetch_list=[cost, acc]) fetch_list=[cost, acc])
......
import unittest import unittest
from paddle.v2.framework.framework import Variable, g_program, Program from paddle.v2.framework.framework import Variable, g_main_program, Program
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import numpy as np import numpy as np
...@@ -18,7 +18,7 @@ class TestVariable(unittest.TestCase): ...@@ -18,7 +18,7 @@ class TestVariable(unittest.TestCase):
self.assertRaises(ValueError, lambda: convert("int8")) self.assertRaises(ValueError, lambda: convert("int8"))
def test_var(self): def test_var(self):
b = g_program.current_block() b = g_main_program.current_block()
w = b.create_var( w = b.create_var(
dtype="float64", shape=[784, 100], lod_level=0, name="fc.w") dtype="float64", shape=[784, 100], lod_level=0, name="fc.w")
self.assertNotEqual(str(w), "") self.assertNotEqual(str(w), "")
......
...@@ -3,13 +3,13 @@ import paddle.v2.framework.layers as layers ...@@ -3,13 +3,13 @@ import paddle.v2.framework.layers as layers
import paddle.v2.framework.core as core import paddle.v2.framework.core as core
import paddle.v2.framework.optimizer as optimizer import paddle.v2.framework.optimizer as optimizer
from paddle.v2.framework.framework import Program, g_program from paddle.v2.framework.framework import Program, g_main_program
from paddle.v2.framework.executor import Executor from paddle.v2.framework.executor import Executor
import numpy as np import numpy as np
init_program = Program() startup_program = Program()
program = Program() main_program = Program()
embed_size = 32 embed_size = 32
hidden_size = 256 hidden_size = 256
...@@ -24,32 +24,32 @@ first_word = layers.data( ...@@ -24,32 +24,32 @@ first_word = layers.data(
name='firstw', name='firstw',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
second_word = layers.data( second_word = layers.data(
name='secondw', name='secondw',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
third_word = layers.data( third_word = layers.data(
name='thirdw', name='thirdw',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
forth_word = layers.data( forth_word = layers.data(
name='forthw', name='forthw',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
next_word = layers.data( next_word = layers.data(
name='nextw', name='nextw',
shape=[1], shape=[1],
data_type='int64', data_type='int64',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
embed_first = layers.embedding( embed_first = layers.embedding(
input=first_word, input=first_word,
...@@ -57,16 +57,16 @@ embed_first = layers.embedding( ...@@ -57,16 +57,16 @@ embed_first = layers.embedding(
data_type='float32', data_type='float32',
is_sparse=is_sparse, is_sparse=is_sparse,
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
embed_second = layers.embedding( embed_second = layers.embedding(
input=second_word, input=second_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
is_sparse=is_sparse, is_sparse=is_sparse,
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
embed_third = layers.embedding( embed_third = layers.embedding(
input=third_word, input=third_word,
...@@ -74,42 +74,43 @@ embed_third = layers.embedding( ...@@ -74,42 +74,43 @@ embed_third = layers.embedding(
data_type='float32', data_type='float32',
is_sparse=is_sparse, is_sparse=is_sparse,
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
embed_forth = layers.embedding( embed_forth = layers.embedding(
input=forth_word, input=forth_word,
size=[dict_size, embed_size], size=[dict_size, embed_size],
data_type='float32', data_type='float32',
is_sparse=is_sparse, is_sparse=is_sparse,
param_attr={'name': 'shared_w'}, param_attr={'name': 'shared_w'},
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
concat_embed = layers.concat( concat_embed = layers.concat(
input=[embed_first, embed_second, embed_third, embed_forth], input=[embed_first, embed_second, embed_third, embed_forth],
axis=1, axis=1,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
hidden1 = layers.fc(input=concat_embed, hidden1 = layers.fc(input=concat_embed,
size=hidden_size, size=hidden_size,
act='sigmoid', act='sigmoid',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
predict_word = layers.fc(input=hidden1, predict_word = layers.fc(input=hidden1,
size=dict_size, size=dict_size,
act='softmax', act='softmax',
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
cost = layers.cross_entropy( cost = layers.cross_entropy(
input=predict_word, input=predict_word,
label=next_word, label=next_word,
program=program, main_program=main_program,
init_program=init_program) startup_program=startup_program)
avg_cost = layers.mean(x=cost, program=program, init_program=init_program) avg_cost = layers.mean(
x=cost, main_program=main_program, startup_program=startup_program)
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001) sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost, init_program) opts = sgd_optimizer.minimize(avg_cost, startup_program)
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.dataset.imikolov.train(word_dict, N), batch_size) paddle.dataset.imikolov.train(word_dict, N), batch_size)
...@@ -117,7 +118,7 @@ train_reader = paddle.batch( ...@@ -117,7 +118,7 @@ train_reader = paddle.batch(
place = core.CPUPlace() place = core.CPUPlace()
exe = Executor(place) exe = Executor(place)
exe.run(init_program, feed={}, fetch_list=[]) exe.run(startup_program, feed={}, fetch_list=[])
PASS_NUM = 100 PASS_NUM = 100
for pass_id in range(PASS_NUM): for pass_id in range(PASS_NUM):
for data in train_reader(): for data in train_reader():
...@@ -145,7 +146,7 @@ for pass_id in range(PASS_NUM): ...@@ -145,7 +146,7 @@ for pass_id in range(PASS_NUM):
next_tensor = core.LoDTensor() next_tensor = core.LoDTensor()
next_tensor.set(next_data, place) next_tensor.set(next_data, place)
outs = exe.run(program, outs = exe.run(main_program,
feed={ feed={
'firstw': first_tensor, 'firstw': first_tensor,
'secondw': second_tensor, 'secondw': second_tensor,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册