Commit 652c65f0 authored by Oiwi Parker Jones's avatar Oiwi Parker Jones
Browse files

tensorboard example

parent 1b1114e0
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting /tmp/TensorBoard_demo/data/train-images-idx3-ubyte.gz\n",
"Extracting /tmp/TensorBoard_demo/data/train-labels-idx1-ubyte.gz\n",
"Extracting /tmp/TensorBoard_demo/data/t10k-images-idx3-ubyte.gz\n",
"Extracting /tmp/TensorBoard_demo/data/t10k-labels-idx1-ubyte.gz\n"
]
}
],
"source": [
"import os\n",
"import os.path\n",
"import shutil\n",
"import tensorflow as tf\n",
"\n",
"LOGDIR = \"/tmp/TensorBoard_demo/\"\n",
"LABELS = os.path.join(os.getcwd(), \"labels_1024.tsv\")\n",
"SPRITES = os.path.join(os.getcwd(), \"sprite_1024.png\")\n",
"### MNIST EMBEDDINGS ###\n",
"mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + \"data\", one_hot=True)\n",
"### Get a sprite and labels file for the embedding projector ###\n",
"\n",
"if not (os.path.isfile(LABELS) and os.path.isfile(SPRITES)):\n",
" print(\"Necessary data files were not found: LABELS and SPRITES\")\n",
" exit(1)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def conv_layer(input, size_in, size_out, name=\"conv\"):\n",
" with tf.name_scope(name):\n",
" w = tf.Variable(tf.truncated_normal([5, 5, size_in, size_out], stddev=0.1), name=\"W\")\n",
" b = tf.Variable(tf.constant(0.1, shape=[size_out]), name=\"B\")\n",
" conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding=\"SAME\")\n",
" act = tf.nn.relu(conv + b)\n",
" tf.summary.histogram(\"weights\", w)\n",
" tf.summary.histogram(\"biases\", b)\n",
" tf.summary.histogram(\"activations\", act)\n",
" return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=\"SAME\")\n",
"\n",
"\n",
"def fc_layer(input, size_in, size_out, name=\"fc\"):\n",
" with tf.name_scope(name):\n",
" w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name=\"W\")\n",
" b = tf.Variable(tf.constant(0.1, shape=[size_out]), name=\"B\")\n",
" act = tf.matmul(input, w) + b\n",
" tf.summary.histogram(\"weights\", w)\n",
" tf.summary.histogram(\"biases\", b)\n",
" tf.summary.histogram(\"activations\", act)\n",
" return act"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def mnist_model(learning_rate):\n",
" tf.reset_default_graph()\n",
" sess = tf.Session()\n",
"\n",
" # Setup placeholders, and reshape the data\n",
" x = tf.placeholder(tf.float32, shape=[None, 784], name=\"x\")\n",
" x_image = tf.reshape(x, [-1, 28, 28, 1])\n",
" tf.summary.image('input', x_image, 3)\n",
" y = tf.placeholder(tf.float32, shape=[None, 10], name=\"labels\")\n",
"\n",
" conv_out = conv_layer(x_image, 1, 16, \"conv\")\n",
"\n",
" flattened = tf.reshape(conv_out, [-1, 7 * 7 * 64])\n",
"\n",
" embedding_input = flattened\n",
" embedding_size = 7*7*64\n",
" logits = fc_layer(flattened, 7*7*64, 10, \"fc\")\n",
"\n",
" with tf.name_scope(\"xent\"):\n",
" xent = tf.reduce_mean(\n",
" tf.nn.softmax_cross_entropy_with_logits(\n",
" logits=logits, labels=y), name=\"xent\")\n",
" tf.summary.scalar(\"xent\", xent)\n",
"\n",
" with tf.name_scope(\"train\"):\n",
" train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)\n",
"\n",
" with tf.name_scope(\"accuracy\"):\n",
" correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))\n",
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
" tf.summary.scalar(\"accuracy\", accuracy)\n",
"\n",
" summ = tf.summary.merge_all()\n",
"\n",
"\n",
" embedding = tf.Variable(tf.zeros([1024, embedding_size]), name=\"test_embedding\")\n",
" assignment = embedding.assign(embedding_input)\n",
" saver = tf.train.Saver()\n",
"\n",
" sess.run(tf.global_variables_initializer())\n",
" writer = tf.summary.FileWriter(LOGDIR)\n",
" writer.add_graph(sess.graph)\n",
"\n",
" config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()\n",
" embedding_config = config.embeddings.add()\n",
" embedding_config.tensor_name = embedding.name\n",
" embedding_config.sprite.image_path = SPRITES\n",
" embedding_config.metadata_path = LABELS\n",
" # Specify the width and height of a single thumbnail.\n",
" embedding_config.sprite.single_image_dim.extend([28, 28])\n",
" tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config)\n",
"\n",
" for i in range(1,2001):\n",
" batch = mnist.train.next_batch(100)\n",
" if i % 5 == 0:\n",
" [train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})\n",
" writer.add_summary(s, i)\n",
" if i % 500 == 0:\n",
" sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]})\n",
" saver.save(sess, os.path.join(LOGDIR, \"model.ckpt\"), i)\n",
" sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done training!\n",
"Run `tensorboard --logdir=/tmp/TensorBoard_demo/` to see the results.\n",
"Running on mac? If you want to get rid of the dialogue asking to give network permissions to TensorBoard, you can provide this flag: --host=localhost\n"
]
}
],
"source": [
"learning_rate = 1E-3\n",
"\n",
"mnist_model(learning_rate)\n",
"print('Done training!')\n",
"print('Run `tensorboard --logdir=%s` to see the results.' % LOGDIR)\n",
"print('Running on mac? If you want to get rid of the dialogue asking to give '\n",
" 'network permissions to TensorBoard, you can provide this flag: '\n",
" '--host=localhost')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting TensorBoard b'47' at http://0.0.0.0:6006\n",
"(Press CTRL+C to quit)\n",
"WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.\n",
"WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.\n",
"WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.\n",
"WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404\n",
"WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404\n",
"WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404\n",
"WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404\n",
"^C\n"
]
}
],
"source": [
"!tensorboard --logdir=/tmp/TensorBoard_demo/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:tensorflow]",
"language": "python",
"name": "conda-env-tensorflow-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:code id: tags:
``` python
import os
import os.path
import shutil
import tensorflow as tf
LOGDIR = "/tmp/TensorBoard_demo/"
LABELS = os.path.join(os.getcwd(), "labels_1024.tsv")
SPRITES = os.path.join(os.getcwd(), "sprite_1024.png")
### MNIST EMBEDDINGS ###
mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + "data", one_hot=True)
### Get a sprite and labels file for the embedding projector ###
if not (os.path.isfile(LABELS) and os.path.isfile(SPRITES)):
print("Necessary data files were not found: LABELS and SPRITES")
exit(1)
```
%%%% Output: stream
Extracting /tmp/TensorBoard_demo/data/train-images-idx3-ubyte.gz
Extracting /tmp/TensorBoard_demo/data/train-labels-idx1-ubyte.gz
Extracting /tmp/TensorBoard_demo/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/TensorBoard_demo/data/t10k-labels-idx1-ubyte.gz
%% Cell type:code id: tags:
``` python
def conv_layer(input, size_in, size_out, name="conv"):
with tf.name_scope(name):
w = tf.Variable(tf.truncated_normal([5, 5, size_in, size_out], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME")
act = tf.nn.relu(conv + b)
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return tf.nn.max_pool(act, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
def fc_layer(input, size_in, size_out, name="fc"):
with tf.name_scope(name):
w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")
b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
act = tf.matmul(input, w) + b
tf.summary.histogram("weights", w)
tf.summary.histogram("biases", b)
tf.summary.histogram("activations", act)
return act
```
%% Cell type:code id: tags:
``` python
def mnist_model(learning_rate):
tf.reset_default_graph()
sess = tf.Session()
# Setup placeholders, and reshape the data
x = tf.placeholder(tf.float32, shape=[None, 784], name="x")
x_image = tf.reshape(x, [-1, 28, 28, 1])
tf.summary.image('input', x_image, 3)
y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")
conv_out = conv_layer(x_image, 1, 16, "conv")
flattened = tf.reshape(conv_out, [-1, 7 * 7 * 64])
embedding_input = flattened
embedding_size = 7*7*64
logits = fc_layer(flattened, 7*7*64, 10, "fc")
with tf.name_scope("xent"):
xent = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=y), name="xent")
tf.summary.scalar("xent", xent)
with tf.name_scope("train"):
train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)
with tf.name_scope("accuracy"):
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar("accuracy", accuracy)
summ = tf.summary.merge_all()
embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding")
assignment = embedding.assign(embedding_input)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter(LOGDIR)
writer.add_graph(sess.graph)
config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()
embedding_config = config.embeddings.add()
embedding_config.tensor_name = embedding.name
embedding_config.sprite.image_path = SPRITES
embedding_config.metadata_path = LABELS
# Specify the width and height of a single thumbnail.
embedding_config.sprite.single_image_dim.extend([28, 28])
tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config)
for i in range(1,2001):
batch = mnist.train.next_batch(100)
if i % 5 == 0:
[train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})
writer.add_summary(s, i)
if i % 500 == 0:
sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]})
saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i)
sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})
```
%% Cell type:code id: tags:
``` python
learning_rate = 1E-3
mnist_model(learning_rate)
print('Done training!')
print('Run `tensorboard --logdir=%s` to see the results.' % LOGDIR)
print('Running on mac? If you want to get rid of the dialogue asking to give '
'network permissions to TensorBoard, you can provide this flag: '
'--host=localhost')
```
%%%% Output: stream
Done training!
Run `tensorboard --logdir=/tmp/TensorBoard_demo/` to see the results.
Running on mac? If you want to get rid of the dialogue asking to give network permissions to TensorBoard, you can provide this flag: --host=localhost
%% Cell type:code id: tags:
``` python
!tensorboard --logdir=/tmp/TensorBoard_demo/
```
%%%% Output: stream
Starting TensorBoard b'47' at http://0.0.0.0:6006
(Press CTRL+C to quit)
WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.
WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.
WARNING:tensorflow:Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events. Overwriting the graph with the newest event.
WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404
WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404
WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404
WARNING:tensorflow:path ../external/data/plugin/text/runs not found, sending 404
^C
%% Cell type:code id: tags:
``` python
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment