Google News
logo
TensorFlow.js - Interview Questions
How to Creating a variable in TensorFlow.js?
tf.Variable() constructor is used to create a variable in TensorFlow.
tensor = tf.Variable([3,4])
Output :
<tf.Variable ‘Variable:0’ shape=(2,) dtype=int32, numpy=array([3, 4], dtype=int32)>
 
Dimension, size, shape, and dtype of a TensorFlow variable :
# import packages
import tensorflow as tf
 
# create variable
tensor1 = tf.Variable([3, 4])
 
# The shape of the variable
print("The shape of the variable: ",
      tensor1.shape)
 
# The number of dimensions in the variable
print("The number of dimensions in the variable:",
      tf.rank(tensor1).numpy())
 
# The size of the variable
print("The size of the tensorflow variable:",
      tf.size(tensor1).numpy())
 
# checking the datatype of the variable
print("The datatype of the tensorflow variable is:",
      tensor1.dtype)
Output :
The shape of the variable:  (2,)
The number of dimensions in the variable: 1
The size of the tensorflow variable: 2
The datatype of the tensorflow variable is: <dtype: 'int32'>
Advertisement