1 #Import Data2 fromTensorflow.examples.tutorials.mnistImportInput_data3 #reading Data4Mnist=input_data.read_data_sets ("mnist_data/", one_hot=True)5 ImportTensorFlow as TF6 7 #defining the convolution operation function8 defconv2d (name,x,w,b):9 returnTf.nn.relu (Tf.nn.bias_add (tf.nn.conv2d (x,w,strides=[1,1,1,1],padding='same'), b), name=name)Ten One #define the sample operation function A defMax_pool (name,x,k): - returnTf.nn.max_pool (x,ksize=[1,k,k,1],strides=[1,k,k,1],padding='same', name=name) - the #Defining a normalization function - defNorm (name,x,lsize=4): - returnTF.NN.LRN (x,lsize,bias=1.0,alpha=0.001/9,beta=0.75,name=name) - + #formally define the alex_net network structure - defalex_net (_x,_weights,_biases,_dropout): + #to deform the input A_x=tf.reshape (_x,shape=[-1,28,28,1]) at - #Build the first convolutional layer -Conv1=conv2d ('CONV1', _x,_weights['WC1'],_biases['BC1']) -Pool1=max_pool ('Pool1', conv1,k=2) -Norm1=norm ('Norm1', pool1,lsize=4) -drop1=tf.nn.dropout (norm1,_dropout) in - #Build a second convolutional layer toConv2=conv2d ('Conv2', drop1,_weights['WC2'],_biases['BC2']) +Pool2=max_pool ('pool2', conv2,k=2) -Norm2=norm ('Norm2', pool2,lsize=4) theDrop2=tf.nn.dropout (norm2,_dropout) * $ #build a third convolutional layerPanax NotoginsengConv3=conv2d ('Conv3', drop2,_weights['wc3'],_biases['BC3']) -Pool3=max_pool ('pool3', conv3,k=2) theNorm3=norm ('NORM3', pool3,lsize=4) +drop3=tf.nn.dropout (norm3,_dropout) A the #deform the output and connect the three fully connected layers +Dense1=tf.reshape (drop3,shape=[-1,_weights['WD1'].get_shape (). As_list () [0]]) -Dense1=tf.nn.relu (Tf.matmul (dense1,_weights['WD1']) +_biases['BD1'],name='FC1') $Dense2=tf.nn.relu (Tf.matmul (dense1,_weights['WD2']) +_biases['Bd2'],name='FC2') $Out=tf.matmul (dense2,_weights[' out']+_biases[' out']) - return out - the #Set Network Training parameters -learning_rate=0.001Wuyitraining_iters=200000 theBatch_size=64 -Display_step=20 Wu - #Setting Data Parameters Aboutn_input=784 $n_classes=10 -dropout=0.8 - - #placeholder input some version written in Tf.types.float32 Ax=Tf.placeholder (Tf.float32,[none,n_input]) +y=Tf.placeholder (tf.float32,[none,n_classes]) thekeep_prob=Tf.placeholder (Tf.float32) - $ #set the network core size, number of layers, sampling steps, etc. theweights={ the 'WC1': TF. Variable (Tf.random_normal ([3,3,1,64])), the 'WC2': TF. Variable (Tf.random_normal ([3,3,64,128])), the 'wc3': TF. Variable (Tf.random_normal ([3,3,128,256])), - 'WD1': TF. Variable (Tf.random_normal ([4*4*256,1024])), in 'WD2': TF. Variable (Tf.random_normal ([1024,1024])), the ' out': TF. Variable (Tf.random_normal ([1024,10])) the } About thebiases={ the 'BC1': TF. Variable (Tf.random_normal ([64])), the 'BC2': TF. Variable (Tf.random_normal ([128])), + 'BC3': TF. Variable (Tf.random_normal ([256])), - 'BD1': TF. Variable (Tf.random_normal ([1024])), the 'Bd2': TF. Variable (Tf.random_normal ([1024])),Bayi ' out': TF. Variable (Tf.random_normal ([n_classes])) the } the - #Build a alex_net network and output the results based on input predictions -Pred=alex_net (X,weights,biases,keep_prob) the the #build a loss function based on the predicted value pred and the true label y thecost=Tf.reduce_mean (Tf.nn.softmax_cross_entropy_with_logits (pred,y)) the//Set optimization function, minimize loss function -Optimizer=tf.train.adamoptimizer (learning_rate=learning_rate). Minimize (cost) the the #set up a test network theCorrect_pred=tf.equal (Tf.argmax (pred,1), Tf.argmax (y,1))94accuracy=Tf.reduce_mean (Tf.cast (correct_pred,tf.float32)) the the #Initialize the network all to the weight of the version written Global_variables_initializer theinit=tf.initialize_all_variables ()98 About With TF. Session () as Sess: - #Initialize network weights101 Sess.run (init)102Step = 1103 104 #Start training The network until the maximum number of iterations is reached the whileStep * Batch_size <training_iters:106 #Get Batch Data107Batch_xs, Batch_ys =Mnist.train.next_batch (batch_size)108 109 #Training Network based on batch data theSess.run (Optimizer, feed_dict={x:batch_xs, Y:batch_ys, keep_prob:dropout})111 the #Print Output113 ifStep% Display_step = =0: the #Calculation Accuracy theACC = sess.run (accuracy, FEED_DICT={X:BATCH_XS, Y:batch_ys, keep_prob:1.}) the #Calculate loss Value117Loss = Sess.run (cost, Feed_dict={x:batch_xs, Y:batch_ys, keep_prob:1.})118 Print "Iter"+ STR (step*batch_size) +", Minibatch loss="+"{:. 6f}". Format (loss) +", Training accuracy="+"{:. 5f}". Format (ACC)119Step + = 1 - 121 Print "optimization finished!"122 #Calculate test Accuracy123 Print "Testing Accuracy:", Sess.run (accuracy, feed_dict={x:mnist.test.images[:256], y:mnist.test.labels[:256], keep_prob:1.})
View Code
AlexNet:
Won the 2012-year champion of the ILSVRC Competition classification project (top-5 error rate 16.4%, using additional data to reach 15.3%, 8 layer neural Network 3 conv+2 pooling+3 FC)
The first successful application of Relu dropout and LRN trick in CNN:
Successfully use Relu as the activation function of CNN, and verify its effect in the deeper to the network over the sigmoid, successfully solve the sigmoid in the deep network to the gradient dispersion problem.
Although the Relu activation function was proposed a long time ago, it was not carried forward until the advent of alexnet.
Use during training
TensorFlow Combat-alexnet