/
permutate.py
135 lines (109 loc) · 3.93 KB
/
permutate.py
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
38
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
import numpy
import six
import chainer
from chainer.backends import cuda
from chainer import function_node
from chainer.utils import type_check
def _check_indices(indices):
if len(indices) == 0:
return
# TODO(unno): Check indices without cpu
indices = cuda.to_cpu(indices)
for i in indices:
if 0 <= i < len(indices):
continue
raise ValueError('Out of bounds index: {}'.format(i))
sort = numpy.sort(indices)
for s, t in six.moves.zip(sort, sort[1:]):
if s == t:
raise ValueError('indices contains duplicate value: {}'.format(s))
def _inverse_indices(indices):
xp = cuda.get_array_module(indices)
r = xp.empty_like(indices)
if xp is numpy:
r[indices] = numpy.arange(len(indices))
else:
cuda.elementwise(
'S ind', 'raw S r',
'r[ind] = i',
'inverse_indices'
)(indices, r)
return r
class Permutate(function_node.FunctionNode):
"""Permutate function."""
def __init__(self, axis=0, inv=False):
self.axis = axis
self.inv = inv
def check_type_forward(self, in_types):
type_check.expect(in_types.size() == 2)
x_type, ind_type = in_types
if self.axis < 0:
type_check.expect(x_type.ndim >= -self.axis)
else:
type_check.expect(x_type.ndim > self.axis)
type_check.expect(
ind_type.dtype.kind == 'i',
ind_type.ndim == 1,
x_type.shape[self.axis] == ind_type.shape[0],
)
def _permutate(self, x, indices, inv):
if inv:
indices = _inverse_indices(indices)
return x[((slice(None),) * self.axis) + (indices,)]
def forward(self, inputs):
self.retain_inputs((1,))
x, inds = inputs
if chainer.is_debug():
_check_indices(inds)
return self._permutate(x, inds, self.inv),
def backward(self, indexes, grad_outputs):
inds = self.inputs[1]
g, = grad_outputs
gx, = Permutate(self.axis, not self.inv).apply((g, inds.data))
return gx, None
def permutate(x, indices, axis=0, inv=False):
"""Permutates a given variable along an axis.
This function permutate ``x`` with given ``indices``.
That means ``y[i] = x[indices[i]]`` for all ``i``.
Note that this result is same as ``y = x.take(indices)``.
``indices`` must be a permutation of ``[0, 1, ..., len(x) - 1]``.
When ``inv`` is ``True``, ``indices`` is treated as its inverse.
That means ``y[indices[i]] = x[i]``.
Args:
x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
:class:`cupy.ndarray`):
Variable to permutate.
A :math:`(s_1, s_2, ..., s_N)` -shaped float array.
indices (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
:class:`cupy.ndarray`):
Indices to extract from the variable. A one-dimensional int array.
axis (int): Axis that the input array is permutate along.
inv (bool): If ``True``, ``indices`` is treated as its inverse.
Returns:
~chainer.Variable: Output variable.
.. admonition:: Example
>>> x = np.arange(6).reshape((3, 2)).astype(np.float32)
>>> x
array([[0., 1.],
[2., 3.],
[4., 5.]], dtype=float32)
>>> indices = np.array([2, 0, 1], np.int32)
>>> y = F.permutate(x, indices)
>>> y.data
array([[4., 5.],
[0., 1.],
[2., 3.]], dtype=float32)
>>> y = F.permutate(x, indices, inv=True)
>>> y.data
array([[2., 3.],
[4., 5.],
[0., 1.]], dtype=float32)
>>> indices = np.array([1, 0], np.int32)
>>> y = F.permutate(x, indices, axis=1)
>>> y.data
array([[1., 0.],
[3., 2.],
[5., 4.]], dtype=float32)
"""
y, = Permutate(axis, inv).apply((x, indices))
return y