algorithm.py 5.2 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#   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.

import paddle.fluid as fluid
import parl.layers as layers
from parl.layers import Network
import parl.framework.policy_distribution as pd
from abc import ABCMeta, abstractmethod


def check_duplicate_spec_names(model):
    """
    Check if there are two specs that have the same name.
    """
    specs = model.get_input_specs() \
            + model.get_action_specs() \
            + model.get_state_specs() \
            + model.get_reward_specs()
    names = [name for name, _ in specs]
    duplicates = set([n for n in names if names.count(n) > 1])
    assert not duplicates, \
        "duplicate names with different specs: " + " ".join(duplicates)


class Model(Network):
    """
H
Haonan 已提交
38
    A Model is owned by an Algorithm. It implements the entire network model of
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    a specific problem.
    """
    __metaclass__ = ABCMeta

    def __init__(self):
        super(Model, self).__init__()

    @abstractmethod
    def get_input_specs(self):
        """
        Output: list of tuples
        """
        pass

    def get_state_specs(self):
        """
        States are optional to a Model.
        Output: list of tuples
        """
        return []

    @abstractmethod
    def get_action_specs(self):
        """
        Output: list of tuples
        """
        pass

    def get_reward_specs(self):
        """
        By default, a scalar reward.
        User can specify a vector of rewards for some problems
        """
        return [("reward", dict(shape=[1]))]

    def policy(self, inputs, states):
        """
        Return: action_dists: a dict of action distribution objects
                states
                An action distribution object can be created with
                PolicyDistribution().
        Optional: a model might not always have to implement policy()
        """
        raise NotImplementedError()

    def value(self, inputs, states):
        """
        Return: values: a dict of estimated values for the current observations and states
                        For example, "q_value" and "v_value"
        Optional: a model might not always have to implement value()
        """
        raise NotImplementedError()


class Algorithm(object):
    """
    An Algorithm implements two functions:
    1. predict() computes forward
    2. learn() computes a cost for optimization

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

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

    def get_input_specs(self):
        return self.model.get_input_specs()

    def get_state_specs(self):
        return self.model.get_state_specs()

    def get_action_specs(self):
        """
        For non-RL algortihms, this can return []
        """
        return self.model.get_action_specs()

    def get_reward_specs(self):
        """
        For non-RL algortihms, this can return []
        """
        return self.model.get_reward_specs()

    def before_every_batch(self):
        """
        A callback function inserted before every batch of training.
        See ComputationTask.learn()
        """
        pass

    def after_every_batch(self):
        """
        A callback function inserted after every batch of training.
        See ComputationTask.learn()
        """
        pass

    def predict(self, inputs, states):
        """
        Given the inputs and states, this function does forward prediction and updates states.
H
Haonan 已提交
145 146 147
        Input: inputs(dict), states(dict)
        Output: actions(dict), states(dict)

148 149 150 151
        Optional: an algorithm might not implement predict()
        """
        pass

H
Haonan 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    def _rl_predict(self, behavior_model, inputs, states):
        """
        Given a behavior model (not necessarily equal to self.model), this function
        performs a normal RL prediction according to inputs and states.
        A behavior model different from self.model indicates off-policy training.

        The user can choose to call this function for convenience.
        """
        distributions, states = behavior_model.policy(inputs, states)
        actions = {}
        for key, dist in distributions.iteritems():
            actions[key] = dist()
        return actions, states

    def learn(self, inputs, next_inputs, states, next_states, next_episode_end,
167 168 169 170 171 172 173 174 175
              actions, rewards):
        """
        This function computes a learning cost to be optimized.
        The return should be the cost.
        Output: cost(dict)

        Optional: an algorithm might not implement learn()
        """
        pass