# 深入浅出解读卷积神经网络

图3 形象的卷积层示例

***步：导入相关库、加载参数

`import math import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import h5py from tensorflow.python.framework import ops from tf_utils import * np.random.seed(1) X_train_orig,Y_train_orig,X_test_orig,Y_test_orig,classes=load_dataset() index=0 plt.imshow(X_train_orig[index]) print("y="+str(np.squeeze(Y_train_orig[:,index]))) plt.show() `

`X_train=X_train_orig/255.0 X_test=X_test_orig/255.0 Y_train=convert_to_one_hot(Y_train_orig,6) Y_test=convert_to_one_hot(Y_test_orig,6) `

`def create_placeholder(num_px,channel,n_y):     X=tf.placeholder(tf.float32,shape=(None,num_px,num_px,channel),name='X')     Y=tf.placeholder(tf.float32,shape=(None,n_y),name='Y')     return X,Y X,Y=create_placeholder(64,3,6) print("X="+str(X)) print("Y="+str(Y))  def weight_variable(shape):     return tf.Variable(tf.truncated_normal(shape,stddev=0.1)) def bias_variable(shape):     return tf.Variable(tf.constant(0.1,shape=shape)) def conv2d(x,W):     return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME') def max_pool_2x2(x):     return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')  def initialize_parameters():     w_conv1=weight_variable([5,5,3,32])     b_conv1=bias_variable([32])          w_conv2=weight_variable([5,5,32,64])     b_conv2=bias_variable([64])          w_fc1=weight_variable([16*16*64,512])     b_fc1=bias_variable([512])          w_fc2=weight_variable([512,6])     b_fc2=bias_variable([6])          parameters={         "w_conv1":w_conv1,         "b_conv1":b_conv1,         "w_conv2":w_conv2,         "b_conv2":b_conv2,         "w_fc1":w_fc1,         "b_fc1":b_fc1,         "w_fc2":w_fc2,         "b_fc2":b_fc2     }     return parameters `

`def forward_propagation(X,parameters):     w_conv1=parameters["w_conv1"]     b_conv1=parameters["b_conv1"]     h_conv1=tf.nn.relu(conv2d(X,w_conv1)+b_conv1)     h_pool1=max_pool_2x2(h_conv1)          w_conv2=parameters["w_conv2"]     b_conv2=parameters["b_conv2"]     h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)     h_pool2=max_pool_2x2(h_conv2)          w_fc1=parameters["w_fc1"]     b_fc1=parameters["b_fc1"]     h_pool2_flat=tf.reshape(h_pool2,[-1,16*16*64])     h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)          #keep_prob=tf.placeholder(tf.float32)     #h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)          w_fc2=parameters["w_fc2"]     b_fc2=parameters["b_fc2"]     y_conv=tf.matmul(h_fc1,w_fc2)+b_fc2     return y_conv `

`def compute_cost(y_conv,Y):      cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv,labels=Y))     return cost `

`def random_mini_batches1(X, Y, mini_batch_size = 64, seed = 0):     m = X.shape[0]                  # number of training examples     mini_batches = []     np.random.seed(seed)     Y=Y.T         #(1080,6)     # Step 1: Shuffle (X, Y)     permutation = list(np.random.permutation(m))     shuffled_X = X[permutation,:,:,:]     shuffled_Y = Y[permutation,:].reshape((m,Y.shape[1]))      # Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.     num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning     for k in range(0, num_complete_minibatches):         mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:,:,:]         mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]         mini_batch = (mini_batch_X, mini_batch_Y)         mini_batches.append(mini_batch)          # Handling the end case (last mini-batch < mini_batch_size)     if m % mini_batch_size != 0:         mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:,:,:]         mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:]         mini_batch = (mini_batch_X, mini_batch_Y)         mini_batches.append(mini_batch)          return mini_batches `

` def model(X_train,Y_train,X_test,Y_test,learning_rate=0.001,num_epochs=20,minibatch_size=32,print_cost=True):        ops.reset_default_graph()  #(1080, 64, 64, 3)     tf.set_random_seed(1)      #Y_train(6, 1080)     seed=3     (m,num_px1,num_px2,c)=X_train.shape     n_y=Y_train.shape[0]     costs=[]     X,Y=create_placeholder(64,3,6)     parameters=initialize_parameters()          Z3=forward_propagation(X,parameters)     cost=compute_cost(Z3,Y)     optm=tf.train.AdamOptimizer(learning_rate).minimize(cost)          correct_prediction=tf.equal(tf.argmax(Z3,1),tf.argmax(Y,1))#居然忘记1了，所以一直出现损失越来越小了，但是准确率却一直是0     accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))     with tf.Session() as sess:         tf.global_variables_initializer().run()         for epoch in range(num_epochs):             epoch_cost=0             num_minibatches=int(m/minibatch_size)             seed+=1             #下面输入要求（6，,1080）格式,所以要加个转置             minibatches=random_mini_batches1(X_train,Y_train,minibatch_size,seed)                          for minibatch in minibatches:                 (minibatch_X,minibatch_Y)=minibatch                 _,minibatch_cost=sess.run([optm,cost],feed_dict={X:minibatch_X,Y:minibatch_Y})                 epoch_cost+=minibatch_cost/num_minibatches             if(print_cost==True and epoch % 2==0):                 #print("Epoch",'%04d' % (epoch+1),"cost={:.9f}".format(epoch_cost))                 print("Cost after epoch %i:%f" % (epoch,epoch_cost))             if(print_cost==True and epoch %1==0):                 costs.append(epoch_cost)                          print("Train Accuracy:",accuracy.eval({X:X_train,Y:Y_train.T}))         print("Test Accuracy:",accuracy.eval({X:X_test,Y:Y_test.T}))         plt.plot(np.squeeze(costs))         plt.ylabel('cost')         plt.xlabel('iterations(per tens)')         plt.title("learning rate="+str(learning_rate))         plt.show()                  parameters=sess.run(parameters)         return parameters parameters=model(X_train,Y_train,X_test,Y_test)  `

## 更多资讯推荐

CPU比GPU训练神经网络快十几倍，英特尔：别用矩阵运算了

Pytorch教程：新手的快速指南

RUDN 大学的数学家团队找到一种新方法，该方法能够让神经网络的大小减小到六分之一，且无需花费更多的资源重新训练。