InputSpec

class paddle.static. InputSpec ( shape, dtype='float32', name=None, stop_gradient=False ) [source]

InputSpec describes the signature information of the model input, such as shape , dtype , name .

This interface is often used to specify input tensor information of models in high-level API. It’s also used to specify the tensor information for each input parameter of the forward function decorated by @paddle.jit.to_static.

Parameters
  • shape (tuple(integers)|list[integers]) – List|Tuple of integers declaring the shape. You can set “None” or -1 at a dimension to indicate the dimension can be of any size. For example, it is useful to set changeable batch size as “None” or -1.

  • dtype (np.dtype|str, optional) – The type of the data. Supported dtype: bool, float16, float32, float64, int8, int16, int32, int64, uint8. Default: float32.

  • name (str) – The name/alias of the variable, see Name for more details.

  • stop_gradient (bool, optional) – A boolean that mentions whether gradient should flow. Default is False, means don’t stop calculate gradients.

Examples

>>> import paddle
>>> from paddle.static import InputSpec

>>> input = InputSpec([None, 784], 'float32', 'x')
>>> label = InputSpec([None, 1], 'int64', 'label')

>>> print(input)
InputSpec(shape=(-1, 784), dtype=paddle.float32, name=x, stop_gradient=False)

>>> print(label)
InputSpec(shape=(-1, 1), dtype=paddle.int64, name=label, stop_gradient=False)
classmethod from_tensor ( tensor, name=None )

from_tensor

Generates a InputSpec based on the description of input tensor.

Parameters

tensor (Tensor) – the source tensor to generate a InputSpec instance

Returns

A InputSpec instance generated from Tensor.

Examples

>>> import paddle
>>> from paddle.static import InputSpec

>>> paddle.disable_static()

>>> x = paddle.ones([2, 2], dtype="float32")
>>> x_spec = InputSpec.from_tensor(x, name='x')
>>> print(x_spec)
InputSpec(shape=(2, 2), dtype=paddle.float32, name=x, stop_gradient=False)
classmethod from_numpy ( ndarray, name=None )

from_numpy

Generates a InputSpec based on the description of input np.ndarray.

Parameters

tensor (Tensor) – the source numpy ndarray to generate a InputSpec instance

Returns

A InputSpec instance generated from Tensor.

Examples

>>> import numpy as np
>>> from paddle.static import InputSpec

>>> x = np.ones([2, 2], np.float32)
>>> x_spec = InputSpec.from_numpy(x, name='x')
>>> print(x_spec)
InputSpec(shape=(2, 2), dtype=paddle.float32, name=x, stop_gradient=False)
batch ( batch_size )

batch

Inserts batch_size in front of the shape.

Parameters

batch_size (int) – the inserted integer value of batch size.

Returns

The original InputSpec instance by inserting batch_size in front of shape.

Examples

>>> from paddle.static import InputSpec

>>> x_spec = InputSpec(shape=[64], dtype='float32', name='x')
>>> x_spec.batch(4)
>>> print(x_spec)
InputSpec(shape=(4, 64), dtype=paddle.float32, name=x, stop_gradient=False)
unbatch ( )

unbatch

Removes the first element of shape.

Returns

The original InputSpec instance by removing the first element of shape .

Examples

>>> from paddle.static import InputSpec

>>> x_spec = InputSpec(shape=[4, 64], dtype='float32', name='x')
>>> x_spec.unbatch()
>>> print(x_spec) # InputSpec(shape=(64,), dtype=paddle.float32, name=x)
InputSpec(shape=(64,), dtype=paddle.float32, name=x, stop_gradient=False)

Used in the guide/tutorials