資源簡介
該代碼是在學習深度學習的過程中,自行編寫的代碼,利用cnn網絡來對mnist手寫字符進行高精度的識別,并加入了詳細的注釋,非常適合作為初次接觸深度學習的新手入門。歡迎下載。
代碼片段和文件信息
import?tensorflow?as?tf
from?tensorflow.examples.tutorials.mnist?import?input_data
#?一些包括網絡結構在內的超參數,全局變量
INPUT_NODE=784
OUTPUT_NODE=10
IMAGE_SIZE=28
NUM_CHANNELS=1
NUM_LABELS=10
CONV1_DEEP=32
CONV1_SIZE=5
CONV2_DEEP=64
CONV2_SIZE=5
FC_SIZE=512
BATCH_SIZE=20
LEARNING_RATE_base=0.06
LEARNING_RATE_DECAY=0.999
REGULARIZATION_RATE=0.0001
TRAINING_STEPS=20
MOVING_AVERAGE_DECAY=0.99
#?前向傳播的計算過程
def?inference(input_tensortrainavg_class=Noneregularizer=Nonereuse=False):
????with?tf.variable_scope(‘layer1_conv1‘reuse=reuse):
????????conv1_weights=tf.get_variable(
????????????‘weight‘[CONV1_SIZECONV1_SIZENUM_CHANNELSCONV1_DEEP]
????????????initializer=tf.truncated_normal_initializer(stddev=0.1))
????????conv1_biases=tf.get_variable(
????????????‘bias‘[CONV1_DEEP]initializer=tf.constant_initializer(0.0))
????????if?avg_class==None:
????????????conv1=tf.nn.conv2d(
????????????????input_tensorconv1_weightsstrides=[1221]padding=‘SAME‘)
????????????relu1=tf.nn.relu(tf.nn.bias_add(conv1conv1_biases))
????????else:
????????????conv1=tf.nn.conv2d(
????????????????input_tensoravg_class.average(conv1_weights)strides=[1221]padding=‘SAME‘)
????????????relu1=tf.nn.relu(tf.nn.bias_add(conv1avg_class.average(conv1_biases)))
????with?tf.name_scope(‘layer2_pool1‘):
????????pool1=tf.nn.max_pool(
????????????relu1ksize=[1221]strides=[1221]padding=‘SAME‘)
????with?tf.variable_scope(‘layer3_conv2‘reuse=reuse):
????????conv2_weights=tf.get_variable(
????????????‘weight‘[CONV2_SIZECONV2_SIZECONV1_DEEPCONV2_DEEP]
????????????initializer=tf.truncated_normal_initializer(stddev=0.1))
????????conv2_baises=tf.get_variable(
????????????‘bias‘[CONV2_DEEP]initializer=tf.constant_initializer(0.0))
????????if?avg_class==None:
????????????conv2=tf.nn.conv2d(
????????????????relu1conv2_weightsstrides=[1221]padding=‘SAME‘)
????????????relu2=tf.nn.relu(tf.nn.bias_add(conv2conv2_baises))
????????else:
????????????conv2=tf.nn.conv2d(
???????????????relu1avg_class.average(conv2_weights)strides=[1221]padding=‘SAME‘)
????????????relu2=tf.nn.relu(tf.nn.bias_add(conv2avg_class.average(conv2_baises)))
????with?tf.name_scope(‘layer4-pool2‘):
????????pool2=tf.nn.max_pool(
????????????relu2ksize=[1221]strides=[1221]padding=‘SAME‘)
????
????pool_shape=pool2.get_shape().as_list()
????nodes=pool_shape[1]*pool_shape[2]*pool_shape[3]
????reshaped=tf.reshape(pool2[-1nodes])
????#?print(type(reshaped))
????with?tf.variable_scope(‘layer5_fc1‘reuse=reuse):
????????fc1_weights=tf.get_variable(
????????????‘weight‘[nodesFC_SIZE]
????????????initializer=tf.truncated_normal_initializer(stddev=0.1))
????????if?regularizer!=None:
????????????tf.add_to_collection(‘losses‘regularizer(fc1_weights))
????????fc1_biases=tf.get_variable(
????????????‘bias‘[FC_SIZE]initializer=tf.constant_initializer(0.1))
????????if?avg_class==None:
????????????fc1=tf.nn.relu(tf.matmu
評論
共有 條評論