Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • TypeScript
  • Vue.js
  • D3.js
  • Collect.js
  • Underscore.js
  • Moment.js
  • Ember.js
  • Tensorflow.js
  • Fabric.js
  • JS Formatter
  • JavaScript
  • Web Technology
Open In App
Next Article:
Tensorflow.js tf.concat() Function
Next article icon

Tensorflow.js tf.conv3d() Function

Last Updated : 10 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Tensorflow.js is javascript library developed by google to run and train machine learning model in the browser or in Node.js.

The tf.conv3d() function is used to compute 3d convolutions over given inputs. The inputs are mainly 3D image data like CT or MRI imaging or any video. To extract features from these data we use a convolution layer which creates convolution kernels and outputs a tensor of 4D or 5D.

Syntax:

tf.conv3d (x, filter, strides, pad, dataFormat?, dilations?) 

Parameters:

  • x : Tensor rank of 4 and 5 is fed into the convolution. Tensor can also be a typed array with  shape [batch, depth, height, width, channels].
  • filter : Filter is of rank 5 and same datatype as x with shape [filterDepth, filterHeight, filterWidth, inChannels, outChannels]. The inchannels of filter and input must match.
  • stride:  Strides are used to move the filter over the input tensor. For this a list is passed of length>=5.
  • pad : Padding algorithm are of 'same' and 'valid' types. 'Same' will create an output of same size as input. Whereas 'valid' output will be of smaller size than input.
  • dataFormat  : Out of two strings it can be either: "NHWC", "NCHW". With the default format "NHWC", the data is stored in the order of: [batch, depth, height, width, channels]. Data format of the input and output data need to be specified. This is optional.
  • dilations : The dilation rate is two tuples of integers that check the rate of the convolution.[dilationDepth,dilationHeight ,dilationWidth]is passed as parameters. This is optional.

Example 1: In this example we will create a input and kernel tensor manually and perform convolution operation. We will print out the shape of the tensors to note the channels number and padding algorithm. 

JavaScript
// Importing libraries import * as from "@tensorflow/tfjs"  // Input tensor const X=tf.tensor5d([[[[[0.],[2.]],                        [[3.],[1.]],                        [[4.],[2.]]],                       [[[1.],[0.]],                        [[2.],[1.]],                        [[4.],[2.]]]]]); console.log('Shape of the input:' ,X.shape);  // Kernel vector has been set const kernel=tf.tensor5d([[[[[0.3,1.2]]],[[[0.6,1.8]]],[[[0.8,1.5]]]]]); console.log('Shape of the kernel:' ,kernel.shape);  // Output tensor after convolution let output=tf.conv3d(X,kernel,strides=[1,1,1,1,1],'same'); output.print(); console.log('Shape of the output tensor:',output.shape); 

Output:

Shape of the input: 1,2,3,2,1 Shape of the kernel: 1,3,1,1,2 Tensor     [[[ [[2        , 5.0999999],],          [[2.8000002, 7.1999998],],          [[1.5      , 4.8000002],]],         [ [[0.8      , 1.5      ],],          [[2.2      , 4.8000002],],          [[1.5      , 4.8000002],]]]] Shape of the output tensor: 1,2,3,1,2

Example 2: In the below code an error will occur as the inchannels number i.e. 5th index of input tensor and 4th index of kernel tensor will not match.

JavaScript
import * as from "@tensorflow/tfjs"  // Input tensor const X=tf.tensor5d([0.,2.,3.,1.,4.,2.,1.,0.,2.,1.,4.,2.],[1,2,3,2,1]);   // Kernel vector has been set const kernel=tf.tensor5d([0.3,1.2,0.6,1.8,0.8,1.5],[1,1,1,2,3]);   // Output tensor after convolution let output=tf.conv3d(X,kernel,strides=[1,1,1,2,1],'same'); output.print(); console.log('Shape of the output tensor:',output.shape); 

Output:

An error occurred on line: 10 Error in conv3d: depth of input (1) must match input depth for filter 2.

Example 3: In this example we will perform two convolution operations. In first we will create a tensor of values randomly picked from a normal distribution and after convolution print the output tensor. After that we take a tensor of bigger size(which can be of 3D image or video) and perform convolution which will again generate a big tensor. We will print the output shape of that tensor.

JavaScript
// Importing libraries import * as from "@tensorflow/tfjs"  // A tensor with values selected from a // normal distribution const x=tf.randomNormal([1,2,4,1,3]);  const kernel=tf.tensor5d([0.3,1.2,0.6,1.8,0.8,1.5],[1,1,2,3,1])  // Output tensor after convolution  let out=tf.conv3d(x,kernel,strides=[1,1,1,1,1],'same')  // Printing the output tensor console.log('First output tensor is:') out.print()  // Input tensor of bigger shape is taken  const y=tf.randomNormal([3,25,25,25,1]);  const kernel2=tf.randomNormal([1,3,3,1,1])  // Convolution is performed let output2=tf.conv3d(y,kernel2,strides=[1,1,1,1,1],'same')  // Output2.print() // Printing out the bigger output shape console.log('\n The shape of the output tensor',output2.shape) 

Output:

First output tensor is: Tensor     [[[ [[-0.702378 ],],          [[1.9029824 ],],          [[-0.1904218],],          [[-2.2287691],]],         [ [[-1.9580686],],          [[-2.8335922],],          [[0.0155853 ],],          [[-3.6478395],]]]]   The shape of the bigger output tensor 3,25,25,25,1

Reference: https://js.tensorflow.org/api/1.0.0/#conv3d


Next Article
Tensorflow.js tf.concat() Function
author
barnadipdey2510
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Tensorflow.js

Similar Reads

  • Tensorflow.js tf.conv2d() Function
    Tensorflow.js is a javascript library developed by Google to run and train machine learning models in the browser or in Node.js.  The tf.conv2d() function is used to compute 2d convolutions over the given input. In a deep neural network, we use this convolution layer which creates a convolution kern
    4 min read
  • Tensorflow.js tf.conv1d() Function
    Introduction: Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .conv1d() function is used to determine a 1D convolution upon the stated input tensor. Syntax: tf.con
    3 min read
  • Tensorflow.js tf.env() Function
    Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .env() function is used to return the present environment i.e. a global entity. Moreover, the environment object in
    1 min read
  • Tensorflow.js tf.cos() Function
    Tensorflow.js is an open-source library which is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .cos() function is used to find the cosine value of the stated tensor input and is done element wise. Syntax
    2 min read
  • Tensorflow.js tf.concat() Function
    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.concat() function is used to concatenate the list of specified Tensors along the given axis. Syntax: tf.concat (tensors, axis)P
    2 min read
  • Tensorflow.js tf.layers.conv3d() Function
    Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.
    3 min read
  • Tensorflow.js tf.cosh() Function
    Tensorflow.js is an open-source library which is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .cosh() function is used to find the hyperbolic cos of the stated tensor input and is done element wise. Syn
    2 min read
  • Tensorflow.js tf.acos() Function
    Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .acos() function is used to find the acos of the stated tensor element input. Syntax:   tf.acos(x)Parameters:   x: It is th
    2 min read
  • Tensorflow.js tf.any() Function
    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. It also helps the developers to develop ML models in the JavaScript language so that ML directly can be used in the browser or in Node
    1 min read
  • Tensorflow.js tf.acosh() Function
    Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .acosh() function is used to find the inverse hyperbolic cos of the stated tensor element input. Syntax:   tf
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences