test_error_clip.py 2.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15
import paddle
16
import paddle.fluid as fluid
17 18 19 20 21

BATCH_SIZE = 128
CLIP_MAX = 2e-6
CLIP_MIN = -1e-6

P
pangyoki 已提交
22
paddle.enable_static()
23 24 25
prog = fluid.framework.Program()

with fluid.program_guard(main_program=prog):
G
GGBond8488 已提交
26
    image = paddle.static.data(name='x', shape=[-1, 784], dtype='float32')
27

C
Charles-hit 已提交
28 29 30
    hidden1 = paddle.static.nn.fc(x=image, size=128, activation='relu')
    hidden2 = paddle.static.nn.fc(x=hidden1, size=64, activation='relu')
    predict = paddle.static.nn.fc(x=hidden2, size=10, activation='softmax')
31

G
GGBond8488 已提交
32
    label = paddle.static.data(name='y', shape=[-1, 1], dtype='int64')
33

34 35 36
    cost = paddle.nn.functional.cross_entropy(
        input=predict, label=label, reduction='none', use_softmax=False
    )
37
    avg_cost = paddle.mean(cost)
38 39

prog_clip = prog.clone()
W
Wu Yi 已提交
40
prog_clip.block(0).var(hidden1.name)._set_error_clip(
41
    paddle.nn.clip.ErrorClipByValue(max=CLIP_MAX, min=CLIP_MIN)
42
)
43 44 45

avg_cost_clip = prog_clip.block(0).var(avg_cost.name)
fluid.backward.append_backward(loss=avg_cost)
46
fluid.backward.append_backward(
47
    loss=avg_cost_clip, callbacks=[paddle.nn.clip.error_clip_callback]
48
)
49 50 51 52

hidden1_grad = prog.block(0).var(hidden1.name + "@GRAD")
hidden1_grad_clip = prog_clip.block(0).var(hidden1.name + "@GRAD")

F
fengjiayi 已提交
53 54 55
hidden2_grad = prog.block(0).var(hidden2.name + "@GRAD")
hidden2_grad_clip = prog_clip.block(0).var(hidden2.name + "@GRAD")

56 57 58 59
train_reader = paddle.batch(
    paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
    batch_size=BATCH_SIZE,
)
60 61 62 63 64 65 66 67 68 69 70

place = fluid.CPUPlace()
exe = fluid.Executor(place)
feeder = fluid.DataFeeder(feed_list=[image, label], place=place)
exe.run(fluid.default_startup_program())

count = 0
for data in train_reader():
    count += 1
    if count > 5:
        break
71 72 73
    out1, out2 = exe.run(
        prog, feed=feeder.feed(data), fetch_list=[hidden1_grad, hidden2_grad]
    )
F
fengjiayi 已提交
74 75 76
    out1_clip, out2_clip = exe.run(
        prog_clip,
        feed=feeder.feed(data),
77 78 79 80 81 82
        fetch_list=[hidden1_grad_clip, hidden2_grad_clip],
    )
    if not (
        (out1.clip(min=CLIP_MIN, max=CLIP_MAX) == out1_clip).all()
        and (out2 == out2_clip).all()
    ):
83 84 85
        exit(1)

exit(0)