提交 80bbb14f authored 作者: James Bergstra's avatar James Bergstra

added sigmoid-based logistic regression example

上级 01370f4e
...@@ -8,7 +8,7 @@ from theano.sandbox import module ...@@ -8,7 +8,7 @@ from theano.sandbox import module
import numpy as N import numpy as N
class LogisticRegression(module.FancyModule): class LogisticRegressionN(module.FancyModule):
class __instance_type__(module.FancyModuleInstance): class __instance_type__(module.FancyModuleInstance):
def initialize(self, n_in, n_out): def initialize(self, n_in, n_out):
#self.component is the LogisticRegressionTemplate instance that built this guy. #self.component is the LogisticRegressionTemplate instance that built this guy.
...@@ -18,7 +18,7 @@ class LogisticRegression(module.FancyModule): ...@@ -18,7 +18,7 @@ class LogisticRegression(module.FancyModule):
self.lr = 0.01 self.lr = 0.01
def __init__(self, x = None, targ = None): def __init__(self, x = None, targ = None):
super(LogisticRegression, self).__init__() #boilerplate super(LogisticRegressionN, self).__init__() #boilerplate
self.x = x if x is not None else T.matrix() self.x = x if x is not None else T.matrix()
self.targ = targ if targ is not None else T.lvector() self.targ = targ if targ is not None else T.lvector()
...@@ -39,19 +39,50 @@ class LogisticRegression(module.FancyModule): ...@@ -39,19 +39,50 @@ class LogisticRegression(module.FancyModule):
updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gparams))) updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gparams)))
self.apply = module.Method([self.x], T.argmax(T.dot(self.x, self.w) + self.b, axis=1)) self.apply = module.Method([self.x], T.argmax(T.dot(self.x, self.w) + self.b, axis=1))
lr = LogisticRegression().make(10, 5, mode='FAST_COMPILE') class LogisticRegression2(module.FancyModule):
class __instance_type__(module.FancyModuleInstance):
def initialize(self, n_in):
#self.component is the LogisticRegressionTemplate instance that built this guy.
self.w = N.random.randn(n_in,1)
self.b = N.random.randn(1)
self.lr = 0.01
def __init__(self, x = None, targ = None):
super(LogisticRegression2, self).__init__() #boilerplate
self.x = x if x is not None else T.matrix()
self.targ = targ if targ is not None else T.lvector()
self.w = module.Member(T.dmatrix()) #automatically names
self.b = module.Member(T.dvector()) #automatically names
self.lr = module.Member(T.dscalar()) #provides an external interface to change it
#and makes it an implicit input to any Method you build.
self.params = [self.w, self.b]
y = nnet_ops.sigmoid(T.dot(self.x, self.w))
xent_elem = self.targ * T.log(y) - (1.0 - self.targ) *T.log(1.0 - y)
xent = T.sum(xent_elem)
gparams = T.grad(xent, self.params)
self.update = module.Method([self.x, self.targ], [xent, self.w, gparams[0]],
updates = dict((p, p - self.lr * g) for p, g in zip(self.params, gparams)))
self.apply = module.Method([self.x], T.argmax(T.dot(self.x, self.w) + self.b, axis=1))
if __name__ == '__main__':
lr = LogisticRegression2().make(10, mode='FAST_COMPILE')
data_x = N.random.randn(10,10) data_x = N.random.randn(10, 10)
data_y = (N.random.randn(10) > 0) data_y = (N.random.randn(10) > 0)
print lr.params print lr.params
print lr.w print lr.w.shape
print lr.b for i in xrange(10):
print lr xe = lr.update(data_x, data_y)
for i in xrange(100): print N.sum(xe), lr.w.shape
xe = lr.update(data_x, data_y)
print N.sum(xe)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论