示例#1
0
def point_test_grad_impl():
    # check gradient implementation
    rng = nprs(42)
    test_ob = rng.uniform(size=(4, ))
    test_act = rng.uniform(size=(4, ))
    test_theta = rng.uniform(size=(4, 5))
    # Check that the shape matches
    assert point_get_grad_logp_action(test_theta, test_ob,
                                      test_act).shape == test_theta.shape
    gradient_check(
        lambda x: point_get_logp_action(x.reshape(test_theta.shape), test_ob,
                                        test_act),
        lambda x: point_get_grad_logp_action(x.reshape(test_theta.shape),
                                             test_ob, test_act).flatten(),
        test_theta.flatten())
示例#2
0
def point_test_grad_impl():
    # check gradient implementation
    rng = nprs(42)
    test_ob = rng.uniform(size=(4,))
    test_act = rng.uniform(size=(4,))
    test_theta = rng.uniform(size=(4, 5))
    # Check that the shape matches
    assert point_get_grad_logp_action(
        test_theta, test_ob, test_act).shape == test_theta.shape
    gradient_check(
        lambda x: point_get_logp_action(
            x.reshape(test_theta.shape), test_ob, test_act),
        lambda x: point_get_grad_logp_action(
            x.reshape(test_theta.shape), test_ob, test_act).flatten(),
        test_theta.flatten()
    )
示例#3
0
from simplepg.simple_utils import register_test, nprs
import numpy as np
from chainer import Variable

register_test("a2c.compute_returns_advantages",
              kwargs=lambda: dict(
                  rewards=nprs(0).uniform(size=(5, 2)),
                  dones=nprs(1).choice([True, False], size=(5, 2)),
                  values=nprs(2).uniform(size=(5, 2)),
                  next_values=nprs(3).uniform(size=(2, )),
                  discount=0.99,
              ),
              desired_output=lambda:
              (np.array([[1.14554925, 1.25462372], [0.60276338, 0.54488318],
                         [2.33579066, 1.90456042], [1.93145037, 1.2713801],
                         [1.50895268, 0.38344152]]),
               np.array([[0.70955434, 1.22869749], [0.0531009, 0.10956079],
                         [1.91542286, 1.5742256], [1.72680173, 0.65210914],
                         [1.20929801, 0.11661424]])))

register_test(
    "a2c.compute_total_loss",
    kwargs=lambda: dict(
        logli=Variable(nprs(0).uniform(size=(10, )).astype(np.float32)),
        all_advs=Variable(nprs(1).uniform(size=(10, )).astype(np.float32)),
        ent_coeff=nprs(2).uniform(),
        ent=Variable(nprs(3).uniform(size=(10, )).astype(np.float32)),
        vf_loss_coeff=nprs(4).uniform(),
        all_returns=Variable(nprs(5).uniform(size=(10, )).astype(np.float32)),
        all_values=Variable(nprs(6).uniform(size=(10, )).astype(np.float32)),
    ),
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""


from chainer import Variable

from simplepg.simple_utils import register_test, nprs
from utils import Gaussian
import numpy as np

register_test(
    "pg.compute_surr_loss",
    kwargs=lambda: dict(
        dists=Gaussian(
            means=Variable(nprs(0).uniform(size=(10, 3)).astype(np.float32)),
            log_stds=Variable(nprs(1).uniform(
                size=(10, 3)).astype(np.float32)),
        ),
        all_acts=Variable(nprs(2).uniform(size=(10, 3)).astype(np.float32)),
        all_advs=Variable(nprs(3).uniform(size=(10,)).astype(np.float32)),
    ),
    desired_output=lambda: Variable(
        np.array(1.9201269149780273, dtype=np.float32))
)
示例#5
0
from chainer import Variable

from simplepg.simple_utils import register_test, nprs
from utils import Gaussian
import numpy as np

register_test(
    "pg.compute_surr_loss",
    kwargs=lambda: dict(
        dists=Gaussian(
            means=Variable(nprs(0).uniform(size=(10, 3)).astype(np.float32)),
            log_stds=Variable(
                nprs(1).uniform(size=(10, 3)).astype(np.float32)),
        ),
        all_acts=Variable(nprs(2).uniform(size=(10, 3)).astype(np.float32)),
        all_advs=Variable(nprs(3).uniform(size=(10, )).astype(np.float32)),
    ),
    desired_output=lambda: Variable(
        np.array(1.9201269149780273, dtype=np.float32)))
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""


from simplepg.simple_utils import register_test, nprs
import numpy as np

register_test(
    "__main__.compute_update",
    kwargs=lambda: dict(
        discount=0.99,
        R_tplus1=1.0,
        theta=nprs(0).uniform(size=(2, 2)),
        s_t=nprs(1).uniform(size=(1,)),
        a_t=nprs(2).choice(2),
        r_t=nprs(3).uniform(),
        b_t=nprs(4).uniform(),
        get_grad_logp_action=lambda theta, *_: theta * 2
    ),
    desired_output=lambda: (
        1.5407979025745755,
        np.array([[0.62978332, 0.82070564], [0.69169275, 0.62527314]])
    )
)

register_test(
    "__main__.compute_baselines",
    kwargs=lambda: dict(
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""


from simplepg.simple_utils import register_test, nprs
import numpy as np
from chainer import Variable

register_test(
    "a2c.compute_returns_advantages",
    kwargs=lambda: dict(
        rewards=nprs(0).uniform(size=(5, 2)),
        dones=nprs(1).choice([True, False], size=(5, 2)),
        values=nprs(2).uniform(size=(5, 2)),
        next_values=nprs(3).uniform(size=(2,)),
        discount=0.99,
    ),
    desired_output=lambda: (
        np.array([[1.14554925, 1.25462372],
                  [0.60276338, 0.54488318],
                  [2.33579066, 1.90456042],
                  [1.93145037, 1.2713801],
                  [1.50895268, 0.38344152]]),
        np.array([[0.70955434, 1.22869749],
                  [0.0531009, 0.10956079],
                  [1.91542286, 1.5742256],
                  [1.72680173, 0.65210914],
示例#8
0
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""

from simplepg.simple_utils import register_test, nprs
import numpy as np

register_test(
    "__main__.compute_update",
    kwargs=lambda: dict(discount=0.99,
                        R_tplus1=1.0,
                        theta=nprs(0).uniform(size=(2, 2)),
                        s_t=nprs(1).uniform(size=(1, )),
                        a_t=nprs(2).choice(2),
                        r_t=nprs(3).uniform(),
                        b_t=nprs(4).uniform(),
                        get_grad_logp_action=lambda theta, *_: theta * 2),
    desired_output=lambda:
    (1.5407979025745755,
     np.array([[0.62978332, 0.82070564], [0.69169275, 0.62527314]])))

register_test("__main__.compute_baselines",
              kwargs=lambda: dict(all_returns=[
                  nprs(0).uniform(size=(10, )),
                  nprs(1).uniform(size=(20, )),
                  [],
              ], ),