Tensorflow

Tensorflow is an open source framework for machine learning specially for neural networks.

pip3 install tensorflow # CPU
pip3 install tensorflow-gpu # GPU


In [1]:
import tensorflow as tf

Computational Graph


In [2]:
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)


Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

In [3]:
sess = tf.Session()
print(sess.run([node1, node2]))


[3.0, 4.0]

In [4]:
node3 = tf.add(node1, node2)
print("node3:", node3)
print("sess.run(node3):", sess.run(node3))


node3: Tensor("Add:0", shape=(), dtype=float32)
sess.run(node3): 7.0

Placeholder


In [5]:
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)

In [6]:
print(sess.run(adder_node, {a: 3, b: 4.5}))
print(sess.run(adder_node, {a: [1, 3], b: [2, 4]}))


7.5
[ 3.  7.]


In [7]:
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b: 4.5}))


22.5

Variable


In [18]:
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)

A Simple Linear Model


In [19]:
linear_model = W * x + b

In [20]:
# do this explicitly to initialize variables
init = tf.global_variables_initializer()
sess.run(init)

In [21]:
print(sess.run(linear_model, {x: [1, 2, 3, 4]}))


[ 0.          0.30000001  0.60000002  0.90000004]

In [22]:
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)  # the loss (cost) function
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))


23.66

Optimizer


In [23]:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

In [28]:
sess.run(init) # reset values to incorrect defaults.
for i in range(1000):
    sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})

cur_W, cur_b = sess.run([W, b])
print('W = %s\nb = %s' % (cur_W[0], cur_b[0]))


W = -0.999997
b = 0.999991