提交 8e9f3ff6 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Rename ReorderRowElements to PermuteRowElements, to better reflect the

implementation.
上级 d147486a
......@@ -2735,8 +2735,8 @@ class InversePermutation(Op):
inverse_permutation = InversePermutation()
class ReorderRowElements(Op):
"""Reorder each row (inner-most dim) of a tensor wrt a permutation.
class PermuteRowElements(Op):
"""Permute the elements of each row (inner-most dim) of a tensor.
The permutation argument (y) will be broadcasted to fit x, then each
row (vector) of x will be reordered according to the corresponding row
......@@ -2784,10 +2784,10 @@ class ReorderRowElements(Op):
self._rec_perform(node, x, y, outs[0], curdim=0)
def grad(self, (x, y), (gz,)):
gx = reorder_row_elements(gz, inverse_permutation(y))
gx = permute_row_elements(gz, inverse_permutation(y))
return [gx, None]
reorder_row_elements = ReorderRowElements()
permute_row_elements = PermuteRowElements()
#########################
......
......@@ -6,7 +6,7 @@ import numpy
from theano.compile import module, In, Component
from theano.gof import Container
from theano.tensor import raw_random, reorder_row_elements
from theano.tensor import raw_random, permute_row_elements
class RandomStreamsInstance(object):
"""RandomStreamsInstance"""
......@@ -192,7 +192,7 @@ class RandomStreams(Component):
def shuffle_row_elements(self, input):
"""Return a variable with every row (rightmost index) shuffled"""
perm = self.permutation(input.ndim-1, input.shape[:-1], input.shape[-1])
shuffled = reorder_row_elements(input, perm)
shuffled = permute_row_elements(input, perm)
return shuffled
......@@ -1832,65 +1832,65 @@ class TestInversePermutation(unittest.TestCase):
assert numpy.all(i_row[p_row] == numpy.arange(10))
class TestReorderRowElements(unittest.TestCase):
class TestPermuteRowElements(unittest.TestCase):
def setUp(self):
utt.seed_rng()
def test_1_1(self):
"""Test ReorderRowElements(vector, vector)"""
"""Test PermuteRowElements(vector, vector)"""
input = vector()
p = ivector()
out = reorder_row_elements(input, p)
reorder = function([input, p], out)
out = permute_row_elements(input, p)
permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(5,))
p_val = rng.permutation(5)
out_val = reorder(input_val, p_val)
out_val = permute(input_val, p_val)
# Should be equivalent to advanced indexing
out_bis = input_val[p_val]
assert numpy.all(out_val == out_bis)
# Verify gradient
def reorder_fixed(s_input):
def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val"""
return reorder_row_elements(s_input, p_val)
utt.verify_grad(reorder_fixed, [input_val])
return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val])
def test_2_1(self):
"""Test broadcasting in ReorderRowElements(matrix, vector)"""
"""Test broadcasting in PermuteRowElements(matrix, vector)"""
input = matrix()
p = ivector()
out = reorder_row_elements(input, p)
reorder = function([input, p], out)
out = permute_row_elements(input, p)
permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(3,5))
p_val = rng.permutation(5)
out_val = reorder(input_val, p_val)
out_val = permute(input_val, p_val)
# The same permutation should be applied to every row of the input matrix.
out_bis = numpy.asarray([row[p_val] for row in input_val])
assert numpy.all(out_val == out_bis)
# Verify gradient
def reorder_fixed(s_input):
def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val"""
return reorder_row_elements(s_input, p_val)
utt.verify_grad(reorder_fixed, [input_val])
return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val])
def test_2_2(self):
"""Test ReorderRowElements(matrix, matrix)"""
"""Test PermuteRowElements(matrix, matrix)"""
input = matrix()
p = imatrix()
out = reorder_row_elements(input, p)
reorder = function([input, p], out)
out = permute_row_elements(input, p)
permute = function([input, p], out)
rng = numpy.random.RandomState(utt.fetch_seed())
input_val = rng.uniform(size=(3,5))
p_val = numpy.asarray([rng.permutation(5) for i in range(3)])
out_val = reorder(input_val, p_val)
out_val = permute(input_val, p_val)
# Each row of p contains a permutation to apply to the corresponding
# row of input
......@@ -1898,10 +1898,10 @@ class TestReorderRowElements(unittest.TestCase):
assert numpy.all(out_val == out_bis)
# Verify gradient
def reorder_fixed(s_input):
def permute_fixed(s_input):
"""Auxiliary op defined to get rid of gradient wrt p_val"""
return reorder_row_elements(s_input, p_val)
utt.verify_grad(reorder_fixed, [input_val])
return permute_row_elements(s_input, p_val)
utt.verify_grad(permute_fixed, [input_val])
class test_tensordot(unittest.TestCase):
def setUp(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论