Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/typing-link
Browse files Browse the repository at this point in the history
  • Loading branch information
okapies committed Dec 19, 2018
2 parents 20c838f + 5a03f32 commit ea24d6c
Show file tree
Hide file tree
Showing 135 changed files with 2,281 additions and 1,409 deletions.
164 changes: 0 additions & 164 deletions .circleci/config.yml

This file was deleted.

5 changes: 1 addition & 4 deletions .gitignore
Expand Up @@ -11,7 +11,6 @@ build
.eggs/
_readthedocs_build
/TAGS
/docs/source/reference/**/generated
/tags
chainer.egg-info/
dist/
Expand All @@ -25,6 +24,4 @@ docs/my.state
docs/my_mnist.model
docs/mnist_result
docs/*.png
docs/source/reference/core
docs/source/reference/generated
docs/source/reference/util
/docs/source/**/reference/**/generated
7 changes: 7 additions & 0 deletions CODE_OF_CONDUCT.md
@@ -0,0 +1,7 @@
# Chainer Code of Conduct

Chainer follows the [NumFOCUS Code of Conduct][homepage] available at https://numfocus.org/code-of-conduct.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at chainer@preferred.jp.

[homepage]: https://numfocus.org/
4 changes: 3 additions & 1 deletion chainer/backends/cuda.py
Expand Up @@ -312,7 +312,7 @@ def get_device(*args):
.. note::
This API is deprecated. Please use
This API is deprecated since v3.0.0. Please use
:func:`~chainer.backends.cuda.get_device_from_id`
or :func:`~chainer.backends.cuda.get_device_from_array` instead.
Expand Down Expand Up @@ -716,6 +716,8 @@ def fuse(*args, **kwargs):
"""
if available:
return cupy.fuse(*args, **kwargs)
elif len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
return args[0]
else:
return lambda f: f

Expand Down
3 changes: 1 addition & 2 deletions chainer/dataset/convert.py
Expand Up @@ -6,7 +6,6 @@
import chainer
from chainer import backend
from chainer.backends import cuda
from chainer import utils


def to_device(device, x):
Expand Down Expand Up @@ -192,7 +191,7 @@ def _concat_arrays_with_padding(arrays, padding):
for i in six.moves.range(len(arrays)):
src = arrays[i]
slices = tuple(slice(dim) for dim in src.shape)
utils._setitem(result, (i,) + slices, src)
result[(i,) + slices] = src

return result

Expand Down
2 changes: 2 additions & 0 deletions chainer/distributions/__init__.py
Expand Up @@ -3,10 +3,12 @@
from chainer.distributions.bernoulli import Bernoulli # NOQA
from chainer.distributions.beta import Beta # NOQA
from chainer.distributions.categorical import Categorical # NOQA
from chainer.distributions.cauchy import Cauchy # NOQA
from chainer.distributions.chisquare import Chisquare # NOQA
from chainer.distributions.dirichlet import Dirichlet # NOQA
from chainer.distributions.exponential import Exponential # NOQA
from chainer.distributions.gamma import Gamma # NOQA
from chainer.distributions.geometric import Geometric # NOQA
from chainer.distributions.gumbel import Gumbel # NOQA
from chainer.distributions.laplace import Laplace # NOQA
from chainer.distributions.log_normal import LogNormal # NOQA
Expand Down
98 changes: 98 additions & 0 deletions chainer/distributions/cauchy.py
@@ -0,0 +1,98 @@
import warnings

import numpy

import chainer
from chainer.backends import cuda
from chainer import distribution
from chainer.functions.math import exponential
from chainer.functions.math import trigonometric


def _cauchy_icdf(x):
x = chainer.as_variable(x)
h = (x - 0.5) * numpy.pi
y = chainer.functions.tan(h)
return y


class Cauchy(distribution.Distribution):

"""Cauchy Distribution.
The probability density function of the distribution is expressed as
.. math::
p(x;x_0,\\gamma) = \\frac{1}{\\pi}\\frac{\\gamma}{(x-x_0)^2+\\gamma^2}
Args:
loc(:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
:class:`cupy.ndarray`): Parameter of distribution representing the \
location :math:`\\x_0`.
scale(:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
:class:`cupy.ndarray`): Parameter of distribution representing the \
scale :math:`\\gamma`.
"""

def __init__(self, loc, scale):
super(Cauchy, self).__init__()
self.loc = chainer.as_variable(loc)
self.scale = chainer.as_variable(scale)

@property
def batch_shape(self):
return self.loc.shape

def cdf(self, x):
return 1 / numpy.pi * trigonometric.arctan(
(x - self.loc) / self.scale) + 0.5

@property
def entropy(self):
return exponential.log(4 * numpy.pi * self.scale)

@property
def event_shape(self):
return ()

def icdf(self, x):
return self.loc + self.scale * _cauchy_icdf(x)

@property
def _is_gpu(self):
return isinstance(self.loc.data, cuda.ndarray)

def log_prob(self, x):
return - numpy.log(numpy.pi) + exponential.log(self.scale) \
- exponential.log((x - self.loc)**2 + self.scale**2)

@property
def mean(self):
warnings.warn("Mean of the cauchy distribution is undefined.",
RuntimeWarning)
xp = cuda.get_array_module(self.loc)
return chainer.as_variable(xp.full_like(self.loc.data, xp.nan))

def sample_n(self, n):
xp = cuda.get_array_module(self.loc)
if xp is cuda.cupy:
eps = xp.random.standard_cauchy(
(n,)+self.loc.shape, dtype=self.loc.dtype)
else:
eps = xp.random.standard_cauchy(
(n,)+self.loc.shape).astype(self.loc.dtype)

noise = self.scale * eps + self.loc

return noise

@property
def support(self):
return 'real'

@property
def variance(self):
warnings.warn("Variance of the cauchy distribution is undefined.",
RuntimeWarning)
xp = cuda.get_array_module(self.loc)
return chainer.as_variable(xp.full_like(self.loc.data, xp.nan))

0 comments on commit ea24d6c

Please sign in to comment.