[TensorFlow] Basic Usage
Install TensorFlow on mac
Install pip
# Mac OS X $ sudo easy_install pip $ sudo easy_install --upgrade six
Install tensorflow
# Mac OS X, CPU only, Python 2.7: $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.1-py2-none-any.whl
Basic Usage
The concepts in TensorFlow
Tensor : a typed multi-dimensional array. Only Tensor are passed between operations in the computation graph.
Graph : a description of computation.
op : (short for operation) the node in the graph
Session : the context in which to execute graphs
Devices : such as CPUs or GPUs, on which Session execute graphs
Variable : maintain state
numpy ndarray : the result structure returned by Sesson.run(graph), which is produced by ops in graph and transformed from tensor
Simple example
import tensorflow as tf # Start with default graph. matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1, matrix2) # Now the default graph has three nodes: two constant() ops and one matmul() op. # Launch the default graph in a Session sess = tf.Session()
result = sess.run(product) print(result) sess.close()
The call 'run(product)' causes the execution of three ops in the graph.
Fetches : to fech the outputs of operations, execute the graph with a run() call on the Session object. E.g. sess.run(product) in above sample
Feeds : a mechanism for patching tensors directly into operations in graph. Supply feed data as argument to a run() call. tf.placeholder is used to create "feed" operations commonly
input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.mul(input1, input2) with tf.Session() as sess: print(sess.run([output], feed_dict={input1:[7.], input2:[2.]})) # output: # [array([ 14.], dtype=float32)]
References
Download and Setup, TensorFlow