tensorflow - taoualiw/My-Knowledge-Base GitHub Wiki

TensorFlow

1. Definitions

TensorFlow:

  • is an open-sourse software library for machine learning
  • is a symbolic math library
  • is used as a system for building and training neural networks to detect and decipher patterns and correlations
  • is used for both research and production at Google often replacing its closed-source predecessor, DistBelief
  • was developed by the Google Brain team for internal use.
  • was released under the Apache 2.0 open source license on 9 November 2015. - provides a Python API as well as C++, Haskell, Java, Go and Rust APIs.
  • All data of TensorFlow is represented as tensors. It is the sole data structure.

In computer science, a tensor is a generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes. A tensor has its rank and shape, rank is its number of dimensions and shape is the size of each dimension.

  • scalar is tensor of rank 0
  • vector is tensor of rank 1
  • matrix is tensor of rank 2

In mathematics:

  • a tensor is a geometric object that maps in a multi-linear manner geometric vectors, scalars, and other tensors to a resulting tensor.
  • a collection of vectors and covectors combined together using the tensor

In physics:

  • an object that is invariant under a change of coordinates but ...has components (projections in coordinate system)that change in a special, predictable way under a change of coordinates.

2. Important Features

  • Computational Graph then Run Session

import tensorflow as tf
# Computational Graph:
c1 = tf.constant(0.034)
c2 = tf.constant(1000.0)
x = tf.multiply(c1, c1)
y = tf.multiply(c1, c2)
final_node = tf.add(x, y)
# Running the session:
with tf.Session() as sess:
    result = sess.run(final_node)
    print(result, type(result))

sess.close()
  • Similarity with Numpy

import tensorflow as tf
session = tf.Session()
x = tf.range(12) #x = np.arange(12)
print(session.run(x))# print(x)
x2 = tf.reshape(tensor=x, shape=(3, 4)) #x2 = x.reshape((3, 4))
x2 = tf.reduce_sum(x2, reduction_indices=[0])#x2 = x2.sum(axis=0)
res = session.run(x2)# res=x2
print(res)
x3 = tf.eye(5, 5)#x3 = np.eye(5, 5)
print(session.run(x3))#print(x3)
  • TensorBoard

TensorFlow provides functions to debug and optimize programs with the help of a visualization tool called TensorBoard.TensorFlow creates the necessary data during its execution.The data are stored in trace files. Tensorboard can be viewed from a browser using http://localhost:6006/.

  • Use filter tags to visualize measures together

We can run the following example program, and it will create the directory "output".

import tensorflow as tf
p = tf.constant(0.034)
c = tf.constant(1000.0)
x = tf.add(c, tf.multiply(p, c))
x = tf.add(x, tf.multiply(p, x))
with tf.Session() as sess:
    writer = tf.summary.FileWriter("output", sess.graph)
    print(sess.run(x))
    writer.close()

We can run now tensorboard: tensorboard --logdir output which will create a webserver: TensorBoard 0.1.8 at http://localhost:6006 (Press CTRL+C to quit)

  • Place Holders

  • A placeholder is used for feeding external data into a Tensorflow computation, i.e. from outside of the graph! If you are training a learning algorithm, a placeholder is used for feeding in your training data.
  • The placeholder behaves similar to the Python "input" statement.
  • There is no need to provide an initial value for a placeholder.
  • The value of a placeholder can be specified at run time with the feed_dict argument inside Session.run
import tensorflow as tf
c1 = tf.placeholder(tf.float32)
c2 = tf.placeholder(tf.float32)
x = tf.multiply(c1, c1)
y = tf.multiply(c1, c2)
final_node = tf.add(x, y)
with tf.Session() as sess:
    result = final_node.eval( {c1: 3.8, c2: 47.11})
    print(result)
    result = final_node.eval( {c1: [3, 5], c2: [1, 3]})
    print(result)
  • Variables

  • Variables are used to add trainable parameters to a graph. They are constructed with a type and initial value. Variables are not initialized when you call tf.Variable.
  • A TensorFlow variable behaves more or less like a Python variable!
  • The difference between tf.Variable and tf.placeholder consists in the time when the values are passed. If you use tf.Variable, you have to provide an initial value when you declare it.
  • To initialize the variables of a TensorFlow graph, we have to call global_variables_initializer.
    import tensorflow as tf
    W = tf.Variable([.5], dtype=tf.float32)
    b = tf.Variable([-1], dtype=tf.float32)
    x = tf.placeholder(tf.float32)
    model = W * x + b
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        print(sess.run(model, {x: [1, 2, 3, 4]}))
  • Reassigning values to variables:
    import tensorflow as tf
    W = tf.Variable([.5], dtype=tf.float32)
    b = tf.Variable([-1], dtype=tf.float32)
    x = tf.placeholder(tf.float32)
    y = tf.placeholder(tf.float32)
    model = W * x + b
    deltas = tf.square(model - y)
    loss = tf.reduce_sum(deltas)
    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
    
        print(sess.run(loss, {x: [1, 2, 3, 4], y: [1, 1, 1, 1]}))
        W_a = tf.assign(W, [0.])
        b_a =  tf.assign(b, [1.])
        sess.run( W_a )
        sess.run( b_a)
        # sess.run( [W_a, b_a] ) # alternatively in one 'run'
    
        print(sess.run(loss, {x: [1, 2, 3, 4], y: [1, 1, 1, 1]}))
  • tf.keras

tf.keras is is TensorFlow's implementation of the Keras API specification. This is a high-level API to build and train deep learning models.

  • Epochs, batch size, steps

Batch size is the number of samples you put into for each training round. An epoch usually means one iteration over all of the training data. So for each epoch, you can split your training sets into multiple batches. For instance if you have 20,000 images and a batch size of 100 then the epoch should contain 20,000 / 100 = 200 steps and the total number of steps 200*2012LORR0105

  • conda install nomkl

References

⚠️ **GitHub.com Fallback** ⚠️