CNN 卷积神经网络 2

建立全连接层

进入全连接层时, 通过tf.reshape()h_pool2的输出值从一个三维的变为一维的数据

-1表示先不考虑输入图片例子维度, 将上一个输出结果展平.

#[n_samples,7,7,64]->>[n_samples,7*7*64]
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])
W_fc1=weight_variable([7*7*64,1024]) 
b_fc1=bias_variable([1024])

将展平后的h_pool2_flat与本层的W_fc1相乘(注意这个时候不是卷积了)

h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)

dropout处理:

h_fc1_drop=tf.nn.dropout(h_fc1,keep_drop)

最后一层的构建:

W_fc2=weight_variable([1024,10]) b_fc2=bias_variable([10])

softmax分类器(多分类,输出是各个类的概率),对输出进行分类

prediction=tf.nn.softmax(tf.matmul(h_fc1_dropt,W_fc2),b_fc2)

选优化方法

利用交叉熵损失函数来定义cost function:

Last updated

Was this helpful?