Add TensorFlow basics notebook.

This commit is contained in:
Donne Martin 2015-12-28 07:51:47 -05:00
parent 4a640dcc60
commit a00a1158a7
2 changed files with 226 additions and 0 deletions

View File

@ -90,6 +90,12 @@ IPython Notebook(s) demonstrating deep learning functionality.
<img src="https://avatars0.githubusercontent.com/u/15658638?v=3&s=100"> <img src="https://avatars0.githubusercontent.com/u/15658638?v=3&s=100">
</p> </p>
### tensor-flow-tutorials
| Notebook | Description |
|--------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [tsf-basics](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/1_intro/basic_operations.ipynb) | Learn basic operations in TensorFlow, a library for various kinds of perceptual and language understanding tasks from Google. |
### tensor-flow-exercises ### tensor-flow-exercises
| Notebook | Description | | Notebook | Description |

View File

@ -0,0 +1,220 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Operations in TensorFlow\n",
"\n",
"Credits: Forked from [TensorFlow-Examples](https://github.com/aymericdamien/TensorFlow-Examples) by Aymeric Damien\n",
"\n",
"## Setup\n",
"\n",
"Refer to the [setup instructions](http://nbviewer.ipython.org/github/donnemartin/data-science-ipython-notebooks/blob/master/deep-learning/tensor-flow-examples/Setup_TensorFlow.md)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Basic constant operations\n",
"# The value returned by the constructor represents the output\n",
"# of the Constant op.\n",
"a = tf.constant(2)\n",
"b = tf.constant(3)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a=2, b=3\n",
"Addition with constants: 5\n",
"Multiplication with constants: 6\n"
]
}
],
"source": [
"# Launch the default graph.\n",
"with tf.Session() as sess:\n",
" print \"a=2, b=3\"\n",
" print \"Addition with constants: %i\" % sess.run(a+b)\n",
" print \"Multiplication with constants: %i\" % sess.run(a*b)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Basic Operations with variable as graph input\n",
"# The value returned by the constructor represents the output\n",
"# of the Variable op. (define as input when running session)\n",
"# tf Graph input\n",
"a = tf.placeholder(tf.types.int16)\n",
"b = tf.placeholder(tf.types.int16)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Define some operations\n",
"add = tf.add(a, b)\n",
"mul = tf.mul(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Addition with variables: 5\n",
"Multiplication with variables: 6\n"
]
}
],
"source": [
"# Launch the default graph.\n",
"with tf.Session() as sess:\n",
" # Run every operation with variable input\n",
" print \"Addition with variables: %i\" % sess.run(add, feed_dict={a: 2, b: 3})\n",
" print \"Multiplication with variables: %i\" % sess.run(mul, feed_dict={a: 2, b: 3})"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# ----------------\n",
"# More in details:\n",
"# Matrix Multiplication from TensorFlow official tutorial\n",
"\n",
"# Create a Constant op that produces a 1x2 matrix. The op is\n",
"# added as a node to the default graph.\n",
"#\n",
"# The value returned by the constructor represents the output\n",
"# of the Constant op.\n",
"matrix1 = tf.constant([[3., 3.]])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create another Constant that produces a 2x1 matrix.\n",
"matrix2 = tf.constant([[2.],[2.]])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.\n",
"# The returned value, 'product', represents the result of the matrix\n",
"# multiplication.\n",
"product = tf.matmul(matrix1, matrix2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 12.]]\n"
]
}
],
"source": [
"# To run the matmul op we call the session 'run()' method, passing 'product'\n",
"# which represents the output of the matmul op. This indicates to the call\n",
"# that we want to get the output of the matmul op back.\n",
"#\n",
"# All inputs needed by the op are run automatically by the session. They\n",
"# typically are run in parallel.\n",
"#\n",
"# The call 'run(product)' thus causes the execution of threes ops in the\n",
"# graph: the two constants and matmul.\n",
"#\n",
"# The output of the op is returned in 'result' as a numpy `ndarray` object.\n",
"with tf.Session() as sess:\n",
" result = sess.run(product)\n",
" print result"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}