提交 47bd74da authored 作者: Olivier Breuleux's avatar Olivier Breuleux

fixed elemwise bug and logistic_regression example

上级 d28f7c55
......@@ -14,7 +14,7 @@ class LogisticRegressionN(module.FancyModule):
#self.component is the LogisticRegressionTemplate instance that built this guy.
self.w = N.random.randn(n_in, n_out)
self.b = N.random.randn( n_out)
self.b = N.random.randn(n_out)
self.lr = 0.01
def __init__(self, x = None, targ = None):
......@@ -47,12 +47,13 @@ class LogisticRegression2(module.FancyModule):
self.w = N.random.randn(n_in,1)
self.b = N.random.randn(1)
self.lr = 0.01
self.__hide__ = ['params']
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.targ = targ if targ is not None else T.lcol()
self.w = module.Member(T.dmatrix()) #automatically names
self.b = module.Member(T.dvector()) #automatically names
......@@ -62,27 +63,38 @@ class LogisticRegression2(module.FancyModule):
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_elem = -self.targ * T.log(y) - (1.0 - self.targ) * T.log(1.0 - y)
xent = T.sum(xent_elem)
self.y = y
self.xent_elem = xent_elem
self.xent = xent
gparams = T.grad(xent, self.params)
self.update = module.Method([self.x, self.targ], [xent, self.w, gparams[0]],
self.update = module.Method([self.x, self.targ], xent,
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')
lrc = LogisticRegression2()
data_x = N.random.randn(10, 10)
data_y = (N.random.randn(10) > 0)
lr = lrc.make(10, mode=theano.Mode('c|py', 'merge')) #'FAST_RUN')
data_x = N.random.randn(5, 10)
data_y = (N.random.randn(5, 1) > 0)
print lr.params
print lr.w.shape
for i in xrange(10):
for i in xrange(10000):
xe = lr.update(data_x, data_y)
print N.sum(xe), lr.w.shape
if i % 100 == 0:
print i, xe
print
print 'TRAINED MODEL:'
print lr
......@@ -207,7 +207,7 @@ class Method(Component):
for k, v in self.updates.iteritems()]
outputs = self.outputs
_inputs = [x.result for x in inputs]
for input in gof.graph.inputs(outputs if isinstance(outputs, (list, tuple)) else [outputs]
for input in gof.graph.inputs((list(outputs) if isinstance(outputs, (list, tuple)) else [outputs])
+ [x.update for x in inputs if getattr(x, 'update', False)],
blockers = _inputs):
if input not in _inputs and not isinstance(input, gof.Value):
......
......@@ -350,6 +350,15 @@ class Elemwise(Op):
return ret
def perform(self, node, inputs, output_storage):
maxsize = max(len(input.shape) for input in inputs)
for dims in zip(*[[(1, True)]*(maxsize - len(input.shape)) + zip(input.shape, sinput.type.broadcastable)
for input, sinput in zip(inputs, node.inputs)]):
if max(d for d,b in dims) != 1 and (1, False) in dims:
raise ValueError('Dimension mismatch; shapes are %s' %
', '.join('(%s)' % ', '.join('*' if b else str(d)
for d, b in zip(input.shape, sinput.type.broadcastable))
for input, sinput in zip(inputs, node.inputs)))
# Other mismatches will be caught by the ufunc
if not self.inplace_pattern:
for output, storage in zip(node.outputs, output_storage):
odat = storage[0]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论