Tensor Attributes
Given below is a list of commonly used attributes
- tensorflow.shape
- tensorflow.zeros
- tensorflow.ones
- tensorflow.dtype
tensorflow.shape
tensorflow.shape used for returning the shape of the tensor
import tensorflow as tf
# Shape of tensor
m_shape = tf.constant([ [10, 11],
[12, 13],
[14, 15] ]
)
m_shape.shape
The output will be TensorShape([Dimension(3), Dimension(2)])
tensorflow.zeros
tensorflow.zeros used for for creating a tensor of the given dimmension with all elements being zero
import tensorflow as tf
# Create a vector of 0
print(tf.zeros(10))
The output will be Tensor("zeros:0", shape=(10,), dtype=float32)
tensorflow.ones
tensorflow.ones used for for creating a tensor of the given dimmension with all elements being one
import tensorflow as tf
# Create a vector of 1
print(tf.ones([10, 10]))
# Create a vector of ones with the same number of rows as m_shape
print(tf.ones(m_shape.shape[0]))
# Create a vector of ones with the same number of column as m_shape
print(tf.ones(m_shape.shape[1]))
print(tf.ones(m_shape.shape))
The output will be Tensor("ones_1:0", shape=(10, 10), dtype=float32) Tensor("ones_2:0", shape=(3,), dtype=float32) Tensor("ones_3:0", shape=(2,), dtype=float32) Tensor("ones_4:0", shape=(3, 2), dtype=float32)
tensorflow.dtype
tensorflow.dtype used to find the data type of the elements of the tensor
import tensorflow as tf
m_shape = tf.constant([ [10, 11],
[12, 13],
[14, 15] ]
)
print(m_shape.dtype)
The output will be <dtype: 'int32'>
import tensorflow as tf
# Change type of data
type_float = tf.constant(3.123456789, tf.float32)
type_int = tf.cast(type_float, dtype=tf.int32)
print(type_float.dtype)
print(type_int.dtype)
The output of the above code will be <dtype: 'float32'> <dtype: 'int32'>