├── README.md ├── backward.py ├── cnn_demo.ipynb ├── forward.py ├── generator.py ├── learning_rate_decay.py ├── mnist_BP └── none ├── mnist_app.py ├── mnist_backward.ipynb ├── mnist_backward.py ├── mnist_demo.py ├── mnist_forward.ipynb ├── mnist_forward.py ├── mnist_lenet ├── cnn_demo.ipynb ├── mnist_lenet_backward.ipynb ├── mnist_lenet_backward.py ├── mnist_lenet_forward.ipynb ├── mnist_lenet_forward.py ├── mnist_lenet_test.ipynb └── none ├── mnist_lenet_backward.ipynb ├── mnist_lenet_backward.py ├── mnist_lenet_forward.ipynb ├── mnist_lenet_forward.py ├── mnist_lenet_test.ipynb ├── mnist_test.ipynb ├── mnist_test.py ├── others ├── backward.py ├── forward.py ├── generator.py ├── learning_rate_decay.py ├── none ├── placeholder.py ├── tf_2layer.py ├── tf_demo.py └── variables.py ├── placeholder.py ├── tf_2layer.py ├── tf_demo.py └── variables.py /README.md: -------------------------------------------------------------------------------- 1 | # tensorflow_notebook 2 | 【北京大学】人工智能实践:Tensorflow笔记 手敲代码共享 3 | 文件夹里的是我稍微整理一下的,第一次创建文件夹,有点乱。 4 | 课程开头的一些代码我这里就没了,可以参考这位同学的: 5 | https://github.com/Tianxiaomo/tensorflow_notebook 6 | -------------------------------------------------------------------------------- /backward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import generator 5 | import forward 6 | 7 | STEPS = 40000 8 | BATCH_SIZE = 32 9 | LEARNING_RATE_BASE = 0.001 10 | LEARNING_RATE_DECAY = 0.999 11 | REGULARIZER = 0.01 12 | 13 | def backward(): 14 | x = tf.placeholder(tf.float32,shape= (None,2)) 15 | y_ = tf.placeholder(tf.float32,shape= (None,1)) 16 | X, Y_, Y_c = generator.generator() 17 | 18 | pred_y = forward.forward(x,REGULARIZER) 19 | global_step = tf.Variable(0,trainable=False) 20 | learning_rate = tf.train.exponential_decay( 21 | LEARNING_RATE_BASE, 22 | global_step, 23 | 300/BATCH_SIZE, 24 | LEARNING_RATE_DECAY, 25 | staircase=True) 26 | 27 | #定义损失函数 28 | loss_mse = tf.reduce_mean(tf.square(pred_y-y_)) 29 | loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) 30 | 31 | #定义反向传播方法:包括正则化 32 | train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) 33 | 34 | with tf.Session() as sess: 35 | init_op = tf.global_variables_initializer() 36 | sess.run(init_op) 37 | for i in range(STEPS): 38 | start = (i*BATCH_SIZE)%300 39 | end = start + BATCH_SIZE 40 | sess.run(train_step,feed_dict={x: X[start:end],y_: Y_[start:end]}) 41 | if i % 200 ==0: 42 | loss_v = sess.run(loss_total,feed_dict={x:X,y_:Y_}) 43 | print("After %d steps, loss_v is %f:"%(i,loss_v)) 44 | xx,yy = np.mgrid[-3:3:0.1,-3:3:0.1] 45 | grid = np.c_[xx.ravel(),yy.ravel()] 46 | probs = sess.run(pred_y,feed_dict={x:grid}) 47 | probs = probs.reshape(xx.shape) 48 | print("xx.shape:",xx.shape) 49 | print("yy.shape:",yy.shape) 50 | plt.scatter(X[:,0],X[:,1],c=np.squeeze(Y_c)) 51 | plt.contour(xx,yy,probs,levels=[0.5]) 52 | 53 | plt.show() 54 | 55 | if __name__ == '__main__': 56 | backward() 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /cnn_demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import matplotlib.pyplot as plt\n", 10 | "import numpy as np\n", 11 | "import tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data\n", 13 | "from tensorflow.python.framework import ops\n", 14 | "ops.reset_default_graph()" 15 | ] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.5.5" 35 | }, 36 | "widgets": { 37 | "state": {}, 38 | "version": "1.1.2" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 2 43 | } 44 | -------------------------------------------------------------------------------- /forward.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #导入模块,生成模拟数据集 3 | import tensorflow as tf 4 | import generator 5 | import numpy as np 6 | # 定义网络输入、参数和输出,定义前向传播过程 7 | def get_weight(shape, regularizer): 8 | W = tf.Variable(tf.random_normal(shape),dtype = tf.float32) 9 | 10 | tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(W)) 11 | return W 12 | 13 | def get_bias(shape): 14 | b = tf.Variable(tf.constant(0.01,shape = shape)) 15 | return b 16 | 17 | def forward(X,regularizer): 18 | W1 = get_weight([2,11],regularizer) 19 | b1 = get_bias([11]) 20 | y1 = tf.nn.relu(tf.matmul(X,W1) + b1) 21 | 22 | W2 = get_weight([11,1],regularizer) 23 | b2 = get_bias([1]) 24 | y =tf.matmul(y1,W2) +b2 #输出层不过激活 25 | 26 | return y 27 | 28 | 29 | if __name__ == '__main__': 30 | X,Y,Y_c = generator. generator() 31 | X = np.array(X) 32 | Y = np.array(Y) 33 | Y_c = np.array(Y_c) 34 | print("X:",X.shape) 35 | print("Y:",Y.shape) 36 | print("Y_c:",Y_c.shape) 37 | -------------------------------------------------------------------------------- /generator.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #导入模块,生成模拟数据集 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | seed = 2 6 | def generator(): 7 | # 基于seed产生随机数 8 | rdm = np.random.RandomState(seed) 9 | # 随机数返回300行2列的矩阵,表示300组坐标点(x0,x1)作为输入数据集 10 | X = rdm.randn(300,2) 11 | # 从X这个300行2列的矩阵中取出一行,判断如果两个坐标平方和小于2,给Yield赋值1,其余赋值0 12 | # Y_作为输入数据集的标签(正确答案) 13 | Y_ = [int(x0*x0+x1*x1<2) for (x0,x1) in X] 14 | # 遍历Y中每个元素,1赋值'red',其余赋值'blue',这样便于可视化观察 15 | Y_c =[['red' if y else 'blue'] for y in Y_] 16 | # 对数据集X和标签Y进行形状整理,第一个元素为-1表示跟随第二列计算; 17 | # 第二个元素表示多少列,可见X为两列,Y为1列 18 | X = np.vstack(X).reshape(-1,2) 19 | Y_ = np.vstack(Y_).reshape(-1,1) 20 | 21 | return X,Y_,Y_c 22 | 23 | if __name__ == '__main__': 24 | X,Y,Y_c = generator() 25 | X = np.array(X) 26 | Y = np.array(Y) 27 | Y_c = np.array(Y_c) 28 | print("X:",X.shape) 29 | print("Y:",Y.shape) 30 | print("Y_c:",Y_c.shape) 31 | print("show_over") 32 | -------------------------------------------------------------------------------- /learning_rate_decay.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | # 整个的学习率算法:定义基础学习率,衰减率(decay_rate)和更新步数(已经训练的步数/延迟步长,如果decay_steps不为1,很明显会产生梯度)。 3 | # 新学习率的公式 : decayed_learning_rate = learning_rate*(decay_rate^(global_steps/decay_steps) 4 | # 5 | 6 | # 设损失函数 loss = (W + 1) ^ 2,令W初值是常数10。反向传播就是求最优W,即求最小loss对应的W值。 7 | # 使用指数衰减的学习率,在迭代初期得到较高的下降速度,可以在较小的训练轮数下取得更有收敛度 8 | 9 | import tensorflow as tf 10 | import matplotlib.pyplot as plt 11 | import numpy as np 12 | 13 | LEARNING_RATE_BASE = 0.3 #最初的学习率 14 | LEARNING_RATE_DECAY = 0.99 #学习衰减率 15 | LEARNING_RATE_STEP = 1 #喂入多少轮BATCH_SIZE后,更新一次学习率,一般设为:总样本数/BATCH_SIZE 16 | 17 | # 运行了几轮BATCH_SIZE的计数器,初值给0,设为不被训练 18 | global_step = tf.Variable(0,trainable = False) 19 | # 定义指数下降学习率,没用自己的公式,而是调用如下函数 20 | learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,LEARNING_RATE_STEP,LEARNING_RATE_DECAY,staircase = True) 21 | 22 | 23 | 24 | W = tf.Variable(tf.constant(5,dtype = tf.float32)) 25 | loss = tf.square(W + 1) 26 | train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step) 27 | 28 | with tf.Session() as sess: 29 | init_op = tf.global_variables_initializer() 30 | sess.run(init_op) 31 | w_val = [] 32 | loss_val = [] 33 | 34 | for i in range(400): 35 | sess.run(train_step) 36 | # 训练的时候需要更新learning_rate,并且获取rate值 37 | learning_rate_val = sess.run(learning_rate) 38 | # global_step竟然也能在会话中更新? 39 | global_step_val = sess.run(global_step) 40 | w_val.append(sess.run(W)) 41 | loss_val.append(sess.run(loss)) 42 | print("After %s steps: learning_rate_val is %f,\t global_step_val is %f ." 43 | % (i,learning_rate_val,global_step_val)) 44 | # print("After %s steps: W is %f,\t loss is %f ." % (i,w_val,loss_val)) 45 | print("len(w_val):",len(w_val)) 46 | length = 40 47 | plt.plot(range(length),w_val[:length],linestyle = '--', color = "b",label = "w_val") 48 | plt.scatter(range(length),w_val[:length],s = 50,color = "b") 49 | plt.plot(range(length),loss_val[:length],color = "r",label = "loss_val") 50 | plt.scatter(range(length),loss_val[:length],s = 50,color = "r") 51 | plt.legend(loc = "best") 52 | plt.show() 53 | -------------------------------------------------------------------------------- /mnist_BP/none: -------------------------------------------------------------------------------- 1 | none 2 | -------------------------------------------------------------------------------- /mnist_app.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Nov 28 22:37:56 2018 4 | 5 | @author: lele 6 | """ 7 | 8 | import tensorflow as tf 9 | import numpy as np 10 | from PIL import Image 11 | import mnist_backward 12 | import mnist_forward 13 | 14 | def restore_model(testPicArr): 15 | with tf.Gragh().as_default() as tg: 16 | x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE]) 17 | y = mnist_forward.forward(x,None) 18 | preValue = tf.arg_max(y) 19 | 20 | variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY) 21 | variable_to_restore = variable_averages.variables_to_restore() 22 | saver = tf.train.Saver(variables_to_restore) 23 | 24 | with tf.Session() as sess: 25 | ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH) 26 | if ckpt and ckpt.model_checkpoint_path: 27 | saver.restore(sess,ckpt.model_checkpoint_path) 28 | 29 | preValue = sess.run(preValue, feed_dict = {x:testPicArr}) 30 | return preValue 31 | else: 32 | print("No checkpoint file found") 33 | return -1 34 | 35 | def pre_pic(picName): 36 | img = Image.open(picName) 37 | reIm = img.resize((28,28),Image.ANTIALIAS) 38 | im_arr = np.array(reIm.convert('L')) 39 | threshold = 50 40 | for i in range(28): 41 | for j in range(28): 42 | im_arr[i][j] = 255 -im_arr[i][j] 43 | if (im_arr[i][j] < threshold): 44 | im_arr[i][j] =0 45 | else: im_arr[i][j] =255 46 | 47 | nm_arr = im_arr.reshape([1,784]) 48 | nm_arr = nm_arr.astype(np.float32) 49 | img_ready = np.multiply(nm_arr, 1.0/255.0) 50 | 51 | return img_ready 52 | 53 | def application(): 54 | testNum = input("input the num of the test pictures") 55 | for i in range(testNum): 56 | testPic = raw_input("the path of test picture:") 57 | testPicArr = pre_pic(testPicArr) 58 | print("the prediction num is:",preValue) 59 | 60 | def main(): 61 | application() 62 | 63 | if __name__=="__main__": 64 | main() 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /mnist_backward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "WARNING:tensorflow:From :54: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 13 | "Instructions for updating:\n", 14 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 15 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", 16 | "Instructions for updating:\n", 17 | "Please write your own downloading logic.\n", 18 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 19 | "Instructions for updating:\n", 20 | "Please use tf.data to implement this functionality.\n", 21 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 22 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 23 | "Instructions for updating:\n", 24 | "Please use tf.data to implement this functionality.\n", 25 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 26 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 27 | "Instructions for updating:\n", 28 | "Please use tf.one_hot on tensors.\n", 29 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 31 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 32 | "Instructions for updating:\n", 33 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 34 | "After 1 training step(s), losses on trianing batch is 2.860926. \n", 35 | "After 1001 training step(s), losses on trianing batch is 0.350883. \n", 36 | "After 2001 training step(s), losses on trianing batch is 0.265956. \n", 37 | "After 3001 training step(s), losses on trianing batch is 0.247919. \n", 38 | "After 4001 training step(s), losses on trianing batch is 0.218711. \n", 39 | "After 5001 training step(s), losses on trianing batch is 0.201225. \n", 40 | "After 6001 training step(s), losses on trianing batch is 0.207533. \n", 41 | "After 7001 training step(s), losses on trianing batch is 0.183419. \n", 42 | "After 8001 training step(s), losses on trianing batch is 0.182193. \n", 43 | "After 9001 training step(s), losses on trianing batch is 0.170735. \n", 44 | "After 10001 training step(s), losses on trianing batch is 0.188354. \n", 45 | "After 11001 training step(s), losses on trianing batch is 0.167919. \n", 46 | "After 12001 training step(s), losses on trianing batch is 0.174971. \n", 47 | "After 13001 training step(s), losses on trianing batch is 0.178794. \n", 48 | "After 14001 training step(s), losses on trianing batch is 0.167945. \n", 49 | "After 15001 training step(s), losses on trianing batch is 0.180192. \n", 50 | "After 16001 training step(s), losses on trianing batch is 0.169211. \n", 51 | "After 17001 training step(s), losses on trianing batch is 0.160823. \n", 52 | "After 18001 training step(s), losses on trianing batch is 0.148409. \n", 53 | "After 19001 training step(s), losses on trianing batch is 0.148997. \n", 54 | "After 20001 training step(s), losses on trianing batch is 0.155089. \n", 55 | "After 21001 training step(s), losses on trianing batch is 0.163209. \n", 56 | "After 22001 training step(s), losses on trianing batch is 0.149414. \n", 57 | "After 23001 training step(s), losses on trianing batch is 0.158236. \n", 58 | "After 24001 training step(s), losses on trianing batch is 0.147007. \n", 59 | "After 25001 training step(s), losses on trianing batch is 0.143372. \n", 60 | "After 26001 training step(s), losses on trianing batch is 0.146931. \n", 61 | "After 27001 training step(s), losses on trianing batch is 0.151124. \n", 62 | "After 28001 training step(s), losses on trianing batch is 0.142916. \n", 63 | "After 29001 training step(s), losses on trianing batch is 0.142049. \n", 64 | "After 30001 training step(s), losses on trianing batch is 0.137897. \n", 65 | "After 31001 training step(s), losses on trianing batch is 0.150624. \n", 66 | "After 32001 training step(s), losses on trianing batch is 0.149548. \n", 67 | "After 33001 training step(s), losses on trianing batch is 0.144256. \n", 68 | "After 34001 training step(s), losses on trianing batch is 0.147530. \n", 69 | "After 35001 training step(s), losses on trianing batch is 0.141618. \n", 70 | "After 36001 training step(s), losses on trianing batch is 0.137964. \n", 71 | "After 37001 training step(s), losses on trianing batch is 0.150764. \n", 72 | "After 38001 training step(s), losses on trianing batch is 0.140176. \n", 73 | "After 39001 training step(s), losses on trianing batch is 0.137057. \n", 74 | "After 40001 training step(s), losses on trianing batch is 0.135586. \n", 75 | "After 41001 training step(s), losses on trianing batch is 0.138417. \n", 76 | "After 42001 training step(s), losses on trianing batch is 0.139068. \n", 77 | "After 43001 training step(s), losses on trianing batch is 0.140603. \n", 78 | "After 44001 training step(s), losses on trianing batch is 0.140301. \n", 79 | "After 45001 training step(s), losses on trianing batch is 0.135959. \n", 80 | "After 46001 training step(s), losses on trianing batch is 0.134512. \n", 81 | "After 47001 training step(s), losses on trianing batch is 0.138384. \n", 82 | "After 48001 training step(s), losses on trianing batch is 0.131829. \n", 83 | "After 49001 training step(s), losses on trianing batch is 0.132271. \n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "import tensorflow as tf\n", 89 | "from tensorflow.examples.tutorials.mnist import input_data\n", 90 | "import mnist_forward\n", 91 | "import os\n", 92 | "\n", 93 | "BATCH_SIZE = 256\n", 94 | "LEARNING_RATE_BASE = 0.1\n", 95 | "LEARNING_RATE_DECAY = 0.99\n", 96 | "REGULARIZER = 0.0001\n", 97 | "STEPS = 50000\n", 98 | "MOVING_AVERAGE_DECAY = 0.99\n", 99 | "MODEL_SAVE_PATH = \"./model/\"\n", 100 | "MODEL_NAME = 'mnist_model'\n", 101 | "\n", 102 | "def backward(mnist):\n", 103 | " \n", 104 | " x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])\n", 105 | " y_ = tf.placeholder(tf.float32,[None,mnist_forward.OUTPUT_NODE])\n", 106 | " y = mnist_forward.forward(x, REGULARIZER)\n", 107 | " global_step = tf.Variable(0, trainable = False)\n", 108 | " \n", 109 | " ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))\n", 110 | " cem = tf.reduce_mean(ce)\n", 111 | " loss = cem + tf.add_n(tf.get_collection(\"losses\"))\n", 112 | " \n", 113 | " learning_rate = tf.train.exponential_decay(\n", 114 | " LEARNING_RATE_BASE,\n", 115 | " global_step,\n", 116 | " mnist.train.num_examples / BATCH_SIZE,\n", 117 | " LEARNING_RATE_DECAY,\n", 118 | " staircase = True)\n", 119 | " \n", 120 | " train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)\n", 121 | " \n", 122 | " ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)\n", 123 | " ema_op = ema.apply(tf.trainable_variables())\n", 124 | " with tf.control_dependencies([train_step, ema_op]):\n", 125 | " train_op = tf.no_op(name=\"train\")\n", 126 | " \n", 127 | " saver = tf.train.Saver()\n", 128 | " \n", 129 | " with tf.Session() as sess:\n", 130 | " init_op = tf.global_variables_initializer()\n", 131 | " sess.run(init_op)\n", 132 | " \n", 133 | " for i in range(STEPS):\n", 134 | " xs, ys = mnist.train.next_batch(BATCH_SIZE)\n", 135 | " _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x:xs, y_: ys})\n", 136 | " if i % 1000 ==0:\n", 137 | " print(\"After %d training step(s), losses on trianing batch is %f. \" %(step, loss_value))\n", 138 | " saver.save(sess, os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step)\n", 139 | " \n", 140 | "def main():\n", 141 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot= True)\n", 142 | " backward(mnist)\n", 143 | " \n", 144 | "if __name__ == \"__main__\":\n", 145 | " main()\n", 146 | " \n", 147 | " " 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 1, 153 | "metadata": {}, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "c: 35\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "a = 23\n", 165 | "b = 12\n", 166 | "print(\"c:\",a+b)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [] 175 | } 176 | ], 177 | "metadata": { 178 | "kernelspec": { 179 | "display_name": "Python 3", 180 | "language": "python", 181 | "name": "python3" 182 | }, 183 | "language_info": { 184 | "codemirror_mode": { 185 | "name": "ipython", 186 | "version": 3 187 | }, 188 | "file_extension": ".py", 189 | "mimetype": "text/x-python", 190 | "name": "python", 191 | "nbconvert_exporter": "python", 192 | "pygments_lexer": "ipython3", 193 | "version": "3.5.5" 194 | }, 195 | "widgets": { 196 | "state": {}, 197 | "version": "1.1.2" 198 | } 199 | }, 200 | "nbformat": 4, 201 | "nbformat_minor": 2 202 | } 203 | -------------------------------------------------------------------------------- /mnist_backward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | import mnist_forward 4 | import os 5 | 6 | BATCH_SIZE = 32 7 | LEARNING_RATE_BASE = 0.1 8 | LEARNING_RATE_DECAY = 0.99 9 | REGULARIZER = 0.0001 10 | STEPS = 50000 11 | MOVING_AVERAGE_DECAY = 0.99 12 | MODEL_SAVE_PATH = "./model/" 13 | MODEL_NAME = 'mnist_model' 14 | 15 | def backward(mnist): 16 | 17 | x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE]) 18 | y_ = tf.placeholder(tf.float32,[None, mnist_forward.OUTPUT_NODE]) 19 | y = mnist_forward.forward(x, REGULARIZER) 20 | global_step = tf.Variable(0, trainable = False) 21 | 22 | ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) 23 | cem = tf.reduce_mean(ce) 24 | loss = cem + tf.add_n(tf.get_collection("losses")) 25 | 26 | learning_rate = tf.train.exponential_decay( 27 | LEARNING_RATE_BASE, 28 | global_step, 29 | mnist.train.num_examples / BATCH_SIZE, 30 | LEARNING_RATE_DECAY, 31 | staircase = True) 32 | 33 | train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step) 34 | 35 | ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) 36 | ema_op = ema.apply(tf.trainable_variables()) 37 | with tf.control_dependencies([train_step, ema_op]): 38 | train_op = tf.no_op(name="train") 39 | 40 | saver = tf.train.Saver() 41 | 42 | with tf.Session() as sess: 43 | init_op = tf.global_variables_initializer() 44 | sess.run(init_op) 45 | ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) 46 | exclude = [''] 47 | 48 | if ckpt and ckpt.model_checkpoint_path: 49 | saver.restore(sess,ckpt.model_checkpoint_path) 50 | 51 | 52 | for i in range(STEPS): 53 | xs, ys = mnist.train.next_batch(BATCH_SIZE) 54 | _, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x:xs, y_: ys}) 55 | if i % 1000 ==0: 56 | print("After %d training step(s), losses on trianing batch is %f. " %(step, loss_value)) 57 | saver.save(sess, os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step) 58 | 59 | def main(): 60 | mnist = input_data.read_data_sets("./MNIST_data/",one_hot= True) 61 | backward(mnist) 62 | 63 | if __name__ == "__main__": 64 | main() 65 | 66 | -------------------------------------------------------------------------------- /mnist_demo.py: -------------------------------------------------------------------------------- 1 | from tensorflow.examples.tutorials.mnist import input_data 2 | import tensorflow as tf 3 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 4 | # print(dir(mnist.train.images)) 5 | # print(mnist.train.images.shape) 6 | 7 | def compute_accuracy(v_xs,v_ys): 8 | global prediction 9 | y_pre = sess.run(prediction,feed_dict = {xs:v_xs}) 10 | """ 11 | print("y_pre.shape:",y_pre.shape) 12 | y_pre.shape: (10000, 10) 13 | tf.argmax(array,0 or 1) can return the index of maxmimum of a variable 14 | and if array = ([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]]) 15 | if axis is 0,will compare per row ,so return 4 value 16 | if axis is 1,will compare per column,so return 3 value 17 | """ 18 | correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1)) 19 | accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 20 | result = sess.run(accuracy,feed_dict = {xs:v_xs,ys:v_ys}) 21 | return result 22 | 23 | 24 | 25 | # define placeholder for inputs to network 26 | xs = tf.placeholder(tf.float32,[None,784]) 27 | ys = tf.placeholder("float32",[None,10]) 28 | 29 | # define Weights and biases for network 30 | W = tf.Variable(tf.zeros([784,10])) 31 | b = tf.Variable(tf.zeros([10])) 32 | 33 | # compute prediction without adding hidden layers 34 | prediction = tf.nn.softmax(tf.matmul(xs,W) + b) 35 | 36 | # the error between prediction and real data 37 | cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1])) 38 | 39 | train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy) 40 | 41 | sess = tf.Session() 42 | 43 | sess.run(tf.global_variables_initializer()) 44 | for i in range(100000): 45 | batch_xs,batch_ys = mnist.train.next_batch(100) 46 | sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys}) 47 | if i % 50 == 0: 48 | print(compute_accuracy( 49 | mnist.test.images,mnist.test.labels)) 50 | 51 | 52 | 53 | 54 | # cross_entropy = -tf.reduce_sum(y_*tf.log(y)) 55 | # train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 56 | 57 | # init = tf.global_variables_initializer() 58 | # sess = tf.Session() 59 | # sess.run(init) 60 | 61 | # print(W) 62 | # print(b) 63 | # for i in range(1000): 64 | # batch_xs,batch_ys = mnist.train.next_batch(100) 65 | 66 | # sess.run(train_step,feed_dict = {x: batch_xs,y_:batch_ys}) 67 | -------------------------------------------------------------------------------- /mnist_forward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "x.shape: (3, 784)\n", 13 | "float32\n", 14 | "pred_y: Tensor(\"strided_slice:0\", shape=(3, 10), dtype=float32)\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "import tensorflow as tf\n", 20 | "import numpy as np\n", 21 | "INPUT_NODE = 784\n", 22 | "OUTPUT_NODE = 10\n", 23 | "LAYER_NODE = 500\n", 24 | "\n", 25 | "def get_weight(shape,regularizer):\n", 26 | " #beijieduande biaozhuncha buhuichaoguo liangge biaozhuncha\n", 27 | " w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))\n", 28 | " if regularizer != None:\n", 29 | " tf.add_to_collection(\"losses\",tf.contrib.layers.l2_regularizer(regularizer)(w))\n", 30 | " return w\n", 31 | " \n", 32 | "def get_bias(shape):\n", 33 | " b = tf.Variable(tf.zeros(shape))\n", 34 | " return b\n", 35 | "\n", 36 | "def forward(x, regularizer):\n", 37 | " w1 = get_weight((INPUT_NODE,LAYER_NODE),regularizer)\n", 38 | " b1 = get_bias(LAYER_NODE)\n", 39 | " y1 = tf.nn.relu(tf.matmul(x,w1)+b1)\n", 40 | " \n", 41 | " w2 = get_weight((LAYER_NODE,OUTPUT_NODE),regularizer)\n", 42 | " b2 = get_bias(OUTPUT_NODE)\n", 43 | " y2 = tf.matmul(y1,w2)+b2\n", 44 | " return y2\n", 45 | "\n", 46 | "def main():\n", 47 | " x = np.random.random((3,784))\n", 48 | " x = x.astype(np.float32)\n", 49 | " print(\"x.shape:\",x.shape)\n", 50 | " regularizer = 0.001\n", 51 | " print(x.dtype)\n", 52 | "\n", 53 | " pred_y = forward(x,regularizer)\n", 54 | " print(\"pred_y:\",pred_y[:])\n", 55 | "if __name__==\"__main__\":\n", 56 | " main()\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "metadata": {}, 63 | "outputs": [], 64 | "source": [] 65 | } 66 | ], 67 | "metadata": { 68 | "kernelspec": { 69 | "display_name": "Python 3", 70 | "language": "python", 71 | "name": "python3" 72 | }, 73 | "language_info": { 74 | "codemirror_mode": { 75 | "name": "ipython", 76 | "version": 3 77 | }, 78 | "file_extension": ".py", 79 | "mimetype": "text/x-python", 80 | "name": "python", 81 | "nbconvert_exporter": "python", 82 | "pygments_lexer": "ipython3", 83 | "version": "3.5.6" 84 | } 85 | }, 86 | "nbformat": 4, 87 | "nbformat_minor": 2 88 | } 89 | -------------------------------------------------------------------------------- /mnist_forward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | INPUT_NODE = 784 4 | OUTPUT_NODE = 10 5 | LAYER_NODE = 500 6 | 7 | def get_weight(shape,regularizer): 8 | #beijieduande biaozhuncha buhuichaoguo liangge biaozhuncha 9 | w = tf.Variable(tf.truncated_normal(shape,stddev=0.1)) 10 | if regularizer != None: 11 | tf.add_to_collection("losses",tf.contrib.layers.l2_regularizer(regularizer)(w)) 12 | return w 13 | 14 | def get_bias(shape): 15 | b = tf.Variable(tf.zeros(shape)) 16 | return b 17 | 18 | def forward(x, regularizer): 19 | w1 = get_weight((INPUT_NODE,LAYER_NODE),regularizer) 20 | b1 = get_bias(LAYER_NODE) 21 | y1 = tf.nn.relu(tf.matmul(x,w1)+b1) 22 | 23 | w2 = get_weight((LAYER_NODE,OUTPUT_NODE),regularizer) 24 | b2 = get_bias(OUTPUT_NODE) 25 | y2 = tf.matmul(y1,w2)+b2 26 | return y2 27 | 28 | def main(): 29 | x = np.random.random((3,784)) 30 | x = x.astype(np.float32) 31 | print("x.shape:",x.shape) 32 | regularizer = 0.001 33 | print(x.dtype) 34 | 35 | pred_y = forward(x,regularizer) 36 | print("pred_y:",pred_y[:]) 37 | if __name__=="__main__": 38 | main() -------------------------------------------------------------------------------- /mnist_lenet/cnn_demo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import matplotlib.pyplot as plt\n", 10 | "import numpy as np\n", 11 | "import tensorflow as tf\n", 12 | "from tensorflow.examples.tutorials.mnist import input_data\n", 13 | "from tensorflow.python.framework import ops\n", 14 | "ops.reset_default_graph()" 15 | ] 16 | } 17 | ], 18 | "metadata": { 19 | "kernelspec": { 20 | "display_name": "Python 3", 21 | "language": "python", 22 | "name": "python3" 23 | }, 24 | "language_info": { 25 | "codemirror_mode": { 26 | "name": "ipython", 27 | "version": 3 28 | }, 29 | "file_extension": ".py", 30 | "mimetype": "text/x-python", 31 | "name": "python", 32 | "nbconvert_exporter": "python", 33 | "pygments_lexer": "ipython3", 34 | "version": "3.5.5" 35 | }, 36 | "widgets": { 37 | "state": {}, 38 | "version": "1.1.2" 39 | } 40 | }, 41 | "nbformat": 4, 42 | "nbformat_minor": 2 43 | } 44 | -------------------------------------------------------------------------------- /mnist_lenet/mnist_lenet_backward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "WARNING:tensorflow:From :69: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 13 | "Instructions for updating:\n", 14 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 15 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", 16 | "Instructions for updating:\n", 17 | "Please write your own downloading logic.\n", 18 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 19 | "Instructions for updating:\n", 20 | "Please use tf.data to implement this functionality.\n", 21 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 22 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 23 | "Instructions for updating:\n", 24 | "Please use tf.data to implement this functionality.\n", 25 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 26 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 27 | "Instructions for updating:\n", 28 | "Please use tf.one_hot on tensors.\n", 29 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 31 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 32 | "Instructions for updating:\n", 33 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 34 | "pool_shape[0]: 16\n", 35 | "After 1 steps,the loss is 5.175639\n", 36 | "After 101 steps,the loss is 2.093199\n", 37 | "After 201 steps,the loss is 1.187238\n", 38 | "After 301 steps,the loss is 0.669913\n", 39 | "After 401 steps,the loss is 0.847421\n", 40 | "After 501 steps,the loss is 0.747313\n", 41 | "After 601 steps,the loss is 0.680314\n", 42 | "After 701 steps,the loss is 0.667357\n", 43 | "After 801 steps,the loss is 1.055429\n", 44 | "After 901 steps,the loss is 0.640755\n", 45 | "After 1001 steps,the loss is 0.673610\n", 46 | "After 1101 steps,the loss is 0.676126\n", 47 | "After 1201 steps,the loss is 0.718006\n", 48 | "After 1301 steps,the loss is 1.066090\n", 49 | "After 1401 steps,the loss is 0.727668\n", 50 | "After 1501 steps,the loss is 0.629173\n", 51 | "After 1601 steps,the loss is 0.752331\n", 52 | "After 1701 steps,the loss is 0.858797\n", 53 | "After 1801 steps,the loss is 0.644868\n", 54 | "After 1901 steps,the loss is 0.743451\n", 55 | "After 2001 steps,the loss is 0.659485\n", 56 | "After 2101 steps,the loss is 0.623510\n", 57 | "After 2201 steps,the loss is 0.649839\n", 58 | "After 2301 steps,the loss is 0.703313\n", 59 | "After 2401 steps,the loss is 0.630919\n", 60 | "After 2501 steps,the loss is 1.053113\n", 61 | "After 2601 steps,the loss is 0.619624\n", 62 | "After 2701 steps,the loss is 0.626574\n", 63 | "After 2801 steps,the loss is 0.657032\n", 64 | "After 2901 steps,the loss is 0.614425\n", 65 | "After 3001 steps,the loss is 0.978504\n", 66 | "After 3101 steps,the loss is 0.625567\n", 67 | "After 3201 steps,the loss is 0.618891\n", 68 | "After 3301 steps,the loss is 0.608601\n", 69 | "After 3401 steps,the loss is 0.620591\n", 70 | "After 3501 steps,the loss is 0.700604\n", 71 | "After 3601 steps,the loss is 0.766301\n", 72 | "After 3701 steps,the loss is 0.684956\n", 73 | "After 3801 steps,the loss is 0.909017\n", 74 | "After 3901 steps,the loss is 0.610638\n", 75 | "After 4001 steps,the loss is 0.601208\n", 76 | "After 4101 steps,the loss is 0.601578\n", 77 | "After 4201 steps,the loss is 0.597598\n", 78 | "After 4301 steps,the loss is 0.612275\n", 79 | "After 4401 steps,the loss is 0.627701\n", 80 | "After 4501 steps,the loss is 0.594224\n", 81 | "After 4601 steps,the loss is 0.844018\n", 82 | "After 4701 steps,the loss is 0.610324\n", 83 | "After 4801 steps,the loss is 0.717005\n", 84 | "After 4901 steps,the loss is 0.590796\n", 85 | "After 5001 steps,the loss is 0.593007\n", 86 | "After 5101 steps,the loss is 0.599946\n", 87 | "After 5201 steps,the loss is 0.643231\n", 88 | "After 5301 steps,the loss is 0.617860\n", 89 | "After 5401 steps,the loss is 0.591883\n", 90 | "After 5501 steps,the loss is 0.589774\n", 91 | "After 5601 steps,the loss is 0.588353\n", 92 | "After 5701 steps,the loss is 0.588788\n", 93 | "After 5801 steps,the loss is 0.582323\n", 94 | "After 5901 steps,the loss is 0.723198\n", 95 | "After 6001 steps,the loss is 0.606674\n", 96 | "After 6101 steps,the loss is 0.682886\n", 97 | "After 6201 steps,the loss is 0.580085\n", 98 | "After 6301 steps,the loss is 0.600956\n", 99 | "After 6401 steps,the loss is 0.576601\n", 100 | "After 6501 steps,the loss is 0.573657\n", 101 | "After 6601 steps,the loss is 0.827755\n", 102 | "After 6701 steps,the loss is 1.303749\n", 103 | "After 6801 steps,the loss is 0.571762\n", 104 | "After 6901 steps,the loss is 0.610755\n", 105 | "After 7001 steps,the loss is 0.640113\n", 106 | "After 7101 steps,the loss is 0.569128\n", 107 | "After 7201 steps,the loss is 0.566080\n", 108 | "After 7301 steps,the loss is 0.564210\n", 109 | "After 7401 steps,the loss is 0.563264\n", 110 | "After 7501 steps,the loss is 0.585070\n", 111 | "After 7601 steps,the loss is 0.561940\n", 112 | "After 7701 steps,the loss is 0.560033\n", 113 | "After 7801 steps,the loss is 0.562185\n", 114 | "After 7901 steps,the loss is 0.558051\n", 115 | "After 8001 steps,the loss is 0.556984\n", 116 | "After 8101 steps,the loss is 0.556139\n", 117 | "After 8201 steps,the loss is 0.577099\n", 118 | "After 8301 steps,the loss is 0.554054\n", 119 | "After 8401 steps,the loss is 0.555307\n", 120 | "After 8501 steps,the loss is 0.551864\n", 121 | "After 8601 steps,the loss is 0.551032\n", 122 | "After 8701 steps,the loss is 0.553207\n", 123 | "After 8801 steps,the loss is 0.578730\n", 124 | "After 8901 steps,the loss is 0.733932\n", 125 | "After 9001 steps,the loss is 0.548291\n", 126 | "After 9101 steps,the loss is 0.547286\n", 127 | "After 9201 steps,the loss is 0.555260\n", 128 | "After 9301 steps,the loss is 0.544677\n", 129 | "After 9401 steps,the loss is 0.546517\n", 130 | "After 9501 steps,the loss is 0.554930\n", 131 | "After 9601 steps,the loss is 0.584289\n", 132 | "After 9701 steps,the loss is 0.554965\n", 133 | "After 9801 steps,the loss is 0.553989\n", 134 | "After 9901 steps,the loss is 0.540608\n", 135 | "After 10001 steps,the loss is 0.554199\n", 136 | "After 10101 steps,the loss is 0.535904\n", 137 | "After 10201 steps,the loss is 0.534765\n", 138 | "After 10301 steps,the loss is 0.566554\n", 139 | "After 10401 steps,the loss is 0.533440\n", 140 | "After 10501 steps,the loss is 0.538789\n", 141 | "After 10601 steps,the loss is 0.531668\n", 142 | "After 10701 steps,the loss is 0.530395\n", 143 | "After 10801 steps,the loss is 0.529498\n", 144 | "After 10901 steps,the loss is 0.527991\n", 145 | "After 11001 steps,the loss is 0.528321\n", 146 | "After 11101 steps,the loss is 0.526199\n", 147 | "After 11201 steps,the loss is 0.628311\n", 148 | "After 11301 steps,the loss is 0.526717\n", 149 | "After 11401 steps,the loss is 0.588199\n", 150 | "After 11501 steps,the loss is 0.523339\n", 151 | "After 11601 steps,the loss is 0.521923\n", 152 | "After 11701 steps,the loss is 0.520764\n", 153 | "After 11801 steps,the loss is 0.534810\n", 154 | "After 11901 steps,the loss is 0.530664\n", 155 | "After 12001 steps,the loss is 0.526832\n", 156 | "After 12101 steps,the loss is 0.519265\n", 157 | "After 12201 steps,the loss is 0.534826\n", 158 | "After 12301 steps,the loss is 0.525652\n", 159 | "After 12401 steps,the loss is 0.516336\n", 160 | "After 12501 steps,the loss is 0.549207\n", 161 | "After 12601 steps,the loss is 0.512329\n", 162 | "After 12701 steps,the loss is 0.510963\n", 163 | "After 12801 steps,the loss is 0.573346\n", 164 | "After 12901 steps,the loss is 0.514642\n", 165 | "After 13001 steps,the loss is 0.608858\n", 166 | "After 13101 steps,the loss is 0.509337\n", 167 | "After 13201 steps,the loss is 0.800575\n", 168 | "After 13301 steps,the loss is 0.506083\n", 169 | "After 13401 steps,the loss is 0.683283\n", 170 | "After 13501 steps,the loss is 0.503503\n", 171 | "After 13601 steps,the loss is 0.622734\n", 172 | "After 13701 steps,the loss is 0.501739\n", 173 | "After 13801 steps,the loss is 0.502209\n", 174 | "After 13901 steps,the loss is 0.500681\n", 175 | "After 14001 steps,the loss is 0.513294\n", 176 | "After 14101 steps,the loss is 0.499554\n", 177 | "After 14201 steps,the loss is 0.497046\n", 178 | "After 14301 steps,the loss is 0.497310\n", 179 | "After 14401 steps,the loss is 0.499418\n", 180 | "After 14501 steps,the loss is 1.235166\n", 181 | "After 14601 steps,the loss is 0.494440\n", 182 | "After 14701 steps,the loss is 0.495551\n", 183 | "After 14801 steps,the loss is 0.492160\n", 184 | "After 14901 steps,the loss is 0.528410\n", 185 | "After 15001 steps,the loss is 0.489849\n", 186 | "After 15101 steps,the loss is 0.495805\n", 187 | "After 15201 steps,the loss is 0.541001\n", 188 | "After 15301 steps,the loss is 0.490161\n", 189 | "After 15401 steps,the loss is 0.486616\n", 190 | "After 15501 steps,the loss is 0.485845\n" 191 | ] 192 | }, 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "After 15601 steps,the loss is 0.484530\n", 198 | "After 15701 steps,the loss is 0.483706\n", 199 | "After 15801 steps,the loss is 0.484086\n", 200 | "After 15901 steps,the loss is 0.481948\n", 201 | "After 16001 steps,the loss is 0.481106\n", 202 | "After 16101 steps,the loss is 0.486761\n", 203 | "After 16201 steps,the loss is 0.481790\n", 204 | "After 16301 steps,the loss is 0.478480\n", 205 | "After 16401 steps,the loss is 0.478640\n", 206 | "After 16501 steps,the loss is 0.476802\n", 207 | "After 16601 steps,the loss is 0.475985\n", 208 | "After 16701 steps,the loss is 0.475035\n", 209 | "After 16801 steps,the loss is 0.714575\n", 210 | "After 16901 steps,the loss is 0.474666\n", 211 | "After 17001 steps,the loss is 0.472400\n", 212 | "After 17101 steps,the loss is 0.471576\n", 213 | "After 17201 steps,the loss is 0.471715\n", 214 | "After 17301 steps,the loss is 0.469967\n", 215 | "After 17401 steps,the loss is 0.469257\n", 216 | "After 17501 steps,the loss is 0.468196\n", 217 | "After 17601 steps,the loss is 0.467872\n", 218 | "After 17701 steps,the loss is 0.466692\n", 219 | "After 17801 steps,the loss is 0.472051\n", 220 | "After 17901 steps,the loss is 0.540070\n", 221 | "After 18001 steps,the loss is 0.463912\n", 222 | "After 18101 steps,the loss is 0.463043\n", 223 | "After 18201 steps,the loss is 0.462325\n", 224 | "After 18301 steps,the loss is 0.470149\n", 225 | "After 18401 steps,the loss is 0.461545\n", 226 | "After 18501 steps,the loss is 0.459744\n", 227 | "After 18601 steps,the loss is 0.458973\n", 228 | "After 18701 steps,the loss is 0.473234\n", 229 | "After 18801 steps,the loss is 0.457377\n", 230 | "After 18901 steps,the loss is 0.456445\n", 231 | "After 19001 steps,the loss is 0.456711\n", 232 | "After 19101 steps,the loss is 0.454847\n", 233 | "After 19201 steps,the loss is 0.454143\n", 234 | "After 19301 steps,the loss is 0.805508\n", 235 | "After 19401 steps,the loss is 0.453425\n", 236 | "After 19501 steps,the loss is 0.457488\n", 237 | "After 19601 steps,the loss is 0.630311\n", 238 | "After 19701 steps,the loss is 0.451499\n", 239 | "After 19801 steps,the loss is 0.648680\n", 240 | "After 19901 steps,the loss is 0.448252\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "import tensorflow as tf\n", 246 | "from tensorflow.examples.tutorials.mnist import input_data\n", 247 | "import mnist_lenet_forward\n", 248 | "import numpy as np\n", 249 | "import os\n", 250 | "\n", 251 | "REGULARIZER = 0.0001\n", 252 | "BATCH_SIZE = 16\n", 253 | "LEARNING_RATE_BASE = 0.1\n", 254 | "LEARNING_RATE_DECAY = 0.99\n", 255 | "MOVING_AVERAGE_DECAY = 0.99\n", 256 | "MODEL_SAVE_PATH = \"./lenet_model/\"\n", 257 | "MODEL_NAME = 'lenet'\n", 258 | "STEPS = 20000\n", 259 | "\n", 260 | "\n", 261 | "def backward(mnist):\n", 262 | " x = tf.placeholder(tf.float32,[\n", 263 | " BATCH_SIZE,\n", 264 | " mnist_lenet_forward.IMAGE_SIZE,\n", 265 | " mnist_lenet_forward.IMAGE_SIZE,\n", 266 | " mnist_lenet_forward.NUM_CHANNELS\n", 267 | " ])\n", 268 | " y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE])\n", 269 | " \n", 270 | " y = mnist_lenet_forward.forward(x,True, regularizer=REGULARIZER)\n", 271 | " \n", 272 | " global_step = tf.Variable(0,trainable=False)\n", 273 | " \n", 274 | " ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1))\n", 275 | " cem = tf.reduce_mean(ce)\n", 276 | " loss = cem + tf.add_n(tf.get_collection(\"losses\"))\n", 277 | " \n", 278 | " learning_rate = tf.train.exponential_decay(\n", 279 | " LEARNING_RATE_BASE,\n", 280 | " global_step,\n", 281 | " mnist.train.num_examples/BATCH_SIZE,\n", 282 | " LEARNING_RATE_DECAY\n", 283 | " )\n", 284 | " train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step)\n", 285 | " ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)\n", 286 | " ema_op = ema.apply(tf.trainable_variables())\n", 287 | " with tf.control_dependencies([train_step,ema_op]):\n", 288 | " train_op = tf.no_op(name=\"train\")\n", 289 | " \n", 290 | " saver = tf.train.Saver()\n", 291 | " \n", 292 | " with tf.Session() as sess:\n", 293 | " init_op = tf.global_variables_initializer()\n", 294 | " sess.run(init_op)\n", 295 | " \n", 296 | " ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)\n", 297 | " if ckpt and ckpt.model_checkpoint_path:\n", 298 | " save.restore(sess,ckpt.model_checkpoint_path)\n", 299 | " \n", 300 | " for i in range(STEPS):\n", 301 | " xs, ys = mnist.train.next_batch(BATCH_SIZE)\n", 302 | " reshape_xs = np.reshape(xs,(BATCH_SIZE,\n", 303 | " mnist_lenet_forward.IMAGE_SIZE,\n", 304 | " mnist_lenet_forward.IMAGE_SIZE,\n", 305 | " mnist_lenet_forward.NUM_CHANNELS))\n", 306 | " _, loss_value, step, = sess.run([train_op,loss,global_step],feed_dict={x:reshape_xs,y_:ys} )\n", 307 | " if i % 100 == 0:\n", 308 | " print(\"After %d steps,the loss is %f\"%(step,loss_value))\n", 309 | " saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step)\n", 310 | " \n", 311 | " \n", 312 | "def main():\n", 313 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot=True)\n", 314 | " backward(mnist)\n", 315 | " \n", 316 | "if __name__ == \"__main__\":\n", 317 | " main()\n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " " 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | } 331 | ], 332 | "metadata": { 333 | "kernelspec": { 334 | "display_name": "Python 3", 335 | "language": "python", 336 | "name": "python3" 337 | }, 338 | "language_info": { 339 | "codemirror_mode": { 340 | "name": "ipython", 341 | "version": 3 342 | }, 343 | "file_extension": ".py", 344 | "mimetype": "text/x-python", 345 | "name": "python", 346 | "nbconvert_exporter": "python", 347 | "pygments_lexer": "ipython3", 348 | "version": "3.5.5" 349 | }, 350 | "widgets": { 351 | "state": {}, 352 | "version": "1.1.2" 353 | } 354 | }, 355 | "nbformat": 4, 356 | "nbformat_minor": 2 357 | } 358 | -------------------------------------------------------------------------------- /mnist_lenet/mnist_lenet_backward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | import mnist_lenet_forward 4 | import numpy as np 5 | import os 6 | 7 | REGULARIZER = 0.0001 8 | BATCH_SIZE = 100 9 | LEARNING_RATE_BASE = 0.1 10 | LEARNING_RATE_DECAY = 0.99 11 | MOVING_AVERAGE_DECAY = 0.99 12 | MODEL_SAVE_PATH = "./lenet_model/" 13 | MODEL_NAME = 'lenet' 14 | STEPS = 20000 15 | 16 | 17 | def backward(mnist): 18 | x = tf.placeholder(tf.float32,[ 19 | BATCH_SIZE, 20 | mnist_lenet_forward.IMAGE_SIZE, 21 | mnist_lenet_forward.IMAGE_SIZE, 22 | mnist_lenet_forward.NUM_CHANNELS 23 | ]) 24 | y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE]) 25 | 26 | y = mnist_lenet_forward.forward(x,True, regularizer=REGULARIZER) 27 | 28 | global_step = tf.Variable(0,trainable=False) 29 | 30 | ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1)) 31 | cem = tf.reduce_mean(ce) 32 | loss = cem + tf.add_n(tf.get_collection("losses")) 33 | 34 | learning_rate = tf.train.exponential_decay( 35 | LEARNING_RATE_BASE, 36 | global_step, 37 | mnist.train.num_examples/BATCH_SIZE, 38 | LEARNING_RATE_DECAY 39 | ) 40 | train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step) 41 | ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) 42 | ema_op = ema.apply(tf.trainable_variables()) 43 | with tf.control_dependencies([train_step,ema_op]): 44 | train_op = tf.no_op(name="train") 45 | 46 | saver = tf.train.Saver() 47 | 48 | with tf.Session() as sess: 49 | init_op = tf.global_variables_initializer() 50 | sess.run(init_op) 51 | 52 | ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) 53 | if ckpt and ckpt.model_checkpoint_path: 54 | save.restore(sess,ckpt.model_checkpoint_path) 55 | 56 | for i in range(STEPS): 57 | xs, ys = mnist.train.next_batch(BATCH_SIZE) 58 | reshape_xs = np.reshape(xs,(BATCH_SIZE, 59 | mnist_lenet_forward.IMAGE_SIZE, 60 | mnist_lenet_forward.IMAGE_SIZE, 61 | mnist_lenet_forward.NUM_CHANNELS)) 62 | _, loss_value, step, = sess.run([train_op,loss,global_step],feed_dict={x:reshape_xs, y_:ys} ) 63 | if i % 100 == 0: 64 | print("After %d steps,the loss is %f"%(step,loss_value)) 65 | saver.save(sess, os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step) 66 | 67 | 68 | def main(): 69 | mnist = input_data.read_data_sets("./MNIST_data/",one_hot=True) 70 | backward(mnist) 71 | 72 | if __name__ == "__main__": 73 | main() 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /mnist_lenet/mnist_lenet_forward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "IMAGE_SIZE = 28\n", 11 | "NUM_CHANNELS = 1\n", 12 | "CONV1_SIZE = 5\n", 13 | "CONV1_KERNEL_NUM = 32\n", 14 | "CONV2_SIZE = 5\n", 15 | "CONV2_KERNEL_NUM = 64\n", 16 | "FC_SIZE = 512\n", 17 | "OUTPUT_SIZE = 10\n", 18 | "\n", 19 | "def get_weight(shape, regularizer):\n", 20 | " w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))\n", 21 | "# print(\"w:\",sess.run(w))\n", 22 | " if regularizer!=None:\n", 23 | " tf.add_to_collection(\"losses\",tf.contrib.layers.l2_regularizer(regularizer)(w))\n", 24 | " return w\n", 25 | " \n", 26 | " \n", 27 | "def get_bias(shape):\n", 28 | " b = tf.Variable(tf.zeros(shape))\n", 29 | " return b\n", 30 | "\n", 31 | "def conv2d(x,w):\n", 32 | " return tf.nn.conv2d(x,w,[1,1,1,1],padding=\"SAME\")\n", 33 | "\n", 34 | "def max_pool_2x2(x):\n", 35 | " return tf.nn.max_pool(x, ksize=[1,2,2,1],strides = [1,2,2,1] ,padding = \"SAME\")\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "def forward(x,train,regularizer):\n", 45 | " conv1_w = get_weight([CONV1_SIZE,CONV1_SIZE,NUM_CHANNELS,CONV1_KERNEL_NUM],regularizer=0.0001)\n", 46 | " conv1_b = get_bias([CONV1_KERNEL_NUM])\n", 47 | " conv1 = conv2d(x,conv1_w)\n", 48 | " relu1 = tf.nn.relu(tf.nn.bias(conv1,conv1_b))\n", 49 | " pool1 = max_pool_2x2(relu1)\n", 50 | " \n", 51 | " conv2_w = get_weight([CONV2_SIZE,CONV2_SIZE,CONV1_KERNEL_NUM,CONV2_KERNEL_NUM],regularizer=0.0001)\n", 52 | " conv2_b = get_bias([CONV2_KERNEL_NUM])\n", 53 | " conv2 = conv2d(pool1,conv2_w)\n", 54 | " relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_b))\n", 55 | " pool2 = max_pool_2x2(relu2)\n", 56 | " \n", 57 | " pool_shape = pool2.get_shape().as_list()\n", 58 | " #pool_shape[0]是一个batch的值\n", 59 | " print(\"pool_shape[0]:\",pool_shape[0])\n", 60 | " nodes = pool_shape[1]* pool_shape[2] * pool_shape[3]\n", 61 | " reshaped = tf.reshape(pool2,[pool_shape[0],nodes])\n", 62 | " \n", 63 | " fc1_w = get_weight([nodes,FC_SIZE],regularizer=0.0001)\n", 64 | " fc1_b = get_bias([FC_SIZE])\n", 65 | " fc1 = tf.nn.relu(tf.matmul(reshape,fc1_w)+fcl_b)\n", 66 | " if train: fcl = tf.nn.dropout(fc1,0.5)\n", 67 | " \n", 68 | " fc2_w = get_weight([FC_SIZE,10],regularizer=0.0001)\n", 69 | " fc2_b = get_bias([10])\n", 70 | " y = tf.matmul(fc1,fc2_w)+fc2_b\n", 71 | " return y\n", 72 | " \n" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "\n", 82 | "\n", 83 | "\n", 84 | "\n", 85 | "\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "def main():\n", 95 | " shape = IMAGE_SIZE\n", 96 | " w = get_weight(shape,0)\n", 97 | " \n", 98 | " with tf.Session() as sess:\n", 99 | " print(\"w:\",sess.run(w))\n", 100 | " \n", 101 | "if __name__ == \"__main__\":\n", 102 | " main()" 103 | ] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.5.5" 123 | }, 124 | "widgets": { 125 | "state": {}, 126 | "version": "1.1.2" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 2 131 | } 132 | -------------------------------------------------------------------------------- /mnist_lenet/mnist_lenet_forward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | IMAGE_SIZE = 28 3 | NUM_CHANNELS = 1 4 | CONV1_SIZE = 5 5 | CONV1_KERNEL_NUM = 32 6 | CONV2_SIZE = 5 7 | CONV2_KERNEL_NUM = 64 8 | FC_SIZE = 512 9 | OUTPUT_SIZE = 10 10 | 11 | def get_weight(shape, regularizer): 12 | w = tf.Variable(tf.truncated_normal(shape,stddev=0.1)) 13 | # print("w:",sess.run(w)) 14 | if regularizer!=None: 15 | tf.add_to_collection("losses",tf.contrib.layers.l2_regularizer(regularizer)(w)) 16 | return w 17 | 18 | 19 | def get_bias(shape): 20 | b = tf.Variable(tf.zeros(shape)) 21 | return b 22 | 23 | def conv2d(x,w): 24 | return tf.nn.conv2d(x,w,[1,1,1,1],padding="SAME") 25 | 26 | def max_pool_2x2(x): 27 | return tf.nn.max_pool(x, ksize=[1,2,2,1] ,strides = [1,2,2,1],padding = "SAME") 28 | 29 | def forward(x,train,regularizer): 30 | conv1_w = get_weight([CONV1_SIZE,CONV1_SIZE,NUM_CHANNELS,CONV1_KERNEL_NUM],regularizer=0.0001) 31 | conv1_b = get_bias([CONV1_KERNEL_NUM]) 32 | conv1 = conv2d(x,conv1_w) 33 | relu1 = tf.nn.relu(tf.nn.bias_add(conv1,conv1_b)) 34 | pool1 = max_pool_2x2(relu1) 35 | 36 | conv2_w = get_weight([CONV2_SIZE,CONV2_SIZE,CONV1_KERNEL_NUM,CONV2_KERNEL_NUM],regularizer=0.0001) 37 | conv2_b = get_bias([CONV2_KERNEL_NUM]) 38 | conv2 = conv2d(pool1,conv2_w) 39 | relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_b)) 40 | pool2 = max_pool_2x2(relu2) 41 | 42 | pool_shape = pool2.get_shape().as_list() 43 | #pool_shape[0]是一个batch的值 44 | print("pool_shape[0]:",pool_shape[0]) 45 | nodes = pool_shape[1]* pool_shape[2] * pool_shape[3] 46 | reshaped = tf.reshape(pool2,[pool_shape[0],nodes]) 47 | 48 | fc1_w = get_weight([nodes,FC_SIZE],regularizer=0.0001) 49 | fc1_b = get_bias([FC_SIZE]) 50 | fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_w)+fc1_b) 51 | if train: fcl = tf.nn.dropout(fc1,0.5) 52 | 53 | fc2_w = get_weight([FC_SIZE,10],regularizer=0.0001) 54 | fc2_b = get_bias([10]) 55 | y = tf.matmul(fc1,fc2_w)+fc2_b 56 | return y -------------------------------------------------------------------------------- /mnist_lenet/mnist_lenet_test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 13 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 14 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 15 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 16 | "pool_shape[0]: 100\n", 17 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 18 | "global_step: 19901\n", 19 | "After 19901 training step(s). test accuracy = 1.000000\n", 20 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 21 | "global_step: 19901\n", 22 | "After 19901 training step(s). test accuracy = 1.000000\n", 23 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 24 | "global_step: 19901\n", 25 | "After 19901 training step(s). test accuracy = 1.000000\n", 26 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 27 | "global_step: 19901\n", 28 | "After 19901 training step(s). test accuracy = 0.990000\n", 29 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 30 | "global_step: 19901\n", 31 | "After 19901 training step(s). test accuracy = 0.980000\n", 32 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 33 | "global_step: 19901\n", 34 | "After 19901 training step(s). test accuracy = 0.990000\n", 35 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 36 | "global_step: 19901\n", 37 | "After 19901 training step(s). test accuracy = 1.000000\n", 38 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 39 | "global_step: 19901\n", 40 | "After 19901 training step(s). test accuracy = 1.000000\n", 41 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 42 | "global_step: 19901\n", 43 | "After 19901 training step(s). test accuracy = 1.000000\n", 44 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 45 | "global_step: 19901\n", 46 | "After 19901 training step(s). test accuracy = 0.990000\n", 47 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 48 | "global_step: 19901\n", 49 | "After 19901 training step(s). test accuracy = 1.000000\n", 50 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 51 | "global_step: 19901\n", 52 | "After 19901 training step(s). test accuracy = 0.990000\n", 53 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 54 | "global_step: 19901\n", 55 | "After 19901 training step(s). test accuracy = 0.990000\n", 56 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 57 | "global_step: 19901\n", 58 | "After 19901 training step(s). test accuracy = 0.980000\n", 59 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 60 | "global_step: 19901\n", 61 | "After 19901 training step(s). test accuracy = 1.000000\n", 62 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 63 | "global_step: 19901\n", 64 | "After 19901 training step(s). test accuracy = 1.000000\n", 65 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 66 | "global_step: 19901\n", 67 | "After 19901 training step(s). test accuracy = 0.970000\n", 68 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 69 | "global_step: 19901\n", 70 | "After 19901 training step(s). test accuracy = 1.000000\n", 71 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 72 | "global_step: 19901\n", 73 | "After 19901 training step(s). test accuracy = 0.990000\n", 74 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 75 | "global_step: 19901\n", 76 | "After 19901 training step(s). test accuracy = 1.000000\n", 77 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 78 | "global_step: 19901\n", 79 | "After 19901 training step(s). test accuracy = 0.980000\n", 80 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 81 | "global_step: 19901\n", 82 | "After 19901 training step(s). test accuracy = 0.990000\n", 83 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 84 | "global_step: 19901\n", 85 | "After 19901 training step(s). test accuracy = 1.000000\n", 86 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 87 | "global_step: 19901\n", 88 | "After 19901 training step(s). test accuracy = 1.000000\n", 89 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 90 | "global_step: 19901\n", 91 | "After 19901 training step(s). test accuracy = 0.990000\n", 92 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 93 | "global_step: 19901\n", 94 | "After 19901 training step(s). test accuracy = 0.980000\n", 95 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 96 | "global_step: 19901\n", 97 | "After 19901 training step(s). test accuracy = 0.990000\n", 98 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 99 | "global_step: 19901\n", 100 | "After 19901 training step(s). test accuracy = 0.990000\n", 101 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 102 | "global_step: 19901\n", 103 | "After 19901 training step(s). test accuracy = 1.000000\n", 104 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 105 | "global_step: 19901\n", 106 | "After 19901 training step(s). test accuracy = 0.990000\n", 107 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 108 | "global_step: 19901\n", 109 | "After 19901 training step(s). test accuracy = 0.990000\n", 110 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 111 | "global_step: 19901\n", 112 | "After 19901 training step(s). test accuracy = 0.990000\n", 113 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 114 | "global_step: 19901\n", 115 | "After 19901 training step(s). test accuracy = 1.000000\n", 116 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 117 | "global_step: 19901\n", 118 | "After 19901 training step(s). test accuracy = 0.970000\n", 119 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 120 | "global_step: 19901\n", 121 | "After 19901 training step(s). test accuracy = 0.960000\n", 122 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 123 | "global_step: 19901\n", 124 | "After 19901 training step(s). test accuracy = 1.000000\n", 125 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 126 | "global_step: 19901\n", 127 | "After 19901 training step(s). test accuracy = 0.990000\n", 128 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 129 | "global_step: 19901\n", 130 | "After 19901 training step(s). test accuracy = 0.990000\n", 131 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 132 | "global_step: 19901\n", 133 | "After 19901 training step(s). test accuracy = 1.000000\n", 134 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 135 | "global_step: 19901\n", 136 | "After 19901 training step(s). test accuracy = 1.000000\n", 137 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 138 | "global_step: 19901\n", 139 | "After 19901 training step(s). test accuracy = 1.000000\n", 140 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 141 | "global_step: 19901\n", 142 | "After 19901 training step(s). test accuracy = 1.000000\n", 143 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 144 | "global_step: 19901\n", 145 | "After 19901 training step(s). test accuracy = 1.000000\n", 146 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 147 | "global_step: 19901\n", 148 | "After 19901 training step(s). test accuracy = 0.980000\n", 149 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 150 | "global_step: 19901\n", 151 | "After 19901 training step(s). test accuracy = 0.990000\n", 152 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 153 | "global_step: 19901\n", 154 | "After 19901 training step(s). test accuracy = 1.000000\n", 155 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 156 | "global_step: 19901\n", 157 | "After 19901 training step(s). test accuracy = 1.000000\n", 158 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 159 | "global_step: 19901\n", 160 | "After 19901 training step(s). test accuracy = 0.990000\n", 161 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 162 | "global_step: 19901\n", 163 | "After 19901 training step(s). test accuracy = 0.980000\n", 164 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 165 | "global_step: 19901\n", 166 | "After 19901 training step(s). test accuracy = 1.000000\n", 167 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 168 | "global_step: 19901\n", 169 | "After 19901 training step(s). test accuracy = 0.990000\n", 170 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 171 | "global_step: 19901\n", 172 | "After 19901 training step(s). test accuracy = 0.970000\n", 173 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 174 | "global_step: 19901\n", 175 | "After 19901 training step(s). test accuracy = 0.990000\n", 176 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 177 | "global_step: 19901\n", 178 | "After 19901 training step(s). test accuracy = 0.990000\n", 179 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 180 | "global_step: 19901\n", 181 | "After 19901 training step(s). test accuracy = 1.000000\n", 182 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 183 | "global_step: 19901\n", 184 | "After 19901 training step(s). test accuracy = 1.000000\n", 185 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n" 186 | ] 187 | }, 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "global_step: 19901\n", 193 | "After 19901 training step(s). test accuracy = 1.000000\n", 194 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 195 | "global_step: 19901\n", 196 | "After 19901 training step(s). test accuracy = 0.990000\n", 197 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 198 | "global_step: 19901\n", 199 | "After 19901 training step(s). test accuracy = 0.990000\n", 200 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 201 | "global_step: 19901\n", 202 | "After 19901 training step(s). test accuracy = 0.990000\n", 203 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 204 | "global_step: 19901\n", 205 | "After 19901 training step(s). test accuracy = 1.000000\n", 206 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 207 | "global_step: 19901\n", 208 | "After 19901 training step(s). test accuracy = 0.990000\n", 209 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 210 | "global_step: 19901\n", 211 | "After 19901 training step(s). test accuracy = 0.980000\n", 212 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 213 | "global_step: 19901\n", 214 | "After 19901 training step(s). test accuracy = 1.000000\n", 215 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 216 | "global_step: 19901\n", 217 | "After 19901 training step(s). test accuracy = 1.000000\n", 218 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 219 | "global_step: 19901\n", 220 | "After 19901 training step(s). test accuracy = 0.990000\n", 221 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 222 | "global_step: 19901\n", 223 | "After 19901 training step(s). test accuracy = 0.990000\n", 224 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 225 | "global_step: 19901\n", 226 | "After 19901 training step(s). test accuracy = 0.990000\n", 227 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 228 | "global_step: 19901\n", 229 | "After 19901 training step(s). test accuracy = 0.990000\n", 230 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 231 | "global_step: 19901\n", 232 | "After 19901 training step(s). test accuracy = 0.990000\n", 233 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 234 | "global_step: 19901\n", 235 | "After 19901 training step(s). test accuracy = 1.000000\n", 236 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 237 | "global_step: 19901\n", 238 | "After 19901 training step(s). test accuracy = 0.980000\n", 239 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 240 | "global_step: 19901\n", 241 | "After 19901 training step(s). test accuracy = 0.990000\n", 242 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 243 | "global_step: 19901\n", 244 | "After 19901 training step(s). test accuracy = 1.000000\n", 245 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 246 | "global_step: 19901\n", 247 | "After 19901 training step(s). test accuracy = 0.990000\n", 248 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 249 | "global_step: 19901\n", 250 | "After 19901 training step(s). test accuracy = 0.990000\n", 251 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 252 | "global_step: 19901\n", 253 | "After 19901 training step(s). test accuracy = 1.000000\n", 254 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 255 | "global_step: 19901\n", 256 | "After 19901 training step(s). test accuracy = 0.990000\n", 257 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 258 | "global_step: 19901\n", 259 | "After 19901 training step(s). test accuracy = 1.000000\n", 260 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 261 | "global_step: 19901\n", 262 | "After 19901 training step(s). test accuracy = 1.000000\n", 263 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 264 | "global_step: 19901\n", 265 | "After 19901 training step(s). test accuracy = 0.990000\n", 266 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 267 | "global_step: 19901\n", 268 | "After 19901 training step(s). test accuracy = 1.000000\n", 269 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 270 | "global_step: 19901\n", 271 | "After 19901 training step(s). test accuracy = 0.980000\n", 272 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 273 | "global_step: 19901\n", 274 | "After 19901 training step(s). test accuracy = 0.980000\n", 275 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 276 | "global_step: 19901\n", 277 | "After 19901 training step(s). test accuracy = 0.990000\n", 278 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 279 | "global_step: 19901\n", 280 | "After 19901 training step(s). test accuracy = 0.980000\n", 281 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 282 | "global_step: 19901\n", 283 | "After 19901 training step(s). test accuracy = 1.000000\n", 284 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 285 | "global_step: 19901\n", 286 | "After 19901 training step(s). test accuracy = 0.980000\n", 287 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 288 | "global_step: 19901\n", 289 | "After 19901 training step(s). test accuracy = 1.000000\n", 290 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 291 | "global_step: 19901\n", 292 | "After 19901 training step(s). test accuracy = 1.000000\n", 293 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 294 | "global_step: 19901\n", 295 | "After 19901 training step(s). test accuracy = 1.000000\n", 296 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 297 | "global_step: 19901\n", 298 | "After 19901 training step(s). test accuracy = 1.000000\n", 299 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 300 | "global_step: 19901\n", 301 | "After 19901 training step(s). test accuracy = 1.000000\n", 302 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 303 | "global_step: 19901\n", 304 | "After 19901 training step(s). test accuracy = 0.980000\n", 305 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 306 | "global_step: 19901\n", 307 | "After 19901 training step(s). test accuracy = 1.000000\n", 308 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 309 | "global_step: 19901\n", 310 | "After 19901 training step(s). test accuracy = 1.000000\n", 311 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 312 | "global_step: 19901\n", 313 | "After 19901 training step(s). test accuracy = 1.000000\n", 314 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 315 | "global_step: 19901\n", 316 | "After 19901 training step(s). test accuracy = 0.990000\n", 317 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 318 | "global_step: 19901\n", 319 | "After 19901 training step(s). test accuracy = 1.000000\n", 320 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 321 | "global_step: 19901\n", 322 | "After 19901 training step(s). test accuracy = 1.000000\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "import time\n", 328 | "import numpy as np\n", 329 | "import tensorflow as tf\n", 330 | "from tensorflow.examples.tutorials.mnist import input_data\n", 331 | "import mnist_lenet_backward\n", 332 | "import mnist_lenet_forward\n", 333 | "\n", 334 | "def test(mnist):\n", 335 | " with tf.Graph().as_default() as g:\n", 336 | " x = tf.placeholder(tf.float32,[mnist_lenet_backward.BATCH_SIZE,\n", 337 | " mnist_lenet_forward.IMAGE_SIZE,\n", 338 | " mnist_lenet_forward.IMAGE_SIZE,\n", 339 | " mnist_lenet_forward.NUM_CHANNELS])\n", 340 | " y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE])\n", 341 | " y = mnist_lenet_forward.forward(x,False,None)\n", 342 | " \n", 343 | " ema = tf.train.ExponentialMovingAverage(mnist_lenet_backward.MOVING_AVERAGE_DECAY)\n", 344 | " ema_restore = ema.variables_to_restore()\n", 345 | " saver = tf.train.Saver(ema_restore)\n", 346 | " \n", 347 | " correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n", 348 | " #求平均 \n", 349 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 350 | " j = 0\n", 351 | " while j<100:\n", 352 | " #gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333)\n", 353 | " #with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:\n", 354 | " with tf.Session() as sess:\n", 355 | " ckpt = tf.train.get_checkpoint_state(mnist_lenet_backward.MODEL_SAVE_PATH)\n", 356 | " if ckpt and ckpt.model_checkpoint_path:\n", 357 | " saver.restore(sess, ckpt.model_checkpoint_path)\n", 358 | " global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]\n", 359 | "# print(\"global_step:\",global_step)\n", 360 | " xs, ys = mnist.test.next_batch(mnist_lenet_backward.BATCH_SIZE)\n", 361 | " xs = np.reshape(xs,(mnist_lenet_backward.BATCH_SIZE,\n", 362 | " mnist_lenet_forward.IMAGE_SIZE,\n", 363 | " mnist_lenet_forward.IMAGE_SIZE,\n", 364 | " mnist_lenet_forward.NUM_CHANNELS))\n", 365 | " accuracy_score = sess.run(accuracy,feed_dict = {x: xs, y_: ys})\n", 366 | " print(\"global_step:\",global_step)\n", 367 | " print(\"After %s training step(s). test accuracy = %f\"%(global_step, accuracy_score))\n", 368 | " else:\n", 369 | " print(\"No checkpoint file found\")\n", 370 | " return\n", 371 | "# time.sleep(TEST_INTERVAL_SECS)\n", 372 | " j += 1\n", 373 | "def main():\n", 374 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot = True)\n", 375 | " test(mnist)\n", 376 | " \n", 377 | "if __name__ == \"__main__\":\n", 378 | " main()\n", 379 | " " 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": {}, 386 | "outputs": [], 387 | "source": [] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [] 395 | } 396 | ], 397 | "metadata": { 398 | "celltoolbar": "Raw Cell Format", 399 | "kernelspec": { 400 | "display_name": "Python 3", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.5.5" 415 | }, 416 | "widgets": { 417 | "state": {}, 418 | "version": "1.1.2" 419 | } 420 | }, 421 | "nbformat": 4, 422 | "nbformat_minor": 2 423 | } 424 | -------------------------------------------------------------------------------- /mnist_lenet/none: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /mnist_lenet_backward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "WARNING:tensorflow:From :69: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 13 | "Instructions for updating:\n", 14 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 15 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", 16 | "Instructions for updating:\n", 17 | "Please write your own downloading logic.\n", 18 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 19 | "Instructions for updating:\n", 20 | "Please use tf.data to implement this functionality.\n", 21 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 22 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 23 | "Instructions for updating:\n", 24 | "Please use tf.data to implement this functionality.\n", 25 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 26 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 27 | "Instructions for updating:\n", 28 | "Please use tf.one_hot on tensors.\n", 29 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 31 | "WARNING:tensorflow:From D:\\Users\\lele\\Anaconda3\\envs\\keras\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 32 | "Instructions for updating:\n", 33 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 34 | "pool_shape[0]: 16\n", 35 | "After 1 steps,the loss is 5.175639\n", 36 | "After 101 steps,the loss is 2.093199\n", 37 | "After 201 steps,the loss is 1.187238\n", 38 | "After 301 steps,the loss is 0.669913\n", 39 | "After 401 steps,the loss is 0.847421\n", 40 | "After 501 steps,the loss is 0.747313\n", 41 | "After 601 steps,the loss is 0.680314\n", 42 | "After 701 steps,the loss is 0.667357\n", 43 | "After 801 steps,the loss is 1.055429\n", 44 | "After 901 steps,the loss is 0.640755\n", 45 | "After 1001 steps,the loss is 0.673610\n", 46 | "After 1101 steps,the loss is 0.676126\n", 47 | "After 1201 steps,the loss is 0.718006\n", 48 | "After 1301 steps,the loss is 1.066090\n", 49 | "After 1401 steps,the loss is 0.727668\n", 50 | "After 1501 steps,the loss is 0.629173\n", 51 | "After 1601 steps,the loss is 0.752331\n", 52 | "After 1701 steps,the loss is 0.858797\n", 53 | "After 1801 steps,the loss is 0.644868\n", 54 | "After 1901 steps,the loss is 0.743451\n", 55 | "After 2001 steps,the loss is 0.659485\n", 56 | "After 2101 steps,the loss is 0.623510\n", 57 | "After 2201 steps,the loss is 0.649839\n", 58 | "After 2301 steps,the loss is 0.703313\n", 59 | "After 2401 steps,the loss is 0.630919\n", 60 | "After 2501 steps,the loss is 1.053113\n", 61 | "After 2601 steps,the loss is 0.619624\n", 62 | "After 2701 steps,the loss is 0.626574\n", 63 | "After 2801 steps,the loss is 0.657032\n", 64 | "After 2901 steps,the loss is 0.614425\n", 65 | "After 3001 steps,the loss is 0.978504\n", 66 | "After 3101 steps,the loss is 0.625567\n", 67 | "After 3201 steps,the loss is 0.618891\n", 68 | "After 3301 steps,the loss is 0.608601\n", 69 | "After 3401 steps,the loss is 0.620591\n", 70 | "After 3501 steps,the loss is 0.700604\n", 71 | "After 3601 steps,the loss is 0.766301\n", 72 | "After 3701 steps,the loss is 0.684956\n", 73 | "After 3801 steps,the loss is 0.909017\n", 74 | "After 3901 steps,the loss is 0.610638\n", 75 | "After 4001 steps,the loss is 0.601208\n", 76 | "After 4101 steps,the loss is 0.601578\n", 77 | "After 4201 steps,the loss is 0.597598\n", 78 | "After 4301 steps,the loss is 0.612275\n", 79 | "After 4401 steps,the loss is 0.627701\n", 80 | "After 4501 steps,the loss is 0.594224\n", 81 | "After 4601 steps,the loss is 0.844018\n", 82 | "After 4701 steps,the loss is 0.610324\n", 83 | "After 4801 steps,the loss is 0.717005\n", 84 | "After 4901 steps,the loss is 0.590796\n", 85 | "After 5001 steps,the loss is 0.593007\n", 86 | "After 5101 steps,the loss is 0.599946\n", 87 | "After 5201 steps,the loss is 0.643231\n", 88 | "After 5301 steps,the loss is 0.617860\n", 89 | "After 5401 steps,the loss is 0.591883\n", 90 | "After 5501 steps,the loss is 0.589774\n", 91 | "After 5601 steps,the loss is 0.588353\n", 92 | "After 5701 steps,the loss is 0.588788\n", 93 | "After 5801 steps,the loss is 0.582323\n", 94 | "After 5901 steps,the loss is 0.723198\n", 95 | "After 6001 steps,the loss is 0.606674\n", 96 | "After 6101 steps,the loss is 0.682886\n", 97 | "After 6201 steps,the loss is 0.580085\n", 98 | "After 6301 steps,the loss is 0.600956\n", 99 | "After 6401 steps,the loss is 0.576601\n", 100 | "After 6501 steps,the loss is 0.573657\n", 101 | "After 6601 steps,the loss is 0.827755\n", 102 | "After 6701 steps,the loss is 1.303749\n", 103 | "After 6801 steps,the loss is 0.571762\n", 104 | "After 6901 steps,the loss is 0.610755\n", 105 | "After 7001 steps,the loss is 0.640113\n", 106 | "After 7101 steps,the loss is 0.569128\n", 107 | "After 7201 steps,the loss is 0.566080\n", 108 | "After 7301 steps,the loss is 0.564210\n", 109 | "After 7401 steps,the loss is 0.563264\n", 110 | "After 7501 steps,the loss is 0.585070\n", 111 | "After 7601 steps,the loss is 0.561940\n", 112 | "After 7701 steps,the loss is 0.560033\n", 113 | "After 7801 steps,the loss is 0.562185\n", 114 | "After 7901 steps,the loss is 0.558051\n", 115 | "After 8001 steps,the loss is 0.556984\n", 116 | "After 8101 steps,the loss is 0.556139\n", 117 | "After 8201 steps,the loss is 0.577099\n", 118 | "After 8301 steps,the loss is 0.554054\n", 119 | "After 8401 steps,the loss is 0.555307\n", 120 | "After 8501 steps,the loss is 0.551864\n", 121 | "After 8601 steps,the loss is 0.551032\n", 122 | "After 8701 steps,the loss is 0.553207\n", 123 | "After 8801 steps,the loss is 0.578730\n", 124 | "After 8901 steps,the loss is 0.733932\n", 125 | "After 9001 steps,the loss is 0.548291\n", 126 | "After 9101 steps,the loss is 0.547286\n", 127 | "After 9201 steps,the loss is 0.555260\n", 128 | "After 9301 steps,the loss is 0.544677\n", 129 | "After 9401 steps,the loss is 0.546517\n", 130 | "After 9501 steps,the loss is 0.554930\n", 131 | "After 9601 steps,the loss is 0.584289\n", 132 | "After 9701 steps,the loss is 0.554965\n", 133 | "After 9801 steps,the loss is 0.553989\n", 134 | "After 9901 steps,the loss is 0.540608\n", 135 | "After 10001 steps,the loss is 0.554199\n", 136 | "After 10101 steps,the loss is 0.535904\n", 137 | "After 10201 steps,the loss is 0.534765\n", 138 | "After 10301 steps,the loss is 0.566554\n", 139 | "After 10401 steps,the loss is 0.533440\n", 140 | "After 10501 steps,the loss is 0.538789\n", 141 | "After 10601 steps,the loss is 0.531668\n", 142 | "After 10701 steps,the loss is 0.530395\n", 143 | "After 10801 steps,the loss is 0.529498\n", 144 | "After 10901 steps,the loss is 0.527991\n", 145 | "After 11001 steps,the loss is 0.528321\n", 146 | "After 11101 steps,the loss is 0.526199\n", 147 | "After 11201 steps,the loss is 0.628311\n", 148 | "After 11301 steps,the loss is 0.526717\n", 149 | "After 11401 steps,the loss is 0.588199\n", 150 | "After 11501 steps,the loss is 0.523339\n", 151 | "After 11601 steps,the loss is 0.521923\n", 152 | "After 11701 steps,the loss is 0.520764\n", 153 | "After 11801 steps,the loss is 0.534810\n", 154 | "After 11901 steps,the loss is 0.530664\n", 155 | "After 12001 steps,the loss is 0.526832\n", 156 | "After 12101 steps,the loss is 0.519265\n", 157 | "After 12201 steps,the loss is 0.534826\n", 158 | "After 12301 steps,the loss is 0.525652\n", 159 | "After 12401 steps,the loss is 0.516336\n", 160 | "After 12501 steps,the loss is 0.549207\n", 161 | "After 12601 steps,the loss is 0.512329\n", 162 | "After 12701 steps,the loss is 0.510963\n", 163 | "After 12801 steps,the loss is 0.573346\n", 164 | "After 12901 steps,the loss is 0.514642\n", 165 | "After 13001 steps,the loss is 0.608858\n", 166 | "After 13101 steps,the loss is 0.509337\n", 167 | "After 13201 steps,the loss is 0.800575\n", 168 | "After 13301 steps,the loss is 0.506083\n", 169 | "After 13401 steps,the loss is 0.683283\n", 170 | "After 13501 steps,the loss is 0.503503\n", 171 | "After 13601 steps,the loss is 0.622734\n", 172 | "After 13701 steps,the loss is 0.501739\n", 173 | "After 13801 steps,the loss is 0.502209\n", 174 | "After 13901 steps,the loss is 0.500681\n", 175 | "After 14001 steps,the loss is 0.513294\n", 176 | "After 14101 steps,the loss is 0.499554\n", 177 | "After 14201 steps,the loss is 0.497046\n", 178 | "After 14301 steps,the loss is 0.497310\n", 179 | "After 14401 steps,the loss is 0.499418\n", 180 | "After 14501 steps,the loss is 1.235166\n", 181 | "After 14601 steps,the loss is 0.494440\n", 182 | "After 14701 steps,the loss is 0.495551\n", 183 | "After 14801 steps,the loss is 0.492160\n", 184 | "After 14901 steps,the loss is 0.528410\n", 185 | "After 15001 steps,the loss is 0.489849\n", 186 | "After 15101 steps,the loss is 0.495805\n", 187 | "After 15201 steps,the loss is 0.541001\n", 188 | "After 15301 steps,the loss is 0.490161\n", 189 | "After 15401 steps,the loss is 0.486616\n", 190 | "After 15501 steps,the loss is 0.485845\n" 191 | ] 192 | }, 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "After 15601 steps,the loss is 0.484530\n", 198 | "After 15701 steps,the loss is 0.483706\n", 199 | "After 15801 steps,the loss is 0.484086\n", 200 | "After 15901 steps,the loss is 0.481948\n", 201 | "After 16001 steps,the loss is 0.481106\n", 202 | "After 16101 steps,the loss is 0.486761\n", 203 | "After 16201 steps,the loss is 0.481790\n", 204 | "After 16301 steps,the loss is 0.478480\n", 205 | "After 16401 steps,the loss is 0.478640\n", 206 | "After 16501 steps,the loss is 0.476802\n", 207 | "After 16601 steps,the loss is 0.475985\n", 208 | "After 16701 steps,the loss is 0.475035\n", 209 | "After 16801 steps,the loss is 0.714575\n", 210 | "After 16901 steps,the loss is 0.474666\n", 211 | "After 17001 steps,the loss is 0.472400\n", 212 | "After 17101 steps,the loss is 0.471576\n", 213 | "After 17201 steps,the loss is 0.471715\n", 214 | "After 17301 steps,the loss is 0.469967\n", 215 | "After 17401 steps,the loss is 0.469257\n", 216 | "After 17501 steps,the loss is 0.468196\n", 217 | "After 17601 steps,the loss is 0.467872\n", 218 | "After 17701 steps,the loss is 0.466692\n", 219 | "After 17801 steps,the loss is 0.472051\n", 220 | "After 17901 steps,the loss is 0.540070\n", 221 | "After 18001 steps,the loss is 0.463912\n", 222 | "After 18101 steps,the loss is 0.463043\n", 223 | "After 18201 steps,the loss is 0.462325\n", 224 | "After 18301 steps,the loss is 0.470149\n", 225 | "After 18401 steps,the loss is 0.461545\n", 226 | "After 18501 steps,the loss is 0.459744\n", 227 | "After 18601 steps,the loss is 0.458973\n", 228 | "After 18701 steps,the loss is 0.473234\n", 229 | "After 18801 steps,the loss is 0.457377\n", 230 | "After 18901 steps,the loss is 0.456445\n", 231 | "After 19001 steps,the loss is 0.456711\n", 232 | "After 19101 steps,the loss is 0.454847\n", 233 | "After 19201 steps,the loss is 0.454143\n", 234 | "After 19301 steps,the loss is 0.805508\n", 235 | "After 19401 steps,the loss is 0.453425\n", 236 | "After 19501 steps,the loss is 0.457488\n", 237 | "After 19601 steps,the loss is 0.630311\n", 238 | "After 19701 steps,the loss is 0.451499\n", 239 | "After 19801 steps,the loss is 0.648680\n", 240 | "After 19901 steps,the loss is 0.448252\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "import tensorflow as tf\n", 246 | "from tensorflow.examples.tutorials.mnist import input_data\n", 247 | "import mnist_lenet_forward\n", 248 | "import numpy as np\n", 249 | "import os\n", 250 | "\n", 251 | "REGULARIZER = 0.0001\n", 252 | "BATCH_SIZE = 16\n", 253 | "LEARNING_RATE_BASE = 0.1\n", 254 | "LEARNING_RATE_DECAY = 0.99\n", 255 | "MOVING_AVERAGE_DECAY = 0.99\n", 256 | "MODEL_SAVE_PATH = \"./lenet_model/\"\n", 257 | "MODEL_NAME = 'lenet'\n", 258 | "STEPS = 20000\n", 259 | "\n", 260 | "\n", 261 | "def backward(mnist):\n", 262 | " x = tf.placeholder(tf.float32,[\n", 263 | " BATCH_SIZE,\n", 264 | " mnist_lenet_forward.IMAGE_SIZE,\n", 265 | " mnist_lenet_forward.IMAGE_SIZE,\n", 266 | " mnist_lenet_forward.NUM_CHANNELS\n", 267 | " ])\n", 268 | " y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE])\n", 269 | " \n", 270 | " y = mnist_lenet_forward.forward(x,True, regularizer=REGULARIZER)\n", 271 | " \n", 272 | " global_step = tf.Variable(0,trainable=False)\n", 273 | " \n", 274 | " ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1))\n", 275 | " cem = tf.reduce_mean(ce)\n", 276 | " loss = cem + tf.add_n(tf.get_collection(\"losses\"))\n", 277 | " \n", 278 | " learning_rate = tf.train.exponential_decay(\n", 279 | " LEARNING_RATE_BASE,\n", 280 | " global_step,\n", 281 | " mnist.train.num_examples/BATCH_SIZE,\n", 282 | " LEARNING_RATE_DECAY\n", 283 | " )\n", 284 | " train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step)\n", 285 | " ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)\n", 286 | " ema_op = ema.apply(tf.trainable_variables())\n", 287 | " with tf.control_dependencies([train_step,ema_op]):\n", 288 | " train_op = tf.no_op(name=\"train\")\n", 289 | " \n", 290 | " saver = tf.train.Saver()\n", 291 | " \n", 292 | " with tf.Session() as sess:\n", 293 | " init_op = tf.global_variables_initializer()\n", 294 | " sess.run(init_op)\n", 295 | " \n", 296 | " ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)\n", 297 | " if ckpt and ckpt.model_checkpoint_path:\n", 298 | " save.restore(sess,ckpt.model_checkpoint_path)\n", 299 | " \n", 300 | " for i in range(STEPS):\n", 301 | " xs, ys = mnist.train.next_batch(BATCH_SIZE)\n", 302 | " reshape_xs = np.reshape(xs,(BATCH_SIZE,\n", 303 | " mnist_lenet_forward.IMAGE_SIZE,\n", 304 | " mnist_lenet_forward.IMAGE_SIZE,\n", 305 | " mnist_lenet_forward.NUM_CHANNELS))\n", 306 | " _, loss_value, step, = sess.run([train_op,loss,global_step],feed_dict={x:reshape_xs,y_:ys} )\n", 307 | " if i % 100 == 0:\n", 308 | " print(\"After %d steps,the loss is %f\"%(step,loss_value))\n", 309 | " saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step)\n", 310 | " \n", 311 | " \n", 312 | "def main():\n", 313 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot=True)\n", 314 | " backward(mnist)\n", 315 | " \n", 316 | "if __name__ == \"__main__\":\n", 317 | " main()\n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " " 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [] 330 | } 331 | ], 332 | "metadata": { 333 | "kernelspec": { 334 | "display_name": "Python 3", 335 | "language": "python", 336 | "name": "python3" 337 | }, 338 | "language_info": { 339 | "codemirror_mode": { 340 | "name": "ipython", 341 | "version": 3 342 | }, 343 | "file_extension": ".py", 344 | "mimetype": "text/x-python", 345 | "name": "python", 346 | "nbconvert_exporter": "python", 347 | "pygments_lexer": "ipython3", 348 | "version": "3.5.5" 349 | }, 350 | "widgets": { 351 | "state": {}, 352 | "version": "1.1.2" 353 | } 354 | }, 355 | "nbformat": 4, 356 | "nbformat_minor": 2 357 | } 358 | -------------------------------------------------------------------------------- /mnist_lenet_backward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.examples.tutorials.mnist import input_data 3 | import mnist_lenet_forward 4 | import numpy as np 5 | import os 6 | 7 | REGULARIZER = 0.0001 8 | BATCH_SIZE = 100 9 | LEARNING_RATE_BASE = 0.1 10 | LEARNING_RATE_DECAY = 0.99 11 | MOVING_AVERAGE_DECAY = 0.99 12 | MODEL_SAVE_PATH = "./lenet_model/" 13 | MODEL_NAME = 'lenet' 14 | STEPS = 20000 15 | 16 | 17 | def backward(mnist): 18 | x = tf.placeholder(tf.float32,[ 19 | BATCH_SIZE, 20 | mnist_lenet_forward.IMAGE_SIZE, 21 | mnist_lenet_forward.IMAGE_SIZE, 22 | mnist_lenet_forward.NUM_CHANNELS 23 | ]) 24 | y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE]) 25 | 26 | y = mnist_lenet_forward.forward(x,True, regularizer=REGULARIZER) 27 | 28 | global_step = tf.Variable(0,trainable=False) 29 | 30 | ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1)) 31 | cem = tf.reduce_mean(ce) 32 | loss = cem + tf.add_n(tf.get_collection("losses")) 33 | 34 | learning_rate = tf.train.exponential_decay( 35 | LEARNING_RATE_BASE, 36 | global_step, 37 | mnist.train.num_examples/BATCH_SIZE, 38 | LEARNING_RATE_DECAY 39 | ) 40 | train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step) 41 | ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) 42 | ema_op = ema.apply(tf.trainable_variables()) 43 | with tf.control_dependencies([train_step,ema_op]): 44 | train_op = tf.no_op(name="train") 45 | 46 | saver = tf.train.Saver() 47 | 48 | with tf.Session() as sess: 49 | init_op = tf.global_variables_initializer() 50 | sess.run(init_op) 51 | 52 | ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH) 53 | if ckpt and ckpt.model_checkpoint_path: 54 | save.restore(sess,ckpt.model_checkpoint_path) 55 | 56 | for i in range(STEPS): 57 | xs, ys = mnist.train.next_batch(BATCH_SIZE) 58 | reshape_xs = np.reshape(xs,(BATCH_SIZE, 59 | mnist_lenet_forward.IMAGE_SIZE, 60 | mnist_lenet_forward.IMAGE_SIZE, 61 | mnist_lenet_forward.NUM_CHANNELS)) 62 | _, loss_value, step, = sess.run([train_op,loss,global_step],feed_dict={x:reshape_xs, y_:ys} ) 63 | if i % 100 == 0: 64 | print("After %d steps,the loss is %f"%(step,loss_value)) 65 | saver.save(sess, os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step) 66 | 67 | 68 | def main(): 69 | mnist = input_data.read_data_sets("./MNIST_data/",one_hot=True) 70 | backward(mnist) 71 | 72 | if __name__ == "__main__": 73 | main() 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /mnist_lenet_forward.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import tensorflow as tf\n", 10 | "IMAGE_SIZE = 28\n", 11 | "NUM_CHANNELS = 1\n", 12 | "CONV1_SIZE = 5\n", 13 | "CONV1_KERNEL_NUM = 32\n", 14 | "CONV2_SIZE = 5\n", 15 | "CONV2_KERNEL_NUM = 64\n", 16 | "FC_SIZE = 512\n", 17 | "OUTPUT_SIZE = 10\n", 18 | "\n", 19 | "def get_weight(shape, regularizer):\n", 20 | " w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))\n", 21 | "# print(\"w:\",sess.run(w))\n", 22 | " if regularizer!=None:\n", 23 | " tf.add_to_collection(\"losses\",tf.contrib.layers.l2_regularizer(regularizer)(w))\n", 24 | " return w\n", 25 | " \n", 26 | " \n", 27 | "def get_bias(shape):\n", 28 | " b = tf.Variable(tf.zeros(shape))\n", 29 | " return b\n", 30 | "\n", 31 | "def conv2d(x,w):\n", 32 | " return tf.nn.conv2d(x,w,[1,1,1,1],padding=\"SAME\")\n", 33 | "\n", 34 | "def max_pool_2x2(x):\n", 35 | " return tf.nn.max_pool(x, ksize=[1,2,2,1],strides = [1,2,2,1] ,padding = \"SAME\")\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "def forward(x,train,regularizer):\n", 45 | " conv1_w = get_weight([CONV1_SIZE,CONV1_SIZE,NUM_CHANNELS,CONV1_KERNEL_NUM],regularizer=0.0001)\n", 46 | " conv1_b = get_bias([CONV1_KERNEL_NUM])\n", 47 | " conv1 = conv2d(x,conv1_w)\n", 48 | " relu1 = tf.nn.relu(tf.nn.bias(conv1,conv1_b))\n", 49 | " pool1 = max_pool_2x2(relu1)\n", 50 | " \n", 51 | " conv2_w = get_weight([CONV2_SIZE,CONV2_SIZE,CONV1_KERNEL_NUM,CONV2_KERNEL_NUM],regularizer=0.0001)\n", 52 | " conv2_b = get_bias([CONV2_KERNEL_NUM])\n", 53 | " conv2 = conv2d(pool1,conv2_w)\n", 54 | " relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_b))\n", 55 | " pool2 = max_pool_2x2(relu2)\n", 56 | " \n", 57 | " pool_shape = pool2.get_shape().as_list()\n", 58 | " #pool_shape[0]是一个batch的值\n", 59 | " print(\"pool_shape[0]:\",pool_shape[0])\n", 60 | " nodes = pool_shape[1]* pool_shape[2] * pool_shape[3]\n", 61 | " reshaped = tf.reshape(pool2,[pool_shape[0],nodes])\n", 62 | " \n", 63 | " fc1_w = get_weight([nodes,FC_SIZE],regularizer=0.0001)\n", 64 | " fc1_b = get_bias([FC_SIZE])\n", 65 | " fc1 = tf.nn.relu(tf.matmul(reshape,fc1_w)+fcl_b)\n", 66 | " if train: fcl = tf.nn.dropout(fc1,0.5)\n", 67 | " \n", 68 | " fc2_w = get_weight([FC_SIZE,10],regularizer=0.0001)\n", 69 | " fc2_b = get_bias([10])\n", 70 | " y = tf.matmul(fc1,fc2_w)+fc2_b\n", 71 | " return y\n", 72 | " \n" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "\n", 82 | "\n", 83 | "\n", 84 | "\n", 85 | "\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": null, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "def main():\n", 95 | " shape = IMAGE_SIZE\n", 96 | " w = get_weight(shape,0)\n", 97 | " \n", 98 | " with tf.Session() as sess:\n", 99 | " print(\"w:\",sess.run(w))\n", 100 | " \n", 101 | "if __name__ == \"__main__\":\n", 102 | " main()" 103 | ] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.5.5" 123 | }, 124 | "widgets": { 125 | "state": {}, 126 | "version": "1.1.2" 127 | } 128 | }, 129 | "nbformat": 4, 130 | "nbformat_minor": 2 131 | } 132 | -------------------------------------------------------------------------------- /mnist_lenet_forward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | IMAGE_SIZE = 28 3 | NUM_CHANNELS = 1 4 | CONV1_SIZE = 5 5 | CONV1_KERNEL_NUM = 32 6 | CONV2_SIZE = 5 7 | CONV2_KERNEL_NUM = 64 8 | FC_SIZE = 512 9 | OUTPUT_SIZE = 10 10 | 11 | def get_weight(shape, regularizer): 12 | w = tf.Variable(tf.truncated_normal(shape,stddev=0.1)) 13 | # print("w:",sess.run(w)) 14 | if regularizer!=None: 15 | tf.add_to_collection("losses",tf.contrib.layers.l2_regularizer(regularizer)(w)) 16 | return w 17 | 18 | 19 | def get_bias(shape): 20 | b = tf.Variable(tf.zeros(shape)) 21 | return b 22 | 23 | def conv2d(x,w): 24 | return tf.nn.conv2d(x,w,[1,1,1,1],padding="SAME") 25 | 26 | def max_pool_2x2(x): 27 | return tf.nn.max_pool(x, ksize=[1,2,2,1] ,strides = [1,2,2,1],padding = "SAME") 28 | 29 | def forward(x,train,regularizer): 30 | conv1_w = get_weight([CONV1_SIZE,CONV1_SIZE,NUM_CHANNELS,CONV1_KERNEL_NUM],regularizer=0.0001) 31 | conv1_b = get_bias([CONV1_KERNEL_NUM]) 32 | conv1 = conv2d(x,conv1_w) 33 | relu1 = tf.nn.relu(tf.nn.bias_add(conv1,conv1_b)) 34 | pool1 = max_pool_2x2(relu1) 35 | 36 | conv2_w = get_weight([CONV2_SIZE,CONV2_SIZE,CONV1_KERNEL_NUM,CONV2_KERNEL_NUM],regularizer=0.0001) 37 | conv2_b = get_bias([CONV2_KERNEL_NUM]) 38 | conv2 = conv2d(pool1,conv2_w) 39 | relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_b)) 40 | pool2 = max_pool_2x2(relu2) 41 | 42 | pool_shape = pool2.get_shape().as_list() 43 | #pool_shape[0]是一个batch的值 44 | print("pool_shape[0]:",pool_shape[0]) 45 | nodes = pool_shape[1]* pool_shape[2] * pool_shape[3] 46 | reshaped = tf.reshape(pool2,[pool_shape[0],nodes]) 47 | 48 | fc1_w = get_weight([nodes,FC_SIZE],regularizer=0.0001) 49 | fc1_b = get_bias([FC_SIZE]) 50 | fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_w)+fc1_b) 51 | if train: fcl = tf.nn.dropout(fc1,0.5) 52 | 53 | fc2_w = get_weight([FC_SIZE,10],regularizer=0.0001) 54 | fc2_b = get_bias([10]) 55 | y = tf.matmul(fc1,fc2_w)+fc2_b 56 | return y -------------------------------------------------------------------------------- /mnist_lenet_test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 4, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 13 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 14 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 15 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 16 | "pool_shape[0]: 100\n", 17 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 18 | "global_step: 19901\n", 19 | "After 19901 training step(s). test accuracy = 1.000000\n", 20 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 21 | "global_step: 19901\n", 22 | "After 19901 training step(s). test accuracy = 1.000000\n", 23 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 24 | "global_step: 19901\n", 25 | "After 19901 training step(s). test accuracy = 1.000000\n", 26 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 27 | "global_step: 19901\n", 28 | "After 19901 training step(s). test accuracy = 0.990000\n", 29 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 30 | "global_step: 19901\n", 31 | "After 19901 training step(s). test accuracy = 0.980000\n", 32 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 33 | "global_step: 19901\n", 34 | "After 19901 training step(s). test accuracy = 0.990000\n", 35 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 36 | "global_step: 19901\n", 37 | "After 19901 training step(s). test accuracy = 1.000000\n", 38 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 39 | "global_step: 19901\n", 40 | "After 19901 training step(s). test accuracy = 1.000000\n", 41 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 42 | "global_step: 19901\n", 43 | "After 19901 training step(s). test accuracy = 1.000000\n", 44 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 45 | "global_step: 19901\n", 46 | "After 19901 training step(s). test accuracy = 0.990000\n", 47 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 48 | "global_step: 19901\n", 49 | "After 19901 training step(s). test accuracy = 1.000000\n", 50 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 51 | "global_step: 19901\n", 52 | "After 19901 training step(s). test accuracy = 0.990000\n", 53 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 54 | "global_step: 19901\n", 55 | "After 19901 training step(s). test accuracy = 0.990000\n", 56 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 57 | "global_step: 19901\n", 58 | "After 19901 training step(s). test accuracy = 0.980000\n", 59 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 60 | "global_step: 19901\n", 61 | "After 19901 training step(s). test accuracy = 1.000000\n", 62 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 63 | "global_step: 19901\n", 64 | "After 19901 training step(s). test accuracy = 1.000000\n", 65 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 66 | "global_step: 19901\n", 67 | "After 19901 training step(s). test accuracy = 0.970000\n", 68 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 69 | "global_step: 19901\n", 70 | "After 19901 training step(s). test accuracy = 1.000000\n", 71 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 72 | "global_step: 19901\n", 73 | "After 19901 training step(s). test accuracy = 0.990000\n", 74 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 75 | "global_step: 19901\n", 76 | "After 19901 training step(s). test accuracy = 1.000000\n", 77 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 78 | "global_step: 19901\n", 79 | "After 19901 training step(s). test accuracy = 0.980000\n", 80 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 81 | "global_step: 19901\n", 82 | "After 19901 training step(s). test accuracy = 0.990000\n", 83 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 84 | "global_step: 19901\n", 85 | "After 19901 training step(s). test accuracy = 1.000000\n", 86 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 87 | "global_step: 19901\n", 88 | "After 19901 training step(s). test accuracy = 1.000000\n", 89 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 90 | "global_step: 19901\n", 91 | "After 19901 training step(s). test accuracy = 0.990000\n", 92 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 93 | "global_step: 19901\n", 94 | "After 19901 training step(s). test accuracy = 0.980000\n", 95 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 96 | "global_step: 19901\n", 97 | "After 19901 training step(s). test accuracy = 0.990000\n", 98 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 99 | "global_step: 19901\n", 100 | "After 19901 training step(s). test accuracy = 0.990000\n", 101 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 102 | "global_step: 19901\n", 103 | "After 19901 training step(s). test accuracy = 1.000000\n", 104 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 105 | "global_step: 19901\n", 106 | "After 19901 training step(s). test accuracy = 0.990000\n", 107 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 108 | "global_step: 19901\n", 109 | "After 19901 training step(s). test accuracy = 0.990000\n", 110 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 111 | "global_step: 19901\n", 112 | "After 19901 training step(s). test accuracy = 0.990000\n", 113 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 114 | "global_step: 19901\n", 115 | "After 19901 training step(s). test accuracy = 1.000000\n", 116 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 117 | "global_step: 19901\n", 118 | "After 19901 training step(s). test accuracy = 0.970000\n", 119 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 120 | "global_step: 19901\n", 121 | "After 19901 training step(s). test accuracy = 0.960000\n", 122 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 123 | "global_step: 19901\n", 124 | "After 19901 training step(s). test accuracy = 1.000000\n", 125 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 126 | "global_step: 19901\n", 127 | "After 19901 training step(s). test accuracy = 0.990000\n", 128 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 129 | "global_step: 19901\n", 130 | "After 19901 training step(s). test accuracy = 0.990000\n", 131 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 132 | "global_step: 19901\n", 133 | "After 19901 training step(s). test accuracy = 1.000000\n", 134 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 135 | "global_step: 19901\n", 136 | "After 19901 training step(s). test accuracy = 1.000000\n", 137 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 138 | "global_step: 19901\n", 139 | "After 19901 training step(s). test accuracy = 1.000000\n", 140 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 141 | "global_step: 19901\n", 142 | "After 19901 training step(s). test accuracy = 1.000000\n", 143 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 144 | "global_step: 19901\n", 145 | "After 19901 training step(s). test accuracy = 1.000000\n", 146 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 147 | "global_step: 19901\n", 148 | "After 19901 training step(s). test accuracy = 0.980000\n", 149 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 150 | "global_step: 19901\n", 151 | "After 19901 training step(s). test accuracy = 0.990000\n", 152 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 153 | "global_step: 19901\n", 154 | "After 19901 training step(s). test accuracy = 1.000000\n", 155 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 156 | "global_step: 19901\n", 157 | "After 19901 training step(s). test accuracy = 1.000000\n", 158 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 159 | "global_step: 19901\n", 160 | "After 19901 training step(s). test accuracy = 0.990000\n", 161 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 162 | "global_step: 19901\n", 163 | "After 19901 training step(s). test accuracy = 0.980000\n", 164 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 165 | "global_step: 19901\n", 166 | "After 19901 training step(s). test accuracy = 1.000000\n", 167 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 168 | "global_step: 19901\n", 169 | "After 19901 training step(s). test accuracy = 0.990000\n", 170 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 171 | "global_step: 19901\n", 172 | "After 19901 training step(s). test accuracy = 0.970000\n", 173 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 174 | "global_step: 19901\n", 175 | "After 19901 training step(s). test accuracy = 0.990000\n", 176 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 177 | "global_step: 19901\n", 178 | "After 19901 training step(s). test accuracy = 0.990000\n", 179 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 180 | "global_step: 19901\n", 181 | "After 19901 training step(s). test accuracy = 1.000000\n", 182 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 183 | "global_step: 19901\n", 184 | "After 19901 training step(s). test accuracy = 1.000000\n", 185 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n" 186 | ] 187 | }, 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "global_step: 19901\n", 193 | "After 19901 training step(s). test accuracy = 1.000000\n", 194 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 195 | "global_step: 19901\n", 196 | "After 19901 training step(s). test accuracy = 0.990000\n", 197 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 198 | "global_step: 19901\n", 199 | "After 19901 training step(s). test accuracy = 0.990000\n", 200 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 201 | "global_step: 19901\n", 202 | "After 19901 training step(s). test accuracy = 0.990000\n", 203 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 204 | "global_step: 19901\n", 205 | "After 19901 training step(s). test accuracy = 1.000000\n", 206 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 207 | "global_step: 19901\n", 208 | "After 19901 training step(s). test accuracy = 0.990000\n", 209 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 210 | "global_step: 19901\n", 211 | "After 19901 training step(s). test accuracy = 0.980000\n", 212 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 213 | "global_step: 19901\n", 214 | "After 19901 training step(s). test accuracy = 1.000000\n", 215 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 216 | "global_step: 19901\n", 217 | "After 19901 training step(s). test accuracy = 1.000000\n", 218 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 219 | "global_step: 19901\n", 220 | "After 19901 training step(s). test accuracy = 0.990000\n", 221 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 222 | "global_step: 19901\n", 223 | "After 19901 training step(s). test accuracy = 0.990000\n", 224 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 225 | "global_step: 19901\n", 226 | "After 19901 training step(s). test accuracy = 0.990000\n", 227 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 228 | "global_step: 19901\n", 229 | "After 19901 training step(s). test accuracy = 0.990000\n", 230 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 231 | "global_step: 19901\n", 232 | "After 19901 training step(s). test accuracy = 0.990000\n", 233 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 234 | "global_step: 19901\n", 235 | "After 19901 training step(s). test accuracy = 1.000000\n", 236 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 237 | "global_step: 19901\n", 238 | "After 19901 training step(s). test accuracy = 0.980000\n", 239 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 240 | "global_step: 19901\n", 241 | "After 19901 training step(s). test accuracy = 0.990000\n", 242 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 243 | "global_step: 19901\n", 244 | "After 19901 training step(s). test accuracy = 1.000000\n", 245 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 246 | "global_step: 19901\n", 247 | "After 19901 training step(s). test accuracy = 0.990000\n", 248 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 249 | "global_step: 19901\n", 250 | "After 19901 training step(s). test accuracy = 0.990000\n", 251 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 252 | "global_step: 19901\n", 253 | "After 19901 training step(s). test accuracy = 1.000000\n", 254 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 255 | "global_step: 19901\n", 256 | "After 19901 training step(s). test accuracy = 0.990000\n", 257 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 258 | "global_step: 19901\n", 259 | "After 19901 training step(s). test accuracy = 1.000000\n", 260 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 261 | "global_step: 19901\n", 262 | "After 19901 training step(s). test accuracy = 1.000000\n", 263 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 264 | "global_step: 19901\n", 265 | "After 19901 training step(s). test accuracy = 0.990000\n", 266 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 267 | "global_step: 19901\n", 268 | "After 19901 training step(s). test accuracy = 1.000000\n", 269 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 270 | "global_step: 19901\n", 271 | "After 19901 training step(s). test accuracy = 0.980000\n", 272 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 273 | "global_step: 19901\n", 274 | "After 19901 training step(s). test accuracy = 0.980000\n", 275 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 276 | "global_step: 19901\n", 277 | "After 19901 training step(s). test accuracy = 0.990000\n", 278 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 279 | "global_step: 19901\n", 280 | "After 19901 training step(s). test accuracy = 0.980000\n", 281 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 282 | "global_step: 19901\n", 283 | "After 19901 training step(s). test accuracy = 1.000000\n", 284 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 285 | "global_step: 19901\n", 286 | "After 19901 training step(s). test accuracy = 0.980000\n", 287 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 288 | "global_step: 19901\n", 289 | "After 19901 training step(s). test accuracy = 1.000000\n", 290 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 291 | "global_step: 19901\n", 292 | "After 19901 training step(s). test accuracy = 1.000000\n", 293 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 294 | "global_step: 19901\n", 295 | "After 19901 training step(s). test accuracy = 1.000000\n", 296 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 297 | "global_step: 19901\n", 298 | "After 19901 training step(s). test accuracy = 1.000000\n", 299 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 300 | "global_step: 19901\n", 301 | "After 19901 training step(s). test accuracy = 1.000000\n", 302 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 303 | "global_step: 19901\n", 304 | "After 19901 training step(s). test accuracy = 0.980000\n", 305 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 306 | "global_step: 19901\n", 307 | "After 19901 training step(s). test accuracy = 1.000000\n", 308 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 309 | "global_step: 19901\n", 310 | "After 19901 training step(s). test accuracy = 1.000000\n", 311 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 312 | "global_step: 19901\n", 313 | "After 19901 training step(s). test accuracy = 1.000000\n", 314 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 315 | "global_step: 19901\n", 316 | "After 19901 training step(s). test accuracy = 0.990000\n", 317 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 318 | "global_step: 19901\n", 319 | "After 19901 training step(s). test accuracy = 1.000000\n", 320 | "INFO:tensorflow:Restoring parameters from ./lenet_model/lenet-19901\n", 321 | "global_step: 19901\n", 322 | "After 19901 training step(s). test accuracy = 1.000000\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "import time\n", 328 | "import numpy as np\n", 329 | "import tensorflow as tf\n", 330 | "from tensorflow.examples.tutorials.mnist import input_data\n", 331 | "import mnist_lenet_backward\n", 332 | "import mnist_lenet_forward\n", 333 | "\n", 334 | "def test(mnist):\n", 335 | " with tf.Graph().as_default() as g:\n", 336 | " x = tf.placeholder(tf.float32,[mnist_lenet_backward.BATCH_SIZE,\n", 337 | " mnist_lenet_forward.IMAGE_SIZE,\n", 338 | " mnist_lenet_forward.IMAGE_SIZE,\n", 339 | " mnist_lenet_forward.NUM_CHANNELS])\n", 340 | " y_ = tf.placeholder(tf.float32,[None,mnist_lenet_forward.OUTPUT_SIZE])\n", 341 | " y = mnist_lenet_forward.forward(x,False,None)\n", 342 | " \n", 343 | " ema = tf.train.ExponentialMovingAverage(mnist_lenet_backward.MOVING_AVERAGE_DECAY)\n", 344 | " ema_restore = ema.variables_to_restore()\n", 345 | " saver = tf.train.Saver(ema_restore)\n", 346 | " \n", 347 | " correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n", 348 | " #求平均 \n", 349 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 350 | " j = 0\n", 351 | " while j<100:\n", 352 | " #gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333)\n", 353 | " #with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:\n", 354 | " with tf.Session() as sess:\n", 355 | " ckpt = tf.train.get_checkpoint_state(mnist_lenet_backward.MODEL_SAVE_PATH)\n", 356 | " if ckpt and ckpt.model_checkpoint_path:\n", 357 | " saver.restore(sess, ckpt.model_checkpoint_path)\n", 358 | " global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]\n", 359 | "# print(\"global_step:\",global_step)\n", 360 | " xs, ys = mnist.test.next_batch(mnist_lenet_backward.BATCH_SIZE)\n", 361 | " xs = np.reshape(xs,(mnist_lenet_backward.BATCH_SIZE,\n", 362 | " mnist_lenet_forward.IMAGE_SIZE,\n", 363 | " mnist_lenet_forward.IMAGE_SIZE,\n", 364 | " mnist_lenet_forward.NUM_CHANNELS))\n", 365 | " accuracy_score = sess.run(accuracy,feed_dict = {x: xs, y_: ys})\n", 366 | " print(\"global_step:\",global_step)\n", 367 | " print(\"After %s training step(s). test accuracy = %f\"%(global_step, accuracy_score))\n", 368 | " else:\n", 369 | " print(\"No checkpoint file found\")\n", 370 | " return\n", 371 | "# time.sleep(TEST_INTERVAL_SECS)\n", 372 | " j += 1\n", 373 | "def main():\n", 374 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot = True)\n", 375 | " test(mnist)\n", 376 | " \n", 377 | "if __name__ == \"__main__\":\n", 378 | " main()\n", 379 | " " 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": {}, 386 | "outputs": [], 387 | "source": [] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [] 395 | } 396 | ], 397 | "metadata": { 398 | "celltoolbar": "Raw Cell Format", 399 | "kernelspec": { 400 | "display_name": "Python 3", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.5.5" 415 | }, 416 | "widgets": { 417 | "state": {}, 418 | "version": "1.1.2" 419 | } 420 | }, 421 | "nbformat": 4, 422 | "nbformat_minor": 2 423 | } 424 | -------------------------------------------------------------------------------- /mnist_test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "WARNING:tensorflow:From :40: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 13 | "Instructions for updating:\n", 14 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 15 | "WARNING:tensorflow:From /home/lyl/anaconda3/envs/keras/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n", 16 | "Instructions for updating:\n", 17 | "Please write your own downloading logic.\n", 18 | "WARNING:tensorflow:From /home/lyl/anaconda3/envs/keras/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 19 | "Instructions for updating:\n", 20 | "Please use tf.data to implement this functionality.\n", 21 | "Extracting ./MNIST_data/train-images-idx3-ubyte.gz\n", 22 | "WARNING:tensorflow:From /home/lyl/anaconda3/envs/keras/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 23 | "Instructions for updating:\n", 24 | "Please use tf.data to implement this functionality.\n", 25 | "Extracting ./MNIST_data/train-labels-idx1-ubyte.gz\n", 26 | "WARNING:tensorflow:From /home/lyl/anaconda3/envs/keras/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 27 | "Instructions for updating:\n", 28 | "Please use tf.one_hot on tensors.\n", 29 | "Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz\n", 30 | "Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz\n", 31 | "WARNING:tensorflow:From /home/lyl/anaconda3/envs/keras/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n", 32 | "Instructions for updating:\n", 33 | "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n", 34 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 35 | "global_step: 49001\n", 36 | "After 49001 training step(s). test accuracy = 0.140625\n", 37 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 38 | "global_step: 49001\n", 39 | "After 49001 training step(s). test accuracy = 0.117188\n", 40 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 41 | "global_step: 49001\n", 42 | "After 49001 training step(s). test accuracy = 0.085938\n", 43 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 44 | "global_step: 49001\n", 45 | "After 49001 training step(s). test accuracy = 0.066406\n", 46 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 47 | "global_step: 49001\n", 48 | "After 49001 training step(s). test accuracy = 0.125000\n", 49 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 50 | "global_step: 49001\n", 51 | "After 49001 training step(s). test accuracy = 0.078125\n", 52 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 53 | "global_step: 49001\n", 54 | "After 49001 training step(s). test accuracy = 0.101562\n", 55 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 56 | "global_step: 49001\n", 57 | "After 49001 training step(s). test accuracy = 0.144531\n", 58 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 59 | "global_step: 49001\n", 60 | "After 49001 training step(s). test accuracy = 0.152344\n", 61 | "INFO:tensorflow:Restoring parameters from ./model/mnist_model-49001\n", 62 | "global_step: 49001\n", 63 | "After 49001 training step(s). test accuracy = 0.097656\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "import time \n", 69 | "import tensorflow as tf \n", 70 | "from tensorflow.examples.tutorials.mnist import input_data\n", 71 | "import mnist_forward\n", 72 | "import mnist_backward\n", 73 | "TEST_INTERVAL_SECS = 5\n", 74 | "\n", 75 | "def test(mnist):\n", 76 | " with tf.Graph().as_default() as g:\n", 77 | " x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])\n", 78 | " y_ = tf.placeholder(tf.float32, [None,mnist_forward.OUTPUT_NODE])\n", 79 | " y = mnist_forward.forward(x,None)\n", 80 | " \n", 81 | " ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)\n", 82 | " ema_restore = ema.variables_to_restore()\n", 83 | " saver = tf.train.Saver(ema_restore)\n", 84 | " \n", 85 | " correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n", 86 | " #qiupingjun \n", 87 | " accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n", 88 | " j = 0\n", 89 | " while j<10:\n", 90 | " #gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333)\n", 91 | " #with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:\n", 92 | " with tf.Session() as sess:\n", 93 | " ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)\n", 94 | " if ckpt and ckpt.model_checkpoint_path:\n", 95 | " saver.restore(sess, ckpt.model_checkpoint_path)\n", 96 | " global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]\n", 97 | "# print(\"global_step:\",global_step)\n", 98 | " xs, ys = mnist.test.next_batch(mnist_backward.BATCH_SIZE)\n", 99 | " accuracy_score = sess.run(accuracy, feed_dict = {x: xs, y_: ys})\n", 100 | " print(\"After %s training step(s). test accuracy = %f\"%(global_step, accuracy_score))\n", 101 | " else:\n", 102 | " print(\"No checkpoint file found\")\n", 103 | " return\n", 104 | "# time.sleep(TEST_INTERVAL_SECS)\n", 105 | " j += 1\n", 106 | "def main():\n", 107 | " mnist = input_data.read_data_sets(\"./MNIST_data/\",one_hot = True)\n", 108 | " test(mnist)\n", 109 | " \n", 110 | "if __name__ == \"__main__\":\n", 111 | " main()" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "kernelspec": { 124 | "display_name": "Python 3", 125 | "language": "python", 126 | "name": "python3" 127 | }, 128 | "language_info": { 129 | "codemirror_mode": { 130 | "name": "ipython", 131 | "version": 3 132 | }, 133 | "file_extension": ".py", 134 | "mimetype": "text/x-python", 135 | "name": "python", 136 | "nbconvert_exporter": "python", 137 | "pygments_lexer": "ipython3", 138 | "version": "3.5.5" 139 | }, 140 | "widgets": { 141 | "state": {}, 142 | "version": "1.1.2" 143 | } 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 2 147 | } 148 | -------------------------------------------------------------------------------- /mnist_test.py: -------------------------------------------------------------------------------- 1 | import time 2 | import tensorflow as tf 3 | from tensorflow.examples.tutorials.mnist import input_data 4 | import mnist_forward 5 | import mnist_backward 6 | TEST_INTERVAL_SECS = 5 7 | 8 | def test(mnist): 9 | with tf.Graph().as_default() as g: 10 | x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE]) 11 | y_ = tf.placeholder(tf.float32, [None,mnist_forward.OUTPUT_NODE]) 12 | y = mnist_forward.forward(x,None) 13 | 14 | ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY) 15 | ema_restore = ema.variables_to_restore() 16 | saver = tf.train.Saver(ema_restore) 17 | 18 | correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) 19 | #qiupingjun 20 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 21 | j = 0 22 | while j<20: 23 | #gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.333) 24 | #with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: 25 | with tf.Session() as sess: 26 | ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH) 27 | # print("----") 28 | # print(ckpt) 29 | # print("----") 30 | if ckpt and ckpt.model_checkpoint_path: 31 | # print("ok?") 32 | saver.restore(sess, ckpt.model_checkpoint_path) 33 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 34 | # print("global_step:",global_step) 35 | accuracy_score = sess.run(accuracy, feed_dict = {x: mnist.test.images[:256], y_: mnist.test.labels[:256]}) 36 | print("After %s training step(s). test accuracy = %f"%(global_step, accuracy_score)) 37 | else: 38 | print("No checkpoint file found") 39 | return 40 | time.sleep(TEST_INTERVAL_SECS) 41 | j += 1 42 | 43 | def main(): 44 | mnist = input_data.read_data_sets("./MNIST_data/",one_hot = True) 45 | test(mnist) 46 | 47 | if __name__ == "__main__": 48 | main() -------------------------------------------------------------------------------- /others/backward.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import generator 5 | import forward 6 | 7 | STEPS = 40000 8 | BATCH_SIZE = 32 9 | LEARNING_RATE_BASE = 0.001 10 | LEARNING_RATE_DECAY = 0.999 11 | REGULARIZER = 0.01 12 | 13 | def backward(): 14 | x = tf.placeholder(tf.float32,shape= (None,2)) 15 | y_ = tf.placeholder(tf.float32,shape= (None,1)) 16 | X, Y_, Y_c = generator.generator() 17 | 18 | pred_y = forward.forward(x,REGULARIZER) 19 | global_step = tf.Variable(0,trainable=False) 20 | learning_rate = tf.train.exponential_decay( 21 | LEARNING_RATE_BASE, 22 | global_step, 23 | 300/BATCH_SIZE, 24 | LEARNING_RATE_DECAY, 25 | staircase=True) 26 | 27 | #定义损失函数 28 | loss_mse = tf.reduce_mean(tf.square(pred_y-y_)) 29 | loss_total = loss_mse + tf.add_n(tf.get_collection('losses')) 30 | 31 | #定义反向传播方法:包括正则化 32 | train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss_total) 33 | 34 | with tf.Session() as sess: 35 | init_op = tf.global_variables_initializer() 36 | sess.run(init_op) 37 | for i in range(STEPS): 38 | start = (i*BATCH_SIZE)%300 39 | end = start + BATCH_SIZE 40 | sess.run(train_step,feed_dict={x: X[start:end],y_: Y_[start:end]}) 41 | if i % 200 ==0: 42 | loss_v = sess.run(loss_total,feed_dict={x:X,y_:Y_}) 43 | print("After %d steps, loss_v is %f:"%(i,loss_v)) 44 | xx,yy = np.mgrid[-3:3:0.1,-3:3:0.1] 45 | grid = np.c_[xx.ravel(),yy.ravel()] 46 | probs = sess.run(pred_y,feed_dict={x:grid}) 47 | probs = probs.reshape(xx.shape) 48 | print("xx.shape:",xx.shape) 49 | print("yy.shape:",yy.shape) 50 | plt.scatter(X[:,0],X[:,1],c=np.squeeze(Y_c)) 51 | plt.contour(xx,yy,probs,levels=[0.5]) 52 | 53 | plt.show() 54 | 55 | if __name__ == '__main__': 56 | backward() 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /others/forward.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #导入模块,生成模拟数据集 3 | import tensorflow as tf 4 | import generator 5 | import numpy as np 6 | # 定义网络输入、参数和输出,定义前向传播过程 7 | def get_weight(shape, regularizer): 8 | W = tf.Variable(tf.random_normal(shape),dtype = tf.float32) 9 | 10 | tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(regularizer)(W)) 11 | return W 12 | 13 | def get_bias(shape): 14 | b = tf.Variable(tf.constant(0.01,shape = shape)) 15 | return b 16 | 17 | def forward(X,regularizer): 18 | W1 = get_weight([2,11],regularizer) 19 | b1 = get_bias([11]) 20 | y1 = tf.nn.relu(tf.matmul(X,W1) + b1) 21 | 22 | W2 = get_weight([11,1],regularizer) 23 | b2 = get_bias([1]) 24 | y =tf.matmul(y1,W2) +b2 #输出层不过激活 25 | 26 | return y 27 | 28 | 29 | if __name__ == '__main__': 30 | X,Y,Y_c = generator. generator() 31 | X = np.array(X) 32 | Y = np.array(Y) 33 | Y_c = np.array(Y_c) 34 | print("X:",X.shape) 35 | print("Y:",Y.shape) 36 | print("Y_c:",Y_c.shape) 37 | -------------------------------------------------------------------------------- /others/generator.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #导入模块,生成模拟数据集 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | seed = 2 6 | def generator(): 7 | # 基于seed产生随机数 8 | rdm = np.random.RandomState(seed) 9 | # 随机数返回300行2列的矩阵,表示300组坐标点(x0,x1)作为输入数据集 10 | X = rdm.randn(300,2) 11 | # 从X这个300行2列的矩阵中取出一行,判断如果两个坐标平方和小于2,给Yield赋值1,其余赋值0 12 | # Y_作为输入数据集的标签(正确答案) 13 | Y_ = [int(x0*x0+x1*x1<2) for (x0,x1) in X] 14 | # 遍历Y中每个元素,1赋值'red',其余赋值'blue',这样便于可视化观察 15 | Y_c =[['red' if y else 'blue'] for y in Y_] 16 | # 对数据集X和标签Y进行形状整理,第一个元素为-1表示跟随第二列计算; 17 | # 第二个元素表示多少列,可见X为两列,Y为1列 18 | X = np.vstack(X).reshape(-1,2) 19 | Y_ = np.vstack(Y_).reshape(-1,1) 20 | 21 | return X,Y_,Y_c 22 | 23 | if __name__ == '__main__': 24 | X,Y,Y_c = generator() 25 | X = np.array(X) 26 | Y = np.array(Y) 27 | Y_c = np.array(Y_c) 28 | print("X:",X.shape) 29 | print("Y:",Y.shape) 30 | print("Y_c:",Y_c.shape) 31 | print("show_over") 32 | -------------------------------------------------------------------------------- /others/learning_rate_decay.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | # 整个的学习率算法:定义基础学习率,衰减率(decay_rate)和更新步数(已经训练的步数/延迟步长,如果decay_steps不为1,很明显会产生梯度)。 3 | # 新学习率的公式 : decayed_learning_rate = learning_rate*(decay_rate^(global_steps/decay_steps) 4 | # 5 | 6 | # 设损失函数 loss = (W + 1) ^ 2,令W初值是常数10。反向传播就是求最优W,即求最小loss对应的W值。 7 | # 使用指数衰减的学习率,在迭代初期得到较高的下降速度,可以在较小的训练轮数下取得更有收敛度 8 | 9 | import tensorflow as tf 10 | import matplotlib.pyplot as plt 11 | import numpy as np 12 | 13 | LEARNING_RATE_BASE = 0.3 #最初的学习率 14 | LEARNING_RATE_DECAY = 0.99 #学习衰减率 15 | LEARNING_RATE_STEP = 1 #喂入多少轮BATCH_SIZE后,更新一次学习率,一般设为:总样本数/BATCH_SIZE 16 | 17 | # 运行了几轮BATCH_SIZE的计数器,初值给0,设为不被训练 18 | global_step = tf.Variable(0,trainable = False) 19 | # 定义指数下降学习率,没用自己的公式,而是调用如下函数 20 | learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,LEARNING_RATE_STEP,LEARNING_RATE_DECAY,staircase = True) 21 | 22 | 23 | 24 | W = tf.Variable(tf.constant(5,dtype = tf.float32)) 25 | loss = tf.square(W + 1) 26 | train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step = global_step) 27 | 28 | with tf.Session() as sess: 29 | init_op = tf.global_variables_initializer() 30 | sess.run(init_op) 31 | w_val = [] 32 | loss_val = [] 33 | 34 | for i in range(400): 35 | sess.run(train_step) 36 | # 训练的时候需要更新learning_rate,并且获取rate值 37 | learning_rate_val = sess.run(learning_rate) 38 | # global_step竟然也能在会话中更新? 39 | global_step_val = sess.run(global_step) 40 | w_val.append(sess.run(W)) 41 | loss_val.append(sess.run(loss)) 42 | print("After %s steps: learning_rate_val is %f,\t global_step_val is %f ." 43 | % (i,learning_rate_val,global_step_val)) 44 | # print("After %s steps: W is %f,\t loss is %f ." % (i,w_val,loss_val)) 45 | print("len(w_val):",len(w_val)) 46 | length = 40 47 | plt.plot(range(length),w_val[:length],linestyle = '--', color = "b",label = "w_val") 48 | plt.scatter(range(length),w_val[:length],s = 50,color = "b") 49 | plt.plot(range(length),loss_val[:length],color = "r",label = "loss_val") 50 | plt.scatter(range(length),loss_val[:length],s = 50,color = "r") 51 | plt.legend(loc = "best") 52 | plt.show() 53 | -------------------------------------------------------------------------------- /others/none: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /others/placeholder.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | input1 = tf.placeholder(tf.float32) 4 | input2 = tf.placeholder(tf.float32) 5 | # t0 = tf.placeholder(tf.float32,[1,2]) 6 | # t1 = tf.placeholder(tf.float32,[2,1]) 7 | # output = tf.multiply(input1,input2) 8 | 9 | # test = np.array([12,12]) 10 | # print("test.shape",test.shape) 11 | 12 | matrix = tf.matmul(t0,t1) 13 | with tf.Session() as sess: 14 | print(sess.run(output,feed_dict = {input1:[7.],input2:[3.]})) 15 | # print(sess.run(output,feed_dict = {t0:[2.,2.],t1:[[1.],[1.]]})) 16 | # can't display the matrix through placeholder 17 | -------------------------------------------------------------------------------- /others/tf_2layer.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | x_data = np.float32(np.random.rand(2,100)) 5 | 6 | #x_data.shape is (2,100) 7 | #np.dot() is the multiply of matrix 8 | #and * is that figure multiply one by one 9 | # original function is y = [0.1,0.2] * [[100个],[100个]]+0.3 10 | # so y is just a figure 11 | y_data = np.dot([0.100,0.200],x_data)+0.300 12 | 13 | b0 = tf.Variable(tf.zeros([20,1])) 14 | W0 = tf.Variable(tf.random_uniform([20,2],-1.0,1.0)) 15 | 16 | y0 = tf.matmul(W0,x_data) + b0 17 | 18 | b1 = tf.Variable(tf.zeros([1])) 19 | W1 = tf.Variable(tf.random_uniform([1,20],-1.0,1.0)) 20 | 21 | y = tf.matmul(W1,y0) + b1 22 | 23 | 24 | loss = tf.reduce_mean(tf.square(y - y_data)) 25 | optimizer = tf.train.GradientDescentOptimizer(0.1) 26 | train = optimizer.minimize(loss) 27 | 28 | init = tf.global_variables_initializer() 29 | 30 | with tf.Session() as sess: 31 | sess.run(init) 32 | for step in range(0,5001): 33 | sess.run(train) 34 | if step % 20 == 0: 35 | print(step,"w0:",sess.run(W0),"b0:",sess.run(b0)) 36 | print(step,"w1:",sess.run(W1),"b1:",sess.run(b1)) 37 | 38 | -------------------------------------------------------------------------------- /others/tf_demo.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | 4 | # 创建一个特征向量列表,该特征列表里只有一个特征向量, 5 | # 该特征向量为实数向量,只有一个元素的数组,且该元素名称为 x, 6 | # 我们还可以创建其他更加复杂的特征列表 7 | feature_columns = [tf.feature_column.numeric_column("x", shape=[1])] 8 | 9 | # 创建一个LinearRegressor训练器,并传入特征向量列表 10 | estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns) 11 | 12 | # 保存训练用的数据 13 | x_train = np.array([1., 2., 3., 6., 8.]) 14 | y_train = np.array([4.8, 8.5, 10.4, 21.0, 25.3]) 15 | 16 | # 保存评估用的数据 17 | x_eavl = np.array([2., 5., 7., 9.]) 18 | y_eavl = np.array([7.6, 17.2, 23.6, 28.8]) 19 | 20 | # 用训练数据创建一个输入模型,用来进行后面的模型训练 21 | # 第一个参数用来作为线性回归模型的输入数据 22 | # 第二个参数用来作为线性回归模型损失模型的输入 23 | # 第三个参数batch_size表示每批训练数据的个数 24 | # 第四个参数num_epochs为epoch的次数,将训练集的所有数据都训练一遍为1次epoch 25 | # 低五个参数shuffle为取训练数据是顺序取还是随机取 26 | train_input_fn = tf.estimator.inputs.numpy_input_fn( 27 | {"x": x_train}, y_train, batch_size=2, num_epochs=None, shuffle=True) 28 | 29 | print("------------") 30 | print("train_input_fn:",train_input_fn) 31 | print("------------") 32 | # 再用训练数据创建一个输入模型,用来进行后面的模型评估 33 | train_input_fn_2 = tf.estimator.inputs.numpy_input_fn( 34 | {"x": x_train}, y_train, batch_size=2, num_epochs=1000, shuffle=False) 35 | 36 | # 用评估数据创建一个输入模型,用来进行后面的模型评估 37 | eval_input_fn = tf.estimator.inputs.numpy_input_fn( 38 | {"x": x_eavl}, y_eavl, batch_size=2, num_epochs=1000, shuffle=False) 39 | 40 | # 使用训练数据训练1000次 41 | estimator.train(input_fn=train_input_fn, steps=1000) 42 | 43 | # 使用原来训练数据评估一下模型,目的是查看训练的结果 44 | train_metrics = estimator.evaluate(input_fn=train_input_fn_2) 45 | print("train metrics: %r" % train_metrics) 46 | 47 | # 使用评估数据评估一下模型,目的是验证模型的泛化性能 48 | eval_metrics = estimator.evaluate(input_fn=eval_input_fn) 49 | print("eval metrics: %s" % eval_metrics) -------------------------------------------------------------------------------- /others/variables.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | state = tf.Variable(0,name='counter') 3 | two = tf.constant(2) 4 | new_value = tf.add(state,two) 5 | update = tf.assign(state,new_value) 6 | init = tf.initialize_all_variables() 7 | with tf.Session() as sess: 8 | sess.run(init) 9 | for _ in range(3): 10 | sess.run(update) 11 | print(sess.run(state)) -------------------------------------------------------------------------------- /placeholder.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | input1 = tf.placeholder(tf.float32) 4 | input2 = tf.placeholder(tf.float32) 5 | # t0 = tf.placeholder(tf.float32,[1,2]) 6 | # t1 = tf.placeholder(tf.float32,[2,1]) 7 | # output = tf.multiply(input1,input2) 8 | 9 | # test = np.array([12,12]) 10 | # print("test.shape",test.shape) 11 | 12 | matrix = tf.matmul(t0,t1) 13 | with tf.Session() as sess: 14 | print(sess.run(output,feed_dict = {input1:[7.],input2:[3.]})) 15 | # print(sess.run(output,feed_dict = {t0:[2.,2.],t1:[[1.],[1.]]})) 16 | # can't display the matrix through placeholder 17 | -------------------------------------------------------------------------------- /tf_2layer.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | x_data = np.float32(np.random.rand(2,100)) 5 | 6 | #x_data.shape is (2,100) 7 | #np.dot() is the multiply of matrix 8 | #and * is that figure multiply one by one 9 | # original function is y = [0.1,0.2] * [[100个],[100个]]+0.3 10 | # so y is just a figure 11 | y_data = np.dot([0.100,0.200],x_data)+0.300 12 | 13 | b0 = tf.Variable(tf.zeros([20,1])) 14 | W0 = tf.Variable(tf.random_uniform([20,2],-1.0,1.0)) 15 | 16 | y0 = tf.matmul(W0,x_data) + b0 17 | 18 | b1 = tf.Variable(tf.zeros([1])) 19 | W1 = tf.Variable(tf.random_uniform([1,20],-1.0,1.0)) 20 | 21 | y = tf.matmul(W1,y0) + b1 22 | 23 | 24 | loss = tf.reduce_mean(tf.square(y - y_data)) 25 | optimizer = tf.train.GradientDescentOptimizer(0.1) 26 | train = optimizer.minimize(loss) 27 | 28 | init = tf.global_variables_initializer() 29 | 30 | with tf.Session() as sess: 31 | sess.run(init) 32 | for step in range(0,5001): 33 | sess.run(train) 34 | if step % 20 == 0: 35 | print(step,"w0:",sess.run(W0),"b0:",sess.run(b0)) 36 | print(step,"w1:",sess.run(W1),"b1:",sess.run(b1)) 37 | 38 | -------------------------------------------------------------------------------- /tf_demo.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tensorflow as tf 3 | 4 | # 创建一个特征向量列表,该特征列表里只有一个特征向量, 5 | # 该特征向量为实数向量,只有一个元素的数组,且该元素名称为 x, 6 | # 我们还可以创建其他更加复杂的特征列表 7 | feature_columns = [tf.feature_column.numeric_column("x", shape=[1])] 8 | 9 | # 创建一个LinearRegressor训练器,并传入特征向量列表 10 | estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns) 11 | 12 | # 保存训练用的数据 13 | x_train = np.array([1., 2., 3., 6., 8.]) 14 | y_train = np.array([4.8, 8.5, 10.4, 21.0, 25.3]) 15 | 16 | # 保存评估用的数据 17 | x_eavl = np.array([2., 5., 7., 9.]) 18 | y_eavl = np.array([7.6, 17.2, 23.6, 28.8]) 19 | 20 | # 用训练数据创建一个输入模型,用来进行后面的模型训练 21 | # 第一个参数用来作为线性回归模型的输入数据 22 | # 第二个参数用来作为线性回归模型损失模型的输入 23 | # 第三个参数batch_size表示每批训练数据的个数 24 | # 第四个参数num_epochs为epoch的次数,将训练集的所有数据都训练一遍为1次epoch 25 | # 低五个参数shuffle为取训练数据是顺序取还是随机取 26 | train_input_fn = tf.estimator.inputs.numpy_input_fn( 27 | {"x": x_train}, y_train, batch_size=2, num_epochs=None, shuffle=True) 28 | 29 | print("------------") 30 | print("train_input_fn:",train_input_fn) 31 | print("------------") 32 | # 再用训练数据创建一个输入模型,用来进行后面的模型评估 33 | train_input_fn_2 = tf.estimator.inputs.numpy_input_fn( 34 | {"x": x_train}, y_train, batch_size=2, num_epochs=1000, shuffle=False) 35 | 36 | # 用评估数据创建一个输入模型,用来进行后面的模型评估 37 | eval_input_fn = tf.estimator.inputs.numpy_input_fn( 38 | {"x": x_eavl}, y_eavl, batch_size=2, num_epochs=1000, shuffle=False) 39 | 40 | # 使用训练数据训练1000次 41 | estimator.train(input_fn=train_input_fn, steps=1000) 42 | 43 | # 使用原来训练数据评估一下模型,目的是查看训练的结果 44 | train_metrics = estimator.evaluate(input_fn=train_input_fn_2) 45 | print("train metrics: %r" % train_metrics) 46 | 47 | # 使用评估数据评估一下模型,目的是验证模型的泛化性能 48 | eval_metrics = estimator.evaluate(input_fn=eval_input_fn) 49 | print("eval metrics: %s" % eval_metrics) -------------------------------------------------------------------------------- /variables.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | state = tf.Variable(0,name='counter') 3 | two = tf.constant(2) 4 | new_value = tf.add(state,two) 5 | update = tf.assign(state,new_value) 6 | init = tf.initialize_all_variables() 7 | with tf.Session() as sess: 8 | sess.run(init) 9 | for _ in range(3): 10 | sess.run(update) 11 | print(sess.run(state)) --------------------------------------------------------------------------------