algorithm_base.py 2.5 KB
Newer Older
B
Bo Zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 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.

from abc import ABCMeta, abstractmethod
H
Hongsheng Zeng 已提交
16
from parl.framework.model_base import Model
B
Bo Zhou 已提交
17 18 19 20 21 22 23

__all__ = ['Algorithm']


class Algorithm(object):
    """
    Algorithm defines the way how we update the model. For example,
H
Hongsheng Zeng 已提交
24
    after defining forward network in `Model` class, you should define how to update the model here.
B
Bo Zhou 已提交
25
    Before creating a customized algorithm, please do check algorithms of PARL.
H
Hongsheng Zeng 已提交
26 27
    Most common used algorithms like DQN/DDPG/PPO/A3C have been providing in algorithms, go and have a try.
    It's easy to use them and just try parl.algorithms.DQN.
B
Bo Zhou 已提交
28 29

    An Algorithm implements two functions:
H
Hongsheng Zeng 已提交
30
    1. define_predict() build forward process which was defined in `Model`
B
Bo Zhou 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    2. define_learn() computes a cost for optimization

    An algorithm should be updating part of a network. The user only needs to 
    implement the rest of the network(forward) in the Model class.
    """

    def __init__(self, model, hyperparas=None):
        assert isinstance(model, Model)
        self.model = model
        self.hp = hyperparas

    def define_predict(self, obs):
        """
        describe process for building predcition program
        """
        raise NotImplementedError()

    def define_learn(self, obs, action, reward, next_obs, terminal):
        """define how to update the model here, you may need to do the following:
            1. define a cost for optimization
            2. specify your optimizer
            3. optimize model defined in Model
        """
        raise NotImplementedError()
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    def get_params(self):
        """ Get parameters of self.model

        Returns:
            List of numpy array. 
        """
        return self.model.get_params()

    def set_params(self, params, gpu_id):
        """ Set parameters of self.model

        Args:
            params: List of numpy array.
            gpu_id: gpu id where self.model in. (if gpu_id < 0, means in cpu.)
        """
        self.model.set_params(params, gpu_id=gpu_id)