Anda di halaman 1dari 3

12/8/2017 Data types - Deep Learning with TensorFlow

 Deep Learning with TensorFlow

PREV NEXT
⏮ ⏭
Shape Variables
   🔎

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

32-bit
DT_FLOAT tf.float32 floating
point.

64-bit
DT_DOUBLE tf.float64 floating
point.

8-bit
DT_INT8 tf.int8 signed
integer.

16-bit
DT_INT16 tf.int16 signed
integer.

32-bit
DT_INT32 tf.int32 signed
integer.

64-bit
DT_INT64 tf.int64 signed
integer.

8-bit
DT_UINT8 tf.uint8 unsigned
integer.

Variable
length
byte
arrays.
Each
DT_STRING tf.string
element
of a
tensor is
a byte
array.

DT_BOOL tf.bool Boolean.

Complex
number
made of
two 32
bits
DT_COMPLEX64 tf.complex64
floating
points,

real and
imaginary
parts.

https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/6a906be0-fd75-4416-bc13-f10aeaa6ee4b.xhtml 1/3
12/8/2017 Data types - Deep Learning with TensorFlow

Data type Python type Description

Complex
number
made up
of two 64
bits
DT_COMPLEX128 tf.complex128
floating
points,
real and
imaginary
parts.

8-bit
signed
integer
DT_QINT8 tf.qint8
used in
quantized
Ops.

32-bit
signed
integer
DT_QINT32 tf.qint32
used in
quantized
ops.

8-bit
unsigned
integer
DT_QUINT8 tf.quint8
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/ver
sions/r0.10/api_docs/python/client/#Interactiv
eSession) 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:

https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/6a906be0-fd75-4416-bc13-f10aeaa6ee4b.xhtml 2/3
12/8/2017 Data types - Deep Learning with TensorFlow

>>>
[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.]]]

Recommended / Queue / History / Topics / Tutorials / Settings / Get the App / Sign Out
© 2017 Safari. Terms of Service / Privacy Policy
PREV NEXT
⏮ ⏭
Shape Variables

https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/6a906be0-fd75-4416-bc13-f10aeaa6ee4b.xhtml 3/3

Anda mungkin juga menyukai