提交 d50fa534 authored 作者: Iban Harlouchet's avatar Iban Harlouchet

Mon premier pull

上级 da3c4cd0
......@@ -2,6 +2,7 @@ import numpy as np
import numpy
import warnings
import theano
import theano.tensor as T
from theano.tensor import basic
from theano.tensor import nlinalg
......@@ -1006,3 +1007,69 @@ def to_one_hot(y, nb_class, dtype=None):
ret = theano.tensor.set_subtensor(ret[theano.tensor.arange(y.shape[0]), y],
1)
return ret
class Unique(theano.Op):
__props__ = ("return_index", "return_inverse", "return_counts")
def __init__(self, return_index=False, return_inverse=False,
return_counts=False):
self.return_index = return_index
self.return_inverse = return_inverse
self.return_counts = return_counts
if self.return_counts == True and np.__version__ < "1.9.0" :
raise RuntimeError(
"Numpy version = " + np.__version__ +
". Option 'return_counts=True' works starting"
" from version 1.9.0.")
def make_node(self, x):
x = T.as_tensor_variable(x)
#x = x.flatten()
outputs = []
# output0 = T.TensorType(broadcastable=[False], dtype=x.dtype)()
output0 = x.flatten().type()
outputs.append(output0)
typ = T.TensorType(broadcastable=[False], dtype='int64')
if self.return_index :
output1 = typ()
outputs.append(output1)
if self.return_inverse :
output2 = typ()#T.TensorType(broadcastable=[False], dtype=x.dtype)
outputs.append(output2)
if self.return_counts :
output3 = typ()#T.TensorType(broadcastable=[False], dtype=x.dtype)
outputs.append(output3)
return theano.Apply(self, [x], outputs)
def perform(self, node, inputs, output_storage):
x = inputs[0]
z = output_storage
param = {}
if self.return_index :
param['return_index'] = True
if self.return_inverse :
param['return_inverse'] = True
if self.return_counts:
param['return_counts'] = True
outs = np.unique(x,**param)
if ((not self.return_inverse) and
(not self.return_index) and
(not self.return_counts)):
z[0][0]=outs
else :
for i in range(len(outs)):
z[i][0] = outs[i]
def infer_shape(self, node, i0_shapes):
ret = node.fgraph.shape_feature.default_infer_shape(node, i0_shapes)
if self.return_inverse :
shape = (np.prod(i0_shapes[0]), )
if self.return_index :
ret[2] = shape
return ret
ret[1] = shape
return ret
return ret
......@@ -2,8 +2,8 @@ from nose.plugins.attrib import attr
import numpy as np
import numpy
import unittest
import theano
from theano.tests import unittest_tools as utt
from theano.tensor.extra_ops import (CumsumOp, cumsum, CumprodOp, cumprod,
......@@ -661,3 +661,66 @@ def test_to_one_hot():
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]])
class test_Unique(utt.InferShapeTester):
def setUp(self):
super(test_Unique, self).setUp()
self.op_class = Unique
self.ops = [Unique(), Unique(True), Unique(True, True),
Unique(False, True)]#, Unique(True, True, True)]
def test_basic_vector(self):
"""
Basic test for a vector.
Done by using the op and checking that it returns the right answer.
"""
x = theano.tensor.vector()
inp = np.asarray([2,1,3,2], dtype=config.floatX)
list_outs_expected = [[[1., 2., 3.]],
[[1., 2., 3.], [1, 0, 2]],
[[1., 2., 3.], [1, 0, 2], [1, 0, 2, 1]],
[[1., 2., 3.], [1, 0, 2, 1]]]
for op, outs_expected in zip(self.ops, list_outs_expected) :
f = theano.function(inputs=[x], outputs=op(x, return_list=True))
outs = f(inp)
print outs
# Compare the result computed to the expected value.
for out, out_exp in zip(outs, outs_expected):
print out
print out_exp
utt.assert_allclose(out, out_exp)
def test_basic_matrix(self):
""" Basic test for a matrix.
Done by using the op and checking that it returns the right answer.
"""
x = theano.tensor.matrix()
inp = np.asarray([[2, 1], [3, 2], [2, 3]], dtype=config.floatX)
list_outs_expected = [[[1., 2., 3.]],
[[1., 2., 3.], [1, 0, 2]],
[[1., 2., 3.], [1, 0, 2], [1, 0, 2, 1, 1, 2]],
[[1., 2., 3.], [1, 0, 2, 1, 1, 2]]]
for op, outs_expected in zip(self.ops, list_outs_expected):
f = theano.function(inputs=[x], outputs=op(x, return_list=True))
outs = f(inp)
print outs
# Compare the result computed to the expected value.
for out, out_exp in zip(outs, outs_expected):
print out
print out_exp
utt.assert_allclose(out, out_exp)
def test_infer_shape_vector(self):
"""
Testing the infer_shape with a vector.
"""
# TODO
pass
def test_infer_shape_matrix(self):
"""
Testing the infer_shape with a vector.
"""
# TODO
pass
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论