- Every tensor is an instance of the
Tensor
class. - A tensor may contain numbers, strings, or Boolean values. Every element of a tensor must have the same type.
- Tensors can be created, transformed, and operated upon using functions of the
tf
package.
tf
package provides seven functions that form tensors with known values. The following table lists them and provides a description of each.Creating Tensors with Known Values
Function | Description |
constant(value, dtype=None, shape = None, name = 'Const', verify_shape=False) |
Returns a tensor containing the given value |
zeros(shape, dtype=tf.float32, name = None) |
Returns a tensor filled with zeros |
ones(shape, dtype=tf.float32, name=None) |
Returns a tensor filled with ones |
fill(dims, value, name=None) |
Returns a tensor filled with the given value |
linspace(start, stop, num, name=None) |
Returns a tensor containing a linear range of values |
range(start, limit, delta=1, dtype=None, name='range') |
Returns a tensor containing a range of values |
range(limit, delta=1, dtype=None, name='range') |
Returns a tensor containing a range of values |
A tensor may have multiple dimensions, and the number of dimensions in a tensor is its rank. The lengths of a tensor’s dimensions form an array called the tensor’s shape. Many of the functions in the table accept a shape
parameter that identifies the desired shape of the new tensor. The following examples demonstrate how you can set this parameter:
[]
— The tensor contains a single value.[3]
— The tensor is a one-dimensional array containing three values.[3, 4]
— The tensor is a 3-x-4 matrix.[3, 4, 5]
— The tensor is a multidimensional array whose dimensions equal 3, 4, and 5.
dtype
argument that identifies the data type of the tensor's elements. The default value of dtype
is float32
, which indicates that, by default, tensors contain single-precision floating-point values. The following table lists float32
and other possible data types.Tensor Data Types
Data Type | Description |
bool |
Boolean values |
uint8/uint16 |
Unsigned integers |
quint8/quint16 |
Quantized unsigned integers |
int8/int16/int32/int64 |
Signed integers |
qint8/qint32 |
Quantized signed integers |
float16/float32/float64 |
Floating-point values |
complex64/complex128 |
Complex floating-point values |
string |
Strings |
Each function in the preceding table accepts an optional name
argument that serves as an identifier for the tensor. Applications can access a tensor by name through the tensor's graph.
The constant function
The most popular function in the table isconstant
. Its only required argument is the first, which defines the value or values to be stored in the tensor. You can provide these values in a list, and the following code creates a one-dimensional tensor containing three floating-point values:
t1 = tf.constant([1.5, 2.5, 3.5])Multidimensional arrays use similar notation. The following code creates a 2-x-2 matrix and sets each of its elements to the letter b:
t2 = tf.constant([['b', 'b'], ['b', 'b']])By default, TensorFlow won’t raise an error if the function’s first argument doesn’t have the shape given by the
shape
argument. But if you set the last argument, verify_shape
, to True
, TensorFlow will verify that the two shapes are equal. The following code provides an example of mismatched shapes:
t3 = tf.constant([4, 2], tf.int16, [3], 'Const', True)In this case, the given shape,
[3]
, doesn't match the shape of the first argument, which is [2]
. As a result, TensorFlow displays the following error:
TypeError: Expected Tensor's shape: (3,), got (2,).
zeros, ones, and fill
The functionszeros
, ones
, and fill
create tensors whose elements all have the same value. For zeros and ones, the only required argument is shape, which identifies the shape of the desired tensor. As an example, the following code creates a simple 1-x-3 vector whose elements equal 0.0:
zero_tensor = tf.zeros([3])Similarly, the following function call creates a 4-x-4 matrix whose elements equal 1.0:
one_tensor = tf.ones([4, 4])The
fill
function requires a value parameter, which sets the value of the tensor's elements. The following code creates a three-dimensional tensor whose values are set to 81.0:
fill_tensor = tf.fill([1, 2, 3], 81.0)Unlike zeros and ones, fill doesn't have a
dtype
argument. It can only create tensors containing 32-bit floating point values.
Creating sequences
Thelinspace
and range
functions create tensors whose elements change regularly between a start and end value. The difference between them is that linspace
creates a tensor with a specific number of values. For example, the following code creates a 1-x-5 tensor whose elements range from 5.0 to 9.0:
lin_tensor = tf.linspace(5., 9., 5)Unlike<strong># Result: [5. 6. 7. 8. 9.]</strong>
linspace
, range
doesn't accept the number of elements in the tensor. Instead, it computes successive elements by adding a value called a delta
. In the following code, delta
is set to 0.5
:
range_tensor = tf.range(3., 7., delta=0.5)Like Python's<strong># Result: [3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5]</strong>
range
function, TensorFlow’s range
function can be called without the start parameter. In this case, the starting value is assumed to be 0.0
. The following code demonstrates this:
range_tensor = tf.range(1.5, delta=0.3)If the<strong># Result: [0.0 0.3 0.6 0.9 1.2]</strong>
delta
parameter is positive, the starting value must be less than the ending value. If delta is negative, the starting value must be greater than the ending value.