Created
September 9, 2018 22:21
-
-
Save xilenteyex/f652be2306573020eec9152a87915324 to your computer and use it in GitHub Desktop.
simple script to log timeline for a mint example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2015 The TensorFlow Authors. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the 'License'); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an 'AS IS' BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# ============================================================================== | |
"""A simple MNIST classifier which displays summaries in TensorBoard. | |
This is an unimpressive MNIST model, but it is a good example of using | |
tf.name_scope to make a graph legible in the TensorBoard graph explorer, and of | |
naming summary tags so that they are grouped meaningfully in TensorBoard. | |
It demonstrates the functionality of every TensorBoard dashboard. | |
""" | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import argparse | |
import os | |
import sys | |
import tensorflow as tf | |
from tensorflow.examples.tutorials.mnist import input_data | |
from tensorflow.python.client import timeline | |
from protobuf_to_dict import protobuf_to_dict | |
from google.protobuf.json_format import MessageToJson | |
import json | |
FLAGS = None | |
tensors_shape_arr = [] | |
def test_device_placer(op): | |
if 'layer1' in op.name: | |
return '/gpu:0' | |
elif 'layer2' in op.name: | |
return '/gpu:1' | |
return '/cpu:0' | |
# return op.device | |
def train(): | |
# Import data | |
mnist = input_data.read_data_sets(FLAGS.data_dir, | |
fake_data=FLAGS.fake_data) | |
config_proto = tf.ConfigProto(allow_soft_placement=True, graph_options=tf.GraphOptions(build_cost_model=5)) | |
# config_proto = tf.ConfigProto( | |
# log_device_placement=log_device_placement, | |
# #allow_soft_placement=allow_soft_placement) | |
# allow_soft_placement=allow_soft_placement, graph_options=tf.GraphOptions(build_cost_model=5)) | |
sess = tf.InteractiveSession(config=config_proto) | |
# Create a multilayer model. | |
# Input placeholders | |
with tf.name_scope('input'): | |
x = tf.placeholder(tf.float32, [None, 784], name='x-input') | |
y_ = tf.placeholder(tf.int64, [None], name='y-input') | |
with tf.name_scope('input_reshape'): | |
image_shaped_input = tf.reshape(x, [-1, 28, 28, 1]) | |
# We can't initialize these variables to 0 - the network will get stuck. | |
def weight_variable(shape): | |
"""Create a weight variable with appropriate initialization.""" | |
initial = tf.truncated_normal(shape, stddev=0.1) | |
return tf.Variable(initial) | |
def bias_variable(shape): | |
"""Create a bias variable with appropriate initialization.""" | |
initial = tf.constant(0.1, shape=shape) | |
return tf.Variable(initial) | |
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu): | |
"""Reusable code for making a simple neural net layer. | |
It does a matrix multiply, bias add, and then uses ReLU to nonlinearize. | |
It also sets up name scoping so that the resultant graph is easy to read, | |
and adds a number of summary ops. | |
""" | |
# Adding a name scope ensures logical grouping of the layers in the graph. | |
with tf.name_scope(layer_name): | |
# This Variable will hold the state of the weights for the layer | |
with tf.name_scope('weights'): | |
weights = weight_variable([input_dim, output_dim]) | |
with tf.name_scope('biases'): | |
biases = bias_variable([output_dim]) | |
with tf.name_scope('Wx_plus_b'): | |
preactivate = tf.matmul(input_tensor, weights) + biases | |
activations = act(preactivate, name='activation') | |
return activations | |
hidden1 = nn_layer(x, 784, 500, 'layer1') | |
with tf.name_scope('dropout'): | |
keep_prob = tf.placeholder(tf.float32) | |
dropped = tf.nn.dropout(hidden1, keep_prob) | |
# Do not apply softmax activation yet, see below. | |
y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity) | |
with tf.name_scope('cross_entropy'): | |
# The raw formulation of cross-entropy, | |
# | |
# tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.softmax(y)), | |
# reduction_indices=[1])) | |
# | |
# can be numerically unstable. | |
# | |
# So here we use tf.losses.sparse_softmax_cross_entropy on the | |
# raw logit outputs of the nn_layer above, and then average across | |
# the batch. | |
with tf.name_scope('total'): | |
cross_entropy = tf.losses.sparse_softmax_cross_entropy( | |
labels=y_, logits=y) | |
with tf.name_scope('train'): | |
train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize( | |
cross_entropy) | |
with tf.name_scope('accuracy'): | |
with tf.name_scope('correct_prediction'): | |
correct_prediction = tf.equal(tf.argmax(y, 1), y_) | |
with tf.name_scope('accuracy'): | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
tf.global_variables_initializer().run() | |
def feed_dict(train): | |
"""Make a TensorFlow feed_dict: maps data onto Tensor placeholders.""" | |
if train or FLAGS.fake_data: | |
xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data) | |
k = FLAGS.dropout | |
else: | |
xs, ys = mnist.test.images, mnist.test.labels | |
k = 1.0 | |
return {x: xs, y_: ys, keep_prob: k} | |
for i in range(FLAGS.max_steps): | |
if i % 10 == 0: | |
acc = sess.run([accuracy], feed_dict=feed_dict(False)) | |
print('Accuracy at step %s: %s' % (i, acc)) | |
else: | |
if i % 100 == 99: # Record execution stats | |
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) | |
run_metadata = tf.RunMetadata() | |
sess.run([train_step], feed_dict=feed_dict(True), options=run_options, run_metadata=run_metadata) | |
print('Adding run metadata for', i) | |
jsonObj = MessageToJson(run_metadata.step_stats) | |
with open('step_stats_%d.json' % (i), 'w') as outfile: | |
json.dump(jsonObj, outfile) | |
trace = timeline.Timeline(step_stats=run_metadata.step_stats) | |
trace_file = open('timeline_%d.json' % (i), 'w') | |
trace_file.write(trace.generate_chrome_trace_format()) | |
trace_file.close() | |
else: | |
sess.run([train_step], feed_dict=feed_dict(True)) | |
def main(_): | |
if tf.gfile.Exists(FLAGS.log_dir): | |
tf.gfile.DeleteRecursively(FLAGS.log_dir) | |
tf.gfile.MakeDirs(FLAGS.log_dir) | |
#with tf.device(test_device_placer): | |
train() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--fake_data', nargs='?', const=True, type=bool, | |
default=False, | |
help='If true, uses fake data for unit testing.') | |
parser.add_argument('--max_steps', type=int, default=100, | |
help='Number of steps to run trainer.') | |
parser.add_argument('--learning_rate', type=float, default=0.001, | |
help='Initial learning rate') | |
parser.add_argument('--dropout', type=float, default=0.9, | |
help='Keep probability for training dropout.') | |
parser.add_argument( | |
'--data_dir', | |
type=str, | |
default=os.path.join(os.getenv('TEST_TMPDIR', '/tmp'), | |
'tensorflow/mnist/input_data'), | |
help='Directory for storing input data') | |
parser.add_argument( | |
'--log_dir', | |
type=str, | |
default=os.path.join(os.getenv('TEST_TMPDIR', '/tmp'), | |
'tensorflow/mnist/logs/mnist_with_summaries'), | |
help='Summaries log directory') | |
FLAGS, unparsed = parser.parse_known_args() | |
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment