<jupyter_start><jupyter_text>TensorFlow版本<jupyter_code>tf.__version__<jupyter_output><empty_output><jupyter_text>MNIST数据集的获取<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(r'data/')
train_x = mnist.train.images
train_y = mnist.train.labels
test_x = mnist.test.images
test_y = mnist.test.labels<jupyter_output><empty_output><jupyter_text>构建LeNet-5模型<jupyter_code>x = tf.placeholder(tf.float32,shape = (None,train_x.shape[1]))
y = tf.placeholder(tf.int32)
y_true = tf.one_hot(y,depth = 10)

#注意是channel last
layer_input = tf.reshape(x,shape = (-1,28,28,1))
layer_input = tf.pad(layer_input,[[0,0],[2,2],[2,2],[0,0]])

layer_c1 = tf.layers.conv2d(layer_input,6,(5,5))

layer_s2 = tf.layers.average_pooling2d(layer_c1,(2,2),2)
layer_s2 = tf.layers.dense(layer_s2,1,activation = tf.sigmoid)

layer_c3 = tf.layers.conv2d(layer_s2,16,(5,5))

layer_s4 = tf.layers.average_pooling2d(layer_c3,(2,2),2)
layer_s4 = tf.layers.dense(layer_s4,1,activation = tf.nn.relu)

layer_c5 = tf.layers.conv2d(layer_s4,120,(5,5))

layer_f6 = tf.layers.dense(layer_c5,120,activation = tf.sigmoid)

layer_f7 = tf.layers.dense(layer_f6,10)<jupyter_output>WARNING:tensorflow:From <ipython-input-5-aeb5ee46aa13>:9: conv2d (from tensorflow.python.layers.convolutional) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.conv2d instead.
WARNING:tensorflow:From D:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From <ipython-input-5-aeb5ee46aa13>:11: average_pooling2d (from tensorflow.python.layers.pooling) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.average_pooling2d instead.
WARNING:tensorflow:From <ipython-input-5-aeb5ee46aa13>:12: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.dense instead.<jupyter_text>学习参数设置<jupyter_code>batch = 1000
epoch = 5
step_each_epoch = int(train_x.shape[0]/batch)
loss = tf.reduce_sum(tf.losses.softmax_cross_entropy(logits = layer_f7,onehot_labels = y_true))
y_pre = tf.arg_max(tf.nn.softmax(layer_f7),3)
train_op = tf.train.AdamOptimizer().minimize(loss)
saver = tf.train.Saver()<jupyter_output>WARNING:tensorflow:From D:\Anaconda3\lib\site-packages\tensorflow\python\ops\losses\losses_impl.py:209: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
WARNING:tensorflow:From <ipython-input-7-b6d2fe2989fb>:2: arg_max (from tensorflow.python.ops.gen_math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.math.argmax` instead<jupyter_text>开始训练<jupyter_code>with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(epoch):
        for j in range(step_each_epoch):
            show_loss,_ = sess.run([loss,train_op],feed_dict = {x:train_x[j*batch:j*batch+batch,:],y:train_y[j*batch:j*batch+batch]})
            if (j%10 == 0):
                print(f'epoch {i},step {j},loss {show_loss}')
#     temp = sess.run(tf.nn.softmax(layer_f7),feed_dict = {x:train_x,y:train_y})
        pre_result = sess.run(y_pre,feed_dict = {x:train_x,y:train_y})
        acc = np.sum(pre_result[:,0,0] == train_y)
        print('Accuary Num:',acc)
    saver_path = saver.save(sess, r'save/model.ckpt')
    print("Model saved in file:", saver_path)<jupyter_output>epoch 0,step 0,loss 2.5295238494873047
epoch 0,step 10,loss 2.3054282665252686
epoch 0,step 20,loss 2.2971293926239014
epoch 0,step 30,loss 2.299272060394287
epoch 0,step 40,loss 2.293409585952759
epoch 0,step 50,loss 2.276235342025757
Accuary Num: 10514
epoch 1,step 0,loss 2.256678819656372
epoch 1,step 10,loss 2.1539809703826904
epoch 1,step 20,loss 1.8754899501800537
epoch 1,step 30,loss 1.464343786239624
epoch 1,step 40,loss 1.2062233686447144
epoch 1,step 50,loss 0.9346644878387451
Accuary Num: 40442
epoch 2,step 0,loss 0.9306538701057434
epoch 2,step 10,loss 0.7667126655578613
epoch 2,step 20,loss 0.6775241494178772
epoch 2,step 30,loss 0.5949601531028748
epoch 2,step 40,loss 0.6168568134307861
epoch 2,step 50,loss 0.5028893947601318
Accuary Num: 46112
epoch 3,step 0,loss 0.548198938369751
epoch 3,step 10,loss 0.4874243438243866
epoch 3,step 20,loss 0.4585815668106079
epoch 3,step 30,loss 0.4045945405960083
epoch 3,step 40,loss 0.476972758769989
epoch 3,step 50,loss 0.37890660762[...]
{'fraction_non_alphanumeric': 0.12078830260648442, 'fraction_numerical': 0.12841703750794659, 'mean_word_length': 6.295208655332303, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '483086', 'license': 'Apache-2.0', 'n_tokens_mistral': 2236, 'n_tokens_neox': 1888, 'n_words': 335, 'path': 'Task5/.ipynb_checkpoints/MNIST-checkpoint.ipynb', 'repo_name': 'qs956/Taidi-face-recognition', 'size': '4719'}