Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment.
The tf.layers.concatenate() function is used to concatenate an array of inputs.
Syntax:
tf.layers.concatenate()
Parameters:
- args(Object): specifies the given object. It is an optional parameter
- axis(number): specifies the axis along which the inputs will concatenate. Its follows 0 based indexing and its value must be in the range [-rank, rank). It can be positive as well as negative. Here, positive axis ranges from 0 to rank(values) and specifies axis-th dimension. Negative value specifies axis + rank(values)-th dimension. It can be positive as well as negative. Default value is 0.
- inputShape: If this parameter is defined, it will create another input layer to insert before this layer.
- batchInputShape: If this parameter is defined, it will create another input layer to insert before this layer.
- batchSize : Used to construct batchInputShape, if not already specified.
- dtype: Specifies the data-type for this layer. Defaults value of this parameter is ‘float32’.
- name: Specifies name for this layer.
- trainable: Specifies whether the weights of this layer are updatable by fit.
- weights: Specifies the initial weight values of the layer.
- inputDType : ‘float32’ or ‘int32’ or ‘bool’ or ‘complex64’ or ‘string’.
Return Value: A single tensor, which is the concatenation of all inputs.
Example 1:
Javascript
// Import the library import * as tf from "@tensorflow/tfjs" // Inputs const input1 = tf.input({shape: [3, 2]}) const input2 = tf.input({shape: [3, 2]}) const input3 = tf.input({shape: [3, 2]}) // Create new concatenate layer const concatenateLayer = tf.layers.concatenate(); const output = concatenateLayer.apply([input1, input2, input3]); // Print shape of resulting tensor console.log(JSON.stringify(output.shape)); |
Output:
[pre][null, 3, 6]
Example 2:
Javascript
// Import the library import * as tf from "@tensorflow/tfjs" // Inputs const input1 = tf.tensor([-2, 1, 0, 5]); const input2 = tf.tensor([3, 2, 3, 2]); const input3 = tf.tensor([4, 3, 1, 2]); // Create new concatenate layer const concatLayer = tf.layers.concatenate(); const output = concatLayer.apply([input1, input2, input3]); // Print resulting tensor console.log(output); |
Output:
Tensor [-2, 1, 0, 5, 3, 2, 3, 2, 4, 3, 1, 2]
Reference: https://js.tensorflow.org/api/latest/#layers.concatenate