DePaul University, Chicago
March 9, 2017
http://playground.tensorflow.org
> import tensorflow as tf
>
> one = tf.constant(1)
> two = tf.constant(2)
> three = one + two
>
> sess = tf.Session()
> sess.run(three)
3
> sess.graph == tf.get_default_graph()
True
> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
<tf.Operation 'Const_1' type=Const>,
<tf.Operation 'add' type=Add>]
> tf.reset_default_graph()
>
> one = tf.constant(1)
> x = tf.placeholder(tf.int32)
> y = x + one
>
> sess = tf.Session()
> sess.run(y, {x:2})
3
> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
<tf.Operation 'Placeholder' type=Placeholder>,
<tf.Operation 'add' type=Add>]
> tf.reset_default_graph()
>
> one = tf.constant(1)
> x = tf.Variable(2)
> y = x + one
>
> sess = tf.Session()
> sess.run(tf.global_variables_initializer())
> sess.run(x)
2
> sess.run(y)
3
> sess.graph.get_operations()
[<tf.Operation 'Const' type=Const>,
<tf.Operation 'Variable/initial_value' type=Const>,
<tf.Operation 'Variable' type=VariableV2>,
<tf.Operation 'Variable/Assign' type=Assign>,
<tf.Operation 'Variable/read' type=Identity>,
<tf.Operation 'add' type=Add>,
<tf.Operation 'init' type=NoOp>]
> tf.reset_default_graph()
>
> x = tf.Variable([[2,1]])
> y = tf.Variable([[1],[2]])
> z = tf.matmul(x, y)
>
> sess = tf.Session()
> sess.run(tf.global_variables_initializer())
> sess.run(z)
array([[4]], dtype=int32)
def train():
for step in range(training_steps):
batch = data.next_batch()
sess.run(train_op, batch)
maybe_log_status(step, batch)
maybe_save_model()
if __name__ == "__main__":
init_data()
init_train()
train()
def init_train():
init_model()
init_train_op()
init_accuracy_op()
init_summaries()
init_session()
def init_model():
global x, y
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
def init_train_op():
global y_, loss, train_op
y_ = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(
-tf.reduce_sum(
y_ * tf.log(y),
reduction_indices=[1]))
train_op = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
def init_accuracy_op():
global accuracy
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
def init_summaries():
global summaries, writer
tf.summary.scalar("loss", loss)
tf.summary.scalar("accuracy", accuracy)
summaries = tf.summary.merge_all()
writer = tf.summary.FileWriter(FLAGS.rundir + "/train")
def init_session():
global sess
sess = tf.Session()
sess.run(tf.global_variables_initializer())
def log_status(step, batch):
accuracy_val, summary = sess.run([accuracy, summaries], batch)
writer.add_summary(summary, step)
print "Step %i: accuracy=%f" % accuracy_val
def save_model():
saver = tf.train.Saver()
saver.save(sess, FLAGS.rundir + "/model/export")
def train():
for step in range(training_steps):
batch = data.next_batch()
sess.run(train_op, batch)
maybe_log_status(step, batch)
maybe_save_model()
if __name__ == "__main__":
init_data()
init_train()
train()
- Get code
- Prepare model for training
- Train model
- Compare results
- Serve model
- TF is a low level library for machine learners
- Frameworks growing in popularity and support (High level API/TFLearn, Keras, TFSlim)
- Graphs are central (define model & ops, run, export, serve)
- Good conventions (patterns) essential for success