coremltools.models.neural_network.builder¶
Neural network builder class to construct Core ML models.
Classes
NeuralNetworkBuilder ([input_features, …]) |
Neural network builder class to construct Core ML models. |
-
class
coremltools.models.neural_network.builder.
NeuralNetworkBuilder
(input_features=None, output_features=None, mode=None, spec=None, nn_spec=None, disable_rank5_shape_mapping=False, training_features=None, use_float_arraytype=False)¶ Neural network builder class to construct Core ML models.
The NeuralNetworkBuilder constructs a Core ML neural network specification layer by layer. The layers should be added in such an order that the inputs to each layer (referred to as blobs) of each layer has been previously defined. The builder can also set pre-processing steps to handle specialized input format (e.g. images), and set class labels for neural network classifiers. Refer to the protobuf messages in specification (NeuralNetwork.proto) for more details.
See also
MLModel
,datatypes
,save_spec
Examples
from coremltools.models.neural_network import datatypes, NeuralNetworkBuilder from coremltools.models.utils import save_spec # Create a neural network binary classifier that classifies # 3-dimensional data points # Specify input and output dimensions >>> input_dim = (3,) >>> output_dim = (2,) # Specify input and output features >>> input_features = [('data', datatypes.Array(*input_dim))] >>> output_features = [('probs', datatypes.Array(*output_dim))] # Build a simple neural network with 1 inner product layer >>> builder = NeuralNetworkBuilder(input_features, output_features) >>> builder.add_inner_product(name='ip_layer', W=weights, b=bias, input_channels=3, output_channels=2, ... has_bias=True, input_name='data', output_name='probs') # save the spec by the builder >>> save_spec(builder.spec, 'network.mlmodel')
-
__init__
(self, input_features=None, output_features=None, mode=None, spec=None, nn_spec=None, disable_rank5_shape_mapping=False, training_features=None, use_float_arraytype=False)¶ Construct a NeuralNetworkBuilder object to build an MLModel specification with model interface or a NeuralNetwork protobuf message, either from scratch or an existing specification.
Parameters: - input_features: [(str, datatypes.Array)] or None
List of input feature of the network. Each feature is a (name, array) tuple, where name the name of the feature, and array is an datatypes.Array object describing the feature type. When spec is None (building from scratch), input_features must not be None. When spec is not None, input_features will be ignored; input feature of existing spec will be used instead.
- output_features: [(str, datatypes.Array or None)] or None
List of output feature of the network. Each feature is a (name, array) tuple, where name is the name of the feature, and array is an datatypes.Array object describing the feature type. array can be None if not known. When spec is None (building from scratch), output_features must not be None. When spec is not None, output_features will be ignored; output feature of existing spec will be used instead.
- mode: str (‘classifier’, ‘regressor’ or None)
Mode (one of ‘classifier’, ‘regressor’, or None).
When mode = ‘classifier’, a NeuralNetworkClassifier spec will be constructed. When mode = ‘regressor’, a NeuralNetworkRegressor spec will be constructed.
- disable_rank5_shape_mapping: bool
Only applicable for neural networks. If True, inputs are no longer forced to map to rank 5 tensors (rank is equal to the length of the shape of the tensor). Instead, for multi-array inputs “EXACT_ARRAY_MAPPING” mapping is used, whereas for image inputs “RANK4_IMAGE_MAPPING” is used. For details, see description of enums “NeuralNetworkMultiArrayShapeMapping” and “NeuralNetworkImageShapeMapping” in NeuralNetwork.proto. When spec is not None, this argument will be ignored.
- spec: None or coremltools.proto.Model_pb2
If None, a new MLModel spec will be created by the builder with input and output features. Otherwise, the builder will continue to build on spec. This is useful when the MLModel is built incrementally.
- nn_spec: None or coremltools.proto.NeuralNetwork_pb2
If None, a new, empty NeuralNetwork proto will be created for spec. If nn_spec is not None and spec is None, the builder will build a NeuralNetwork spec without wrapping it within an MLModel. This is useful to create nested NeuralNetworks for models with control flow operations.
- use_float_arraytype: bool
If true, the datatype of input/output multiarrays is set to Float32 instead of double.
See also
Examples
# Construct a builder that builds a neural network classifier with a 299 x 299 x 3 # dimensional input and 1000 dimensional output >>> input_features = [('data', datatypes.Array((299, 299, 3)))] >>> output_features = [('probs', datatypes.Array((1000,)))] >>> builder = NeuralNetworkBuilder(input_features, output_features, mode='classifier')
-
add_acos
(self, name, input_name, output_name)¶ Add an acos layer to the model that computes element-wise arc-cosine for the input tensor. Refer to the AcosLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_acosh
(self, name, input_name, output_name)¶ Add an acosh layer to the model that computes element-wise inverse hyperbolic cosine for the input tensor. Refer to the AcoshLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_activation
(self, name, non_linearity, input_name, output_name, params=None)¶ Add an activation layer to the model. Refer to the specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- non_linearity: str
The non_linearity (activation) function of this layer. It can be one of the following:
‘RELU’: Rectified Linear Unit (ReLU) function.
‘SIGMOID’: sigmoid function.
‘TANH’: tanh function.
‘SCALED_TANH’: scaled tanh function, defined as:
f(x) = alpha * tanh(beta * x)
where alpha and beta are constant scalars.
‘SOFTPLUS’: softplus function.
‘SOFTSIGN’: softsign function.
‘SIGMOID_HARD’: hard sigmoid function, defined as:
f(x) = min(max(alpha * x + beta, -1), 1)
where alpha and beta are constant scalars.
‘LEAKYRELU’: leaky relu function, defined as:
f(x) = (x >= 0) * x + (x < 0) * alpha * x
where alpha is a constant scalar.
‘PRELU’: Parametric ReLU function, defined as:
f(x) = (x >= 0) * x + (x < 0) * alpha * x
where alpha is a multi-dimensional array of same size as x.
‘ELU’: Exponential linear unit function, defined as:
f(x) = (x >= 0) * x + (x < 0) * (alpha * exp(x) - 1)
where alpha is a constant scalar.
‘PARAMETRICSOFTPLUS’: Parametric softplus function, defined as:
f(x) = alpha * log(1 + exp(beta * x))
where alpha and beta are two multi-dimensional arrays of same size as x.
‘THRESHOLDEDRELU’: Thresholded ReLU function, defined as:
f(x) = (x >= alpha) * x
where alpha is a constant scalar.
‘LINEAR’: linear function.
f(x) = alpha * x + beta
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- params: list of float or numpy.array
Parameters for the activation, depending on non_linearity.
- When non_linearity is one of [‘RELU’, ‘SIGMOID’, ‘TANH’, ‘SCALED_TANH’, ‘SOFTPLUS’, ‘SOFTSIGN’], params is ignored.
- When non_linearity is one of [‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’], param is a list of 2 floats [alpha, beta].
- When non_linearity is one of [‘LEAKYRELU’, ‘ELU’, ‘THRESHOLDEDRELU’], param is a list of 1 float [alpha].
- When non_linearity is ‘PRELU’, param is a list of 1 numpy array [alpha]. The shape of alpha is (C,), where C is either the number of input channels or 1. When C = 1, same alpha is applied to all channels.
- When non_linearity is ‘PARAMETRICSOFTPLUS’, param is a list of 2 numpy arrays [alpha, beta]. The shape of alpha and beta is (C, ), where C is either the number of input channels or 1. When C = 1, same alpha and beta are applied to all channels.
See also
-
add_add_broadcastable
(self, name, input_names, output_name)¶ Add an add_broadcastable layer to the model that performs element-wise addition operation with broadcast support. Refer to the AddBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_argmax
(self, name, input_name, output_name, axis, keepdims=True)¶ Add an argmax layer to the model that returns the indices of the maximum value along a specified axis in the input tensor. Refer to the ArgMaxLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
axis along which the argmax is computed. Negative indexing is supported.
- keepdims: bool, optional
if true, output rank is same as input rank, default: true.
See also
-
add_argmin
(self, name, input_name, output_name, axis, keepdims=True)¶ Add an argmin layer to the model that returns the indices of the minimum value along a specified axis in the input tensor. Refer to the ArgMinLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
axis along which the argmin is computed. Negative indexing is supported.
- keepdims: bool, optional
if true, output rank is same as input rank, default: true.
See also
-
add_asin
(self, name, input_name, output_name)¶ Add an asin layer to the model that computes element-wise arc-sine for the input tensor. Refer to the AsinLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_asinh
(self, name, input_name, output_name)¶ Add an asinh layer to the model that computes element-wise inverse hyperbolic sine for the input tensor. Refer to the AsinhLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_atan
(self, name, input_name, output_name)¶ Add an atan layer to the model that computes element-wise arc-tangent for the input tensor. Refer to the AtanLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_atanh
(self, name, input_name, output_name)¶ Add an atanh layer to the model that computes element-wise inverse hyperbolic tangent for the input tensor. Refer to the AtanhLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_batched_mat_mul
(self, name, input_names, output_name, transpose_a=False, transpose_b=False, weight_matrix_rows=0, weight_matrix_columns=0, W=None, bias=None, is_quantized_weight=False, quantization_type='linear', nbits=8, quant_scale=None, quant_bias=None, quant_lut=None)¶ Add a N-D Batched Matrix Multiplication layer with numpy like broadcasting. Refer to the BatchedMatMulLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- transpose_a: bool, optional
Whether or not to transpose A, default: false.
- transpose_b: bool, optional
Whether or not to transpose B, default: false.
- weight_matrix_rows: int, optional
Must be equal to the last dimension of the input, default: 0.
- weight_matrix_columns: int, optional
Must be equal to the last dimension of the output, default: 0.
- W: float32 numpy.array or bytes(), optional
Weight matrix of shape (weight_matrix_rows, weight_matrix_columns) If W is of type bytes(), i.e. quantized to 1-8 bits, other quantization related arguments must be provided as well (see below).
- bias: float32 numpy.array, optional
Bias vector of shape (weight_matrix_columns,).
- Quantization arguments expected in kwargs, when W is of type bytes():
- is_quantized_weight: bool, optional
Set it to true when W is of type bytes(), representing quantized weights, default: false.
- quantization_type: str, optional
When weights are quantized (i.e. W is of type bytes()), this should be either “linear” or “lut”, default: linear.
- nbits: int, optional
Should be between 1 and 8 (inclusive). Number of bits per weight value, default: 8.
- quant_scale: numpy.array(dtype=numpy.float32), optional
scale vector to be used with linear quantization. Must be of length either 1 or weight_matrix_columns, default: None.
- quant_bias: numpy.array(dtype=numpy.float32), optional
bias vector to be used with linear quantization. Must be of length either 1 or weight_matrix_columns, default: None.
- quant_lut: numpy.array(dtype=numpy.float32), optional
The LUT (look up table) to be used with LUT quantization. Must be of length 2^nbits, default: None.
See also
-
add_batchnorm
(self, name, channels, gamma, beta, mean=None, variance=None, input_name='data', output_name='out', compute_mean_var=False, instance_normalization=False, epsilon=1e-05)¶ Add a batch normalization layer. Batch normalization operation is defined as:
y = gamma * (x - mean) / sqrt(variance + epsilon) + beta
Refer to the BatchnormLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- channels: int
Number of channels of the input blob.
- gamma: numpy.array
Values of gamma. Must be numpy array of shape (channels, ).
- beta: numpy.array
Values of beta. Must be numpy array of shape (channels, ).
- mean: numpy.array
Means of the input blob on each channel. Must be numpy array of shape (channels, ).
- variance:
Variances of the input blob on each channel. Must be numpy array of shape (channels, ).
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- compute_mean_var: bool
Set to True if mean and variance is to be computed from the input data.
- instance_normalization: bool
Set compute_mean_var and this to True to perform instance normalization i.e., mean and variance are computed from the single input instance.
- epsilon: float
Value of epsilon. Defaults to 1e-5 if not specified.
See also
-
add_bias
(self, name, b, input_name, output_name, shape_bias=None)¶ Add a bias layer to the model. Refer to the BiasLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- b: int or numpy.array
Bias to add to the input.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- shape_bias: list of int
List of ints that specifies the shape of the bias parameter (if present). Can be [1] or [C] or [1,H,W] or [C,H,W].
See also
-
add_bidirlstm
(self, name, W_h, W_x, b, W_h_back, W_x_back, b_back, hidden_size, input_size, input_names, output_names, inner_activation='SIGMOID', cell_state_update_activation='TANH', output_activation='TANH', peep=None, peep_back=None, output_all=False, forget_bias=False, coupled_input_forget_gate=False, cell_clip_threshold=50000.0)¶ Add a Bi-directional LSTM layer to the model. Refer to the BiDirectionalLSTMLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W_h: [numpy.array]
List of recursion weight matrices for the forward layer. The ordering is [R_i, R_f, R_o, R_z], where R_i, R_f, R_o, R_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, hidden_size).
- W_x: [numpy.array]
List of input weight matrices for the forward layer. The ordering is [W_i, W_f, W_o, W_z], where W_i, W_f, W_o, W_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, input_size).
- b: [numpy.array]
List of biases for the forward layer. The ordering is [b_i, b_f, b_o, b_z], where b_i, b_f, b_o, b_z are biases at input gate, forget gate, output gate and cell gate. If None, biases are ignored. Otherwise the shapes of the biases are (hidden_size, ).
- W_h_back: [numpy.array]
List of recursion weight matrices for the backward layer. The ordering is [R_i, R_f, R_o, R_z], where R_i, R_f, R_o, R_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, hidden_size).
- W_x_back: [numpy.array]
List of input weight matrices for the backward layer. The ordering is [W_i, W_f, W_o, W_z], where W_i, W_f, W_o, W_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, input_size).
- b_back: [numpy.array]
List of biases for the backward layer. The ordering is [b_i, b_f, b_o, b_z], where b_i, b_f, b_o, b_z are biases at input gate, forget gate, output gate and cell gate. The shapes of the biases (hidden_size).
- hidden_size: int
Number of hidden units. This is equal to the number of channels of output shape.
- input_size: int
Number of the number of channels of input shape.
- input_names: list of str
The input blob names of this layer, in the order of [x, h_input, c_input, h_reverse_input, c_reverse_input].
- output_names: list of str
The output blob names of this layer, in the order of [y, h_output, c_output, h_reverse_output, c_reverse_output].
- inner_activation: str
Inner activation function used at input and forget gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. Defaults to ‘SIGMOID’.
- cell_state_update_activation: str
Cell state update activation function used at the cell state update gate. [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. Defaults to ‘TANH’.
- output_activation: str
Activation function used at the output gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. Defaults to ‘TANH’.
- peep: [numpy.array] or None
List of peephole vectors for the forward layer. The ordering is [p_i, p_f, p_o], where p_i, p_f, and p_o are peephole vectors at input gate, forget gate, output gate. The shapes of the peephole vectors are (hidden_size,). Defaults to None.
- peep_back: [numpy.array] or None
List of peephole vectors for the backward layer. The ordering is [p_i, p_f, p_o], where p_i, p_f, and p_o are peephole vectors at input gate, forget gate, output gate. The shapes of the peephole vectors are (hidden_size,). Defaults to None.
- output_all: boolean
Whether the LSTM layer should output at every time step. Defaults to False.
- If False, the output is the result after the final state update.
- If True, the output is a sequence, containing outputs at all time steps.
- forget_bias: boolean
If True, a vector of 1s is added to forget gate bias. Defaults to False.
- coupled_input_forget_gate: boolean
If True, the input gate and forget gate is coupled. i.e. forget gate is not used. Defaults to False.
- cell_clip_threshold: float
The limit on the maximum and minimum values on the cell state. Defaults to 50.0.
See also
-
add_branch
(self, name, input_name, if_branch=None, else_branch=None)¶ Add a branch layer to the model that provides the functionality of branching or an
if-else
block. Refer to the BranchLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- if_branch: NeuralNetwork
Neural network to execute if the absolute value of the input tensor is greater than 1e-6.
- else_branch: NeuralNetwork, optional
Neural network to execute if the absolute value of the input tensor is less than 1e-6.
See also
-
add_broadcast_to_dynamic
(self, name, input_names, output_name)¶ Add a broadcast_to_dynamic layer to the model that broadcasts a tensor to a compatible shape. Refer to the BroadcastToDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_broadcast_to_like
(self, name, input_names, output_name)¶ Add a broadcast_to_like layer to the model that broadcasts a tensor to a compatible shape. Refer to the BroadcastToLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_broadcast_to_static
(self, name, input_name, output_name, output_shape)¶ Add a broadcast_to_static layer to the model that broadcasts a tensor to a compatible shape. Refer to the BroadcastToStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
The target shape of the output tensor.
-
add_categorical_distribution
(self, name, input_name, output_name, num_samples, is_logits=True, eps=1e-10, temperature=1.0, seed=-1)¶ Add a categorical_distribution layer to the model that fills the output tensor with random values from categorical distribution. Refer to the CategoricalDistributionLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- num_samples: int
List of dimensions for the reduce operations.
- is_logits: bool, optional
If true, the input is log probabilities. If false, the input is probabilities, default: True
- eps: float, optional
Epsilon parameter for categorical distribution, default 1e-10.
- temperature: float, optional
Temperature parameter for categorical distribution, default 1.0.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_ceil
(self, name, input_name, output_name)¶ Add a ceil layer to the model that performs element-wise ceil operation on the input tensor that rounds the value to the smallest integer not less than x. Refer to the CeilLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_clip
(self, name, input_name, output_name, min_value=0.0, max_value=1.0)¶ Add a clip layer to the model that performs element-wise clip operation. Clip the values in the input tensor to the range [min_value, max_value]. Refer to the ClipLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- min_value: float, optional
Lower bound / minimum value for clip, default: 0.0.
- max_value: float, optional
Upper bound / maximum value for clip, default: 1.0.
-
add_concat_nd
(self, name, input_names, output_name, axis)¶ Add a concat_nd layer to the model that performs concatenation along the given axis. Refer to the ConcatNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
Axis to perform the concat operation on.
-
add_constant_pad
(self, name, input_names, output_name, value=0.0, pad_to_given_output_size_mode=False, pad_amounts=[])¶ Add a constant pad layer. Refer to the ConstantPaddingLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob name(s) of this layer.
- output_name: str
The output blob name of this layer.
- value: float
value to be used for padding.
- pad_to_given_output_size_mode: bool
if true, pad_amounts are interpreted as output shapes (see example in NeuralNetwork.proto)
- pad_amounts: [int], optional
must be non negative. Amount to pad in each dimension. Length of the list must be twice the input/output rank. Not required if second input is present.
See also
-
add_convolution
(self, name, kernel_channels, output_channels, height, width, stride_height, stride_width, border_mode, groups, W, b, has_bias, is_deconv=False, output_shape=None, input_name='data', output_name='out', dilation_factors=[1, 1], padding_top=0, padding_bottom=0, padding_left=0, padding_right=0, same_padding_asymmetry_mode='BOTTOM_RIGHT_HEAVY', **kwargs)¶ Add a convolution layer to the network. Refer to the ConvolutionLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- kernel_channels: int
Number of channels for the convolution kernels.
- output_channels: int
Number of filter kernels. This is equal to the number of channels in the output blob.
- height: int
Height of each kernel.
- width: int
Width of each kernel.
- stride_height: int
Stride along the height direction.
- stride_width: int
Stride along the height direction.
- border_mode: str
Option for the padding type and output blob shape. Can be either ‘valid’ or ‘same’.
- groups: int
Number of kernel groups. Input is divided into groups along the channel axis. Each kernel group share the same weights.
- W: numpy.array or bytes() or None
Weight of the convolution kernels.
- If is_deconv is False, W should have shape (height, width, kernel_channels, output_channels), where kernel_channel = input_channels / groups
- If is_deconv is True, W should have shape (height, width, kernel_channels, output_channels / groups), where kernel_channel = input_channels
If W is of type bytes(), i.e. quantized, other quantization related arguments must be provided as well (see below). For Core ML specification version >=4, W can be None. In this case, the convolution layer takes 2 inputs, where the 1st input represents the input feature map, the 2nd input represents the weight blob.
- b: numpy.array
Biases of the convolution kernels. b should have shape (outputChannels, ).
- has_bias: boolean
Whether bias is ignored.
- If True, bias is not ignored.
- If False, bias is ignored.
- is_deconv: boolean
Whether the convolution layer is performing a convolution or a transposed convolution (deconvolution).
- If True, the convolution layer is performing transposed convolution.
- If False, the convolution layer is performing regular convolution.
- output_shape: tuple or None
Either None or a 2-tuple, specifying the output shape (output_height, output_width). Used only when is_deconv == True. When is_deconv == False, this parameter is ignored. If it is None, the output shape is calculated automatically using the border_mode.
- input_name: str or list of str
The input blob name(s) of this layer.
- output_name: str
The output blob name of this layer.
- dilation_factors: list of int
Dilation factors across height and width directions. Must be a list of two positive integers. Defaults to [1, 1]
- padding_top, padding_bottom, padding_left, padding_right: int
values of height (top, bottom) and width (left, right) padding to be used if border_more is “valid”.
- same_padding_asymmetry_mode: str.
Type of asymmetric padding to be used when border_mode is ‘same’. Can be either ‘BOTTOM_RIGHT_HEAVY’ or ‘TOP_LEFT_HEAVY’.
- Depthwise convolution is a special case of convolution, where we have:
kernel_channels = 1 (== input_channels / groups) output_channels = channel_multiplier * input_channels groups = input_channels W: [Kernel_height, Kernel_width, 1, channel_multiplier * input_channels]
- Quantization arguments expected in kwargs, when W is of type bytes():
- quantization_type: str
When weights are quantized (i.e. W is of type bytes()), this should be either “linear” or “lut”.
- nbits: int
Should be between 1 and 8 (inclusive). Number of bits per weight value. Only applicable when weights are quantized.
- quant_scale: numpy.array(dtype=numpy.float32)
scale vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_bias: numpy.array(dtype=numpy.float32)
bias vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_lut: numpy.array(dtype=numpy.float32)
the LUT (look up table) to be used with LUT quantization. Must be of length 2^nbits.
See also
-
add_copy
(self, name, input_name, output_name)¶ Add a copy layer to the model that copies its input tensor to the output tensor. Input tensor and output tensor must have distinct names. Refer to the CopyLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_cos
(self, name, input_name, output_name)¶ Add a cos layer to the model that computes element-wise cosine for the input tensor. Refer to the CosLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_cosh
(self, name, input_name, output_name)¶ Add a osh layer to the model that computes element-wise hyperbolic cosine for the input tensor. Refer to the CoshLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_crop
(self, name, left, right, top, bottom, offset, input_names, output_name)¶ Add a cropping layer to the model. The cropping layer have two functional modes:
- When it has 1 input blob, it crops the input blob based on the 4 parameters [left, right, top, bottom].
- When it has 2 input blobs, it crops the first input blob based on the dimension of the second blob with an offset.
Refer to the CropLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- left: int
Number of elements to be cropped on the left side of the input blob. When the crop layer takes 2 inputs, this parameter is ignored.
- right: int
Number of elements to be cropped on the right side of the input blob. When the crop layer takes 2 inputs, this parameter is ignored.
- top: int
Number of elements to be cropped on the top of the input blob. When the crop layer takes 2 inputs, this parameter is ignored.
- bottom: int
Number of elements to be cropped on the bottom of the input blob. When the crop layer takes 2 inputs, this parameter is ignored.
- offset: list of int
Offset along the height and width directions when the crop layer takes 2 inputs. Must be a list of length 2. When the crop layer takes 1 input, this parameter is ignored.
- input_names: list of str
The input blob names of this layer. Must be either a list of 1 string (1 input crop layer), or a list of 2 strings (2-input crop layer).
- output_name: str
The output blob name of this layer.
See also
-
add_crop_resize
(self, name, input_names, output_name, target_height=1, target_width=1, mode='STRICT_ALIGN_ENDPOINTS_MODE', normalized_roi=False, box_indices_mode='CORNERS_HEIGHT_FIRST', spatial_scale=1.0)¶ Add a crop resize layer to the model. A layer that extracts cropped spatial patches or RoIs (regions of interest) from the input and resizes them to a pre-specified size using bilinear interpolation. Note that RoI Align layer can be implemented with this layer followed by a pooling layer. Refer to the CropResizeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
Must be a list of two names: image feature map and crop indices/RoI input. First input corresponds to a blob with shape
[1, Batch, C, H_in, W_in]
. This represents a batch of input image feature data with C channels. The second input shape must be[N, 1, 4, 1, 1]
or[N, 1, 5, 1, 1]
. This represents the bounding box coordinates for N patches/RoIs. N: number of patches/RoIs to be extracted If RoI shape = [N, 1, 4, 1, 1] The channel axis corresponds to the four coordinates specifying the bounding box. All the N~ RoIs are extracted from all the batches of the input. If RoI shape = [N, 1, 5, 1, 1] The first element of the channel axis specifies the input batch id from which to extract the RoI and must be in the interval[0, Batch - 1]
. That is, n-th RoI is extracted from the RoI[n,0,0,0]-th input batch id. The last four elements of the channel axis specify the bounding box coordinates.- output_name: str
The output blob name of this layer.
- target_height: int
Output height dimension.
- target_width: int
Output width dimension.
- mode: str
Following values are supported: ‘STRICT_ALIGN_ENDPOINTS_MODE’, ‘ALIGN_ENDPOINTS_MODE’, ‘UPSAMPLE_MODE’, ‘ROI_ALIGN_MODE’. This parameter determines the sampling grid used for bilinear interpolation.
- normalized_roi: bool
If true the bounding box coordinates must be in the interval [0, 1]. They are scaled by (input_height - 1), (input_width - 1), i.e. based on the input spatial dimensions. If false the bounding box coordinates must be in the interval [0, input_height - 1] and [0, input_width - 1], respectively for height and width dimensions.
- box_indices_mode: str
Following values are supported: ‘CORNERS_HEIGHT_FIRST’, ‘CORNERS_WIDTH_FIRST’, ‘CENTER_SIZE_HEIGHT_FIRST’, ‘CENTER_SIZE_WIDTH_FIRST’ Representation used to interpret the bounding box coordinates (RoI) input. ‘CORNERS_HEIGHT_FIRST’: [h_start, w_start, h_end, w_end] ‘CORNERS_WIDTH_FIRST’: [w_start, h_start, w_end, h_end] ‘CENTER_SIZE_HEIGHT_FIRST’: [h_center, w_center, box_height, box_width] ‘CENTER_SIZE_WIDTH_FIRST’: [w_center, h_center, box_width, box_height]
- spatial_scale: float
Additional spatial scale that multiplies the bounding box coordinates. Generally used while implementing the RoI Align layer, which uses unnormalized RoI coordinates along with a spatial scale less than or equal to 1.
See also
-
add_custom
(self, name, input_names, output_names, custom_proto_spec=None)¶ Add a custom layer. Refer to the CustomLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names to this layer.
- output_names: list of str
The output blob names from this layer.
- custom_proto_spec: CustomLayerParams
A protobuf CustomLayerParams message. This can also be left blank and filled in later.
-
add_divide_broadcastable
(self, name, input_names, output_name)¶ Add a divide_broadcastable layer to the model that performs element-wise division operation with broadcast support. Refer to the DivideBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_elementwise
(self, name, input_names, output_name, mode, alpha=None)¶ Add an element-wise operation layer to the model. Refer to the specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
A list of input blob names of this layer. The input blobs should have the same shape.
- output_name: str
The output blob name of this layer.
- mode: str
A string specifying the mode of the elementwise layer. It can be one of the following:
- ‘CONCAT’: concatenate input blobs along the channel axis.
- ‘SEQUENCE_CONCAT’: concatenate input blobs along the sequence axis.
- ‘ADD’: perform an element-wise summation over the input blobs.
- ‘MULTIPLY’: perform an element-wise multiplication over the input blobs.
- ‘DOT’: compute the dot product of the two input blobs. In this mode, the length of input_names should be 2.
- ‘COS’: compute the cosine similarity of the two input blobs. In this mode, the length of input_names should be 2.
- ‘MAX’: compute the element-wise maximum over the input blobs.
- ‘MIN’: compute the element-wise minimum over the input blobs.
- ‘AVE’: compute the element-wise average over the input blobs.
- alpha: float
if mode == ‘ADD’ and there is only one input_name, alpha is added to the input if mode == ‘MULTIPLY’ and there is only one input_name, alpha is multiplied to the input
See also
-
add_embedding
(self, name, W, b, input_dim, output_channels, has_bias, input_name, output_name, is_quantized_weight=False, quantization_type='linear', nbits=8, quant_scale=None, quant_bias=None, quant_lut=None)¶ Add an embedding layer to the model. Refer to the EmbeddingLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W: float32 numpy.array or bytes()
Weight matrix of shape (output_channels, input_dim). If W is of type bytes(), i.e. quantized to 1-8 bits, other quantization related arguments must be provided as well (see below).
- b: numpy.array
Bias vector of shape (output_channels, ).
- input_dim: int
Size of the vocabulary (1 + maximum integer index of the words).
- output_channels: int
Number of output channels.
- has_bias: boolean
Whether the bias vector of this layer is ignored in the spec.
- If True, the bias vector of this layer is not ignored.
- If False, the bias vector is ignored.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- Quantization arguments expected, when W is of type bytes():
- is_quantized_weight: bool
Set it to true when W is of type bytes(), representing quantized weights
- quantization_type: str
When weights are quantized (i.e. W is of type bytes()), this should be either “linear” or “lut”.
- nbits: int
Should be between 1 and 8 (inclusive). Number of bits per weight value.
- quant_scale: numpy.array(dtype=numpy.float32)
scale vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_bias: numpy.array(dtype=numpy.float32)
bias vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_lut: numpy.array(dtype=numpy.float32)
the LUT (look up table) to be used with LUT quantization. Must be of length 2^nbits.
See also
-
add_embedding_nd
(self, name, input_name, output_name, vocab_size, embedding_size, W, b=None, is_quantized_weight=False, quantization_type='linear', nbits=8, quant_scale=None, quant_bias=None, quant_lut=None)¶ Add an embedding layer to the model that performs a matrix lookup and optionally adds a bias. Refer to the EmbeddingNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- vocab_size: int
Size of the vocabulary (1 + maximum integer index of the words).
- embedding_size: int
Size of the embedded vector.
- W: float32 numpy.array or bytes()
Weight matrix of shape (embedding_size, vocab_size). If W is of type bytes(), i.e. quantized to 1-8 bits, other quantization related arguments must be provided as well (see below).
- b: numpy.array , optional
Bias vector of shape (embedding_size, ).
- Quantization arguments expected, when W is of type bytes():
- is_quantized_weight: bool
Set it to true when W is of type bytes(), representing quantized weights
- quantization_type: str
When weights are quantized (i.e. W is of type bytes()), this should be either “linear” or “lut”.
- nbits: int
Should be between 1 and 8 (inclusive). Number of bits per weight value.
- quant_scale: numpy.array(dtype=numpy.float32)
scale vector to be used with linear quantization. Must be of length either 1 or embedding_size.
- quant_bias: numpy.array(dtype=numpy.float32)
bias vector to be used with linear quantization. Must be of length either 1 or embedding_size.
- quant_lut: numpy.array(dtype=numpy.float32)
the LUT (look up table) to be used with LUT quantization. Must be of length 2^nbits.
See also
-
add_equal
(self, name, input_names, output_name, alpha=0.0)¶ Add an equal layer to the model that performs the element-wise equal (=) operation. Broadcasting is supported. Refer to the EqualLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- alpha: float, optional
y = x1 != alpha, if only one input is provided, default: 0.
See also
-
add_erf
(self, name, input_name, output_name)¶ Add an erf function (gaussian error function) layer to the model. Refer to the ErfLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_exp2
(self, name, input_name, output_name)¶ Add an exp2 layer to the model that performs element-wise experiential operation. Refer to the Exp2LayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_expand_dims
(self, name, input_name, output_name, axes)¶ Add an expand dims layer to the model that increases the rank of the input tensor by adding unit dimensions. Refer to the ExpandDimsLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int
Dimensions the operation perform on.
See also
-
add_fill_dynamic
(self, name, input_name, output_name, value=0.0)¶ Add a fill_dynamic layer to the model that outputs a tensor filled with a scalar value. Refer to the FillDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- value: float, optional
A scalar value for the fill operation, default: 0.
See also
-
add_fill_like
(self, name, input_name, output_name, value=0.0)¶ Add a fill_like layer to the model outputs a tensor filled with a scalar value. Refer to the FillLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- value: float, optional
A scalar value for the fill operation, default 0.
See also
-
add_fill_static
(self, name, output_name, output_shape, value=0.0)¶ Add a fill_static layer to the model that outputs a tensor filled with a scalar value given shape as parameter. Refer to the FillStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
The target shape of the output tensor.
- value: float, optional
A scalar value for the fill operation, default 0.
See also
-
add_flatten
(self, name, mode, input_name, output_name)¶ Add a flatten layer. Only flattens the channel, height and width axis. Leaves the sequence axis as is. Refer to the FlattenLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- mode: int
- If mode == 0, the flatten layer is in CHANNEL_FIRST mode.
- If mode == 1, the flatten layer is in CHANNEL_LAST mode.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_flatten_to_2d
(self, name, input_name, output_name, axis=1)¶ Add a flatten_to_2d layer to the model that flattens the input tensor into a 2-dimensional matrix. Refer to the FlattenTo2DLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The of input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: int, optional
Axis to perform the operation, default: 1.
See also
-
add_floor
(self, name, input_name, output_name)¶ Add a floor layer to the model that performs element-wise floor operation on the input tensor that rounds the value to the largest integer not greater than x. Refer to the FloorLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_floor_div_broadcastable
(self, name, input_names, output_name)¶ Add a floor_div_broadcastable layer to the model that performs floor division operation with broadcast support. Refer to the FloorDivBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_gather
(self, name, input_names, output_name, axis=0)¶ Add a gather layer to the model that gathers elements or slices from data and store to a tensor whose shape is defined by indices from the input. Refer to the GatherLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int, optional
The axis the operation perform on, default: 0.
-
add_gather_along_axis
(self, name, input_names, output_name, axis=0)¶ Add a gather_along_axis layer to the model that gathers elements or slices from data and store to a tensor whose shape is defined by indices from the input along the given axis into the output tensor. Refer to the GatherAlongAxisLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int, optional
The axis the operation perform on, default: 0.
-
add_gather_nd
(self, name, input_names, output_name)¶ Add a gather layer to the model that gathers elements or slices from data and store to a tensor whose shape is defined by indices from the input. This is the reverse operation of the scatter operation. Refer to the GatherNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_gelu
(self, name, input_name, output_name, mode='EXACT')¶ Add a GELU (gaussian error linear unit) activation layer, which is:
0.5 * x * (1 + erf(x / sqrt(2)))
. Refer to the GeluLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- mode: str, optional
Gelu mode in [EXACT | TANH_APPROXIMATION | SIGMOID_APPROXIMATION], default EXACT.
-
add_get_shape
(self, name, input_name, output_name)¶ Add a get_shape layer to the model. Refer to the GetShapeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_greater_than
(self, name, input_names, output_name, use_greater_than_equal=False, alpha=0.0)¶ Add a greater_than layer to the model that performs the element-wise greater-than (>) operation or greater-than-or-equal-to (>=) operation. Broadcasting is supported. Refer to the GreaterThanLayerParams, GreaterEqualLayerParams messages in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- use_greater_than_equal: bool, optional
Whether or not to allow greater than or equal to, default: false.
- alpha: float, optional
y = x1 != alpha, if only one input is provided, default: 0.
See also
-
add_gru
(self, name, W_h, W_x, b, hidden_size, input_size, input_names, output_names, activation='TANH', inner_activation='SIGMOID_HARD', output_all=False, reverse_input=False)¶ Add a Gated-Recurrent Unit (GRU) layer to the model. Refer to the GRULayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W_h: [numpy.array]
List of recursion weight matrices. The ordering is [R_z, R_r, R_o], where R_z, R_r and R_o are weight matrices at update gate, reset gate and output gate. The shapes of these matrices are (hidden_size, hidden_size).
- W_x: [numpy.array]
List of input weight matrices. The ordering is [W_z, W_r, W_o], where W_z, W_r, and W_o are weight matrices at update gate, reset gate and output gate. The shapes of these matrices are (hidden_size, input_size).
- b: [numpy.array] or None
List of biases of the GRU layer. The ordering is [b_z, b_r, b_o], where b_z, b_r, b_o are biases at update gate, reset gate and output gate. If None, biases are ignored. Otherwise the shapes of the biases are (hidden_size, ).
- hidden_size: int
Number of hidden units. This is equal to the number of channels of output shape.
- input_size: int
Number of the number of channels of input shape.
- activation: str
Activation function used at the output gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. Defaults to ‘TANH’. See add_activation for more detailed description.
- inner_activation: str
Inner activation function used at update and reset gates. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. Defaults to ‘SIGMOID_HARD’. See add_activation for more detailed description.
- input_names: list of str
The input blob names list of this layer, in the order of [x, h_input].
- output_names: list of str
The output blob names list of this layer, in the order of [y, h_output].
- output_all: boolean
Whether the recurrent layer should output at every time step.
- If False, the output is the result after the final state update.
- If True, the output is a sequence, containing outputs at all time steps.
- reverse_input: boolean
Whether the recurrent layer should process the input sequence in the reverse order.
- If False, the input sequence order is not reversed.
- If True, the input sequence order is reversed.
See also
-
add_inner_product
(self, name, W, b, input_channels, output_channels, has_bias, input_name, output_name, **kwargs)¶ Add an inner product layer to the model. Refer to the InnerProductLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W: numpy.array or bytes()
Weight matrix of shape (output_channels, input_channels) If W is of type bytes(), i.e. quantized, other quantization related arguments must be provided as well (see below).
- b: numpy.array
Bias vector of shape (output_channels, ).
- input_channels: int
Number of input channels.
- output_channels: int
Number of output channels.
- has_bias: boolean
Whether the bias vector of this layer is ignored in the spec.
- If True, the bias vector of this layer is not ignored.
- If False, the bias vector is ignored.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- Quantization arguments expected in kwargs, when W is of type bytes():
- quantization_type: str
When weights are quantized (i.e. W is of type bytes()), this should be either “linear” or “lut”.
- nbits: int
Should be between 1 and 8 (inclusive). Number of bits per weight value. Only applicable when weights are quantized.
- quant_scale: numpy.array(dtype=numpy.float32)
scale vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_bias: numpy.array(dtype=numpy.float32)
bias vector to be used with linear quantization. Must be of length either 1 or output_channels.
- quant_lut: numpy.array(dtype=numpy.float32)
the LUT (look up table) to be used with LUT quantization. Must be of length 2^nbits.
See also
-
add_l2_normalize
(self, name, input_name, output_name, epsilon=1e-05)¶ Add L2 normalize layer. Normalizes the input by the L2 norm, i.e. divides by the the square root of the sum of squares of all elements of the input along C, H and W dimensions. Refer to the L2NormalizeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- epsilon: float
small bias to avoid division by zero.
-
add_layer_normalization
(self, name, input_name, output_name, normalized_shape, gamma, beta, eps=1e-05)¶ Add a layer normalization layer to the model that applies layer normalization over the input tensor. Refer to the LayerNormalizationLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- normalized_shape: list of int or tuple of int
Input shape from an expected input of size.
- gamma: WeightParams
Weight parameters.
- beta: WeightParams
Bias parameters.
- eps: float, optional
Constant value added to the denominator, default: 1e-5.
-
add_less_than
(self, name, input_names, output_name, use_less_than_equal=False, alpha=0.0)¶ Add a less_than layer to the model that performs the element-wise less-than (<) operation or less-than-or-equal-to (<=) operation. Broadcasting is supported. Refer to the LessThanL_ayerParams, LessEqualLayerParams messages in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- use_less_than_equal: bool, optional
Whether or not to allow less than or equal to, default: false.
- alpha: float, optional
y = x1 != alpha, if only one input is provided, default: 0.
See also
-
add_load_constant
(self, name, output_name, constant_value, shape)¶ Add a load constant layer. Refer to the LoadConstantLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- constant_value: numpy.array
value of the constant as a numpy array.
- shape: list of int or tuple of int
List of ints representing the shape of the constant. Must be of length 3: [C,H,W]
See also
-
add_load_constant_nd
(self, name, output_name, constant_value, shape)¶ Add a load_constant layer that loads data as a parameter and provides it as an output. Refer to the LoadConstantNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- constant_value: numpy.array()
value of the constant as a numpy array.
- shape: list of int or tuple of int
List of ints representing the shape of the constant.
See also
-
add_logical
(self, name, input_names, output_name, mode)¶ Add a logical layer to the model that performs element-wise logical and/or/xor/not operation. Broadcasting is supported. Refer to the LogicalOrLayerParams, LogicalNotLayerParams, LogicalNotLayerParams, LogicalAndLayerParam messages in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- mode: str
Logical operation mode in [AND | OR | XOR | NOT].
-
add_loop
(self, name, body_network=None, input_name=None, condition=None, condition_network=None, max_iterations=None)¶ Add a loop layer to the model that provides the functionality of a
for
loop, or awhile
loop. Refer to the LoopLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- body_network: NeuralNetwork
Neural network to execute for the body of the loop.
- input_name: str
The input blob name of this layer.
- condition: str, optional
Condition of the loop.
- condition_network: NeuralNetwork, optional
Neural network to execute for the condition of the loop.
- max_iterations: int, optional
Maximum number of iterations of the loop.
See also
-
add_loop_break
(self, name)¶ Add a loop_break layer to the model that terminates the loop that contains this layer. Must reside in the
bodyNetwork
of the loop layer. Refer to the LoopBreakLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
See also
-
add_loop_continue
(self, name)¶ Add a loop_continue layer to the model that stops the current loop iteration and continue on the next iteration. Must reside in the
bodyNetwork
of the loop layer. Refer to the LoopContinueLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
See also
-
add_lower_triangular
(self, name, input_name, output_name, k=0)¶ Add a lower_triangular layer to the model that copies a tensor setting everything outside lower triangular to zero. Refer to the LowerTriangularLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The of input blob name of this layer.
- output_name: str
The output blob name of this layer.
- k: int, optional
Diagonal below which to zero elements, default: 0 (main diagonal), k < 0 is lower it and k > 0 is upper.
See also
-
add_lrn
(self, name, input_name, output_name, alpha, beta, local_size, k=1.0)¶ Add a LRN (local response normalization) layer. Supports “across” channels normalization. Refer to the LRNLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- alpha: float
multiplicative constant in the denominator.
- beta: float
exponent of the normalizing term in the denominator.
- k: float
bias term in the denominator. Must be positive.
- local_size: int
size of the neighborhood along the channel axis.
See also
-
add_matrix_band_part
(self, name, input_name, output_name, num_lower=-1, num_upper=-1)¶ Add a matrix_band_part layer to the model that copies a tensor setting everything outside a central band in each inner-most matrix to zero. Refer to the MatrixBandPartLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The of input blob name of this layer.
- output_name: str
The output blob name of this layer.
- num_lower: int, optional
Number of lower sub-diagonals to keep. Default: -1 (keep entire lower triangle).
- num_upper: int, optional
Number of upper sub-diagonals to keep. Default: -1 (keep entire upper triangle).
See also
-
add_max_broadcastable
(self, name, input_names, output_name)¶ Add a max_broadcastable layer to the model that performs element-wise maximum operation with broadcast support. Refer to the MaxBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_min_broadcastable
(self, name, input_names, output_name)¶ Add a min_broadcastable layer to the model that performs element-wise minimum operation with broadcast support. Refer to the MinBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_mod_broadcastable
(self, name, input_names, output_name)¶ Add a mod_broadcastable layer to the model that performs element-wise modular operation with broadcast support. Refer to the ModBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_multiply_broadcastable
(self, name, input_names, output_name)¶ Add a multiply_broadcastable layer to the model that performs element-wise multiplication operation with broadcast support. Refer to the MultiplyBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_mvn
(self, name, input_name, output_name, across_channels=True, normalize_variance=True, epsilon=1e-05)¶ Add an MVN (mean variance normalization) layer. Computes mean, variance and normalizes the input. Refer to the MeanVarianceNormalizeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- across_channels: boolean
If False, each channel plane is normalized separately If True, mean/variance is computed across all C, H and W dimensions
- normalize_variance: boolean
If False, only mean subtraction is performed.
- epsilon: float
small bias to avoid division by zero.
See also
-
add_nms
(self, name, input_names, output_names, iou_threshold=0.5, score_threshold=0.0, max_boxes=1, per_class_suppression=False)¶ Add a non maximum suppression layer. Refer to the NonMaximumSuppressionLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer. Must be at least 2, and maximum 5.
- output_names: list of str
The output blob names of this layer. Must be of length 4 exactly.
- iou_threshold: float
intersection over union threshold for suppression. Ignored if 3rd input is present.
- score_threshold: float
threshold for selecting boxes to be used for NMS algorithm. Ignored if 4th input is present.
- max_boxes: int
maximum number of boxes to output. Ignored if 5th input is present.
- per_class_suppression: bool
If true, boxes are organized into classes and suppression is applied to each class group separately
See also
-
add_not_equal
(self, name, input_names, output_name, alpha=0.0)¶ Add a not_equal layer to the model that performs the element-wise not equal (!=) operation. Broadcasting is supported. Refer to the NotEqualLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- alpha: float, optional
y = x1 != alpha, if only one input is provided, default: 0.
See also
-
add_optionals
(self, optionals_in, optionals_out)¶ Add optional inputs and outputs to the model spec.
Parameters: - optionals_in: list of str
List of inputs that are optionals.
- optionals_out: list of str
List of outputs that are optionals.
See also
-
add_padding
(self, name, left=0, right=0, top=0, bottom=0, value=0, input_name='data', output_name='out', padding_type='constant')¶ Add a padding layer to the model that performs padding along spatial dimensions. Refer to the PaddingLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- left: int
Number of elements to be padded on the left side of the input blob.
- right: int
Number of elements to be padded on the right side of the input blob.
- top: int
Number of elements to be padded on the top of the input blob.
- bottom: int
Number of elements to be padded on the bottom of the input blob.
- value: float
Value of the elements padded. Used only when padding_type = ‘constant’
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- padding_type: str
Type of the padding. Can be one of ‘constant’, ‘reflection’ or ‘replication’.
See also
-
add_permute
(self, name, dim, input_name, output_name)¶ Add a permute layer. Assumes that the input has dimensions in the order [Seq, C, H, W] Refer to the PermuteLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- dim: tuple
The order in which to permute the input dimensions = [seq,C,H,W]. Must have length 4 and a permutation of
[0, 1, 2, 3]
.examples:
Lets say input has shape: [seq, C, H, W].
If
dim
is set to[0, 3, 1, 2]
, then the output has shape[W,C,H]
and has the same sequence length that of the input.If
dim
is set to[3, 1, 2, 0]
, and the input is a sequence of data with lengthSeq
and shape[C, 1, 1]
, then the output is a unit sequence of data with shape[C, 1, Seq]
.If
dim
is set to[0, 3, 2, 1]
, the output is a reverse of the input:[C, H, W] -> [W, H, C]
.If
dim
is not set, or is set to[0, 1, 2, 3]
, the output is the same as the input.- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_pooling
(self, name, height, width, stride_height, stride_width, layer_type, padding_type, input_name, output_name, exclude_pad_area=True, is_global=False, padding_top=0, padding_bottom=0, padding_left=0, padding_right=0, same_padding_asymmetry_mode='BOTTOM_RIGHT_HEAVY')¶ Add a pooling layer to the model that performs spatial pooling. Refer to the PoolingLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- height: int
Height of pooling region.
- width: int
Width of pooling region.
- stride_height: int
Stride along the height direction.
- stride_width: int
Stride along the width direction.
- layer_type: str
Type of pooling performed. Can either be ‘MAX’, ‘AVERAGE’ or ‘L2’.
- padding_type: str
Option for the type of padding and output blob shape. Can be either ‘VALID’ , ‘SAME’ or ‘INCLUDE_LAST_PIXEL’.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- exclude_pad_area: boolean
Whether to exclude padded area in the ‘AVERAGE’ pooling operation, default: true. - If True, the value of the padded area will be excluded. - If False, the padded area will be included. This flag is only used with average pooling.
- is_global: boolean
Whether the pooling operation is global. Defaults to False. - If True, the pooling operation is global – the pooling region is of the same size of the input blob. Parameters height, width, stride_height, stride_width will be ignored. - If False, the pooling operation is not global.
- padding_top, padding_bottom, padding_left, padding_right: int
values of height (top, bottom) and width (left, right) padding to be used if padding type is “VALID” or “INCLUDE_LAST_PIXEL”.
- same_padding_asymmetry_mode: str.
Type of asymmetric padding to be used when padding_type = ‘SAME’. Can be either ‘BOTTOM_RIGHT_HEAVY’ or ‘TOP_LEFT_HEAVY’.
See also
-
add_pow_broadcastable
(self, name, input_names, output_name)¶ Add a pow_broadcastable layer to the model that performs element-wise power operation with broadcast support. Refer to the PowBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_random_bernoulli_dynamic
(self, name, input_names, output_name, prob=0.5, seed=-1)¶ Add a random_bernoulli_dynamic layer to the model that fills the output tensor with random values from Bernoulli distribution. Refer to the RandomBernoulliDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- prob: float, optional
Probabilities for Bernoulli distribution, default: 0.5.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_random_bernoulli_like
(self, name, input_name, output_name, prob=0.5, seed=-1)¶ Add a random_bernoulli_like layer to the model that fills the output tensor with random values from Bernoulli distribution. Refer to the RandomBernoulliLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- prob: float, optional
Probabilities for Bernoulli distribution, default: 0.5.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_random_bernoulli_static
(self, name, output_name, output_shape, prob=0.5, seed=-1)¶ Add a random_bernoulli_static layer to the model that fills the output tensor with random values from Bernoulli distribution. Refer to the RandomBernoulliStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
Target shape of the output tensor.
- prob: float, optional
Probabilities for Bernoulli distribution, default: 0.5.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_random_normal_dynamic
(self, name, input_names, output_name, mean=0.0, stddev=0.0, seed=-1)¶ Add a random_normal_dynamic layer to the model that fills the output tensor with random values from normal distribution. Refer to the RandomNormalDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- mean: float, optional
The mean of the normal distribution, default: 0.0.
- stddev: float, optional
The standard deviation of the normal distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution. Default -1 (random).
-
add_random_normal_like
(self, name, input_name, output_name, mean=0.0, stddev=0.0, seed=-1)¶ Add a random_normal_like layer to the model that fills the output tensor with random values from normal distribution. Refer to the RandomNormalLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- mean: float, optional
The mean of the normal distribution, default: 0.0.
- stddev: float, optional
The standard deviation of the normal distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution, default -1 (random).
-
add_random_normal_static
(self, name, output_name, output_shape, mean=0.0, stddev=0.0, seed=-1)¶ Add a random_normal_static layer to the model that fills the output tensor with random values from normal distribution. Refer to the RandomNormaStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
Target shape of the output tensor.
- mean: float, optional
The mean of the normal distribution, default: 0.0.
- stddev: float, optional
The standard deviation of the normal distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution. Default -1 (random).
-
add_random_uniform_dynamic
(self, name, input_names, output_name, minval=0.0, maxval=1.0, seed=-1)¶ Add a random_uniform_dynamic layer to the model that fills the output tensors with random values from uniform distribution. Refer to the RandomUniformDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- minval: float, optional
Lower bound / minimum value of the uniform distribution, default: 0.0.
- maxval: float, optional
Upper bound / maximum value of the uniform distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_random_uniform_like
(self, name, input_name, output_name, minval=0.0, maxval=1.0, seed=-1)¶ Add a random_uniform_like layer to the model that fills the output tensors with random values from uniform distribution. Refer to the RandomUniformLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- minval: float, optional
Lower bound / minimum value of the uniform distribution, default: 0.0.
- maxval: float, optional
Upper bound / maximum value of the uniform distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_random_uniform_static
(self, name, output_name, output_shape, minval=0.0, maxval=1.0, seed=-1)¶ Add a random_uniform_static layer to the model that fills the output tensors with random values from uniform distribution. Refer to the RandomUniformStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
Target shape of the output tensor.
- minval: float, optional
Lower bound / minimum value of the uniform distribution, default: 0.0.
- maxval: float, optional
Upper bound / maximum value of the uniform distribution, default: 1.0.
- seed: int, optional
Used to create a random seed for the distribution. default -1 (random).
-
add_range_dynamic
(self, name, input_names, output_name, start=0, step=1)¶ Add a range_dynamic layer that returns a tensor that contains evenly spaced values. This layer has up to three inputs or no input and three parameters. Refer to the RangeDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names. If input size == 1: end is input, start and step are read from parameters If input size == 2: end, start are inputs, step is read from parameters If input size == 3: start, end, step are all inputs, none of the parameters are used.
- output_name: str
The output blob name of this layer.
- start: int, optional
Range parameter: start. Ignored if start is provided as input, default: 0.
- step: int, optional
Range parameter: step. Ignored if step is provided as input, default: 1.
See also
-
add_range_static
(self, name, output_name, input_names=None, end=1, start=0, step=1)¶ Add a range_static layer that returns a tensor that contains evenly spaced values. This layer has no input and three parameters. Refer to the RangeStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- output_name: str
The output blob name of this layer.
- input_names: list of str
The input blob names of this layer.
- end: int, optional
Range parameter: end, default: 1.
- start: int, optional
Range parameter: start, default: 0.
- step: int, optional
Range parameter: step size, default: 1.
See also
-
add_rank_preserving_reshape
(self, name, input_name, output_name, output_shape)¶ Add a rank_preserving_reshape layer to the model that reshapes the input tensor without altering the rank of the tensor. Refer to the RankPreservingReshapeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
Determines the shape of the output blob. 0: copy the dimension of the input to output -1: calculate dimensions from the rest of the shape
-
add_reduce
(self, name, input_name, output_name, axis, mode, epsilon=1e-06)¶ Add a reduce layer. Applies the function specified by the parameter mode, along dimension(s) specified by the parameter axis. Refer to the ReduceLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: str
dimensions along which the reduction operation is applied. Allowed values: ‘CHW’, ‘HW’, ‘C’, ‘H’, ‘W’
- mode: str
Reduction operation to be applied. Allowed values: ‘sum’, ‘avg’, ‘prod’, ‘logsum’, ‘sumsquare’, ‘L1’, ‘L2’, ‘max’, ‘min’, ‘argmax’. ‘argmax’ is only supported with axis values ‘C’, ‘H’ and ‘W’.
- epsilon: float
number that is added to the input when ‘logsum’ function is applied.
See also
-
add_reduce_l1
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_l1 layer to the model that reduces the input tensor using
l1_normalization(elements across given dimensions)
. Refer to the ReduceL1LayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_l2
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_l2 layer to the model that reduces the input tensor using
l2_normalization(elements across given dimensions)
. Refer to the ReduceL2LayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_logsum
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_logsum layer to the model that reduces the input tensor using log(sum(elements across given dimensions)). Refer to the ReduceLogSumLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_logsumexp
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_logsumexp layer to the model that computes
log(sum(exp(tensor)))
and reduces along the given axis. Refer to the ReduceLogSumExpLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_max
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_max layer to the model that reduces the input tensor using
max(elements across given dimensions)
. Refer to the ReduceMaxLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_mean
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_mean layer to the model that reduces the input tensor using
mean(elements across given dimensions)
. Refer to the ReduceMeanLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_min
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_min layer to the model that reduces the input tensor using
min(elements across given dimensions)
. Refer to the ReduceMinLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_prod
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_prod layer to the model that reduces the input tensor using
prod(elements across given dimensions)
. Refer to the ReduceProdLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes. If axes list is empty, it will be set to true, default: false.
-
add_reduce_sum
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_sum layer to the model that reduces the input tensor using
sum(elements across given dimensions)
. Refer to the ReduceSumLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reduce_sumsquare
(self, name, input_name, output_name, axes=None, keepdims=True, reduce_all=False)¶ Add a reduce_sumsquare layer to the model that reduces the input tensor using
sum(square(elements across given dimensions))
. Refer to the ReduceSumSquareLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
List of dimensions for the reduce operations. Each should be in range [-rank(input), rank(input)), default: None (reduce_all)
- keepdims: bool, optional
Whether or not to retain the reduced dimensions with length 1, default: true.
- reduce_all: bool, optional
Whether or not to reduce on all axes, default: false.
-
add_reorganize_data
(self, name, input_name, output_name, mode='SPACE_TO_DEPTH', block_size=2)¶ Add a data reorganization layer of type “SPACE_TO_DEPTH” or “DEPTH_TO_SPACE”. Refer to the specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- mode: str
- If mode == ‘SPACE_TO_DEPTH’: data is moved from the spatial to the channel dimension. Input is spatially divided into non-overlapping blocks of size block_size X block_size and data from each block is moved to the channel dimension. Output CHW dimensions are: [C * block_size * block_size, H/block_size, C/block_size].
- If mode == ‘DEPTH_TO_SPACE’: data is moved from the channel to the spatial dimension. Reverse of the operation ‘SPACE_TO_DEPTH’. Output CHW dimensions are: [C/(block_size * block_size), H * block_size, C * block_size].
- block_size: int
Must be greater than 1. Must divide H and W, when mode is ‘SPACE_TO_DEPTH’. (block_size * block_size) must divide C when mode is ‘DEPTH_TO_SPACE’.
See also
-
add_reshape
(self, name, input_name, output_name, target_shape, mode)¶ Add a reshape layer. Refer to the ReshapeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- target_shape: tuple
Shape of the output blob. The product of target_shape must be equal to the shape of the input blob. Can be either length 3 (C,H,W) or length 4 (Seq,C,H,W).
- mode: int
- If mode == 0, the reshape layer is in CHANNEL_FIRST mode.
- If mode == 1, the reshape layer is in CHANNEL_LAST mode.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_reshape_dynamic
(self, name, input_names, output_name)¶ Add a reshape_dynamic layer to the model that reshapes a tensor. Refer to the ReshapeDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_reshape_like
(self, name, input_names, output_name)¶ Add a reshape_like layer to the model that reshapes a tensor. Refer to the ReshapeLikeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_reshape_static
(self, name, input_name, output_name, output_shape)¶ Add a reshape_static layer to the model that reshapes a tensor. Refer to the ReshapeStaticLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- output_shape: list of int or tuple of int
Target shape of the output tensor.
-
add_resize_bilinear
(self, name, input_name, output_name, target_height=1, target_width=1, mode='ALIGN_ENDPOINTS_MODE')¶ Add a resize bilinear layer to the model. A layer that resize the input to a given spatial size using bilinear interpolation. Refer to the ResizeBilinearLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- target_height: int
Output height dimension.
- target_width: int
Output width dimension.
- mode: str
Following values are supported: ‘STRICT_ALIGN_ENDPOINTS_MODE’, ‘ALIGN_ENDPOINTS_MODE’, ‘UPSAMPLE_MODE’, ‘ROI_ALIGN_MODE’. This parameter determines the sampling grid used for bilinear interpolation.
See also
-
add_reverse
(self, name, input_name, output_name, reverse_dim=None)¶ Add a reverse layer to the model that reverses specific dimensions of the input tensor. Refer to the ReverseLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- reverse_dim: list of int or tuple of int
Reverse along the dimension, default [1].
See also
-
add_reverse_sequence
(self, name, input_names, output_name, batch_axis=0, seq_axis=-1)¶ Add a reverse sequence layer to the model that reverses variable length slices. Refer to the ReverseSeqLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- batch_axis: int, optional
Slices input along the dimension batch_axis, default 0.
- seq_axis: int, optional
Reverse along the dimension seq_axis, default: -1.
See also
-
add_round
(self, name, input_name, output_name)¶ Add a round layer to the model that performs element-wise round operation on the input tensor that rounds the value to the nearest integer. Refer to the RoundLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_scale
(self, name, W, b, has_bias, input_name, output_name, shape_scale=None, shape_bias=None)¶ Add a scale layer to the model. Refer to the ScaleLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W: int or numpy.array
Scale of the input.
- b: int or numpy.array
Bias to add to the input.
- has_bias: boolean
Whether the bias vector of this layer is ignored in the spec.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- shape_scale: list of int or tuple of int
List of ints that specifies the shape of the scale parameter. Can be [1] or [C] or [1,H,W] or [C,H,W].
- shape_bias: list of int
List of ints that specifies the shape of the bias parameter (if present). Can be [1] or [C] or [1,H,W] or [C,H,W].
See also
-
add_scatter
(self, name, input_names, output_name, axis=0, mode='UPDATE')¶ Add a scatter layer to the model that scatters data into a new tensor according to indices from the input. Refer to the ScatterLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
The axis the operation perform on, default: 0.
- mode: str, optional
Scatter accumulation mode in [UPDATE | ADD | SUB | MUL | DIV | MAX | MIN], default: UPDATE.
-
add_scatter_along_axis
(self, name, input_names, output_name, axis=0, mode='UPDATE')¶ Add a scatter_along_axis layer to the model that scatters data into a new tensor according to indices from the input along the given axis into the output tensor. Refer to the ScatterAlongAxisLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
The axis to perform on, default: 0.
- mode: str, optional
Scatter accumulation mode in [UPDATE | ADD | SUB | MUL | DIV | MAX | MIN], default: UPDATE
-
add_scatter_nd
(self, name, input_names, output_name, mode='UPDATE')¶ Add a scatter layer to the model that scatters data into a new tensor according to indices from input. This is the reverse operation of the gather operation. Refer to the ScatterNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- mode: str, optional
Scatter accumulation mode in [UPDATE | ADD | SUB | MUL | DIV | MAX | MIN], default: UPDATE
-
add_sequence_repeat
(self, name, nrep, input_name, output_name)¶ Add a sequence repeat layer to the model. Refer to the SequenceRepeatLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- nrep: int
Number of repetitions of the input blob along the sequence axis.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_sign
(self, name, input_name, output_name)¶ Add a sign layer to the model that performs element-wise sign operation (+1 for positive values, -1 for negative values, 0 for zeroes). Refer to the SignLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_simple_rnn
(self, name, W_h, W_x, b, hidden_size, input_size, activation, input_names, output_names, output_all=False, reverse_input=False)¶ Add a simple recurrent layer to the model. Refer to the SimpleRecurrentLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W_h: numpy.array
Weights of the recurrent layer’s hidden state. Must be of shape (hidden_size, hidden_size).
- W_x: numpy.array
Weights of the recurrent layer’s input. Must be of shape (hidden_size, input_size).
- b: numpy.array or None
Bias of the recurrent layer’s output. If None, bias is ignored. Otherwise it must be of shape (hidden_size, ).
- hidden_size: int
Number of hidden units. This is equal to the number of channels of output shape.
- input_size: int
Number of the number of channels of input shape.
- activation: str
Activation function name. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. See add_activation for more detailed description.
- input_names: list of str
The input blob names list of this layer, in the order of [x, h_input].
- output_names: list of str
The output blob names list of this layer, in the order of [y, h_output].
- output_all: boolean
Whether the recurrent layer should output at every time step.
- If False, the output is the result after the final state update.
- If True, the output is a sequence, containing outputs at all time steps.
- reverse_input: boolean
Whether the recurrent layer should process the input sequence in the reverse order.
- If False, the input sequence order is not reversed.
- If True, the input sequence order is reversed.
See also
-
add_sin
(self, name, input_name, output_name)¶ Add a sin layer to the model that computes element-wise sine for the input tensor. Refer to the SinLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_sinh
(self, name, input_name, output_name)¶ Add a sinh layer to the model that computes element-wise hyperbolic sine for the input tensor. Refer to the SinhLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_slice
(self, name, input_name, output_name, axis, start_index=0, end_index=-1, stride=1)¶ Add a slice layer. Equivalent to to numpy slice [start_index:end_index:stride], start_index is included, while end_index is exclusive. Refer to the SliceLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: str
axis along which input is sliced. allowed values: ‘channel’, ‘height’, ‘width’
- start_index: int
must be non-negative.
- end_index: int
negative indexing is supported.
- stride: int
must be positive.
See also
-
add_slice_dynamic
(self, name, input_names, output_name, end_ids=None, strides=None, begin_masks=None, end_masks=None)¶ Add a slice_dynamic layer to the model that extracts a slice of size
(end - begin) / stride
from the given input tensor. Refer to the SliceDynamicLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- end_ids: list of int or tuple of int, optional
End offsets for slice layer, default: [1].
- strides: list of int or tuple of int, optional
Strides for slice layer, default: [1].
- begin_masks: list of bool, optional
Boolean masks for begin offsets, default: [false].
- end_masks: list of bool, optional
Boolean masks for end offsets, default: [false].
See also
-
add_slice_static
(self, name, input_name, output_name, begin_ids, end_ids, strides, begin_masks, end_masks)¶ Add a slice_static layer to the model that extracts a slice of size
(end - begin) / stride
from the given input tensor. Refer to the SliceStaticLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- begin_ids: list of int or tuple of int
Begin offsets for slice layer.
- end_ids: list of int or tuple of int
End offsets for slice layer.
- strides: list of int or tuple of int
Strides for slice layer.
- begin_masks: list of bool
Boolean masks for begin offsets.
- end_masks: list of bool
Boolean masks for end offsets.
See also
-
add_sliding_windows
(self, name, input_name, output_name, axis, window_size, step=1)¶ Add a sliding_windows layer to the model that returns a tensor containing all windows of size
window_size
* separated bystep
along the dimensionaxis
. Refer to the SlidingWindowsLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The of input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
Axis to perform the operation.
- window_size: int
Number of elements in the sliding window.
- step: int, optional
The stride of the input elements in the sliding window, default: 1.
See also
-
add_softmax
(self, name, input_name, output_name)¶ Add a softmax layer to the model. Refer to the SoftmaxLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_softmax_nd
(self, name, input_name, output_name, axis)¶ Add a softmax_nd layer to the model that performs softmax operation along the given axis. Refer to the SoftmaxNDLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axis: int
Axis to perform the softmax operation on.
-
add_split
(self, name, input_name, output_names)¶ Add a split layer that uniformly splits the input along the channel dimension to produce multiple outputs. Refer to the SplitLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_names: list of str
List of output blob names of this layer.
See also
-
add_split_nd
(self, name, input_name, output_names, axis, num_splits=2, split_sizes=None)¶ Add a split layer to the model that splits the input tensor into multiple output tensors. Either uniformly split the input tensor into
num_splits
tensors, or split into given size listsplit_sizes
output tensors. Refer to the SplitNDLayerParams message in specification (NeuralNetwork.proto) for more details.Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_names: list of str
The output blob names of this layer.
- axis: int
Axis to perform split on.
- num_splits: int, optional
Number of splits, default: 2.
- split_sizes: list of int or tuple of int, optional
List of size to split, default [] or None.
-
add_squeeze
(self, name, input_name, output_name, axes=None, squeeze_all=False)¶ Add a squeeze layer to the model that decrease the rank of the input tensor by removing unit dimensions. Refer to the SqueezeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- axes: list of int or tuple of int, optional
Dimensions to perform the operation, default: None (squeeze_all).
- squeeze_all: bool, optional
If true, all dimensions that are 1 are squeezed, default: false.
See also
-
add_stack
(self, name, input_names, output_name, axis=0)¶ Add a stack layer to the model that performs stack operation on a list of tensors into one rank+1 tensor on the given axis. Refer to the StackLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
- axis: int, optional
The axis to perform stack operation, default: 0.
-
add_subtract_broadcastable
(self, name, input_names, output_name)¶ Add a subtract_broadcastable layer to the model that performs element-wise subtraction operation with broadcast support. Refer to the SubtractBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
-
add_tan
(self, name, input_name, output_name)¶ Add a tan layer to the model that computes element-wise tangent for the input tensor. Refer to the TanLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_tanh
(self, name, input_name, output_name)¶ Add a tanh layer to the model that computes element-wise hyperbolic tangent for the input tensor. Refer to the TanhLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
-
add_tile
(self, name, input_name, output_name, reps)¶ Add a tile layer to the model that construct a tensor by repeating the input tensor multiple number of times. Refer to the TileLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- reps: list of int or tuple of int
Number of times to replicate.
See also
-
add_topk
(self, name, input_names, output_names, k=0, axis=0, use_bottom_k=False)¶ Add a topk layer to the model that returns top or bottom k values and the corresponding indices of the input tensor along a given axis. Refer to the TopKLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer. It must be of length 1 or 2. The optional second input corresponds to value of K.
- output_names: list of str
The output blob names of this layer. First and second correspond to values and indices, respectively.
- k: int, optional
number of values/indices to be computed along the axis. Need not be given of there are two inputs, default: 0.
- axis: int, optional
axis along which the topk values/indices are computed. negative indexing is supported, default: 0
- use_bottom_k: bool, optional
if true, bottom k values are computed instead, default: false.
-
add_transpose
(self, name, axes, input_name, output_name)¶ Add a N-D transpose layer with axes as a parameter. Refer to the TransposeLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- axes: list of int or tuple of int
The list containing a permutation of “[0,1,2,…,N-1]” where N is the rank of input/output tensor.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_unary
(self, name, input_name, output_name, mode, alpha=1.0, shift=0, scale=1.0, epsilon=1e-06)¶ Add a Unary layer. Applies the specified function (mode) to all the elements of the input. Prior to the application of the function the input can be scaled and shifted by using the ‘scale’, ‘shift’ parameters. Refer to the UnaryFunctionLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- mode: str
Unary function. Allowed values: ‘sqrt’, ‘rsqrt’, ‘inverse’, ‘power’, ‘exp’, ‘log’, ‘abs’, threshold’.
- alpha: float
constant used in with modes ‘power’ and ‘threshold’.
- shift, scale: float
input is modified by scale and shift prior to the application of the unary function.
- epsilon: float
small bias to prevent division by zero.
See also
-
add_unilstm
(self, name, W_h, W_x, b, hidden_size, input_size, input_names, output_names, inner_activation='SIGMOID', cell_state_update_activation='TANH', output_activation='TANH', peep=None, output_all=False, forget_bias=False, coupled_input_forget_gate=False, cell_clip_threshold=50000.0, reverse_input=False)¶ Add a Uni-directional LSTM layer to the model. Refer to the UniDirectionalLSTMLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- W_h: [numpy.array]
List of recursion weight matrices. The ordering is [R_i, R_f, R_o, R_z], where R_i, R_f, R_o, R_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, hidden_size).
- W_x: [numpy.array]
List of input weight matrices. The ordering is [W_i, W_f, W_o, W_z], where W_i, W_f, W_o, W_z are weight matrices at input gate, forget gate, output gate and cell gate. The shapes of these matrices are (hidden_size, input_size).
- b: [numpy.array] or None
List of biases. The ordering is [b_i, b_f, b_o, b_z], where b_i, b_f, b_o, b_z are biases at input gate, forget gate, output gate and cell gate. If None, biases are ignored. Otherwise the shapes of the biases are (hidden_size, ).
- hidden_size: int
Number of hidden units. This is equal to the number of channels of output shape.
- input_size: int
Number of the number of channels of input shape.
- input_names: list of str
The input blob names list of this layer, in the order of [x, h_input, c_input].
- output_names: list of str
The output blob names list of this layer, in the order of [y, h_output, c_output].
- inner_activation: str
Inner activation function used at input and forget gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’].
- cell_state_update_activation: str
Cell state update activation function used at the cell state update gate. [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’].
- output_activation: str
Activation function used at the output gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’].
- peep: [numpy.array] or None
List of peephole vectors. The ordering is [p_i, p_f, p_o], where p_i, p_f, and p_o are peephole vectors at input gate, forget gate, output gate. The shapes of the peephole vectors are (hidden_size,).
- output_all: boolean
Whether the LSTM layer should output at every time step.
- If False, the output is the result after the final state update.
- If True, the output is a sequence, containing outputs at all time steps.
- forget_bias: boolean
If True, a vector of 1s is added to forget gate bias.
- coupled_input_forget_gate: boolean
If True, the input gate and forget gate is coupled. i.e. forget gate is not used.
- cell_clip_threshold: float
The limit on the maximum and minimum values on the cell state. If not provided, it is defaulted to 50.0.
- reverse_input: boolean
Whether the LSTM layer should process the input sequence in the reverse order.
- If False, the input sequence order is not reversed.
- If True, the input sequence order is reversed.
See also
-
add_upper_triangular
(self, name, input_name, output_name, k=0)¶ Add a upper_triangular layer to the model that copies a tensor setting everything outside upper triangular to zero. Refer to the UpperTriangularLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The of input blob name of this layer.
- output_name: str
The output blob name of this layer.
- k: int, optional
Diagonal above which to zero elements, default: 0 (main diagonal), k < 0 is lower it and k > 0 is upper.
See also
-
add_upsample
(self, name, scaling_factor_h, scaling_factor_w, input_name, output_name, mode='NN')¶ Add an upsample layer to the model. Refer to the UpsampleLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- scaling_factor_h: int
Scaling factor on the vertical direction.
- scaling_factor_w: int
Scaling factor on the horizontal direction.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
- mode: str
Following values are supported: ‘NN’: nearest neighbour ‘BILINEAR’: bilinear interpolation
See also
-
add_where_broadcastable
(self, name, input_names, output_name)¶ Add a where_broadcastable layer to the model that returns the elements either from tensor x or tensor y, depending on the value in the condition tensor. Refer to the WhereBroadcastableLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_names: list of str
The input blob names of this layer.
- output_name: str
The output blob name of this layer.
See also
-
add_where_nonzero
(self, name, input_name, output_name)¶ Add a where_nonzero layer to the model that returns a tensor containing the indices of all non-zero elements of input tensor. Refer to the WhereNonZeroLayerParams message in specification (NeuralNetwork.proto) for more details.
Parameters: - name: str
The name of this layer.
- input_name: str
The input blob name of this layer.
- output_name: str
The output blob name of this layer.
See also
-
inspect_conv_channels
(self, layer_name)¶ Prints the output and kernel channels of a convolution layer.
-
inspect_innerproduct_channels
(self, layer_name)¶ Prints the output and kernel channels of an innerProduct layer.
-
inspect_input_features
(self)¶ Prints the name and type of input features.
-
inspect_layers
(self, last=-1, verbose=False)¶ Prints the summary for last “last” number of layers.
Parameters: - last: int
The numbers of layers to inspect, starting from the last one.
- verbose: bool
Whether to display layer-specific parameters or not
-
inspect_loss_layers
(self)¶ Prints the summary for the loss layer.
-
inspect_optimizer
(self)¶ Prints the summary for the optimizer.
-
inspect_output_features
(self)¶ Prints the name and type of output features.
-
inspect_updatable_layers
(self)¶ Prints all updatable layers with their inputs and outputs.
-
make_updatable
(self, trainables)¶ Make the builder’s NeuralNetwork spec updatable.
Parameters: - trainables: list of str
List of layer names to be set trainable.
-
set_categorical_cross_entropy_loss
(self, name, input)¶ Categorical Cross Entropy is used for single label categorization (only one category is applicable for each data point).
Parameters: - name: The name of the loss layer
- input: The name of the input, which should be a vector of length N representing the distribution over N categories. This must be the output of a softmax.
- .. math::
- Loss_ {CCE}(input, target) = -sum_{i = 1} ^ {N}(target == i) log(input[i]) = - log(input[target])
-
set_class_labels
(self, class_labels, predicted_feature_name='classLabel', prediction_blob='')¶ Set class labels to the model spec to make it a neural network classifier.
Parameters: - class_labels: list of int or list of str
A list of integers or strings that map the index of the output of a neural network to labels in a classifier.
- predicted_feature_name: str
Name of the output feature for the class labels exposed in the Core ML neural network classifier, defaults: ‘class_output’.
- prediction_blob: str
If provided, then this is the name of the neural network blob which generates the probabilities for each class label (typically the output of a softmax layer). If not provided, then the last output layer is assumed.
See also
-
set_input
(self, input_names, input_dims)¶ Set the inputs of the network spec.
Parameters: - input_names: list of str
The input names of the network.
- input_dims: [tuple]
The input dimensions of the network. The ordering of input_dims is the same as input_names.
See also
Examples
# Set the neural network spec inputs to be 3 dimensional vector data1 and # 4 dimensional vector data2. >>> builder.set_input(input_names=['data1', 'data2'], input_dims=[(3,), (4,)])
-
set_mean_squared_error_loss
(self, name, input_feature=None)¶ - input_feature: [(str, datatypes.Array)] or None
- The input feature of the loss layer. Each feature is a (name, array) tuple, where name is the name of the model’s tensor our loss will be attached to, and array is a datatypes.Array object describing the shape of that tensor. Both the name and the array’s shape must be provided in the tuple. >>> feature = [(‘output_tensor’, datatypes.Array((299, 299, 3)))]
-
set_output
(self, output_names, output_dims)¶ Set the outputs of the network spec.
Parameters: - output_names: list of str
The output names of the network.
- output_dims: [tuple]
The output dimensions of the network. The ordering of output_dims is the same as output_names.
See also
Examples
# Set the neural network spec outputs to be 3 dimensional vector feature1 and # 4 dimensional vector feature2. >>> builder.set_output(output_names=['feature1', 'feature2'], output_dims=[(3,), (4,)])
-
set_pre_processing_parameters
(self, image_input_names=None, is_bgr=False, red_bias=0.0, green_bias=0.0, blue_bias=0.0, gray_bias=0.0, image_scale=1.0, image_format='NCHW')¶ Add a pre-processing parameters layer to the neural network object.
Parameters: - image_input_names: list of str
Name of input blobs that are images
- is_bgr: boolean or dict()
Channel order for input blobs that are images. BGR if True else RGB. To specify a different value for each image input, provide a dictionary with input names as keys.
- red_bias: float or dict()
Image re-centering parameter (red channel)
- blue_bias: float or dict()
Image re-centering parameter (blue channel)
- green_bias: float or dict()
Image re-centering parameter (green channel)
- gray_bias: float or dict()
Image re-centering parameter (for grayscale images)
- image_scale: float or dict()
Value by which to scale the images.
- image_format: str
Image format, either ‘NCHW’ / ‘NHWC’
See also
-
set_training_input
(self, training_input)¶ Set the training inputs of the network spec.
Parameters: - training_input: [tuple]
The training input names and type of the network.
Examples
# Set the neural network spec training inputs to be 3 dimensional vector for 'input' and # Double for 'target'. >>> builder.set_training_input([('input', datatypes.Array(3)), ('target', 'Double')])