# TensorFlow Data Types

TensorFlow has its own data types. We’ll discuss data types in tensorflow and how to use variables.

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.

**Related Course:**

Deep Learning for Computer Vision with Tensor Flow and Keras

### 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 seamlessly 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 easy

b = 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.

© 2007 - 2019 pythonprogramminglanguage.com.

Cookie Policy & Privacy Policy.