提交 664e4261 authored 作者: Olivier Breuleux's avatar Olivier Breuleux

moved nnet_ops and nnet_ops tests to tensor from sandbox

上级 d3fc88fe
...@@ -2,7 +2,7 @@ import sys ...@@ -2,7 +2,7 @@ import sys
sys.path.insert(0, '..') sys.path.insert(0, '..')
import theano import theano
from theano import tensor as T from theano import tensor as T
from theano.sandbox import nnet_ops from theano.tensor import nnet_ops
from theano.sandbox import module from theano.sandbox import module
from theano.sandbox import pprint from theano.sandbox import pprint
...@@ -97,7 +97,7 @@ if __name__ == '__main__': ...@@ -97,7 +97,7 @@ if __name__ == '__main__':
# sys.exit(0) # sys.exit(0)
lr = lrc.make(10, 2, mode=theano.Mode('py', 'fast_run')) lr = lrc.make(10, 2, mode=theano.Mode('c|py', 'fast_run'))
#lr = lrc.make(10, 2, mode=theano.Mode('py', 'merge')) #'FAST_RUN') #lr = lrc.make(10, 2, mode=theano.Mode('py', 'merge')) #'FAST_RUN')
data_x = N.random.randn(5, 10) data_x = N.random.randn(5, 10)
......
## This file contain ops that are not currently integrated in the core of threano. ## This file contain ops that are not currently integrated in the core of threano.
## Not all of those ops have been thoroughly tested. ## Not all of those ops have been thoroughly tested.
import theano #from theano import tensor, scalar
from theano import tensor, scalar from .. import gof
from .. import scalar
import basic as tensor
import elemwise
import numpy import numpy
############ ############
...@@ -33,7 +36,7 @@ class ScalarSigmoid(scalar.UnaryScalarOp): ...@@ -33,7 +36,7 @@ class ScalarSigmoid(scalar.UnaryScalarOp):
: 1.0 /(1.0+exp(-%(x)s));""" % locals() : 1.0 /(1.0+exp(-%(x)s));""" % locals()
raise NotImplementedError('only floatingpoint is implemented') raise NotImplementedError('only floatingpoint is implemented')
scalar_sigmoid = ScalarSigmoid(scalar.upgrade_to_float, name='scalar_sigmoid') scalar_sigmoid = ScalarSigmoid(scalar.upgrade_to_float, name='scalar_sigmoid')
sigmoid = tensor.Elemwise(scalar_sigmoid, name='sigmoid') sigmoid = elemwise.Elemwise(scalar_sigmoid, name='sigmoid')
class ScalarSoftplus(scalar.UnaryScalarOp): class ScalarSoftplus(scalar.UnaryScalarOp):
@staticmethod @staticmethod
...@@ -57,7 +60,7 @@ class ScalarSoftplus(scalar.UnaryScalarOp): ...@@ -57,7 +60,7 @@ class ScalarSoftplus(scalar.UnaryScalarOp):
: log1p(exp(%(x)s));""" % locals() : log1p(exp(%(x)s));""" % locals()
raise NotImplementedError('only floating point x is implemented') raise NotImplementedError('only floating point x is implemented')
scalar_softplus = ScalarSoftplus(scalar.upgrade_to_float, name='scalar_softplus') scalar_softplus = ScalarSoftplus(scalar.upgrade_to_float, name='scalar_softplus')
softplus = tensor.Elemwise(scalar_softplus, name='softplus') softplus = elemwise.Elemwise(scalar_softplus, name='softplus')
############ ############
...@@ -66,7 +69,7 @@ softplus = tensor.Elemwise(scalar_softplus, name='softplus') ...@@ -66,7 +69,7 @@ softplus = tensor.Elemwise(scalar_softplus, name='softplus')
# #
class SoftmaxWithBias(theano.Op): class SoftmaxWithBias(gof.Op):
""" """
An L{Op} for the output of neural-net multiclass classifiers. An L{Op} for the output of neural-net multiclass classifiers.
...@@ -80,7 +83,7 @@ class SoftmaxWithBias(theano.Op): ...@@ -80,7 +83,7 @@ class SoftmaxWithBias(theano.Op):
nin = 2 nin = 2
nout = 1 nout = 1
def __init__(self, **kwargs): def __init__(self, **kwargs):
theano.Op.__init__(self, **kwargs) gof.Op.__init__(self, **kwargs)
def make_node(self, x, b): def make_node(self, x, b):
x = tensor.as_tensor(x) x = tensor.as_tensor(x)
...@@ -93,7 +96,7 @@ class SoftmaxWithBias(theano.Op): ...@@ -93,7 +96,7 @@ class SoftmaxWithBias(theano.Op):
raise ValueError('b must be 1-d tensor of floats') raise ValueError('b must be 1-d tensor of floats')
sm = x.type.make_result() sm = x.type.make_result()
return theano.Apply(self, [x, b], [sm]) return gof.Apply(self, [x, b], [sm])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
x, b = input_storage x, b = input_storage
...@@ -232,18 +235,18 @@ class SoftmaxWithBias(theano.Op): ...@@ -232,18 +235,18 @@ class SoftmaxWithBias(theano.Op):
softmax_with_bias = SoftmaxWithBias() softmax_with_bias = SoftmaxWithBias()
class SoftmaxWithBiasDx(theano.Op): class SoftmaxWithBiasDx(gof.Op):
nin = 2 nin = 2
nout = 1 nout = 1
"""Gradient wrt x of the SoftmaxWithBias Op""" """Gradient wrt x of the SoftmaxWithBias Op"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
theano.Op.__init__(self, **kwargs) gof.Op.__init__(self, **kwargs)
def make_node(self, dy, sm, **kwargs): def make_node(self, dy, sm, **kwargs):
dy = tensor.as_tensor(dy) dy = tensor.as_tensor(dy)
sm = tensor.as_tensor(sm) sm = tensor.as_tensor(sm)
return theano.Apply(self, [dy, sm], [sm.type.make_result()]) return gof.Apply(self, [dy, sm], [sm.type.make_result()])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
dy, sm = input_storage dy, sm = input_storage
...@@ -317,7 +320,7 @@ def softmax(x, **kwargs): ...@@ -317,7 +320,7 @@ def softmax(x, **kwargs):
return softmax_with_bias(x, b, **kwargs) return softmax_with_bias(x, b, **kwargs)
class CrossentropySoftmaxArgmax1HotWithBias(theano.Op): class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
"""A special compound L{Op} for the output of neural-net classifiers. """A special compound L{Op} for the output of neural-net classifiers.
@type x: is a matrix of floats (32 or 64) @type x: is a matrix of floats (32 or 64)
...@@ -343,7 +346,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op): ...@@ -343,7 +346,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op):
nin=3 nin=3
nout=3 nout=3
def __init__(self, **kwargs): def __init__(self, **kwargs):
theano.Op.__init__(self, **kwargs) gof.Op.__init__(self, **kwargs)
def make_node(self, x, b, y_idx): def make_node(self, x, b, y_idx):
x = tensor.as_tensor(x) x = tensor.as_tensor(x)
...@@ -365,7 +368,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op): ...@@ -365,7 +368,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op):
# nll = Tensor(x.dtype, y.broadcastable) # nll = Tensor(x.dtype, y.broadcastable)
sm = x.type.make_result() sm = x.type.make_result()
am = y_idx.type.make_result() am = y_idx.type.make_result()
return theano.Apply(self, [x, b, y_idx], [nll, sm, am]) return gof.Apply(self, [x, b, y_idx], [nll, sm, am])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
""" """
The math, where x is an input vector, and t is a target index: The math, where x is an input vector, and t is a target index:
...@@ -503,17 +506,17 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op): ...@@ -503,17 +506,17 @@ class CrossentropySoftmaxArgmax1HotWithBias(theano.Op):
code_template = ''.join(self.c_code_template()) code_template = ''.join(self.c_code_template())
return code_template % dict(locals(), **sub) return code_template % dict(locals(), **sub)
class CrossentropySoftmax1HotWithBiasDx (theano.Op): class CrossentropySoftmax1HotWithBiasDx (gof.Op):
nin=3 nin=3
nout=1 nout=1
"""Gradient wrt x of the CrossentropySoftmax1Hot Op""" """Gradient wrt x of the CrossentropySoftmax1Hot Op"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
theano.Op.__init__(self,**kwargs) gof.Op.__init__(self,**kwargs)
def make_node(self, dy, sm, y_idx,**kwargs): def make_node(self, dy, sm, y_idx,**kwargs):
dy = tensor.as_tensor(dy) dy = tensor.as_tensor(dy)
sm = tensor.as_tensor(sm) sm = tensor.as_tensor(sm)
y_idx = tensor.as_tensor(y_idx) y_idx = tensor.as_tensor(y_idx)
return theano.Apply(self, [dy, sm, y_idx],[sm.type.make_result()]) return gof.Apply(self, [dy, sm, y_idx],[sm.type.make_result()])
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
dy,sm,y_idx = input_storage dy,sm,y_idx = input_storage
dx = numpy.zeros_like(sm) dx = numpy.zeros_like(sm)
...@@ -609,7 +612,7 @@ def crossentropy_softmax_1hot(x, y_idx, **kwargs): ...@@ -609,7 +612,7 @@ def crossentropy_softmax_1hot(x, y_idx, **kwargs):
return crossentropy_softmax_1hot_with_bias(x, b, y_idx, **kwargs) return crossentropy_softmax_1hot_with_bias(x, b, y_idx, **kwargs)
class MultinomialCrossentropy1Hot(theano.Op): class MultinomialCrossentropy1Hot(gof.Op):
pass pass
...@@ -625,7 +628,7 @@ def binary_crossentropy(output, target): ...@@ -625,7 +628,7 @@ def binary_crossentropy(output, target):
class Prepend_scalar_constant_to_each_row(theano.Op): class Prepend_scalar_constant_to_each_row(gof.Op):
def __init__(self, val = 0): def __init__(self, val = 0):
if isinstance(val, float): if isinstance(val, float):
val = scalar.constant(val) val = scalar.constant(val)
...@@ -633,14 +636,14 @@ class Prepend_scalar_constant_to_each_row(theano.Op): ...@@ -633,14 +636,14 @@ class Prepend_scalar_constant_to_each_row(theano.Op):
def make_node(self, mat): def make_node(self, mat):
#check type of input #check type of input
if not isinstance(mat,theano.Result) or not mat.type==tensor.matrix().type: if not isinstance(mat,gof.Result) or not mat.type==tensor.matrix().type:
raise TypeError("Expected a matrix as input") raise TypeError("Expected a matrix as input")
x = tensor.as_tensor(mat) x = tensor.as_tensor(mat)
y = tensor.as_tensor(self.val) y = tensor.as_tensor(self.val)
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError("the value to prepend don't have the same type as the matrix") TypeError("the value to prepend don't have the same type as the matrix")
node = theano.Apply(op=self, inputs=[mat], outputs=[tensor.matrix()]) node = gof.Apply(op=self, inputs=[mat], outputs=[tensor.matrix()])
return node return node
def perform(self, node, (mat, ), (output, )): def perform(self, node, (mat, ), (output, )):
...@@ -662,19 +665,19 @@ class Prepend_scalar_constant_to_each_row(theano.Op): ...@@ -662,19 +665,19 @@ class Prepend_scalar_constant_to_each_row(theano.Op):
def grad(self, (mat,), (goutput,)): def grad(self, (mat,), (goutput,)):
return goutput[:,1:] return goutput[:,1:]
class Prepend_scalar_to_each_row(theano.Op): class Prepend_scalar_to_each_row(gof.Op):
def make_node(self, val, mat): def make_node(self, val, mat):
#check type of input #check type of input
if isinstance(val, float): if isinstance(val, float):
val = scalar.constant(val) val = scalar.constant(val)
if not isinstance(mat,theano.Result) or not mat.type==tensor.matrix().type: if not isinstance(mat,gof.Result) or not mat.type==tensor.matrix().type:
raise TypeError("Expected a matrix as input") raise TypeError("Expected a matrix as input")
x = tensor.as_tensor(mat) x = tensor.as_tensor(mat)
y = tensor.as_tensor(val) y = tensor.as_tensor(val)
if x.type.dtype != y.type.dtype: if x.type.dtype != y.type.dtype:
TypeError("the value to prepend don't have the same type as the matrix") TypeError("the value to prepend don't have the same type as the matrix")
node = theano.Apply(op=self, inputs=[val,mat], outputs=[tensor.matrix()]) node = gof.Apply(op=self, inputs=[val,mat], outputs=[tensor.matrix()])
return node return node
def perform(self, node, (val,mat), (output, )): def perform(self, node, (val,mat), (output, )):
...@@ -699,7 +702,7 @@ prepend_scalar_to_each_row = Prepend_scalar_to_each_row() ...@@ -699,7 +702,7 @@ prepend_scalar_to_each_row = Prepend_scalar_to_each_row()
prepend_0_to_each_row = Prepend_scalar_constant_to_each_row(0.) prepend_0_to_each_row = Prepend_scalar_constant_to_each_row(0.)
prepend_1_to_each_row = Prepend_scalar_constant_to_each_row(1.) prepend_1_to_each_row = Prepend_scalar_constant_to_each_row(1.)
class solve(theano.Op): class solve(gof.Op):
""" """
Find the solution to the linear equation Ax=b, Find the solution to the linear equation Ax=b,
where A is a 2d matrix and b is a 1d or 2d matrix. where A is a 2d matrix and b is a 1d or 2d matrix.
...@@ -707,12 +710,12 @@ class solve(theano.Op): ...@@ -707,12 +710,12 @@ class solve(theano.Op):
""" """
def make_node(self, A, b): def make_node(self, A, b):
if not isinstance(A, theano.Result) or not A.type==tensor.matrix().type: if not isinstance(A, gof.Result) or not A.type==tensor.matrix().type:
raise TypeError("We expected that A had a matrix type") raise TypeError("We expected that A had a matrix type")
if not isinstance(B, theano.Result) or not B.type==tensor.matrix().type: if not isinstance(B, gof.Result) or not B.type==tensor.matrix().type:
raise TypeError("We expected that B had a matrix type") raise TypeError("We expected that B had a matrix type")
node = theano.Apply(op=self, inputs=[A, B], outputs=[tensor.matrix()]) node = gof.Apply(op=self, inputs=[A, B], outputs=[tensor.matrix()])
return node return node
def perform(self, node, (A, B), (output, )): def perform(self, node, (A, B), (output, )):
......
import unittest import unittest
import theano import theano
import theano._test_tensor as TT from theano import tensor as T
from theano import gof
import test_basic as TT
import numpy import numpy
from nnet_ops import * from theano.tensor.nnet_ops import *
class T_sigmoid(unittest.TestCase): class T_sigmoid(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -22,53 +25,45 @@ class T_Softmax(unittest.TestCase): ...@@ -22,53 +25,45 @@ class T_Softmax(unittest.TestCase):
def setUp(self): def setUp(self):
numpy.random.seed(9999) numpy.random.seed(9999)
def test0(self): def test0(self):
class Dummy(object): def f(a):
def make_node(self, a): return softmax(a)[:,0]
return [softmax(a)[:,0]] TT.verify_grad(self, f, [numpy.random.rand(3,4)])
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4)])
def test1(self): def test1(self):
class Dummy(object): def f(a):
def make_node(self, a): return softmax(a)[:,1]
return [softmax(a)[:,1]] TT.verify_grad(self, f, [numpy.random.rand(3,4)])
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4)])
def test2(self): def test2(self):
class Dummy(object): def f(a):
def make_node(self, a): return softmax(a)[:,2]
return [softmax(a)[:,2]] TT.verify_grad(self, f, [numpy.random.rand(3,4)])
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4)])
def test3(self): def test3(self):
class Dummy(object): def f(a):
def make_node(self, a): return softmax(a)[:,3]
return [softmax(a)[:,3]] TT.verify_grad(self, f, [numpy.random.rand(3,4)])
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4)])
class T_SoftmaxWithBias(unittest.TestCase): class T_SoftmaxWithBias(unittest.TestCase):
def setUp(self): def setUp(self):
numpy.random.seed(9999) numpy.random.seed(9999)
def test0(self): def test0(self):
class Dummy(object): def f(a, b):
def make_node(self, a, b): return softmax_with_bias(a, b)[:,0]
return [softmax_with_bias(a, b)[:,0]] TT.verify_grad(self, f, [numpy.random.rand(3,4),
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
class Dummy(object): def f(a, b):
def make_node(self, a, b): return softmax_with_bias(a, b)[:,1]
return [softmax_with_bias(a, b)[:,1]] TT.verify_grad(self, f, [numpy.random.rand(3,4),
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test2(self): def test2(self):
class Dummy(object): def f(a, b):
def make_node(self, a, b): return softmax_with_bias(a, b)[:,2]
return [softmax_with_bias(a, b)[:,2]] TT.verify_grad(self, f, [numpy.random.rand(3,4),
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test3(self): def test3(self):
class Dummy(object): def f(a, b):
def make_node(self, a, b): return softmax_with_bias(a, b)[:,3]
return [softmax_with_bias(a, b)[:,3]] TT.verify_grad(self, f, [numpy.random.rand(3,4),
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
class T_CrossentropySoftmax1Hot(unittest.TestCase): class T_CrossentropySoftmax1Hot(unittest.TestCase):
...@@ -76,18 +71,15 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase): ...@@ -76,18 +71,15 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
numpy.random.seed(9999) numpy.random.seed(9999)
def test0(self): def test0(self):
y_idx = [0,1,3] y_idx = [0,1,3]
class Dummy(object): def f(a, b):
def make_node(self, a,b): return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0]
return crossentropy_softmax_1hot_with_bias(a, b, y_idx)[0:1] TT.verify_grad(self, f, [numpy.random.rand(3,4),
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4),
numpy.random.rand(4)]) numpy.random.rand(4)])
def test1(self): def test1(self):
y_idx = [0,1,3] y_idx = [0,1,3]
class Dummy(object): def f(a):
def make_node(self, a): return crossentropy_softmax_1hot(a, y_idx)[0]
return crossentropy_softmax_1hot(a, y_idx)[0:1] TT.verify_grad(self, f, [numpy.random.rand(3,4)])
TT.verify_grad(self, Dummy(), [numpy.random.rand(3,4)])
class T_prepend(unittest.TestCase): class T_prepend(unittest.TestCase):
def test0(self): def test0(self):
...@@ -106,7 +98,7 @@ class T_prepend(unittest.TestCase): ...@@ -106,7 +98,7 @@ class T_prepend(unittest.TestCase):
"""basic functionality""" """basic functionality"""
x=tensor.matrix('x') x=tensor.matrix('x')
y=Prepend_scalar_to_each_row()(5.,x) y=Prepend_scalar_to_each_row()(5.,x)
f=theano.function([x],[y]) f=theano.function([x],y)
m=numpy.ones((3,5),dtype="float32") m=numpy.ones((3,5),dtype="float32")
my = f(m) my = f(m)
self.failUnless(str(my.dtype) == 'float64') self.failUnless(str(my.dtype) == 'float64')
...@@ -122,7 +114,7 @@ class T_solve(unittest.TestCase): ...@@ -122,7 +114,7 @@ class T_solve(unittest.TestCase):
b=numpy.array(range(5),dtype=float) b=numpy.array(range(5),dtype=float)
x=numpy.linalg.solve(A,b) x=numpy.linalg.solve(A,b)
Ax = numpy.dot(A,x) Ax = numpy.dot(A,x)
are = theano.gradient.numeric_grad.abs_rel_err(Ax, b) are = T.numeric_grad.abs_rel_err(Ax, b)
self.failUnless(numpy.all(are < 1.0e-5), (are, Ax, b)) self.failUnless(numpy.all(are < 1.0e-5), (are, Ax, b))
#print A,b #print A,b
#print numpy.dot(A,x) #print numpy.dot(A,x)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论