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

(左右滑动，查看完整代码）

```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)
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):
def max_pool_2x2(x):

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)

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)```

197 篇文章98 人订阅

0 条评论

## 相关文章

42460

235100

1.9K50

52280

### 机器学习模型训练时候tricks

1. 当训练集的效果（准确率）上不去，和贝叶斯估计（人的表现）存在一定差距的时候：（1）增加模型的复杂度。 或者直接多加几层。（2）现在目前效果非常好的一些模型...

22170

### 深度学习中IU、IoU(Intersection over Union)的概念理解以及python程序实现

Intersection over Union是一种测量在特定数据集中检测相应物体准确度的一个标准。我们可以在很多物体检测挑战中，例如PASCAL VOC ch...

1.1K30

38060

21120

92050

12310