Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
"Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
"Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
"Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n"
]
}
],
"source": [
"from tensorflow.examples.tutorials.mnist import input_data\n",
"mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Modelling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Input"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"x = tf.placeholder(tf.float32, [None, 784])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Variables"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"W = tf.Variable(tf.zeros([784, 10]))\n",
"B = tf.Variable(tf.zeros([10]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Output"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"y = tf.nn.softmax(tf.matmul(x, W) + B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Training"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Placeholder for correct answers"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"y_ = tf.placeholder(tf.float32, [None, 10])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Loss Function\n",
"_The loss function determines how accurate your model is_\n",
"\n",
"The function used here: cross-entropy function\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Trainer/Gradient Descent\n",
"Gradient Descent adjusts the weights and biases by an amount to get a better accuracy"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"trainer = tf.train.GradientDescentOptimizer(0.5)\n",
"train_step = trainer.minimize(cross_entropy)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Run"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"init = tf.global_variables_initializer()\n",
"\n",
"with tf.Session() as sess:\n",
" sess.run(init)\n",
"\n",
" for _ in range(1000):\n",
" batch_xs, batch_ys = mnist.train.next_batch(100)\n",
" sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Evaluate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Calculation"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
"accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Run"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 9.799999743700027%\n"
]
}
],
"source": [
"init = tf.global_variables_initializer()\n",
"\n",
"with tf.Session() as sess:\n",
" sess.run(init)\n",
" \n",
" result = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})\n",
" \n",
"print('Accuracy: {}%'.format(result * 100))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.