|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import os |
| 18 | +import time |
| 19 | +import warnings |
| 20 | +import numpy as np |
| 21 | +import logging |
| 22 | +import paddle.fluid as fluid |
| 23 | + |
| 24 | +from paddlerec.core.utils import envs |
| 25 | +from paddlerec.core.metric import Metric |
| 26 | +from paddlerec.core.trainers.framework.runner import RunnerBase |
| 27 | + |
| 28 | +logging.basicConfig( |
| 29 | + format='%(asctime)s - %(levelname)s: %(message)s', level=logging.INFO) |
| 30 | + |
| 31 | + |
| 32 | +class OnlineLearningRunner(RunnerBase): |
| 33 | + def __init__(self, context): |
| 34 | + print("Running OnlineLearningRunner.") |
| 35 | + |
| 36 | + def run(self, context): |
| 37 | + epochs = int( |
| 38 | + envs.get_global_env("runner." + context["runner_name"] + |
| 39 | + ".epochs")) |
| 40 | + model_dict = context["env"]["phase"][0] |
| 41 | + model_class = context["model"][model_dict["name"]]["model"] |
| 42 | + metrics = model_class._metrics |
| 43 | + |
| 44 | + dataset_list = [] |
| 45 | + dataset_index = 0 |
| 46 | + for day_index in range(len(days)): |
| 47 | + day = days[day_index] |
| 48 | + cur_path = "%s/%s" % (path, str(day)) |
| 49 | + filelist = fleet.split_files(hdfs_ls([cur_path])) |
| 50 | + dataset = create_dataset(use_var, filelist) |
| 51 | + dataset_list.append(dataset) |
| 52 | + dataset_index += 1 |
| 53 | + |
| 54 | + dataset_index = 0 |
| 55 | + for epoch in range(len(days)): |
| 56 | + day = days[day_index] |
| 57 | + begin_time = time.time() |
| 58 | + result = self._run(context, model_dict) |
| 59 | + end_time = time.time() |
| 60 | + seconds = end_time - begin_time |
| 61 | + message = "epoch {} done, use time: {}".format(epoch, seconds) |
| 62 | + |
| 63 | + # TODO, wait for PaddleCloudRoleMaker supports gloo |
| 64 | + from paddle.fluid.incubate.fleet.base.role_maker import GeneralRoleMaker |
| 65 | + if context["fleet"] is not None and isinstance(context["fleet"], |
| 66 | + GeneralRoleMaker): |
| 67 | + metrics_result = [] |
| 68 | + for key in metrics: |
| 69 | + if isinstance(metrics[key], Metric): |
| 70 | + _str = metrics[key].calc_global_metrics( |
| 71 | + context["fleet"], |
| 72 | + context["model"][model_dict["name"]]["scope"]) |
| 73 | + metrics_result.append(_str) |
| 74 | + elif result is not None: |
| 75 | + _str = "{}={}".format(key, result[key]) |
| 76 | + metrics_result.append(_str) |
| 77 | + if len(metrics_result) > 0: |
| 78 | + message += ", global metrics: " + ", ".join(metrics_result) |
| 79 | + print(message) |
| 80 | + with fluid.scope_guard(context["model"][model_dict["name"]][ |
| 81 | + "scope"]): |
| 82 | + train_prog = context["model"][model_dict["name"]][ |
| 83 | + "main_program"] |
| 84 | + startup_prog = context["model"][model_dict["name"]][ |
| 85 | + "startup_program"] |
| 86 | + with fluid.program_guard(train_prog, startup_prog): |
| 87 | + self.save(epoch, context, True) |
| 88 | + |
| 89 | + context["status"] = "terminal_pass" |
0 commit comments