test_memopt_fit_a_line.py 2.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Y
ying 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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.

15 16
import numpy as np
import paddle.v2 as paddle
17
import paddle.fluid as fluid
T
typhoonzero 已提交
18 19
import math
import sys
20

21 22 23 24 25
# need to fix random seed and training data to compare the loss
# value accurately calculated by the default and the memory optimization
# version.
fluid.default_startup_program().random_seed = 111

26 27 28
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y = fluid.layers.data(name='y', shape=[1], dtype='float32')

Q
qijun 已提交
29 30 31 32 33
device_type = 'CPU'
use_nccl = False
place = fluid.CPUPlace()
if fluid.core.is_compiled_with_cuda():
    device_type = 'CUDA'
Q
qijun 已提交
34
    use_nccl = False
Q
qijun 已提交
35 36
    place = fluid.CUDAPlace(0)

Q
qijun 已提交
37
places = fluid.layers.get_places(device_count=0, device_type=device_type)
Q
qijun 已提交
38
pd = fluid.layers.ParallelDo(places, use_nccl=use_nccl)
39 40 41 42 43 44 45
with pd.do():
    x_ = pd.read_input(x)
    y_ = pd.read_input(y)
    y_predict = fluid.layers.fc(input=x_, size=1, act=None)
    cost = fluid.layers.square_error_cost(input=y_predict, label=y_)
    avg_cost = fluid.layers.mean(x=cost)
    pd.write_output(avg_cost)
46

47 48 49
cost = pd()
avg_cost = fluid.layers.mean(x=cost)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.01)
50 51
sgd_optimizer.minimize(avg_cost)

52
fluid.memory_optimize(fluid.default_main_program())
53 54 55

BATCH_SIZE = 200

56
# fix the order of training data
57
train_reader = paddle.batch(
58 59 60 61 62 63
    paddle.dataset.uci_housing.train(), batch_size=BATCH_SIZE)

# train_reader = paddle.batch(
#     paddle.reader.shuffle(
#         paddle.dataset.uci_housing.train(), buf_size=500),
#     batch_size=BATCH_SIZE)
64 65 66 67 68 69 70 71 72 73 74

feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
exe = fluid.Executor(place)

exe.run(fluid.default_startup_program())

PASS_NUM = 100
for pass_id in range(PASS_NUM):
    fluid.io.save_persistables(exe, "./fit_a_line.model/")
    fluid.io.load_persistables(exe, "./fit_a_line.model/")
    for data in train_reader():
75
        avg_loss_value, = exe.run(fluid.default_main_program(),
76 77 78 79 80
                                  feed=feeder.feed(data),
                                  fetch_list=[avg_cost])

        if avg_loss_value[0] < 10.0:
            exit(0)  # if avg cost less than 10.0, we think our code is good.
81
        print avg_loss_value[0]
T
typhoonzero 已提交
82 83
        if math.isnan(float(avg_loss_value)):
            sys.exit("got NaN loss, training failed.")
84
exit(1)