Core Layers API#


This section describes all the available layers in the Keras-like API.

To set the name of a specific layer, you call the method setName(name) of that layer.


Masking#

Use a mask value to skip timesteps for a sequence.

Masking(maskValue = 0.0, inputShape = null)

Parameters:

  • maskValue: Mask value. For each timestep in the input (the second dimension), if all the values in the input at that timestep are equal to ‘maskValue’, then the timestep will be masked (skipped) in all downstream layers.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Masking
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Masking[Float](inputShape = Shape(3)))
val input = Tensor[Float](2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
1.4539868       1.5623108       -1.4101523
0.77073747      -0.18994702     2.2574463
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
1.4539868       1.5623108       -1.4101523
0.77073747      -0.18994702     2.2574463
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

SparseDense#

SparseDense is the sparse version of layer Dense. SparseDense has two different from Dense: firstly, SparseDense’s input Tensor is a SparseTensor. Secondly, SparseDense doesn’t backward gradient to next layer in the backpropagation by default, as the gradInput of SparseDense is useless and very big in most cases.

But, considering model like Wide&Deep, we provide backwardStart and backwardLength to backward part of the gradient to next layer.

The most common input is 2D.

SparseDense(outputDim, init = "glorot_uniform", activation = null, wRegularizer = null, bRegularizer = null, backwardStart = -1, backwardLength = -1, initWeight = null, initBias = null, initGradWeight = null, initGradBias = null, bias = true, inputShape = null)

Parameters:

  • outputDim: The size of the output dimension.

  • init: String representation of the initialization method for the weights of the layer. Default is ‘glorot_uniform’.

  • activation: String representation of the activation function to use. Default is null.

  • wRegularizer: An instance of [Regularizer], applied to the input weights matrices. Default is null.

  • bRegularizer: An instance of [Regularizer], applied to the bias. Default is null.

  • bias: Whether to include a bias (i.e. make the layer affine rather than linear). Default is true.

  • backwardStart: Backward start index, counting from 1.

  • backwardLength: Backward length.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

  • name: String to set the name of the layer. If not specified, its name will by default to be a generated string.

import com.intel.analytics.bigdl.dllib.keras.layers.SparseDense
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val layer = SparseDense[Float](outputDim = 5, inputShape = Shape(2, 4))
layer.build(Shape(-1, 2, 4))
val input = Tensor[Float](Array(2, 4)).rand()
input.setValue(1, 1, 1f)
input.setValue(2, 3, 3f)
val sparseInput = Tensor.sparse(input)
val output = layer.forward(sparseInput)

Input is:

input:
(0, 0) : 1.0
(0, 1) : 0.2992794
(0, 2) : 0.11227019
(0, 3) : 0.722947
(1, 0) : 0.6147614
(1, 1) : 0.4288646
(1, 2) : 3.0
(1, 3) : 0.7749917
[com.intel.analytics.bigdl.tensor.SparseTensor of size 2x4]

Output is:

output:
0.053516	0.33429605	0.22587383	-0.8998945	0.24308181
0.76745665	-1.614114	0.5381658	-2.2226436	-0.15573677
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x5]

SoftShrink#

Applies the soft shrinkage function element-wise to the input.

When you use this layer as the first layer of a model, you need to provide the argument inputShape (a Single Shape, does not include the batch dimension).

Remark: This layer is from Torch and wrapped in Keras style.

SoftShrink(value = 0.5, inputShape = null)

Parameters:

  • value: value The threshold value. Default is 0.5.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

  • name: String to set the name of the layer. If not specified, its name will by default to be a generated string.

import com.intel.analytics.bigdl.dllib.keras.layers.SoftShrink
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(SoftShrink[Float](0.6, inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-0.36938807	0.023556225	-1.1655436	-0.34449077
0.9444338	-0.086538695	-1.0425501	1.364976
-1.2563878	-0.1842559	0.43428117	1.0756494

(1,2,.,.) =
-0.19888283	1.251872	0.114836805	-0.6208773
0.0051822234	-0.8998633	0.06937465	-0.3929931
-0.1058129	0.6945743	-0.40083578	-0.6252444

(2,1,.,.) =
-0.9899709	-0.77926594	-0.15497442	-0.15031165
-0.6028622	0.86623466	-2.1543107	0.41970536
-0.8215522	0.3014275	-0.32184362	0.14445356

(2,2,.,.) =
0.74701905	0.10044397	-0.40519297	0.03822808
0.30726334	0.27862388	1.731753	0.032177072
-1.3476961	-0.2294767	0.99794704	0.7398458

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.0	0.0	-0.56554353	0.0
0.34443378	0.0	-0.44255006	0.764976
-0.6563878	0.0	0.0	0.47564936

(1,2,.,.) =
0.0	0.6518719	0.0	-0.020877302
0.0	-0.29986328	0.0	0.0
0.0	0.09457427	0.0	-0.025244355

(2,1,.,.) =
-0.3899709	-0.17926592	0.0	0.0
-0.0028621554	0.26623464	-1.5543107	0.0
-0.2215522	0.0	0.0	0.0

(2,2,.,.) =
0.14701903	0.0	0.0	0.0
0.0	0.0	1.131753	0.0
-0.74769604	0.0	0.397947	0.13984579

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Reshape#

Reshapes an output to a certain shape.

Supports shape inference by allowing one -1 in the target shape. For example, if input shape is (2, 3, 4), target shape is (3, -1), then output shape will be (3, 8).

Reshape(targetShape, inputShape = null)

Parameters:

  • targetShape: The target shape that you desire to have. Batch dimension should be excluded.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Reshape
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Reshape(Array(3, 8), inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-1.7092276	-1.3941092	-0.6348466	0.71309644
0.3605411	0.025597548	0.4287048	-0.548675
0.4623341	-2.3912702	0.22030865	-0.058272455

(1,2,.,.) =
-1.5049093	-1.8828062	0.8230564	-0.020209199
-0.3415721	1.1219939	1.1089007	-0.74697906
-1.503861	-1.616539	0.048006497	1.1613717

(2,1,.,.) =
0.21216023	1.0107462	0.8586909	-0.05644316
-0.31436008	1.6892323	-0.9961186	-0.08169463
0.3559391	0.010261055	-0.70408463	-1.2480727

(2,2,.,.) =
1.7663039	0.07122444	0.073556066	-0.7847014
0.17604464	-0.99110585	-1.0302067	-0.39024687
-0.0260166	-0.43142694	0.28443158	0.72679126

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-1.7092276	-1.3941092	-0.6348466	0.71309644	    0.3605411	0.025597548	0.4287048	-0.548675
0.4623341	-2.3912702	0.22030865	-0.058272455	-1.5049093	-1.8828062	0.8230564	-0.020209199
-0.3415721	1.1219939	1.1089007	-0.74697906	    -1.503861	-1.616539	0.048006497	1.1613717

(2,.,.) =
0.21216023	1.0107462	0.8586909	-0.05644316	    -0.31436008	1.6892323	-0.9961186	-0.08169463
0.3559391	0.010261055	-0.70408463	-1.2480727	    1.7663039	0.07122444	0.073556066	-0.7847014
0.17604464	-0.99110585	-1.0302067	-0.39024687	    -0.0260166	-0.43142694	0.28443158	0.72679126

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3x8]

Merge#

Used to merge a list of inputs into a single output, following some merge mode.

Merge must have at least two input layers.

Merge(layers = null, mode = "sum", concatAxis = -1, inputShape = null)

Parameters:

  • layers: A list of layer instances. Must be more than one layer.

  • mode: Merge mode. String, must be one of: ‘sum’, ‘mul’, ‘concat’, ‘ave’, ‘cos’, ‘dot’, ‘max’. Default is ‘sum’.

  • concatAxis: Integer, axis to use when concatenating layers. Only specify this when merge mode is ‘concat’. Default is -1, meaning the last axis of the input.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a MultiShape object. For Python API, it should be a list of shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.InputLayer
import com.intel.analytics.bigdl.dllib.keras.layers.Merge
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.utils.{Shape, T}
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
val l1 = InputLayer[Float](inputShape = Shape(2, 3))
val l2 = InputLayer[Float](inputShape = Shape(2, 3))
val layer = Merge[Float](layers = List(l1, l2), mode = "sum")
model.add(layer)
val input1 = Tensor[Float](2, 2, 3).rand(0, 1)
val input2 = Tensor[Float](2, 2, 3).rand(0, 1)
val input = T(1 -> input1, 2 -> input2)
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.utils.Table =
{
    2: (1,.,.) =
    0.87815475	0.15025006	0.34412447
    0.07909282	0.008027249	0.111715704

    (2,.,.) =
    0.52245367	0.2547527	0.35857987
    0.7718501	0.26783863	0.8642062

    [com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]
    1: (1,.,.) =
    0.5377018	0.28364193	0.3424284
    0.0075349305	0.9018168	0.9435114

    (2,.,.) =
    0.09112563	0.88585275	0.3100201
    0.7910178	0.57497376	0.39764535

    [com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]
}

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
1.4158566	0.433892	0.6865529
0.08662775	0.90984404	1.0552272

(2,.,.) =
0.6135793	1.1406054	0.66859996
1.5628679	0.8428124	1.2618515

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

MaxoutDense#

A dense maxout layer that takes the element-wise maximum of linear layers.

This allows the layer to learn a convex, piecewise linear activation function over the inputs.

The input of this layer should be 2D.

MaxoutDense(outputDim, nbFeature = 4, wRegularizer = null, bRegularizer = null, bias = true, inputShape = null)

Parameters:

  • outputDim: The size of output dimension.

  • nbFeature: Number of Dense layers to use internally. Integer. Default is 4.

  • wRegularizer: An instance of Regularizer, (eg. L1 or L2 regularization), applied to the input weights matrices. Default is null.

  • bRegularizer: An instance of Regularizer, applied to the bias. Default is null.

  • bias: Whether to include a bias (i.e. make the layer affine rather than linear). Default is true.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.MaxoutDense
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(MaxoutDense(2, inputShape = Shape(3)))
val input = Tensor[Float](2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
-1.3550005	-1.1668127	-1.2882779
0.83600295	-1.94683	1.323666
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Output is: .. code-block:: scala

output: com.intel.analytics.bigdl.nn.abstractnn.Activity = 0.71675766 1.2987505 0.9871184 0.6634239 [com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2]


Squeeze#

Delete the singleton dimension(s). The batch dimension needs to be unchanged.

For example, if input has size (2, 1, 3, 4, 1):

Squeeze(1) will give output size (2, 3, 4, 1),

Squeeze() will give output size (2, 3, 4)

Squeeze(dims = null, inputShape = null)

Parameters:

  • dims: The dimension(s) to squeeze. 0-based index. Cannot squeeze the batch dimension. The selected dimensions must be singleton, i.e. having size 1. Default is null, and in this case all the non-batch singleton dimensions will be deleted.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Squeeze
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Squeeze[Float](1, inputShape = Shape(1, 1, 32)))
val input = Tensor[Float](1, 1, 1, 32).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
0.5521966       -1.2199087      0.365958        1.3845297       0.115254946     -0.20352958     2.4912808       0.987046        -0.2115477      3.0530396      -1.0043625      1.4688021       -1.2412603      -0.25383064     0.49164283      -0.40329486     0.26323202      0.7979045       0.025444122   0.47221214       1.3995043       0.48498031      -0.86961967     -0.058370713    -0.85965866     -1.2727696      0.45570874      0.73393697      0.2567143      1.4261572       -0.37773672     -0.7339463

[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x1x1x32]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
0.5521966       -1.2199087      0.365958        1.3845297       0.115254946     -0.20352958     2.4912808       0.987046        -0.2115477      3.0530396      -1.0043625      1.4688021       -1.2412603      -0.25383064     0.49164283      -0.40329486     0.26323202      0.7979045       0.025444122   0.47221214       1.3995043       0.48498031      -0.86961967     -0.058370713    -0.85965866     -1.2727696      0.45570874      0.73393697      0.2567143      1.4261572       -0.37773672     -0.7339463

[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x1x32]

BinaryThreshold#

Threshold the input.

If an input element is smaller than the threshold value, it will be replaced by 0; otherwise, it will be replaced by 1.

BinaryThreshold(value = 1e-6, inputShape = null)

Parameters:

  • value: The threshold value to compare with. Default is 1e-6.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.BinaryThreshold
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(BinaryThreshold[Float](inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-1.1907398      -0.18995096     -2.0344417      -1.3789974
-1.8801064      -0.74757665     -0.4339697      0.0058485097
0.7012256       -0.6363152      2.0156987       -0.5512639

(1,2,.,.) =
-0.5251603      0.082127444     0.29550993      1.6357868
-1.3828015      -0.11842779     0.3316966       -0.14360528
0.21216457      -0.117370956    -0.12934707     -0.35854268

(2,1,.,.) =
-0.9071151      -2.8566089      -0.4796377      -0.915065
-0.8439908      -0.25404388     -0.39926198     -0.15191565
-1.0496653      -0.403675       -1.3591816      0.5311797

(2,2,.,.) =
0.53509855      -0.08892822     1.2196561       -0.62759316
-0.47476718     -0.43337926     -0.10406987     1.4035174
-1.7120812      1.1328355       0.9219375       1.3813454

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.0     0.0     0.0     0.0
0.0     0.0     0.0     1.0
1.0     0.0     1.0     0.0

(1,2,.,.) =
0.0     1.0     1.0     1.0
0.0     0.0     1.0     0.0
1.0     0.0     0.0     0.0

(2,1,.,.) =
0.0     0.0     0.0     0.0
0.0     0.0     0.0     0.0
0.0     0.0     0.0     1.0

(2,2,.,.) =
1.0     0.0     1.0     0.0
0.0     0.0     0.0     1.0
0.0     1.0     1.0     1.0

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Sqrt#

Applies an element-wise square root operation to the input.

Sqrt(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Sqrt
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Sqrt[Float](inputShape = Shape(3)))
val input = Tensor[Float](2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
0.6950394       0.5234307       1.7375475
0.25833175      0.02685826      -0.6046901
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
0.8336902       0.7234851       1.3181607
0.50826347      0.16388491      NaN
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Mul#

Multiply a single scalar factor to the incoming data

Mul(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

  • name: String to set the name of the layer. If not specified, its name will by default to be a generated string.

import com.intel.analytics.bigdl.dllib.keras.layers.Mul
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Mul[Float](inputShape = Shape(3, 4)))
val input = Tensor[Float](2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
-1.2316265  -2.008802 -1.3908259  -0.61135375
-0.48992255 0.1786112 0.18872596  0.49621895
-0.6931602  -0.919745 -0.09019699 -0.41218707

(2,.,.) =
-0.3135355  -0.4385771  -0.3317269  1.0412029
-0.8859662  0.17758773  -0.73779273 -0.4445366
0.3921595 1.6923207 0.014470488 0.4044164

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-0.59036994 -0.9629025  -0.6666808  -0.29304734
-0.2348403  0.0856158 0.09046422  0.23785843
-0.33226058 -0.44087213 -0.043235175  -0.19757845

(2,.,.) =
-0.15029064 -0.21022828 -0.15901053 0.49909195
-0.42468053 0.0851252 -0.3536548  -0.21308492
0.18797839  0.81119984  0.006936308 0.19385365

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3x4]

MulConstant#

Multiply the input by a (non-learnable) scalar constant.

MulConstant(constant, inputShape = null)

Parameters:

  • constant: The scalar constant to be multiplied.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.MulConstant
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(MulConstant[Float](2.2, inputShape = Shape(3, 4)))
val input = Tensor[Float](2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
-0.16873977     1.0812985       1.0942211       -0.67091423
1.0086882       0.5915831       0.26184535      -1.361431
1.5616825       -0.037591368    1.2794676       1.0692137

(2,.,.) =
0.29868057      -0.23266982     -0.7679556      -2.209848
-0.13954644     -0.1368473      -0.54510623     1.8397199
-0.58691734     -0.56410027     -1.5567777      0.050648995

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-0.3712275      2.3788567       2.4072864       -1.4760114
2.219114        1.3014828       0.57605976      -2.9951482
3.4357016       -0.08270101     2.8148286       2.3522704

(2,.,.) =
0.6570973       -0.5118736      -1.6895024      -4.8616657
-0.3070022      -0.30106407     -1.1992338      4.047384
-1.2912182      -1.2410206      -3.424911       0.11142779

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3x4]

Scale#

Scale is the combination of CMul and CAdd.

Computes the element-wise product of the input and weight, with the shape of the weight “expand” to match the shape of the input.

Similarly, perform an expanded bias and perform an element-wise add.

Scale(size, inputShape = null)

Parameters:

  • size: Size of the weight and bias.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Scale
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
var array = Array(1, 2)
model.add(Scale[Float](array, inputShape = Shape(3)))
val input = Tensor[Float](2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
-0.006399727    -0.06412822     -0.2334789
0.31029955      1.6557469       1.9614618
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
0.09936619      0.57585865      0.20324506
0.38537437      -0.8598822      -1.0186496
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Log#

Applies a log transformation to the input.

Log(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Log
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Log[Float](inputShape = Shape(2, 4, 4)))
val input = Tensor[Float](1, 2, 4, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
0.38405678      -0.5502389      -0.383079       -0.988537
-0.6294056      -0.7838047      0.8747865       -1.0659786
-2.2445498      -0.5488076      -0.42898977     0.6916364
1.6542299       -0.9966279      -0.38244298     1.6954672

(1,2,.,.) =
0.43478605      -0.6678534      1.9530942       -0.5209587
0.12899925      0.20572199      2.0359943       0.55223215
0.65247816      0.8792108       -0.38860792     0.48663738
-1.0084358      0.31141177      0.69208467      0.48385203

[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x2x4x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
-0.95696485     NaN     NaN     NaN
NaN     NaN     -0.13377543     NaN
NaN     NaN     NaN     -0.36869493
0.5033356       NaN     NaN     0.5279584

(1,2,.,.) =
-0.83290124     NaN     0.6694149       NaN
-2.0479486      -1.5812296      0.7109843       -0.5937868
-0.4269776      -0.12873057     NaN     -0.720236
NaN     -1.1666392      -0.36804697     -0.72597617

[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x2x4x4]

Identity#

Identity just return the input to output.

It’s useful in same parallel container to get an origin input.

Identity(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.layers.Identity
import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Identity[Float](inputShape = Shape(4, 4)))
val input = Tensor[Float](3, 4, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
1.9601166       -0.86010313     0.0023731247    -0.81219757
1.1469674       -1.5375912      -1.5348053      -0.34829113
-1.236773       -0.7183283      -0.89256984     0.8605067
0.7937664       0.52992857      -1.6157389      0.36134166

(2,.,.) =
-0.44434744     -0.23848957     -0.01632014     -0.58109635
-0.19856784     -2.3421717      -0.5868049      -0.76775354
0.80254126      1.78778 -1.1835604      1.4489703
0.8731402       0.8906672       0.2800079       -0.6715317

(3,.,.) =
1.4093032       2.358169        -1.4620789      1.1904576
-0.18263042     -0.31869793     2.01061 1.2159953
-0.5801479      1.2949371       -0.7510707      -1.0707517
0.30815956      -1.161963       -0.26964024     -0.4759499

[com.intel.analytics.bigdl.tensor.DenseTensor of size 3x4x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
1.9601166       -0.86010313     0.0023731247    -0.81219757
1.1469674       -1.5375912      -1.5348053      -0.34829113
-1.236773       -0.7183283      -0.89256984     0.8605067
0.7937664       0.52992857      -1.6157389      0.36134166

(2,.,.) =
-0.44434744     -0.23848957     -0.01632014     -0.58109635
-0.19856784     -2.3421717      -0.5868049      -0.76775354
0.80254126      1.78778 -1.1835604      1.4489703
0.8731402       0.8906672       0.2800079       -0.6715317

(3,.,.) =
1.4093032       2.358169        -1.4620789      1.1904576
-0.18263042     -0.31869793     2.01061 1.2159953
-0.5801479      1.2949371       -0.7510707      -1.0707517
0.30815956      -1.161963       -0.26964024     -0.4759499

[com.intel.analytics.bigdl.tensor.DenseTensor of size 3x4x4]

Select#

Select an index of the input in the given dim and return the subset part.

The batch dimension needs to be unchanged.

For example, if input is:

[[1, 2, 3], [4, 5, 6]]

Select(1, 1) will give output [2 5]

Select(1, -1) will give output [3 6]

Select(dim, index, inputShape = null)

Parameters:

  • dim: The dimension to select. 0-based index. Cannot select the batch dimension. -1 means the last dimension of the input.

  • index: The index of the dimension to be selected. 0-based index. -1 means the last dimension of the input.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Select
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Select[Float](1, 2, inputShape = Shape(3, 1, 3)))
val input = Tensor[Float](1, 3, 1, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-0.67646945     -0.5485965      -0.11103154
(1,2,.,.) =
-0.13488655     0.43843046      -0.04482145
(1,3,.,.) =
-0.18094881     0.19431554      -1.7624844
[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x3x1x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-0.18094881     0.19431554      -1.7624844
[com.intel.analytics.bigdl.tensor.DenseTensor of size 1x1x3]

Dense#

A densely-connected NN layer.

The most common input is 2D.

Dense(outputDim, init = "glorot_uniform", activation = null, wRegularizer = null, bRegularizer = null, bias = true, inputShape = null)

Parameters:

  • outputDim: The size of the output dimension.

  • init: Initialization method for the weights of the layer. Default is Xavier.You can also pass in corresponding string representations such as ‘glorot_uniform’ or ‘normal’, etc. for simple init methods in the factory method.

  • activation: Activation function to use. Default is null.You can also pass in corresponding string representations such as ‘relu’or ‘sigmoid’, etc. for simple activations in the factory method.

  • wRegularizer: An instance of Regularizer, applied to the input weights matrices. Default is null.

  • bRegularizer: An instance of Regularizer, applied to the bias. Default is null.

  • bias: Whether to include a bias (i.e. make the layer affine rather than linear). Default is true.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Dense
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Dense[Float](5, activation = "relu", inputShape = Shape(4)))
val input = Tensor[Float](2, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
1.4289935       -1.7659454      -0.08306135     -1.0153456
1.0191492       0.37392816      1.3076705       -0.19495767
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
0.5421522       0.49008092      0.0     0.0     0.0
0.07940009      0.0     0.12953377      0.0     0.0
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x5]

Negative#

Computes the negative value of each element of the input.

Negative(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Negative
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Negative[Float](inputShape = Shape(2, 3)))
val input = Tensor[Float](2, 2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
1.031705        -0.5723963      1.998631
-0.32908052     2.4069138       -2.4111257
(2,.,.) =
0.5355049       -1.4404331      -0.38116863
-0.45641592     -1.1485358      0.94766915
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-1.031705       0.5723963       -1.998631
0.32908052      -2.4069138      2.4111257
(2,.,.) =
-0.5355049      1.4404331       0.38116863
0.45641592      1.1485358       -0.94766915
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

CAdd#

This layer has a bias with given size.

The bias will be added element-wise to the input.

If the element number of the bias matches the input, a simple element-wise addition will be done.

Or the bias will be expanded to the same size of the input.

The expand means repeat on unmatched singleton dimension (if some unmatched dimension isn’t a singleton dimension, an error will be raised).

CAdd(size, bRegularizer = null, inputShape = null)

Parameters:

  • size: the size of the bias

  • bRegularizer: An instance of Regularizer, applied to the bias. Default is null.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.CAdd
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(CAdd[Float](Array(2, 3), inputShape = Shape(2, 3)))
val input = Tensor[Float](2, 2, 3).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
0.2183351       0.32434112      0.89350265
0.3348259       0.78677046      0.24054797
(2,.,.) =
0.9945844       0.72363794      0.7737936
0.05522544      0.3517818       0.7417069
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
0.1358028       0.6956667       1.0837181
0.6767027       0.7955346       0.5063505
(2,.,.) =
0.9120521       1.0949634       0.96400905
0.3971022       0.36054593      1.0075095
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

RepeatVector#

Repeats the input n times.

The input of this layer should be 2D, i.e. (num_samples, features). The output of thi layer should be 3D, i.e. (num_samples, n, features).

RepeatVector(n, inputShape = null)

Parameters:

  • n: Repetition factor. Integer.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

  • name: String to set the name of the layer. If not specified, its name will by default to be a generated string.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.RepeatVector
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(RepeatVector[Float](4, inputShape = Shape(3)))
val input = Tensor[Float](2, 3).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
-0.31839952 -0.3495366  0.542486
-0.54981124 -0.8428188  0.8225184
[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
-0.31839952 -0.3495366  0.542486
-0.31839952 -0.3495366  0.542486
-0.31839952 -0.3495366  0.542486
-0.31839952 -0.3495366  0.542486

(2,.,.) =
-0.54981124 -0.8428188  0.8225184
-0.54981124 -0.8428188  0.8225184
-0.54981124 -0.8428188  0.8225184
-0.54981124 -0.8428188  0.8225184

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x4x3]

GaussianSampler#

Takes {mean, log_variance} as input and samples from the Gaussian distribution.

GaussianSampler(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a MultiShape object that consists of two identical Single Shape. For Python API, it should be a list of two identical shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.GaussianSampler
import com.intel.analytics.bigdl.utils.{Shape, MultiShape, T}
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
val shape1 = Shape(2, 3)
val shape2 = Shape(2, 3)
model.add(GaussianSampler[Float](inputShape = MultiShape(List(shape1,shape2))))
val input1 = Tensor[Float](2, 2, 3).rand(0, 1)
val input2 = Tensor[Float](2, 2, 3).rand(0, 1)
val input = T(1 -> input1, 2 -> input2)
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.utils.Table =
{
        2: (1,.,.) =
        0.9996127    0.8964211       0.7424038
        0.40628982   0.37035564      0.20108517

        (2,.,.) =
        0.6974727    0.60202897      0.1535999
        0.012422224  0.5993025       0.96206

        [com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]
        1: (1,.,.) =
        0.21060324   0.576583        0.21633287
        0.1484059    0.2730577       0.25317845

        (2,.,.) =
        0.58513683   0.58095694      0.18811373
        0.7029449    0.41235915      0.44636542

        [com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]
}

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
1.5258198       1.9536011       -1.8591263
-1.0618867      -0.751225       0.35412917

(2,.,.) =
1.3334517       -0.60312974     0.7324476
0.09502721      0.8094909       0.44807082

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Exp#

Applies element-wise exp to the input.

When you use this layer as the first layer of a model, you need to provide the argument inputShape (a Single Shape, does not include the batch dimension).

Exp(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a MultiShape object that consists of two identical Single Shape. For Python API, it should be a list of two identical shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Exp
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Exp[Float](inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-1.5841372      -0.13795324     -2.144475       0.09272669
1.055668        -1.2310301      1.2145554       -0.6073714
0.9296467       0.2923885       1.3364213       0.1652137

(1,2,.,.) =
0.2099718       -0.3856573      -0.92586        -0.5317779
0.6618383       -0.9677452      -1.5014665      -0.35464883
2.045924        -0.317644       -1.812726       0.95438373

(2,1,.,.) =
-0.4536791      -0.34785584     1.6424289       -0.07981159
-0.8022624      -0.4211059      0.3461831       1.9598864
-0.84695745     -0.6115283      0.7729755       2.3077402

(2,2,.,.) =
-0.08438411     -0.908458       0.6688936       -0.7292123
-0.26337254     0.55425745      -0.14925817     -0.010179609
-0.62562865     -1.0517743      -0.23839666     -1.144982

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.20512469      0.8711394       0.11712951      1.0971619
2.8738942       0.29199165      3.3687959       0.544781
2.533614        1.3396233       3.8054006       1.1796452

(1,2,.,.) =
1.2336433       0.6800035       0.39619055      0.5875594
1.9383523       0.37993878      0.22280318      0.7014197
7.7363033       0.7278619       0.16320862      2.5970695

(2,1,.,.) =
0.63528657      0.70620066      5.167706        0.92329025
0.44831353      0.6563206       1.4136615       7.0985208
0.42871734      0.5425211       2.1662023       10.051684

(2,2,.,.) =
0.9190782       0.4031454       1.9520763       0.48228875
0.76845556      1.740648        0.8613467       0.98987204
0.53492504      0.34931743      0.7878901       0.31822965

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Square#

Applies an element-wise square operation to the input.

When you use this layer as the first layer of a model, you need to provide the argument inputShape (a Single Shape, does not include the batch dimension).

Square(inputShape = null)

Parameters:

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a MultiShape object that consists of two identical Single Shape. For Python API, it should be a list of two identical shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Square
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Square[Float](inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).randn()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
-0.108013034    1.8879265       1.2232096       -1.5076439
1.4895755       -0.37966672     -0.34892964     0.15224025
-0.9296686      -1.1523775      0.14153497      -0.26954007

(1,2,.,.) =
-1.0875931      2.190617        -0.6903083      1.0039362
-0.1275677      -1.1096588      0.37359753      -0.17367937
0.23349741      0.14639114      -0.2330162      0.5343827

(2,1,.,.) =
0.3222191       0.21463287      -1.0157064      -0.22627507
1.1714277       0.43371263      1.069315        0.5122436
0.1958086       -1.4601041      2.5394423       -0.470833

(2,2,.,.) =
-0.38708544     -0.951611       -0.37234613     0.26813275
1.9477026       0.32779223      -1.2308712      -2.2376378
0.19652915      0.3304719       -1.7674786      -0.86961496

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.011666816     3.5642662       1.4962418       2.2729902
2.218835        0.14414681      0.1217519       0.023177093
0.86428374      1.3279738       0.020032147     0.07265185

(1,2,.,.) =
1.1828587       4.7988033       0.47652552      1.0078878
0.016273517     1.2313428       0.13957511      0.030164523
0.05452104      0.021430366     0.054296546     0.28556487

(2,1,.,.) =
0.10382515      0.046067268     1.0316595       0.05120041
1.3722429       0.18810664      1.1434345       0.26239353
0.038341008     2.131904        6.448767        0.22168371

(2,2,.,.) =
0.14983514      0.9055635       0.13864164      0.07189517
3.7935455       0.10744774      1.5150439       5.007023
0.038623706     0.109211676     3.1239805       0.7562302

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Power#

Applies an element-wise power operation with scale and shift to the input.

f(x) = (shift + scale * x)^power^

Power(power, scale = 1, shift = 0, inputShape = null)

Parameters:

  • power: The exponent

  • scale: The scale parameter. Default is 1.

  • shift: The shift parameter. Default is 0.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Power
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Power[Float](2, inputShape = Shape(2, 3)))
val input = Tensor[Float](2, 2, 3).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
0.24691099      0.7588585       0.5785183
0.10356348      0.2252714       0.3129436

(2,.,.) =
0.6277785       0.75136995      0.044648796
0.46396527      0.9793776       0.92727077

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
0.060965035     0.5758662       0.3346834
0.010725395     0.050747205     0.0979337

(2,.,.) =
0.39410582      0.5645568       0.001993515
0.21526377      0.95918053      0.8598311

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

AddConstant#

Add a (non-learnable) scalar constant to the input.

AddConstant(constant, inputShape = null)

Parameters:

  • constant: The scalar constant to be added.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.AddConstant
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(AddConstant[Float](1, inputShape = Shape(2, 3)))
val input = Tensor[Float](2, 2, 3).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,.,.) =
0.5658301       0.3508225       0.4012322
0.1941942       0.18934165      0.6909284

(2,.,.) =
0.5985211       0.5485885       0.778548
0.16745302      0.10363362      0.92185616

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,.,.) =
1.5658301       1.3508224       1.4012322
1.1941942       1.1893417       1.6909285

(2,.,.) =
1.5985211       1.5485885       1.778548
1.167453        1.1036336       1.9218562

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3]

Narrow#

Narrow the input with the number of dimensions not being reduced.

The batch dimension needs to be unchanged.

For example, if input is:

[[1 2 3], [4 5 6]]

Narrow(1, 1, 2) will give output

[[2 3], [5 6]]

Narrow(1, 2, -1) will give output

[3, 6]

Narrow(dim, offset, length = 1, inputShape = null)

Parameters:

  • dim: The dimension to narrow. 0-based index. Cannot narrow the batch dimension. -1 means the last dimension of the input.

  • offset: Non-negative integer. The start index on the given dimension. 0-based index.

  • length: The length to narrow. Default is 1. Can use a negative length such as -1 in the case where input size is unknown.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Narrow
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Narrow[Float](1, 1, inputShape = Shape(2, 3, 4)))
val input = Tensor[Float](2, 2, 3, 4).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
0.13770224      0.63719153      0.7776689       0.46612367
0.9026256       0.11982094      0.8282868       0.05095969
0.889799        0.6386537       0.35438475      0.298043

(1,2,.,.) =
0.5029727       0.20103335      0.20150806      0.06437344
0.2255908       0.5388977       0.59737855      0.5210477
0.4055072       0.11848069      0.7118382       0.9796308

(2,1,.,.) =
0.63957494      0.1921936       0.7749439       0.19744827
0.91683346      0.16140814      0.9753973       0.8161283
0.8481694       0.8802563       0.1233245       0.5732614

(2,2,.,.) =
0.275001        0.35905758      0.15939762      0.09233412
0.16610192      0.032060683     0.37298614      0.48936844
0.031097537     0.82767457      0.10246291      0.9951448

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x3x4]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.5029727       0.20103335      0.20150806      0.06437344
0.2255908       0.5388977       0.59737855      0.5210477
0.4055072       0.11848069      0.7118382       0.9796308

(2,1,.,.) =
0.275001        0.35905758      0.15939762      0.09233412
0.16610192      0.032060683     0.37298614      0.48936844
0.031097537     0.82767457      0.10246291      0.9951448

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x1x3x4]

Permute#

Permutes the dimensions of the input according to a given pattern.

Useful for connecting RNNs and convnets together.

Permute(dims, inputShape = null)

Parameters:

  • dims: Int array. Permutation pattern, does not include the batch dimension. Indexing starts at 1.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.Permute
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential[Float]()
model.add(Permute[Float](Array(2, 1, 3), inputShape = Shape(2, 2, 3)))
val input = Tensor[Float](2, 2, 2, 3).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
0.8451549       0.06361471      0.7324815
0.31086245      0.21210302      0.35112163

(1,2,.,.) =
0.61466074      0.50173014      0.8759959
0.19090249      0.671227        0.73089105
(2,1,.,.) =
0.47867084      0.9341955       0.063592255
0.24063066      0.502274        0.9114748
(2,2,.,.) =
0.93335986      0.25173688      0.88615775
0.5394321       0.330763        0.89036304

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x2x3]

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.8451549       0.06361471      0.7324815
0.61466074      0.50173014      0.8759959

(1,2,.,.) =
0.31086245      0.21210302      0.35112163
0.19090249      0.671227        0.73089105
(2,1,.,.) =
0.47867084      0.9341955       0.063592255
0.93335986      0.25173688      0.88615775
(2,2,.,.) =
0.24063066      0.502274        0.9114748
0.5394321       0.330763        0.89036304

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x2x3]

ResizeBilinear#

Resize the input image with bilinear interpolation. The input image must be a float tensor with NHWC or NCHW layout.

ResizeBilinear(outputHeight, outputWidth, alignCorners = false, dimOrdering = "th", inputShape = null)

Parameters:

  • outputHeight: output height

  • outputWidth: output width

  • alignCorners: align corner or not

  • dimOrdering: Format of input data. Either DataFormat.NCHW (dimOrdering=’th’) or DataFormat.NHWC (dimOrdering=’tf’). Default is NCHW.

  • inputShape: Only need to specify this argument when you use this layer as the first layer of a model. For Scala API, it should be a Shape object. For Python API, it should be a shape tuple. Batch dimension should be excluded.

import com.intel.analytics.bigdl.dllib.keras.models.Sequential
import com.intel.analytics.bigdl.dllib.keras.layers.ResizeBilinear
import com.intel.analytics.bigdl.dllib.utils.Shape
import com.intel.analytics.bigdl.tensor.Tensor

val model = Sequential()
model.add(ResizeBilinear[Float](2, 3, inputShape = Shape(2, 3, 5)))
val input = Tensor[Float](2, 2, 3, 5).rand()
val output = model.forward(input)

Input is:

input: com.intel.analytics.bigdl.tensor.Tensor[Float] =
(1,1,.,.) =
0.6991891       0.007127314     0.73871046      0.95916307      0.9433856
0.41275907      0.37573513      0.99193203      0.06930728      0.5922364
0.024281504     0.2592453       0.3898136       0.6635241       0.85888565

(1,2,.,.) =
0.38028112      0.43709648      0.62538666      0.8468501       0.6445014
0.45252413      0.48801896      0.59471387      0.013207023     0.3567462
0.85187584      0.49279585      0.7973665       0.81287366      0.07852263

(2,1,.,.) =
0.1452374       0.6140467       0.36384684      0.066476084     0.96101314
0.54862195      0.66091377      0.86857307      0.6844842       0.7368217
0.25342992      0.71737933      0.12789607      0.21691357      0.7543404

(2,2,.,.) =
0.79176855      0.1204049       0.58971256      0.115073755     0.10459962
0.5225398       0.742363        0.7612815       0.9881919       0.13359445
0.9026869       0.13972941      0.92064524      0.9435532       0.5502235

[com.intel.analytics.bigdl.tensor.DenseTensor of...

Output is:

output: com.intel.analytics.bigdl.nn.abstractnn.Activity =
(1,1,.,.) =
0.6991891       0.4948494       0.9539039
0.21852028      0.5664119       0.48613077

(1,2,.,.) =
0.38028112      0.56262326      0.7794005
0.6522  0.6274959       0.34790504

(2,1,.,.) =
0.1452374       0.4472468       0.36465502
0.40102595      0.5618719       0.54899293

(2,2,.,.) =
0.79176855      0.43327665      0.111582376
0.71261334      0.70765764      0.75788474

[com.intel.analytics.bigdl.tensor.DenseTensor of size 2x2x2x3]