{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# tfgraphviz\n",
"\n",
"Examples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import tfgraphviz as tfg"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"calc_g = tf.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with calc_g.as_default():\n",
" a = tf.constant(1, name=\"a\")\n",
" b = tf.constant(2, name=\"b\")\n",
" c = tf.add(a, b, name=\"add\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualize a graph with tfg.board(...)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tfg.board(calc_g)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"reg_g = tf.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with reg_g.as_default():\n",
" import numpy as np\n",
" x_data = np.random.rand(100).astype(np.float32)\n",
" y_data = x_data * 0.1 + 0.3\n",
" W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\n",
" b = tf.Variable(tf.zeros([1]))\n",
" y = W * x_data + b\n",
" loss = tf.reduce_mean(tf.square(y - y_data))\n",
" optimizer = tf.train.GradientDescentOptimizer(0.5)\n",
" train = optimizer.minimize(loss)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tfg.board(reg_g)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tfg.board(reg_g, depth=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## MNIST For ML Beginners\n",
"\n",
"https://www.tensorflow.org/get_started/mnist/beginners"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mnist_g = tf.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with mnist_g.as_default():\n",
" x = tf.placeholder(tf.float32, [None, 784])\n",
" W = tf.Variable(tf.zeros([784, 10]))\n",
" b = tf.Variable(tf.zeros([10]))\n",
" y = tf.nn.softmax(tf.matmul(x, W) + b)\n",
" y_ = tf.placeholder(tf.float32, [None, 10])\n",
" cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))\n",
" train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tfg.board(mnist_g, depth=1)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "30px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}