Skip to content

Instantly share code, notes, and snippets.

@welschma
Created October 21, 2016 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welschma/a5d06c384402e0e24e1b358660fac421 to your computer and use it in GitHub Desktop.
Save welschma/a5d06c384402e0e24e1b358660fac421 to your computer and use it in GitHub Desktop.
A jupyter notebook explaining the basic usage of Tensorflow
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function\n",
"\n",
"import tensorflow as tf\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tensorflow Basics\n",
"This tutorial should give a brief overview over the basic functionality of Tensorflow (TF).\n",
"In TF computations are represented as graphs. A graph consists of nodes, which represent computations with Tensors. The graph is evaluated in a Session."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Graphs\n",
"Generally the first step in writing a TF program is constructing the computation graph. When you import TF, a default graph is created. It is also possible to manually create different indepent graphs. Let's add some operations to the graph:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tensor(\"Add_24:0\", shape=(), dtype=int32)\n",
"Tensor(\"Add_25:0\", shape=(9,), dtype=int64)\n",
"Tensor(\"MatMul:0\", shape=(3, 3), dtype=float32)\n",
"Tensor(\"Reshape:0\", shape=(9,), dtype=float32)\n"
]
}
],
"source": [
"# operations added to default graph\n",
"add_int = tf.add(1,3)\n",
"\n",
"array = np.arange(9)\n",
"add_array = tf.add(array,array)\n",
"\n",
"# define new graph\n",
"new_graph = tf.Graph()\n",
"with new_graph.as_default():\n",
" tensor = tf.constant([i for i in range(1,10)], dtype=tf.float32, shape=[3,3])\n",
" mult_tensor =tf.matmul(tensor,tensor)\n",
"\n",
" reshape_tensor = tf.reshape(tensor, shape=[-1])\n",
"\n",
"print(add_int)\n",
"print(add_array) \n",
"print(mult_tensor) \n",
"print(reshape_tensor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Usually operations can take any tensor argument, that is accepted by [tf.convert_to_tensor](https://www.tensorflow.org/versions/r0.11/api_docs/python/framework.html#convert_to_tensor). There are a lot of defined operation you can use on tensors like [math](https://www.tensorflow.org/versions/r0.11/api_docs/python/math_ops.html#math), [tensor transformation](https://www.tensorflow.org/versions/r0.11/api_docs/python/array_ops.html#tensor-transformations), [special neural network](https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#neural-network) operations and many more."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to evaluate the operations, we have to launch the graph in a session:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Evaluating operations on default graph:\n",
"4\n",
"[ 0 2 4 6 8 10 12 14 16]\n",
"<type 'numpy.ndarray'>\n",
"\n",
"Trying to evaluate operation on another graph:\n",
"Fetch argument <tf.Tensor 'MatMul:0' shape=(3, 3) dtype=float32> cannot be interpreted as a Tensor. (Tensor Tensor(\"MatMul:0\", shape=(3, 3), dtype=float32) is not an element of this graph.)\n",
"\n",
"Now the operations on the other graph can be evaluated\n",
"[[ 30. 36. 42.]\n",
" [ 66. 81. 96.]\n",
" [ 102. 126. 150.]]\n",
"[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]\n"
]
}
],
"source": [
"with tf.Session() as sess:\n",
" print('Evaluating operations on default graph:')\n",
" print(sess.run(add_int))\n",
" print(sess.run(add_array))\n",
" print(type(sess.run(add_array)))\n",
" print('\\nTrying to evaluate operation on another graph:')\n",
" try:\n",
" print(sess.run(mult_tensor))\n",
" except Exception as e:\n",
" print(e)\n",
" \n",
"with tf.Session(graph=new_graph)as sess:\n",
" print('\\nNow the operations on the other graph can be evaluated')\n",
" print(sess.run(mult_tensor))\n",
" print(sess.run(reshape_tensor))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Beside tf.constant, there are other common data structures to represent data:"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Var:\n",
"[[ 0.30460876 1.07989275]\n",
" [ 0.12586282 -1.57130408]]\n",
"\n",
"Operation:\n",
"[[ 1.30460882 2.07989264]\n",
" [ 1.12586284 -0.57130408]]\n",
"\n",
" Cannot feed value of shape (3, 3) for Tensor u'Placeholder_13:0', which has shape '(2, 2)'\n"
]
}
],
"source": [
"var = tf.Variable(tf.random_normal(shape=[2,2]))\n",
"plc = tf.placeholder(dtype=tf.float32, shape=[2,2])\n",
"operation = tf.add(var,plc)\n",
"\n",
"feed_data = np.ones((2,2))\n",
"feed_data2 = np.ones((3,3))\n",
"\n",
"init = tf.initialize_all_variables()\n",
"\n",
"with tf.Session() as sess:\n",
" sess.run(init)\n",
" print('Var:')\n",
" print(sess.run(var))\n",
" print('\\nOperation:')\n",
" print(sess.run(operation, feed_dict={plc: feed_data}))\n",
" try:\n",
" print(sess.run(operation, feed_dict={plc: feed_data2}))\n",
" except Exception as e:\n",
" print('\\n', e)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A [tf.Variable](https://www.tensorflow.org/versions/r0.11/api_docs/python/state_ops.html#Variable) has to initialized via the init operation. Variables are used to store parameters of a model. They can also be saved in a file.\n",
"The [tf.placeholder](https://www.tensorflow.org/versions/r0.11/api_docs/python/io_ops.html#placeholder) is used to feed data inside the model. They can initialized to have a certain shape, to make sure only suitable data is fed into the computation."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"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"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment