smallnet_mnist_cifar.py 1.9 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
D
dangqingqing 已提交
14 15 16 17
#!/usr/bin/env python

from paddle.trainer_config_helpers import *

18 19
height = 32
width = 32
D
dangqingqing 已提交
20 21
num_class = 10

22
batch_size = get_config_arg('batch_size', int, 128)
D
dangqingqing 已提交
23

24 25 26
args = {'height': height, 'width': width, 'color': True, 'num_class': num_class}
define_py_data_sources2(
    "train.list", None, module="provider", obj="process", args=args)
D
dangqingqing 已提交
27 28

settings(
29 30 31 32
    batch_size=batch_size,
    learning_rate=0.01 / batch_size,
    learning_method=MomentumOptimizer(0.9),
    regularization=L2Regularization(0.0005 * batch_size))
D
dangqingqing 已提交
33 34 35

# conv1
net = data_layer('data', size=height * width * 3)
36 37 38 39 40 41 42
net = img_conv_layer(
    input=net,
    filter_size=5,
    num_channels=3,
    num_filters=32,
    stride=1,
    padding=2)
D
dangqingqing 已提交
43 44 45
net = img_pool_layer(input=net, pool_size=3, stride=2, padding=1)

# conv2
46 47 48 49
net = img_conv_layer(
    input=net, filter_size=5, num_filters=32, stride=1, padding=2)
net = img_pool_layer(
    input=net, pool_size=3, stride=2, padding=1, pool_type=AvgPooling())
D
dangqingqing 已提交
50 51

# conv3
52 53 54 55
net = img_conv_layer(
    input=net, filter_size=3, num_filters=64, stride=1, padding=1)
net = img_pool_layer(
    input=net, pool_size=3, stride=2, padding=1, pool_type=AvgPooling())
D
dangqingqing 已提交
56 57 58 59 60 61 62

net = fc_layer(input=net, size=64, act=ReluActivation())
net = fc_layer(input=net, size=10, act=SoftmaxActivation())

lab = data_layer('label', num_class)
loss = classification_cost(input=net, label=lab)
outputs(loss)