coremltools.models.neural_network

Neural network builder class to construct Core ML models.

Functions

set_transform_interface_params(spec, ...[, ...]) Common utilities to set transform interface params.

Classes

NeuralNetworkBuilder(input_features, ...[, mode]) Neural network builder class to construct Core ML models.
class coremltools.models.neural_network.NeuralNetworkBuilder(input_features, output_features, mode=None)

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 (refered 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.

Please see the Core ML neural network protobuf message for more information on neural network layers, blobs, and parameters.

See also

MLModel, datatypes, save_spec

Examples

# 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, Wb = bias, nB = 3, nC = 2,
... has_bias = True, input_name = 'data', output_name = 'probs')

# save the spec by the builder
>>> save_spec(builder.spec, 'network.mlmodel')
__init__(input_features, output_features, mode=None)

Construct a NeuralNetworkBuilder object and set protobuf specification interface.

Parameters:

input_features: [(str, tuple)]

List of input feature of the network. Each feature is a (name, shape) tuple, is the name of the feature, and shape is a (d1, d2, ..., dN) tuple that describes the dimensions of the input feature.

output_features: [(str, tuple)]

List of output feature of the network. Each feature is a (name, shape) tuple, where name is the name of the feature, and shape is a (d1, d2, ..., dN) tuple that describes the dimensions of the output feature.

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.

Examples

# Construct a builder that builds a neural network classifier with a 299x299x3
# 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_activation(name, non_linearity, input_name, output_name, params=None)

Add an activation layer to the model.

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.

input_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

params: [float] | [numpy.array]

Parameters for the activation, depending on non_linearity. When non_linearity is:

  • ‘RELU’, ‘SIGMOID’, ‘TANH’, ‘SCALED_TANH’, ‘SOFTPLUS’, ‘SOFTSIGN’, ‘LINEAR’: params is ignored.
  • ‘SCALED_TANH’, ‘SIGMOID_HARD’: param is a list of 2 floats [alpha, beta].
  • ‘LEAKYRELU’, ‘ELU’, ‘THRESHOLDEDRELU’: param is a list of 1 float [alpha].
  • ‘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.
  • ‘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.
add_batchnorm(name, channels, gamma, beta, mean, variance, input_name, output_name, epsilon=1e-05, computeMeanVar=False, instanceNormalization=False)

Add a Batch Normalization layer. Batch Normalization operation is defined as: y = gamma * (x - mean) / sqrt(variance + epsilon) + beta

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.

epsilon: float

Value of epsilon. Defaults to 1e-5 if not specified.

computeMeanVar: boolean

Defaults to False.

  • If True, the mean and variance of input blob is computed on the fly, and parameters mean and variance are ignored.
  • If False, the mean and variance of input blob is set to provided values of mean and variance, and the parameter instanceNormalization is ignored.

instanceNormalization: boolean

When computeMeanVar is False, this flag is ignored.

  • If True, the mean and variance of input blob is computed for every single input instance.
  • If False, the mean and variance is computed for the whole batch.
add_bidirlstm(name, hidden_size, input_size, input_names, output_names, W_h, W_x, W_h_back, W_x_back, inner_activation='SIGMOID', cell_state_update_activation='TANH', output_activation='TANH', b=None, b_back=None, peep=None, peep_back=None, output_all=False, forget_bias=False, coupled_input_forget_gate=False, cell_clip_threshold=50.0)

Add a Bi-directional LSTM layer to the model.

Parameters:

name: str

The name of this layer.

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_name: str

The input blob name of this layer.

output_name: str

The output blob 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_z, R_o], where R_i, R_f, R_z, R_o are weight matrices at input gate, forget gate, cell gate and output 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_z, W_o], where W_i, W_f, W_z, W_o are weight matrices at input gate, forget gate, cell gate and output gate. The shapes of these matrices are (hidden_size, input_size).

W_h_back: [numpy.array]

List of recursion weight matrices for the backward layer. The ordering is [R_i, R_f, R_z, R_o], where R_i, R_f, R_z, R_o are weight matrices at input gate, forget gate, cell gate and output 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_z, W_o], where W_i, W_f, W_z, W_o are weight matrices at input gate, forget gate, cell gate and output gate. The shapes of these matrices are (hidden_size, input_size).

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’].

b: [numpy.array]

List of biases for the forward layer. The ordering is [b_i, b_f, b_z, b_o], where b_i, b_f, b_z, b_o are biases at input gate, forget gate, cell gate and output gate. The shapes of the biases (hidden_size).

b_back: [numpy.array]

List of biases for the backward layer. The ordering is [b_i, b_f, b_z, b_o], where b_i, b_f, b_z, b_o are biases at input gate, forget gate, cell gate and output gate. The shapes of the biases (hidden_size).

peep: [numpy.array] | 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,).

peep_back: [numpy.array] | 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,).

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 inpute 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.

add_convolution(name, kernelChannels, outputChannels, height, width, stride_height, stride_width, borderMode, groups, W, b, has_bias, is_deconv, output_shape, input_name, output_name)

Add a convolution layer to the network.

Please see the ConvolutionLayerParams in Core ML neural network protobuf message for more information input and output blob dimensions.

Parameters:

name: str

The name of this layer.

kernelChannels: int

Number of channels for the convolution kernels.

outputChannels: 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.

borderMode: str

Option for the output blob shape. Can be either ‘valid’ or ‘same’.

groups: int

Number of kernel groups. Each kernel group share the same weights. This is equal to (input channels / kernelChannels).

W: numpy.array

Weights of the convolution kernels. - If is_deconv is False, W should have shape (outputChannels, kernelChannels, height, width). - If is_deconv is True, W should have shape (kernelChannels,outputChannels,kernelHeight,kernelWidth).

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

A 3-tuple, specifying the output shape (output_height, output_width, output_channels). Used only when is_deconv == True.

input_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

add_crop(name, left, right, top, bottom, offset, input_name, output_name)

Add a cropping layer to the model.

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 1 input. 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 1 input. 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 1 input. 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 1 input. When the crop layer takes 2 inputs, this parameter is ignored.

offset: (int, int)

Offset along the height and width directions when the crop layer takes 2 inputs. When the crop layer takes 1 input, this parameter is ignored.

input_names: str | list(str)

The input blob name(s) of this layer. Must be either a string, 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.

add_elementwise(name, input_names, output_name, mode)

Add an element-wise operation layer to the model.

Parameters:

name: str

The name of this layer

input_names: list[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.
  • ‘AVE’: compute the element-wise average over the input blobs.
add_embedding(name, W, Wb, nB, nC, has_bias, input_name, output_name)

Add an embedding layer to the model.

Parameters:

name: str

The name of this layer

W: numpy.array

Weight matrix of shape (nB, nC).

Wb: numpy.array

Bias vector of shape (nC, ).

nB: int

Number of input channels.

nC: 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.

add_flatten(mode, name, input_name, output_name)

Add a flatten layer.

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.

add_gru(name, W_h, W_x, b, activation, inner_activation, hidden_size, input_size, input_names, output_names, output_all=False, reverse_input=False)

Add a Gated-Recurrent Unit (GRU) layer to the model.

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]

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. The shapes of the biases are (hidden_size, ).

activation: str

Activation function used at the output gate. Can be one of the following option: [‘RELU’, ‘TANH’, ‘SIGMOID’, ‘SCALED_TANH’, ‘SIGMOID_HARD’, ‘LINEAR’]. 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’]. See add_activation for more detailed description.

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_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

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.

add_inner_product(name, W, Wb, nB, nC, has_bias, input_name, output_name)

Add an inner product layer to the model.

Parameters:

name: str

The name of this layer

W: numpy.array

Weight matrix of shape (nB, nC).

Wb: numpy.array

Bias vector of shape (nC, ).

nB: int

Number of input channels.

nC: 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.

add_optionals(optionals_in, optionals_out)

Add optional inputs and outputs to the model spec.

Parameters:

optionals_in: [str]

List of inputs that are optionals.

input_dims: [tuple]

List of outputs that are optionals.

See also

set_input, set_output

add_padding(name, left, right, top, bottom, value, input_name, output_name)

Add a padding layer to the model.

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.

input_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

add_permute(name, input_name, output_name, dim)

Add a permute layer.

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.

dim: tuple

Dimension of the output blob. The product of dim must be equal to the shape of the input blob.

add_pooling(name, height, width, stride_height, stride_width, layer_type, padding_type, exclude_pad_area, is_global, input_name, output_name)

Add a pooling layer to the model.

Parameters:

name: str

The name of this layer.

height: int

Height of pooling region.

width: int

Number of elements to be padded on the right side of the input blob.

stride_height: int

Stride along the height direction.

stride_width: int

Stride along the height direction.

layer_type: str

Type of pooling performed. Can either be ‘MAX’, ‘AVERAGE’ or ‘L2’.

padding_type: str

Option for the output blob shape. Can be either ‘VALID’ or ‘SAME’.

exclude_pad_area: boolean

Whether to exclude padded area in the pooling operation. - 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. - 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.

input_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

add_repeat(name, nrep, input_name, output_name)

Add sequence repeat layer to the model.

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.

add_reshape(name, input_name, output_name, target_shape, mode)

Add a reshape layer.

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_shape: tuple

Shape of the output blob. The product of target_shape must be equal to the shape of the input blob.

mode: int

  • If mode == 0, the reshape layer is in CHANNEL_FIRST mode.
  • If mode == 1, the reshape layer is in CHANNEL_LAST mode.
add_softmax(name, input_name, output_name)

Add a softmax layer to the model.

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_unilstm(name, hidden_size, input_size, input_names, output_names, W_h, W_x, inner_activation='SIGMOID', cell_state_update_activation='TANH', output_activation='TANH', b=None, peep=None, output_all=False, forget_bias=False, coupled_input_forget_gate=False, cell_clip_threshold=50.0, reverse_input=False)

Add a Uni-directional LSTM layer to the model.

Parameters:

name: str

The name of this layer.

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_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

W_h: [numpy.array]

List of recursion weight matrices. The ordering is [R_i, R_f, R_z, R_o], where R_i, R_f, R_z, R_o are weight matrices at input gate, forget gate, cell 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_i, W_f, W_z, W_o], where W_i, W_f, W_z, W_o are weight matrices at input gate, forget gate, cell gate and output gate. The shapes of these matrices are (hidden_size, input_size).

b: [numpy.array]

List of biases. The ordering is [b_i, b_f, b_z, b_o], where b_i, b_f, b_z, b_o are biases at input gate, forget gate, cell gate and output gate. The shapes of the biases (hidden_size).

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] | 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 inpute 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.

add_upsample(name, scaling_factor_h, scaling_factor_w, input_name, output_name)

Add upsample layer to the model.

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.

add_vanilla_rnn(name, W_h, W_x, b, activation, hidden_size, input_size, input_names, output_names, output_all=False, reverse_input=False)

Add a simple recurrent layer to the model.

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

Bias of the recurrent layer’s output. Must be of shape (hidden_size, ).

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.

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_name: str

The input blob name of this layer.

output_name: str

The output blob name of this layer.

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.

set_class_labels(class_labels, predicted_feature_name='classLabel')

Set class labels to the model spec to make it a neural network classifier.

Parameters:

class_labels: list[int or 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 to ‘class_output’.

set_input(input_names, input_dims)

Set the inputs of the network spec.

Parameters:

input_names: [str]

List of input names of the network.

input_dims: [tuple]

List of input dimensions of the network. The ordering of input_dims is the same as input_names.

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'], [(3,), (4,)])
set_output(output_names, output_dims)

Set the outputs of the network spec.

Parameters:

output_names: [str]

List of output names of the network.

output_dims: [tuple]

List of output dimensions of the network. The ordering of output_dims is the same as output_names.

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'], [(3,), (4,)])
set_pre_processing_parameters(image_input_names=[], is_bgr=False, red_bias=0.0, green_bias=0.0, blue_bias=0.0, gray_bias=0.0, image_scale=1.0)

Add pre-processing parameters to the neural network object

Parameters:

image_input_names: [str]

Name if input blobs that are images

is_bgr: bool

Image pixel order (RGB or BGR)

red_bias: float

Image re-centering parameter (red channel)

blue_bias: float

Image re-centering parameter (blue channel)

green_bias: float

Image re-centering parameter (green channel)

image_scale: float

Value by which to scale the images.