In TensorFlow, data isn’t stored as integers, floats, or strings. These values are encapsulated in an object called a tensor. Tensors come in a variety of sizes as shown below:
# A is a 0-dimensional int32 tensor A = tf.constant(1234) # B is a 1-dimensional int32 tensor B = tf.constant([123,456,789]) # C is a 2-dimensional int32 tensor C = tf.constant([ [123,456,789], [222,333,444] ])
TensorFlow’s API is built around the idea of a computational graph, a way of visualizing a mathematical process. A “TensorFlow Session” is an environment for running a graph. The session is in charge of allocating the operations to GPU(s) and/or CPU(s), including remote machines.
import tensorflow as tf # Create TensorFlow object called hello_constant hello_constant = tf.constant('Hello World!') with tf.Session() as sess: # Run the tf.constant operation in the session output = sess.run(hello_constant) print(output)
The code first creates the tensor,
hello_constant, from the previous lines. The next step is to evaluate the tensor in a session.