{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorflow.examples.tutorials.mnist import input_data" ] }, { "cell_type": "code", "execution_count": 2, "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": [ "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n", "\n", "# 输入图片 28*28 个像素\n", "n_inputs = 28 # 输入的每行有 28 个数据,输入层 神经元的个数\n", "max_time = 28 # 输入的次数为 28 次\n", "lstm_size = 100 # 隐藏层 block 单元\n", "n_classes = 10 # 分类个数\n", "batch_size = 50 # 单批次的样本数量\n", "n_batch = mnist.train.num_examples / batch_size # 一共会分成多少批次" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = tf.placeholder(tf.float32, [None, 784])\n", "y = tf.placeholder(tf.float32, [None, 10])\n", "weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))\n", "biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def LSTM(x, weights, biases):\n", " inputs = tf.reshape(x, [-1, max_time, n_inputs])\n", " # 定义隐藏层 block 单元\n", " lstm_cell = tf.contrib.rnn.BasicLSTMCell(lstm_size)\n", " # final_state[0]: cell state\n", " # final_state[1]: hidden_state\n", " outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, inputs, dtype=tf.float32)\n", " return tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "prediction = LSTM(x, weights, biases)\n", "cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))\n", "train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)\n", "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))\n", "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", "init = tf.global_variables_initializer()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Iterator: 0 , Accuracy: 0.7244\n", "Iterator: 10 , Accuracy: 0.946\n", "Iterator: 20 , Accuracy: 0.9646\n", "Iterator: 30 , Accuracy: 0.9696\n", "Iterator: 40 , Accuracy: 0.9717\n", "Iterator: 50 , Accuracy: 0.9764\n", "Iterator: 60 , Accuracy: 0.9776\n", "Iterator: 70 , Accuracy: 0.9801\n", "Iterator: 80 , Accuracy: 0.9814\n", "Iterator: 90 , Accuracy: 0.9793\n", "Iterator: 100 , Accuracy: 0.9814\n" ] } ], "source": [ "with tf.Session() as sess:\n", " sess.run(init)\n", " for epoch in range(101):\n", " for batch in range(int(n_batch)):\n", " batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n", " sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})\n", " if epoch % 10 == 0:\n", " acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})\n", " print(\"Iterator:\", str(epoch), \", Accuracy:\", str(acc))" ] } ], "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.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }