-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,872 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"a = tf.placeholder(\"float\") # Create a symbolic variable 'a'\n", | ||
"b = tf.placeholder(\"float\") # Create a symbolic variable 'b'\n", | ||
"\n", | ||
"y = tf.multiply(a, b) # multiply the symbolic variables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"2.000000 should equal 2.0\n", | ||
"9.000000 should equal 9.0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"with tf.Session() as sess: # create a session to evaluate the symbolic expressions\n", | ||
" print(\"%f should equal 2.0\" % sess.run(y, feed_dict={a: 1, b: 2})) # eval expressions with parameters for a and b\n", | ||
" print(\"%f should equal 9.0\" % sess.run(y, feed_dict={a: 3, b: 3}))" | ||
] | ||
} | ||
], | ||
"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.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"trX = np.linspace(-1, 1, 101)\n", | ||
"trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is approximately linear but with some random noise" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"X = tf.placeholder(\"float\") # create symbolic variables\n", | ||
"Y = tf.placeholder(\"float\")\n", | ||
"\n", | ||
"def model(X, w):\n", | ||
" return tf.multiply(X, w) # lr is just X*w so this model line is pretty simple\n", | ||
"\n", | ||
"w = tf.Variable(0.0, name=\"weights\") # create a shared variable (like theano.shared) for the weight matrix\n", | ||
"y_model = model(X, w)\n", | ||
"\n", | ||
"cost = tf.square(Y - y_model) # use square error for cost function\n", | ||
"\n", | ||
"train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # construct an optimizer to minimize cost and fit line to my data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"2.00863\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# Launch the graph in a session\n", | ||
"with tf.Session() as sess:\n", | ||
" # you need to initialize variables (in this case just variable W)\n", | ||
" tf.global_variables_initializer().run()\n", | ||
"\n", | ||
" for i in range(100):\n", | ||
" for (x, y) in zip(trX, trY):\n", | ||
" sess.run(train_op, feed_dict={X: x, Y: y})\n", | ||
"\n", | ||
" print(sess.run(w)) # It should be something around 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"import numpy as np\n", | ||
"from tensorflow.examples.tutorials.mnist import input_data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def init_weights(shape):\n", | ||
" return tf.Variable(tf.random_normal(shape, stddev=0.01))\n", | ||
"\n", | ||
"def model(X, w):\n", | ||
" return tf.matmul(X, w) # notice we use the same model as linear regression, this is because there is a baked in cost function which performs softmax and cross entropy\n", | ||
"\n", | ||
"mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n", | ||
"trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"X = tf.placeholder(\"float\", [None, 784]) # create symbolic variables\n", | ||
"Y = tf.placeholder(\"float\", [None, 10])\n", | ||
"\n", | ||
"w = init_weights([784, 10]) # like in linear regression, we need a shared variable weight matrix for logistic regression\n", | ||
"\n", | ||
"py_x = model(X, w)\n", | ||
"\n", | ||
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute mean cross entropy (softmax is applied internally)\n", | ||
"train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct optimizer\n", | ||
"predict_op = tf.argmax(py_x, 1) # at predict time, evaluate the argmax of the logistic regression" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Launch the graph in a session\n", | ||
"with tf.Session() as sess:\n", | ||
" # you need to initialize all variables\n", | ||
" tf.global_variables_initializer().run()\n", | ||
"\n", | ||
" for i in range(100):\n", | ||
" for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):\n", | ||
" sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})\n", | ||
" print(i, np.mean(np.argmax(teY, axis=1) ==\n", | ||
" sess.run(predict_op, feed_dict={X: teX})))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"import numpy as np\n", | ||
"from tensorflow.examples.tutorials.mnist import input_data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def init_weights(shape):\n", | ||
" return tf.Variable(tf.random_normal(shape, stddev=0.01))\n", | ||
"\n", | ||
"def model(X, w_h, w_o):\n", | ||
" h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions\n", | ||
" return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us\n", | ||
"\n", | ||
"mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n", | ||
"trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"X = tf.placeholder(\"float\", [None, 784])\n", | ||
"Y = tf.placeholder(\"float\", [None, 10])\n", | ||
"\n", | ||
"w_h = init_weights([784, 625]) # create symbolic variables\n", | ||
"w_o = init_weights([625, 10])\n", | ||
"\n", | ||
"py_x = model(X, w_h, w_o)\n", | ||
"\n", | ||
"cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute costs\n", | ||
"train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer\n", | ||
"predict_op = tf.argmax(py_x, 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Launch the graph in a session\n", | ||
"with tf.Session() as sess:\n", | ||
" # you need to initialize all variables\n", | ||
" tf.global_variables_initializer().run()\n", | ||
"\n", | ||
" for i in range(100):\n", | ||
" for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):\n", | ||
" sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})\n", | ||
" print(i, np.mean(np.argmax(teY, axis=1) ==\n", | ||
" sess.run(predict_op, feed_dict={X: teX})))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.