#! coding:utf-8
"""
Tensor Flow
Getting Started With TensorFlow
https://www.tensorflow.org/get_started/get_started
"""
import tensorflow as tf
# Building the computational graph.
# =================================
node1 = tf.constant(3.0, 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)
# Running the computational graph
# ================================
sess = tf.Session();
print(sess.run([node1, node2]))
# > [3.0, 4.0]
node3 = tf.add(node1, node2, name="add_rother")
print("node3: ", node3)
print("sess.run(node3):", sess.run(node3))
# > node3: Tensor("Add:0", shape=(), dtype=float32)
# > sess.run(node3): 7.0
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # same tf.add(a,b)
print(sess.run(adder_node, {a:3, b:4}))
print(sess.run(adder_node, {a:[1,3], b:[2,5]}))
# > 7.0
# > [ 3. 8.]
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a:3, b:5}))
# > 24.0
# Linear Model
# ============
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
# > Error : print(sess.run(linear_model, {x:[1,2,3,4,5]}))
# > FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(linear_model, {x:[1,2,3,4,5]}))
# > [ 0. 0.30000001 0.60000002 0.90000004 1.20000005]
y = tf.placeholder(tf.float32)
loss = tf.reduce_sum(tf.square(linear_model - y))
print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
# > 23.66
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))
# > 0.0
# tf.train API
# ============
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init)
for i in range(5000):
sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
print(sess.run([W,b]))
# > [array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
#! coding:utf-8
"""
Tensor Flow
Getting Started With TensorFlow
https://www.tensorflow.org/get_started/get_started
"""
import tensorflow as tf
import numpy as np
# Building the computational graph.
# =================================
# Linear Model
# ============
# Model parameters
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32) # Input
linear_model = W * x + b
y = tf.placeholder(tf.float32) # Output
loss = tf.reduce_sum(tf.square(linear_model - y))
# tf.train API
# ============
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
summary_writer = tf.summary.FileWriter('linear_data', graph_def=sess.graph_def)
sess.run(init)
for i in range(5000):
sess.run(train, {x:x_train, y:y_train})
# evalute training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W:{}, b:{}, loss:{}".format(curr_W, curr_b, curr_loss))
# > W:[-0.99999911], b:[ 0.99999744], loss:4.206412995699793e-12