권적 신경 네트워크 코드 실현

3435 단어 심도 있는 학습
# -*- coding=utf-8 -*-
import numpy as np
import tensorflow as tf
#     MNIST     (55000*28*28)55000     
from tensorflow .examples.tutorials.mnist import input_data

#one_hot      (encoding)  
#0,1,2 9 10   
# 0    0 : 1000000000
# 1    1 : 0100000000
# 2    2 : 0010000000     
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

#     28*28
#None    (Tensor)             
input_x = tf.placeholder(tf.float32,[None,28*28])

#   10   
output_y = tf.placeholder(tf.int32,[None,10])

#      ,-1           1      ,         
images = tf.reshape(input_x,[-1,28,28,1])

# Test      3000             
test_x = mnist.test.images[:3000]#    
test_y = mnist.test.labels[:3000]#    


#        
#     
conv1 = tf.layers.conv2d(inputs=images,
                         filters=6,
                         kernel_size=[5,5],
                         strides=1,
                         padding='same',
                         activation=tf.nn.relu
                         )#      32*32(         0),      28*28

#     
pool1 = tf.layers.max_pooling2d(inputs=conv1,
                                pool_size=[2,2],
                                strides=2
                                )#    [14*14,32]

#     
conv2 = tf.layers.conv2d(inputs=pool1,
                         filters=16,
                         kernel_size=[5,5],
                         strides=1,
                         activation=tf.nn.relu)#    [10*10,6]

#     
pool2 = tf.layers.max_pooling2d(inputs=conv2,
                                pool_size=[2,2],
                                strides=2
                                )#    [5*5,6]
conv3 = tf.layers.conv2d(inputs=pool2,
                         filters=120,
                         kernel_size=[5,5],
                         strides=1,
                         activation=tf.nn.relu)

#   
flat = tf.reshape(conv3,[-1,1*1*120])

#      
dense = tf.layers.dense(inputs=flat,
                        units=1024,
                        activation=tf.nn.relu)

#dropout   50%
dropout = tf.layers.dropout(inputs=dense,rate=0.5)

#10         ,            
logits = tf.layers.dense(inputs=dropout,units=10)

#    ,  softmax       
loss = tf.losses.softmax_cross_entropy(onehot_labels=output_y,
                                       logits=logits)

#   Adam         ,     0.001
#train_op = tf.train.AdamOptimizer(learning_rate=0.001.minimize(loss))
train_op = tf.train.AdamOptimizer(0.001).minimize(loss)

#               
#  (accuracy,update_op),         
accuracy = tf.metrics.accuracy(labels=tf.argmax(output_y,axis=1),
                               predictions=tf.argmax(logits,axis=1),)[1]

#    
sess = tf.Session()

#     ,     
init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())

sess.run(init)

for x in range(2000):
    batch = mnist.train.next_batch(50) # train        50   
    train_loss,train_op_ = sess.run([loss,train_op],{input_x:batch[0],output_y:batch[1]})
    if x % 100 == 0:# 100     
        test_accuracy = sess.run(accuracy,{input_x:test_x,output_y:test_y})
        print("step=%d,train loss = %.4f,test accuracy = %.2f" % (x,train_loss,test_accuracy))


#  ,  20          
test_output = sess.run(logits,{input_x:test_x[:20]})
inferenced_y = np.argmax(test_output,1)
print(inferenced_y,'inferenced numbers')#     
print(np.argmax(test_y[:20],1),'real numbers')#     










좋은 웹페이지 즐겨찾기