当前位置: 首页>>代码示例>>Python>>正文


Python functions.clip方法代码示例

本文整理汇总了Python中chainer.functions.clip方法的典型用法代码示例。如果您正苦于以下问题:Python functions.clip方法的具体用法?Python functions.clip怎么用?Python functions.clip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer.functions的用法示例。


在下文中一共展示了functions.clip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: pretraining

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def pretraining(optimizer):
    logger.info('pretraining')
    copy_grand_opt = copy.deepcopy(optimizer.grand_optimizer)
    losses = []
    for _ in range(10):
        x = optimizer.optnet.xp.random.normal(
            scale=10., size=(10000, 1)).astype('f')
        g = optimizer.optnet.step(x)
        # loss forcing g's sign to be the flip of input's sign
        # theta = theta - c*gradient
        # theta = theta + g
        loss = F.mean(F.clip(g, 0, 100) * (x > 0)
                      + F.clip(-g, 0, 100) * (x < 0))
        optimizer.optnet.cleargrads()
        loss.backward()
        optimizer.meta_update()
        optimizer.optnet.reset_state()
        losses.append(loss.item())
    logger.info('finished pretraining. losses {}'.format(losses))
    optimizer.release_all()
    # reset adam state
    optimizer = nets.optnets.OptimizerByNet(optimizer.optnet, copy_grand_opt)
    return optimizer, copy_grand_opt 
开发者ID:chainer,项目名称:models,代码行数:25,代码来源:train_mnist.py

示例2: get_aabb_corners

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def get_aabb_corners(grids, image_size):
    _, _, height, width = grids.shape
    grids = (grids + 1) / 2
    x_points = grids[:, 0, ...] * image_size.width
    y_points = grids[:, 1, ...] * image_size.height
    x_points = F.clip(x_points, 0., float(image_size.width))
    y_points = F.clip(y_points, 0., float(image_size.height))
    top_left_x = F.get_item(x_points, [..., 0, 0])
    top_left_y = F.get_item(y_points, [..., 0, 0])
    top_right_x = F.get_item(x_points, [..., 0, width - 1])
    top_right_y = F.get_item(y_points, [..., 0, width - 1])
    bottom_right_x = F.get_item(x_points, [..., height - 1, width - 1])
    bottom_right_y = F.get_item(y_points, [..., height - 1, width - 1])
    bottom_left_x = F.get_item(x_points, [..., height - 1, 0])
    bottom_left_y = F.get_item(y_points, [..., height - 1, 0])

    top_left_x_aabb = F.minimum(top_left_x, bottom_left_x)
    top_left_y_aabb = F.minimum(top_left_y, top_right_y)
    bottom_right_x_aabb = F.maximum(top_right_x, bottom_right_x)
    bottom_right_y_aabb = F.maximum(bottom_left_y, bottom_right_y)

    return top_left_y_aabb, top_left_x_aabb, bottom_right_y_aabb, bottom_right_x_aabb 
开发者ID:Bartzi,项目名称:kiss,代码行数:24,代码来源:match_bbox.py

示例3: _elementwise_clip

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def _elementwise_clip(x, x_min, x_max):
    """Elementwise clipping

    Note: chainer.functions.clip supports clipping to constant intervals
    """
    return F.minimum(F.maximum(x, x_min), x_max) 
开发者ID:chainer,项目名称:chainerrl,代码行数:8,代码来源:ppo.py

示例4: _lossfun

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def _lossfun(self,
                 entropy, vs_pred, log_probs,
                 vs_pred_old, log_probs_old,
                 advs, vs_teacher):

        prob_ratio = F.exp(log_probs - log_probs_old)

        loss_policy = - F.mean(F.minimum(
            prob_ratio * advs,
            F.clip(prob_ratio, 1 - self.clip_eps, 1 + self.clip_eps) * advs))

        if self.clip_eps_vf is None:
            loss_value_func = F.mean_squared_error(vs_pred, vs_teacher)
        else:
            loss_value_func = F.mean(F.maximum(
                F.square(vs_pred - vs_teacher),
                F.square(_elementwise_clip(vs_pred,
                                           vs_pred_old - self.clip_eps_vf,
                                           vs_pred_old + self.clip_eps_vf)
                         - vs_teacher)
            ))
        loss_entropy = -F.mean(entropy)

        self.value_loss_record.append(float(loss_value_func.array))
        self.policy_loss_record.append(float(loss_policy.array))

        loss = (
            loss_policy
            + self.value_func_coef * loss_value_func
            + self.entropy_coef * loss_entropy
        )

        return loss 
开发者ID:chainer,项目名称:chainerrl,代码行数:35,代码来源:ppo.py

示例5: numpy_clip

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def numpy_clip(a, a_min, a_max, out=None):
    check_attribute_scalar(a_min, 'numpy.clip', 'a_min')
    check_attribute_scalar(a_max, 'numpy.clip', 'a_max')
    check_attribute_value(out, None, 'numpy.clip', 'out')

    a = F.clip(a, a_min, a_max)
    return a 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:9,代码来源:custom_functions.py

示例6: forward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def forward(self, x):
        y1 = F.clip(x, -1.0, 1.0)
        return y1 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:5,代码来源:MathMisc.py

示例7: main

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def main():
    np.random.seed(314)

    x = np.random.rand(6, 4).astype(np.float32)
    s_int = np.array(-10)
    s_float = np.array(10.0)

    testtools.generate_testcase(Sin(), [x], subname='sin')
    testtools.generate_testcase(Sinh(), [x], subname='sinh')
    testtools.generate_testcase(Sign(), [x], subname='sign')
    testtools.generate_testcase(Cos(), [x], subname='cos')
    testtools.generate_testcase(Cosh(), [x], subname='cosh')
    testtools.generate_testcase(Tan(), [x], subname='tan')
    testtools.generate_testcase(Tanh(), [x], subname='tanh')
    testtools.generate_testcase(ArcSin(), [x], subname='arcsin')
    testtools.generate_testcase(ArcCos(), [x], subname='arccos')
    testtools.generate_testcase(ArcTan(), [x], subname='arctan')
    testtools.generate_testcase(Exp(), [x], subname='exp')
    testtools.generate_testcase(Log(), [x], subname='log')
    testtools.generate_testcase(Clip(), [x], subname='clip')
    testtools.generate_testcase(ClipNp(), [x], subname='clip_np')
    testtools.generate_testcase(Abs(), [x], subname='abs')
    testtools.generate_testcase(AbsNp(), [x], subname='abs_np')
    testtools.generate_testcase(Sqrt(), [x], subname='sqrt')
    testtools.generate_testcase(Round(), [x], subname='round')
    testtools.generate_testcase(AbsBuiltin(), [x], subname='abs_builtin')
    testtools.generate_testcase(AbsBuiltin(), [s_float], subname='abs_builtin_scalar_float')
    testtools.generate_testcase(AbsBuiltin(), [s_int], subname='abs_builtin_scalar_int') 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:30,代码来源:MathMisc.py

示例8: __call__

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def __call__(self, x):
        x = self.head(x)
        x = self.sigmoid(x)
        if self.do_nms:
            y = self.pool(x)
            x = x * (y.array == x.array)
        else:
            eps = 1e-4
            x = F.clip(x, x_min=eps, x_max=(1.0 - eps))
        return x 
开发者ID:osmr,项目名称:imgclsmob,代码行数:12,代码来源:centernet.py

示例9: __call__

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def __call__(self, x):
        return F.clip(x, x_min=0.0, x_max=1.0) 
开发者ID:osmr,项目名称:imgclsmob,代码行数:4,代码来源:ghostnet.py

示例10: __call__

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def __call__(self, x):
        return F.clip(x, 0.0, 6.0) 
开发者ID:osmr,项目名称:imgclsmob,代码行数:4,代码来源:common.py

示例11: _compute_ppo_loss

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def _compute_ppo_loss(self, obs, acts, at, vt, old_params):
        params = self._pi_f(obs)
        cv = F.flatten(self._vf_f(obs))
        ratio = F.exp(self._logp(params, acts) - self._logp(old_params, acts))
        surr1 = ratio * at
        surr2 = F.clip(ratio, 1 - self._ppo_clipparam, 1 + self._ppo_clipparam) * at
        ppo_surr_loss = (
                -sym_mean(F.minimum(surr1, surr2))
                + self._ppo_klcoeff * sym_mean(self.kl(old_params, params))
                + sym_mean(F.square(cv - vt))
        )
        return ppo_surr_loss 
开发者ID:openai,项目名称:EPG,代码行数:14,代码来源:agents.py

示例12: check_forward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def check_forward(self, x_data):
        x_min, x_max = self.x_min_max
        x = chainer.Variable(x_data)
        y = functions.clip(x, x_min, x_max)
        self.assertEqual(y.data.dtype, self.dtype)

        y_expect = self.x.copy()
        for i in numpy.ndindex(self.x.shape):
            if (x_min is not None) and (self.x[i] < x_min):
                y_expect[i] = x_min
            elif (x_max is not None) and (self.x[i] > x_max):
                y_expect[i] = x_max

        testing.assert_allclose(y_expect, y.data) 
开发者ID:chainer,项目名称:chainer,代码行数:16,代码来源:test_clip.py

示例13: check_backward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def check_backward(self, x_data, y_grad):
        def f(x):
            x_min, x_max = self.x_min_max
            return functions.clip(x, x_min, x_max)

        gradient_check.check_backward(
            f, x_data, y_grad, dtype=numpy.float64) 
开发者ID:chainer,项目名称:chainer,代码行数:9,代码来源:test_clip.py

示例14: check_double_backward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def check_double_backward(self, x_data, y_grad, gx_grad):
        def f(x):
            x_min, x_max = self.x_min_max
            return functions.clip(x, x_min, x_max)

        gradient_check.check_double_backward(
            f, x_data, y_grad, gx_grad, dtype=numpy.float64, atol=1e-3) 
开发者ID:chainer,项目名称:chainer,代码行数:9,代码来源:test_clip.py

示例15: test_invalid_interval

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import clip [as 别名]
def test_invalid_interval(self):
        with self.assertRaises(ValueError):
            functions.clip(self.x, 1.0, -1.0) 
开发者ID:chainer,项目名称:chainer,代码行数:5,代码来源:test_clip.py


注:本文中的chainer.functions.clip方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。