# Syntax

For the complete nd4j-api index, please consult the Javadoc.

There are three types of operations used in ND4J: scalars, transforms and accumulations. We’ll use the word op synonymously with operation.

Most of the ops just take enums, or a list of discrete values that you can autocomplete. Activation functions are the exception, because they take strings such as `"relu"`

or `"tanh"`

.

Scalars, transforms and accumulations each have their own patterns. Transforms are the simplest, since the take a single argument and perform an operation on it. Absolute value is a transform that takes the argument `x`

like so `abs(IComplexNDArray ndarray)`

and produces the result which is the absolute value of x. Similarly, you would apply to the sigmoid transform `sigmoid()`

to produce the “sigmoid of x”.

Scalars just take two arguments: the input and the scalar to be applied to that input. For example, `ScalarAdd()`

takes two arguments: the input `INDArray x`

and the scalar `Number num`

; i.e. `ScalarAdd(INDArray x, Number num)`

. The same format applies to every Scalar op.

Finally, we have accumulations, which are also known as reductions in GPU-land. Accumulations add arrays and vectors to one another and can *reduce* the dimensions of those arrays in the result by adding their elements in a rowwise op. For example, we might run an accumulation on the array

Which would give us the vector

Reducing the columns (i.e. dimensions) from two to one.

Accumulations can be either pairwise or scalar. In a pairwise reduction, we might be dealing with two arrays, x and y, which have the same shape. In that case, we could calculate the cosine similarity of x and y by taking their elements two by two.

Or take `EuclideanDistance(arr, arr2)`

, a reduction between one array `arr`

and another `arr2`

.

Many ND4J ops are overloaded, meaning methods sharing a common name have different argument lists. Below we will explain only the simplest configurations.

As you can see, there are three possible argument types with ND4J ops: inputs, optional arguments and outputs. The outputs are specified in the ops’ constructor. The inputs are specified in the parentheses following the method name, always in the first position, and the optional arguments are used to transform the inputs; e.g. the scalar to add; the coefficient to multiply by, always in the second position.

Method | What it does |

| |

ACos(INDArray x) | Trigonometric inverse cosine, elementwise. The inverse of cos such that, if |

ASin(INDArray x) | Also known as arcsin. Inverse sine, elementwise. |

ATan(INDArray x) | Trigonometric inverse tangent, elementwise. The inverse of tan, such that, if |

Transforms.tanh(myArray) | Hyperbolic tangent: a sigmoidal function. This applies elementwise tanh inplace. |

Nd4j.getExecutioner().exec(Nd4j.getOpFactory() .createTransform(“tanh”, myArray)) | equivalent to the above |

For other transforms, please see this page.

Here are two examples of performing `z = tanh(x)`

, in which the original array `x`

is unmodified.

The latter two examples above use ND4J’s basic convention for all ops, in which we have 3 NDArrays, x, y and z.

Frequently, `z = x`

(this is the default if you use a constructor with only one argument). But there are exceptions for situations like `x = x + y`

. Another possibility is `z = x + y`

, etc.

### Accumulations

Most accumulations are accessable directly via the INDArray interface.

For example, to add up all elements of an NDArray:

Accum along dimension example - i.e., sum values in each row:

Accumulations along dimensions generalize, so you can sum along two dimensions of any array with two or more dimensions.

### Subset Operations on Arrays

A simple example:

Interval is fromInclusive, toExclusive; note that can equivalently use inclusive version: NDArrayIndex.interval(1,2,true);

These are views of the underlying array, **not** copy operations (which provides greater flexibility and doesn’t have cost of copying).

To avoid in-place behaviour, random.get(…).dup() to make a copy.

| |

INDArray.add(number) | Returns the result of adding |

INDArray.addi(number) | Returns the result of adding |

ScalarAdd(INDArray x, Number num) | Returns the result of adding |

ScalarDivision(INDArray x, Number num) | Returns the result of dividing each entry of |

ScalarMax(INDArray x, Number num) | Compares each entry of |

ScalarMultiplication(INDArray x, Number num) | Returns the result of multiplying each entry of |

ScalarReverseDivision(INDArray x, Number num) | Returns the result of dividing |

ScalarReverseSubtraction(INDArray x, Number num) | Returns the result of subtracting each entry of |

ScalarSet(INDArray x, Number num) | This sets the value of each entry of |

ScalarSubtraction(INDArray x, Number num) | Returns the result of subtracting |

If you do not understand the explanation of ND4J’s syntax, cannot find a definition for a method, or would like to request that a function be added, please let us know on the community forums.

Last updated