Trainer API

Parameters

paddle.v2.parameters.create(layers)

Create parameter pool by topology.

参数:layers
返回:
class paddle.v2.parameters.Parameters

Parameters is a dictionary contains Paddle’s parameter. The key of Parameters is the name of parameter. The value of Parameters is a plain numpy.ndarry .

Basically usage is

data = paddle.layers.data(...)
...
out = paddle.layers.fc(...)

parameters = paddle.parameters.create(out)

parameter_names = parameters.names()
fc_mat = parameters.get('fc')
print fc_mat
keys()

keys are the names of each parameter.

返回:list of parameter name
返回类型:list
names()

names of each parameter.

返回:list of parameter name
返回类型:list
has_key(key)

has_key return true if there are such parameter name == key

参数:key (basestring) – Parameter name
返回:True if contains such key
get_shape(key)

get shape of the parameter.

参数:key (basestring) – parameter name
返回:parameter’s shape
返回类型:tuple
get(parameter_name)

Get parameter by parameter name.

Note:It will always copy the parameter from C++ side.
参数:parameter_name (basestring) – parameter name
返回:The parameter matrix.
返回类型:np.ndarray
set(parameter_name, value)

Set parameter by parameter name & matrix.

参数:
  • parameter_name (basestring) – parameter name
  • value (np.ndarray) – parameter matrix
返回:

Nothing.

append_gradient_machine(gradient_machine)

append gradient machine to parameters. This method is used internally in Trainer.train.

参数:gradient_machine (api.GradientMachine) – Paddle C++ GradientMachine object.
返回:
serialize(name, f)
参数:
  • name
  • f (file) –
返回:

deserialize(name, f)
参数:
  • name
  • f (file) –
返回:

Trainer

class paddle.v2.trainer.SGD(cost, parameters, update_equation)

Simple SGD Trainer. TODO(yuyang18): Complete comments

参数:
  • update_equation (paddle.v2.optimizer.Optimizer) – The optimizer object.
  • cost (paddle.v2.config_base.Layer) – Target cost that neural network should be optimized.
  • parameters (paddle.v2.parameters.Parameters) – The parameters dictionary.
train(reader, num_passes=1, event_handler=None, feeding=None)

Training method. Will train num_passes of input data.

参数:
  • reader
  • num_passes – The total train passes.
  • event_handler ((BaseEvent) => None) – Event handler. A method will be invoked when event occurred.
  • feeding (dict) – Feeding is a map of neural network input name and array index that reader returns.
返回:

Event

All training events.

There are:

  • BeginIteration
  • EndIteration
  • BeginPass
  • EndPass

TODO(yuyang18): Complete it!

class paddle.v2.event.TestResult(evaluator, cost)

Result that trainer.test return.

class paddle.v2.event.BeginPass(pass_id)

Event On One Pass Training Start.

class paddle.v2.event.EndPass(pass_id, evaluator)

Event On One Pass Training Complete.

class paddle.v2.event.BeginIteration(pass_id, batch_id)

Event On One Batch Training Start.

class paddle.v2.event.EndIteration(pass_id, batch_id, cost, evaluator)

Event On One Batch Training Complete.

Inference

paddle.v2.infer(output, parameters, input=None, batch_size=None, reader=None, feeding=None, field='value')

Infer a neural network by given neural network output and parameters. The user should pass either a batch of input data or reader method.

Example usages:

result = paddle.infer(prediction, parameters, input=SomeData,
                      batch_size=32)
print result
参数:
  • output (paddle.v2.config_base.Layer) – output of the neural network that would be inferred
  • parameters (paddle.v2.parameters.Parameters) – parameters of the neural network.
  • input (collections.Iterable) – input data batch. Should be a python iterable object, and each element is the data batch.
  • batch_size (int) – the batch size when perform inference. Default is the length of input.
  • reader (callable) – input data reader creator in batch. If this field is set, the input and batch_size will be ignored.
  • feeding – Reader dictionary. Default could generate from input value.
  • field (str) – The prediction field. It should in [value, ids]. value means return the prediction probabilities, ids means return the prediction labels. Default is value
返回:

a numpy array

返回类型:

numpy.ndarray