Files
2018-05-06 22:19:59 -04:00

158 lines
6.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
"Extracting /tmp/data/train-images-idx3-ubyte.gz\n",
"Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
"Extracting /tmp/data/train-labels-idx1-ubyte.gz\n",
"Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
"Extracting /tmp/data/t10k-images-idx3-ubyte.gz\n",
"Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
"Extracting /tmp/data/t10k-labels-idx1-ubyte.gz\n",
"WARNING:tensorflow:From <ipython-input-1-92265976c34d>:45: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"\n",
"Future major versions of TensorFlow will allow gradients to flow\n",
"into the labels input on backprop by default.\n",
"\n",
"See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
"\n",
"WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
"Instructions for updating:\n",
"Use `tf.global_variables_initializer` instead.\n",
"Epoch 0 completed out of 10 loss: 1664569.60834\n",
"Epoch 1 completed out of 10 loss: 414550.968437\n",
"Epoch 2 completed out of 10 loss: 229022.354944\n",
"Epoch 3 completed out of 10 loss: 136420.393392\n",
"Epoch 4 completed out of 10 loss: 88019.3560204\n",
"Epoch 5 completed out of 10 loss: 56024.820509\n",
"Epoch 6 completed out of 10 loss: 37434.4423951\n",
"Epoch 7 completed out of 10 loss: 29640.3100017\n",
"Epoch 8 completed out of 10 loss: 24399.9572706\n",
"Epoch 9 completed out of 10 loss: 23351.0056713\n",
"Accuracy: 0.9534\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"from tensorflow.examples.tutorials.mnist import input_data\n",
"mnist=input_data.read_data_sets(\"/tmp/data/\", one_hot=True) #one component is on, all others are off\n",
"#10 classes, 0 through 9\n",
"#one-hot outputs 0=[1,0,0,0,0,0,0,0,0], being the 1 is in the algorithm's guess (0)\n",
"#3=[0,0,0,1,0,0,0,0,0]\n",
"n_nodes_hl1=500 #hl1 = hidden layer 1\n",
"n_nodes_hl2=500\n",
"n_nodes_hl3=500\n",
"n_classes=10 #number of categories\n",
"batch_size=100 #divies up the data to be more efficient, as opposed to loading all samples at once\n",
"\n",
"x=tf.placeholder('float',[None, 784])\n",
"y=tf.placeholder('float')\n",
"\n",
"def neural_network_model(data):\n",
" #(inputs*weights)+biases\n",
" hidden_1_layer={'weights':tf.Variable(tf.random_normal([784, n_nodes_hl1])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}\n",
" \n",
" hidden_2_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}\n",
" \n",
" hidden_3_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])), \n",
" 'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}\n",
" \n",
" output_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),\n",
" 'biases': tf.Variable(tf.random_normal([n_classes]))}\n",
" \n",
" l1=tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])\n",
" l1=tf.nn.relu(l1)\n",
" \n",
" l2=tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])\n",
" l2=tf.nn.relu(l2)\n",
" \n",
" l3=tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])\n",
" l3=tf.nn.relu(l3)\n",
" \n",
" output=tf.matmul(l3, output_layer['weights'])+ output_layer['biases']\n",
" \n",
" return output\n",
" \n",
"def train_neural_network(x):\n",
" prediction=neural_network_model(x)\n",
" cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))\n",
" optimizer=tf.train.AdamOptimizer().minimize(cost)\n",
" \n",
" hm_epochs=10\n",
" \n",
" with tf.Session() as sess:\n",
" sess.run(tf.initialize_all_variables())\n",
" \n",
" for epoch in range(hm_epochs):\n",
" epoch_loss=0\n",
" for _ in range(int(mnist.train.num_examples/batch_size)):\n",
" epoch_x,epoch_y=mnist.train.next_batch(batch_size)\n",
" _,c=sess.run([optimizer,cost], feed_dict={x:epoch_x, y:epoch_y})\n",
" epoch_loss+=c\n",
" print('Epoch', epoch, 'completed out of ', hm_epochs, 'loss:', epoch_loss)\n",
" \n",
" correct=tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))\n",
" \n",
" accuracy=tf.reduce_mean(tf.cast(correct, 'float'))\n",
" print('Accuracy:', accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))\n",
" \n",
"\n",
" \n",
" \n",
"train_neural_network(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}