# 18.3 实现几何图形及颜色分类

## 18.3 实现几何图形及颜色分类⚓︎

### 18.3.2 用前馈神经网络解决问题⚓︎

ef dnn_model():
num_output = 9
max_epoch = 50
batch_size = 16
learning_rate = 0.01
params = HyperParameters_4_2(
learning_rate, max_epoch, batch_size,
net_type=NetType.MultipleClassifier,
init_method=InitialMethod.MSRA,
optimizer_name=OptimizerName.Momentum)

net = NeuralNet_4_2(params, "color_shape_dnn")

f1 = FcLayer_2_0(784, 128, params)
r1 = ActivationLayer(Relu())

f2 = FcLayer_2_0(f1.output_size, 64, params)
r2 = ActivationLayer(Relu())

f3 = FcLayer_2_0(f2.output_size, num_output, params)
s3 = ClassificationLayer(Softmax())

return net


......
epoch=49, total_iteration=15199
loss_train=0.003370, accuracy_train=1.000000
loss_valid=0.510589, accuracy_valid=0.883333
time used: 25.34346342086792
testing...
0.9011111111111111
0.8988888888888888


### 18.3.3 用卷积神经网络解决问题⚓︎

def cnn_model():
num_output = 9
max_epoch = 20
batch_size = 16
learning_rate = 0.1
params = HyperParameters_4_2(
learning_rate, max_epoch, batch_size,
net_type=NetType.MultipleClassifier,
init_method=InitialMethod.MSRA,
optimizer_name=OptimizerName.SGD)

net = NeuralNet_4_2(params, "shape_color_cnn")

c1 = ConvLayer((3,28,28), (8,3,3), (1,1), params)
r1 = ActivationLayer(Relu())
p1 = PoolingLayer(c1.output_shape, (2,2), 2, PoolingTypes.MAX)

c2 = ConvLayer(p1.output_shape, (16,3,3), (1,0), params)
r2 = ActivationLayer(Relu())
p2 = PoolingLayer(c2.output_shape, (2,2), 2, PoolingTypes.MAX)

params.learning_rate = 0.1

f3 = FcLayer_2_0(p2.output_size, 32, params)
bn3 = BnLayer(f3.output_size)
r3 = ActivationLayer(Relu())

f4 = FcLayer_2_0(f3.output_size, num_output, params)
s4 = ClassificationLayer(Softmax())

return net


......
epoch=19, total_iteration=6079
loss_train=0.005184, accuracy_train=1.000000
loss_valid=0.118708, accuracy_valid=0.957407
time used: 131.77996039390564
testing...
0.97
0.97


### 代码位置⚓︎

ch18, Level3_ColorAndShapeConvNet.py

### 思考和练习⚓︎

1. 我们使用了3x3的卷积核，如果用5x5的卷积核，但是在其它参数不变的情况下，其效果会不会更好？