TensorFlow has its own types on top of the Python ones. They look similar but they carry extra information, like where the value sits on the GPU and how it can be computed.
For most code you will use three of them.
For most code you will use three of them.
TensorFlow accepts Python native types like booleans, strings and numeric (int, float). But you should use the tensorflow data types instead.
Why? Because TensorFlow has to infer with Python type.
Data types
There are many data types available, both 32 bit, 64 bit numbers and others. Variables must be initialized (more on that later in the article).
The Tensorflow data types include:
| type | tensorflow |
|---|---|
| floating point: | tf.float32, tf.float64 |
| integers: | tf.int8, tf.int16, tf.int32, tf.int64 |
| unsigned integers: | tf.uint8, tf.unit16 |
| strings: | tf.string |
| booleans: | tf.bool |
| complex numbers: | tf.complex64, tf.complex128 |
| integer with quantuized ops: | tf.qint8, tf.qint32, tf.quint8 |
TensorFlow data types intergrate with numpy:tf.int64 == np.int64 # True
Tensors
Tensors are a big part of tensorflow. You can create different types of tensors: 0-d tensor (scalar), 1-d tensor (vector) or 2-d tensor (matrix)*.
Optionally you can also assign a name to your variables. That looks nice in tensorboard but isn’t required.
To create a 0-d tensor:a = tf.Variable(1, name="scalar")
Making a 1-d tensor is just as easyb = tf.Variable([1,2,3], name="vector")
To make a 2x2 tensor (matrix)t_2 = tf.Variable([[0,1,0],[1,1,0],[1,0,1]], name="matrix")
Must initialize variables
Variables must always be initialized. If you don’t you’ll run into an error like this:tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable_1
Luckily initializing variables is a piece of cake.
A simple example that adds two scalars below:
import tensorflow as tf |
You may also initialize variables globally:init = tf.global_variables_initializer()
If you have multiples sessions, each session has a copy of the variables.
The types you will use
- A tensor is any number or array of numbers. Scalars are tensors with no shape.
- A constant,
tf.constant(3), is a value you set once and never change. - A variable,
tf.Variable(3), is a value the training updates. - A placeholder,
tf.compat.v1.placeholder, is a value you feed in later. In TensorFlow 2 you pass data as a function argument instead. tf.float32is the normal type for the maths,tf.int32for whole numbers andtf.boolfor True and False.
Shapes matter. tf.reshape(x, [-1, 1]) turns a flat list into a column, and
the -1 means "figure this one out yourself".
Cast with tf.cast(x, tf.float32) when numpy gives you float64 and
TensorFlow wants float32. Mixing types in one operation gives an error.
Reading helps, writing fixes it. PyChallenge has exercises on this and you can try them right now.
