1+ # -*- coding: utf-8 -*-
2+ # Author: XuMing <[email protected] > 3+ # Brief:
4+ import numpy as np
5+ import keras
6+ from keras .layers import Conv2D , MaxPool2D
7+ from keras .layers import Dense , Dropout , Flatten
8+ from keras .models import Sequential
9+
10+ x_train = np .random .random ((100 , 100 , 100 , 3 ))
11+ y_train = keras .utils .to_categorical (np .random .randint (10 , size = (100 , 1 )), num_classes = 10 )
12+ x_test = np .random .random ((20 , 100 , 100 , 3 ))
13+ y_test = keras .utils .to_categorical (np .random .randint (10 , size = (20 , 1 )), num_classes = 10 )
14+
15+ model = Sequential ()
16+ # input is 100x100 images with 3 channels
17+ model .add (Conv2D (32 , (3 , 3 ), activation = 'relu' , input_shape = (100 , 100 , 3 )))
18+ model .add (Conv2D (32 , (3 , 3 ), activation = 'relu' ))
19+ model .add (MaxPool2D (pool_size = (2 , 2 )))
20+ model .add (Dropout (0.25 ))
21+
22+ model .add (Conv2D (64 , (3 , 3 ), activation = 'relu' ))
23+ model .add (Conv2D (64 , (3 , 3 ), activation = 'relu' ))
24+ model .add (MaxPool2D (pool_size = (2 , 2 )))
25+ model .add (Dropout (0.25 ))
26+
27+ model .add (Flatten ())
28+ model .add (Dense (256 , activation = 'relu' ))
29+ model .add (Dropout (0.5 ))
30+ model .add (Dense (10 , activation = 'softmax' ))
31+
32+ sgd = keras .optimizers .SGD (lr = 0.01 , decay = 1e-6 , momentum = 0.9 , nesterov = True )
33+ model .compile (loss = 'categorical_crossentropy' , optimizer = sgd , metrics = ['accuracy' ])
34+ model .fit (x_train , y_train , epochs = 10 , batch_size = 32 )
35+ score = model .evaluate (x_test , y_test , batch_size = 32 )
36+ print ('total loss on test set:' , score [0 ])
37+ print ('accuracy of test set:' , score [1 ])
38+
39+ # rmsprop loss 2.28, acc 0.13
40+ # sgd loss 2.26, acc 0.18
0 commit comments