multi_fc.py 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
"""
A fake model with multiple FC layers to test CINN on a more complex model.
"""
import paddle
import paddle.fluid as fluid

size = 2
num_layers = 4
paddle.enable_static()

G
GGBond8488 已提交
24 25
a = paddle.static.data(name="A", shape=[-1, size], dtype='float32')
label = paddle.static.data(name="label", shape=[-1, size], dtype='float32')
26

C
Charles-hit 已提交
27 28
fc_out = paddle.static.nn.fc(
    x=a,
29
    size=size,
C
Charles-hit 已提交
30
    activation="relu",
31 32 33
    bias_attr=fluid.ParamAttr(name="fc_bias"),
    num_flatten_dims=1,
)
34 35

for i in range(num_layers - 1):
C
Charles-hit 已提交
36 37
    fc_out = paddle.static.nn.fc(
        x=fc_out,
38
        size=size,
C
Charles-hit 已提交
39
        activation="relu",
40 41 42
        bias_attr=fluid.ParamAttr(name="fc_bias"),
        num_flatten_dims=1,
    )
43 44

cost = fluid.layers.square_error_cost(fc_out, label)
2
201716010711 已提交
45
avg_cost = paddle.mean(cost)
46 47 48 49 50 51 52 53 54 55

optimizer = fluid.optimizer.SGD(learning_rate=0.001)
optimizer.minimize(avg_cost)

cpu = fluid.core.CPUPlace()
loss = exe = fluid.Executor(cpu)

exe.run(fluid.default_startup_program())

fluid.io.save_inference_model("./multi_fc_model", [a.name], [fc_out], exe)
56 57 58 59 60 61 62 63 64
fluid.io.save_inference_model(
    "./multi_fc_model",
    [a.name],
    [fc_out],
    exe,
    None,
    "fc.pdmodel",
    "fc.pdiparams",
)
65

66
print('output name', fc_out.name)