提交 a33d82f6 authored 作者: Adam Becker's avatar Adam Becker

random fixes

上级 2d3baf8f
......@@ -259,10 +259,10 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
'''
return code % locals()
def make_node(self, inp, k):
def make_node(self, inp, kth):
ctx_name = infer_context_name(inp)
inp = as_gpuarray_variable(inp, ctx_name)
k = as_tensor_variable(k)
kth = as_tensor_variable(kth)
bcast = inp.type.broadcastable
outs = []
if self.return_values:
......@@ -272,7 +272,7 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
dtype=self.idx_dtype,
broadcastable=bcast,
context_name=ctx_name)())
return Apply(self, [inp, k], outs)
return Apply(self, [inp, kth], outs)
def get_params(self, node):
return node.inputs[0].type.context
......
......@@ -357,7 +357,7 @@ class TopKOp(theano.Op):
return '%(op)s{axis=%(axis)d}' % dict(
op=self.__class__.__name__, axis=self.axis)
def make_node(self, inp, k):
def make_node(self, inp, kth):
# numpy always uses int64 as output dtype for arg*() routines
# however, we add this option as memory is more precious on gpu
inp = theano.tensor.as_tensor_variable(inp)
......@@ -369,7 +369,7 @@ class TopKOp(theano.Op):
'"axis" parameter out of range,'
' expected integer within [%d, %d]' % (-ndim, ndim - 1))
k = theano.tensor.as_tensor_variable(k)
kth = theano.tensor.as_tensor_variable(kth)
bcast = inp.type.broadcastable
outs = []
if self.return_values:
......@@ -377,7 +377,7 @@ class TopKOp(theano.Op):
if self.return_indices:
outs.append(theano.tensor.TensorType(
dtype=self.idx_dtype, broadcastable=bcast)())
return theano.Apply(self, [inp, k], outs)
return theano.Apply(self, [inp, kth], outs)
def perform(self, node, inputs, output_storage):
x, k = inputs
......@@ -428,7 +428,7 @@ class TopKOp(theano.Op):
return [x_grad, k_grad]
def topk(x, k, axis=-1, idx_dtype='int64'):
def topk(x, kth, axis=-1, idx_dtype='int64'):
"""
Returns the k-largest elements along an axis.
......@@ -437,7 +437,7 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
x: tensor instance
k: integer constant/variable
kth: integer constant/variable
Must not be 0. If negative, gives k-smallest elements instead.
axis: integer or ``None``
......@@ -460,10 +460,10 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
if axis is None:
x = theano.tensor.flatten(x)
axis = -1
return TopKOp(axis=axis, idx_dtype=idx_dtype)(x, k)[0]
return TopKOp(axis=axis, idx_dtype=idx_dtype)(x, kth)[0]
def argtopk(x, k, axis=-1, idx_dtype='int64'):
def argtopk(x, kth, axis=-1, idx_dtype='int64'):
"""
Returns the indices of k-largest elements along an axis.
......@@ -472,7 +472,7 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
x: tensor instance
k: integer constant/variable
kth: integer constant/variable
Must not be 0. If negative, gives k-smallest elements instead.
axis: integer, tuple/list of integers, or ``None``
......@@ -499,10 +499,10 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
axis = -1
return TopKOp(
axis=axis,
idx_dtype=idx_dtype)(x, k)[1]
idx_dtype=idx_dtype)(x, kth)[1]
def topk_and_argtopk(x, k, axis=-1, idx_dtype='int64'):
def topk_and_argtopk(x, kth, axis=-1, idx_dtype='int64'):
"""
Returns the results of both topk() and argtopk() in one Op.
......@@ -518,4 +518,4 @@ def topk_and_argtopk(x, k, axis=-1, idx_dtype='int64'):
axis = -1
return TopKOp(
axis=axis,
idx_dtype=idx_dtype)(x, k)
idx_dtype=idx_dtype)(x, kth)
......@@ -22,7 +22,6 @@ def gen_unique_vector(size, dtype):
return (retval[np.random.permutation(size)] - size * 1.5).astype(dtype)
'''
class Test_sort(unittest.TestCase):
def setUp(self):
......@@ -230,7 +229,6 @@ def test_argsort_grad():
data = np.random.rand(2, 3, 3).astype(theano.config.floatX)
utt.verify_grad(lambda x: argsort(x, axis=2), [data])
'''
class Test_TopK(unittest.TestCase):
......@@ -408,59 +406,13 @@ class Test_TopK(unittest.TestCase):
continue
def op(x):
return theano.ggtensor.sort(topk(x, k=k, axis=axis), axis=axis)
return theano.tensor.sort(topk(x, k, axis=axis), axis=axis)
xval = np.random.rand(*shp).astype(theano.config.floatX)
utt.verify_grad(op, [xval])
class TopKInferShapeTester(utt.InferShapeTester):
@utt.parameterized.expand(product(
((15, 17), (11, 7, 5), (2, 3, 5, 7, 11), (2, 4, 3, 1)),
(1, '(1+n)//2', 'n-1', '-n')))
def test_topk_infer_shape(self, shp, k_):
ndim = len(shp)
for axis in range(-ndim, ndim):
if isinstance(k_, str):
k = eval(k_.replace('n', str(shp[axis])))
else:
k = k_
if k == 0:
continue
x = theano.tensor.tensor(
name='x', broadcastable=(False,) * len(shp),
dtype=theano.config.floatX)
y = topk(x, k, axis=axis)
size = reduce(int.__mul__, shp)
xval = gen_unique_vector(size, theano.config.floatX).reshape(shp)
self._compile_and_check(
[x], [y], [xval], TopKOp)
@utt.parameterized.expand(product(
((15, 17), (11, 7, 5), (2, 3, 5, 7, 11), (2, 4, 3, 1)),
(-1, '(1+n)//2', '1-n')))
def test_argtopk_infer_shape(self, shp, k_):
ndim = len(shp)
for axis in range(-ndim, ndim):
if isinstance(k_, str):
k = eval(k_.replace('n', str(shp[axis])))
else:
k = k_
if k == 0:
continue
x = theano.tensor.tensor(
name='x', broadcastable=(False,) * len(shp),
dtype=theano.config.floatX)
y = argtopk(x, k, axis=axis, idx_dtype='int32')
size = reduce(int.__mul__, shp)
xval = gen_unique_vector(size, theano.config.floatX).reshape(shp)
self._compile_and_check(
[x], [y], [xval], TopKOp)
@utt.parameterized.expand(product(
((2, 3), (15, 17), (11, 7, 5), (2, 3, 5, 7, 11), (2, 4, 3, 1)),
(1, '(1+n)//2', 'n-1', 'n')))
......
......@@ -83,7 +83,7 @@ def seed_rng(pseed=None):
def verify_grad(op, pt, n_tests=2, rng=None, *args, **kwargs):
"""
Wrapper for tensor/basic.py:verify_grad
Wrapper for gradient.py:verify_grad
Takes care of seeding the random number generator if None is given
"""
if rng is None:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论