Open In App

Tensorflow.js tf.layers.permute() Function

The layers in models are the basic blocks in constructions of models, for every layer perform some computation on input and output to the next layer. 
The tf.layers.permute() function is inherited from layers class and is used to permute the dimensions of the input in a given pattern, Also used to connect RNNs and convnet together. In this post, we are going to know how this function works.

Syntax: 



tf.layers.permute(agrs)

Parameters :

Returns: Permute



Example 1: In this example, we are going to create a model with a single layer and only passing the required agrs to permute() function.




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
//create model
const model = tf.sequential();
 
//add layer into model and use permute() method
model.add(tf.layers.permute({
   dims: [2,1],
   inputShape:[8,8]
}));
 
//print outputShape
console.log("Output Shape"+model.outputShape);
 
//model summary()
model.summary()

Output:

Output Shape,8,8
_________________________________________________________________
Layer (type)                 Output shape              Param #    
=================================================================
permute_Permute11 (Permute)  [null,8,8]                0          
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

Example 2: In this example, we are going to create model with 2 layers layer 1 and layer 2 with permute() function and passing all agrs to it.




// Importing the tensorflow.js library
//import * as tf from "@tensorflow/tfjs"
 
//create model
const model = tf.sequential();
 
//add layer into model and use permute() method
//layer 1
model.add(tf.layers.permute({
   dims: [2,1],
   inputShape:[10,64],
   dtype:'int32',
   name:'layer1',
   batchSize:2,
  trainable:true,
  inputDType:'int32'
   
}));
 
//add layer2
model.add(tf.layers.permute({
   dims: [2,1],
   inputShape:[8,16],
   dtype:'int32',
   name:'layer2',
   batchSize:2,
  trainable:true,
  inputDType:'int32'
   
}));
 
//print outputShape
console.log("Output Shape :"+model.outputShape);
 
//model summary()
model.summary()

Output: 

Output Shape :2,10,64
_________________________________________________________________
Layer (type)                 Output shape              Param #    
=================================================================
layer1 (Permute)             [2,64,10]                 0          
_________________________________________________________________
layer2 (Permute)             [2,10,64]                 0          
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________

References: https://js.tensorflow.org/api/latest/#layers.permute


Article Tags :