To create a tensor, you can use tf.constant() as shown in the below TensorFlow tensor shape example:
tf.constant(value, dtype, name = "")
tf.constant(value, shape=(col, row) )
'''
- `value`: Value of n dimension to define the tensor. Optional
- `dtype`: Define the type of data:
- `tf.float32`: Float variable
- `tf.int16`: Integer variable
- `tf.string`: String variable
- `tf.bool`: Boolean variable
- "name": Name of the tensor. Optional. By default, `Const_1:0`
'''Below, a float tensor is converted to integer using you use the method cast (<dtype: 'float32'>)
# 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)x = tf.constant([4.0], dtype = tf.float32)
print(tf.sqrt(x))The output returned a tensor object and not the result of the square of 4. In the example, you print the definition of the tensor and not the actual evaluation of the operation. In the next section, you will learn how TensorFlow works to execute the operations. Following is a list of commonly used operations. The idea is the same. Each operation requires one or more arguments
- tf.add(a, b)
- tf.substract(a, b)
- tf.multiply(a, b)
- tf.div(a, b)
- tf.pow(a, b)
- tf.exp(a)
- tf.sqrt(a)