pgl.utils.paddle_helper module: Some helper function for Paddle.

paddle_helper package contain some simple function to help building paddle models.

pgl.utils.paddle_helper.constant(name, value, dtype, hide_batch_size=True)[source]

Create constant variable with given data.

This function helps to create constants variable with given numpy.ndarray data.

Parameters
  • name – variable name

  • value – numpy.ndarray the value of constant

  • dtype – the type of constant

  • hide_batch_size – If set the first dimenstion as unknown, the explicit batch size may cause some error in paddle. For example, when the value has a shape of (batch_size, dim1, dim2), it will return a variable with shape (-1, dim1, dim2).

Returns

A tuple contain the constant variable and the constant variable initialize function.

Examples

import paddle.fluid as fluid
place = fluid.CPUPlace()
exe = fluid.Executor(place)
constant_var, constant_var_init = constant(name="constant",
                  value=np.array([5.0],
                  dtype="float32"))
exe.run(fluid.default_startup_program())
# Run After default startup
constant_var_init(place)
pgl.utils.paddle_helper.gather(input, index)[source]

Gather input from given index.

Slicing input data with given index. This function rewrite paddle.fluid.layers.gather to fix issue: https://github.com/PaddlePaddle/Paddle/issues/17509 when paddlepaddle’s version is less than 1.5.

Parameters
  • input – Input tensor to be sliced

  • index – Slice index

Returns

A tensor that are sliced from given input data.

pgl.utils.paddle_helper.lod_constant(name, value, lod, dtype)[source]

Create constant lod variable with given data,

This function helps to create constants lod variable with given numpy.ndarray data and lod information.

Parameters
  • name – variable name

  • value – numpy.ndarray the value of constant

  • dtype – the type of constant

  • lod – lod infos of given value.

Returns

A tuple contain the constant variable and the constant variable initialize function.

Examples

import paddle.fluid as fluid
place = fluid.CPUPlace()
exe = fluid.Executor(place)
constant_var, constant_var_init = lod_constant(name="constant",
                  value=np.array([[5.0], [1.0], [2.0]],
                  lod=[2, 1],
                  dtype="float32"))
exe.run(fluid.default_startup_program())
# Run After default startup
constant_var_init(place)
pgl.utils.paddle_helper.scatter_add(input, index, updates)[source]

Scatter add updates to input by given index.

Adds sparse updates to input variables.

Parameters
  • input – Input tensor to be updated

  • index – Slice index

  • updates – Must have same type as input.

Returns

Same type and shape as input.

pgl.utils.paddle_helper.scatter_max(input, index, updates)[source]

Scatter max updates to input by given index.

Adds sparse updates to input variables.

Parameters
  • input – Input tensor to be updated

  • index – Slice index

  • updates – Must have same type as input.

Returns

Same type and shape as input.

pgl.utils.paddle_helper.sequence_softmax(x)[source]

Compute sequence softmax over paddle LodTensor

This function compute softmax normalization along with the length of sequence. This function is an extention of fluid.layers.sequence_softmax which can only deal with LodTensor whose last dimension is 1.

Parameters

x – The input variable which is a LodTensor.

Returns

Output of sequence_softmax