Data types

In addition to rank and shape, tensors have a data type. The following is a list of the data types:

Data type Python type Description
DT_FLOAT tf.float32 32-bit floating point.
DT_DOUBLE tf.float64 64-bit floating point.
DT_INT8 tf.int8 8-bit signed integer.
DT_INT16 tf.int16 16-bit signed integer.
DT_INT32 tf.int32 32-bit signed integer.
DT_INT64 tf.int64 64-bit signed integer.
DT_UINT8 tf.uint8 8-bit unsigned integer.
DT_STRING tf.string Variable length byte arrays. Each element of a tensor is a byte array.
DT_BOOL tf.bool Boolean.
DT_COMPLEX64 tf.complex64 Complex number made of two 32 bits floating points, real and imaginary parts.
DT_COMPLEX128 tf.complex128 Complex number made up of two 64 bits floating points, real and imaginary parts.
DT_QINT8 tf.qint8 8-bit signed integer used in quantized Ops.
DT_QINT32 tf.qint32 32-bit signed integer used in quantized ops.
DT_QUINT8 tf.quint8 8-bit unsigned integer used in quantized ops.

We believe the preceding table is self-explanatory hence we did not provide details on the data types. Note that the TensorFlow APIs are implemented to manage data to and from NumPy arrays.

To build a tensor with a constant value, pass a NumPy array to the tf.constant() operator, and the result will be a TensorFlow tensor with that value:

import tensorflow as tf 
import numpy as np

tensor_1d = np.array([1,2,3,4,5,6,7,8,9,10])
tensor_1d = tf.constant(tensor_1d)
with tf.Session() as sess:
print (tensor_1d.get_shape())
print sess.run(tensor_1d)

Running the example, we obtain the following output:

>>> 
(10,)
[ 1 2 3 4 5 6 7 8 9 10]

To build a tensor, with variable values, use a NumPy array and pass it to the tf.Variable constructor, the result will be a TensorFlow variable tensor with that initial value:

import tensorflow as tf 
import numpy as np

tensor_2d = np.array([(1,2,3),(4,5,6),(7,8,9)])
tensor_2d = tf.Variable(tensor_2d)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (tensor_2d.get_shape())
print sess.run(tensor_2d)

The result is as follows:

>>> 
(3, 3)
[[1 2 3]
[4 5 6]
[7 8 9]]

For ease of use in interactive Python environments, we can use the InteractiveSession class, (https://www.tensorflow.org/versions/r0.10/api_docs/python/client/#InteractiveSession) and then use that session for all Tensor.eval() and Operation.run() calls:

import tensorflow as tf 
import numpy as np

interactive_session = tf.InteractiveSession()
tensor = np.array([1,2,3,4,5])
tensor = tf.constant(tensor)
print(tensor.eval())
interactive_session.close()

The result is as follows:

>>> 
[1 2 3 4 5]

This can be easier in an interactive setting, such as the shell or an IPython Notebook, when it's tedious to pass around a Session object everywhere.

Another way to define a tensor uses the TensorFlow statement tf.convert_to_tensor:

import tensorflow as tf 
import numpy as np

tensor_3d = np.array([[[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8]],
[[ 9, 10, 11],[12, 13, 14],[15, 16, 17]],
[[18, 19, 20],[21, 22, 23],[24, 25, 26]]])

tensor_3d = tf.convert_to_tensor(tensor_3d, dtype=tf.float64)
with tf.Session() as sess:
print (tensor_3d.get_shape())
print sess.run(tensor_3d)

The result is as follows:

>>> 
(3, 3, 3)
[[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]]

[[ 9. 10. 11.]
[ 12. 13. 14.]
[ 15. 16. 17.]]

[[ 18. 19. 20.]
[ 21. 22. 23.]
[ 24. 25. 26.]]]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset