example.py 1.4 KB
Newer Older
R
rensilin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
This is an example of network building
"""

from __future__ import print_function, division
import paddle
from paddle import fluid

def inference():
    """Build inference network(without loss and optimizer)

    Returns:
        list<Variable>: inputs
        and
R
rensilin 已提交
18
        list<Variable>: outputs
R
rensilin 已提交
19 20 21 22 23
    """
    # TODO: build network here
    cvm_input = fluid.layers.data(name='cvm_input', shape=[4488], dtype='float32')

    net = cvm_input
R
rensilin 已提交
24 25 26 27 28 29 30
    net = fluid.layers.fc(net, 512, act='relu', name='fc_1')
    net = fluid.layers.fc(net, 256, act='relu', name='fc_2')
    net = fluid.layers.fc(net, 256, act='relu', name='fc_3')
    net = fluid.layers.fc(net, 128, act='relu', name='fc_4')
    net = fluid.layers.fc(net, 128, act='relu', name='fc_5')
    net = fluid.layers.fc(net, 128, act='relu', name='fc_6')
    net = fluid.layers.fc(net, 128, act='relu', name='fc_7')
R
rensilin 已提交
31

R
rensilin 已提交
32 33
    ctr_output = fluid.layers.fc(net, 1, act='sigmoid', name='ctr')
    return [cvm_input], [ctr_output]
R
rensilin 已提交
34

R
rensilin 已提交
35
def loss_function(ctr_output):
R
rensilin 已提交
36 37
    """
    Args:
R
rensilin 已提交
38
        *outputs: the second result of inference()
R
rensilin 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51

    Returns:
        Variable: loss
        and
        list<Variable>: labels
    """
    # TODO: calc loss here

    label = fluid.layers.data(name='label_ctr', shape=ctr_output.shape, dtype='float32')
    loss = fluid.layers.square_error_cost(input=ctr_output, label=label)
    loss = fluid.layers.mean(loss, name='loss_ctr')

    return loss, [label]