add constraints and regularizers modules

This commit is contained in:
Michael Oliver 2015-04-23 11:36:26 -07:00
parent a8dab51eeb
commit babc0b9dd8
2 changed files with 35 additions and 0 deletions

16
keras/constraints.py Normal file

@ -0,0 +1,16 @@
from __future__ import absolute_import
import theano
import theano.tensor as T
import numpy as np
def maxnorm(m=2):
def maxnorm_wrap(p):
norms = T.sqrt(T.sum(T.sqr(p), axis=0))
desired = T.clip(norms, 0, m)
p = p * (desired / (1e-7 + norms))
return p
return maxnorm_wrap
def nonneg(p):
p *= T.ge(p,0)
return p

19
keras/regularizers.py Normal file

@ -0,0 +1,19 @@
from __future__ import absolute_import
import theano
import theano.tensor as T
import numpy as np
def l1(lam=.01):
def l1wrap(g,p):
g += T.sgn(p) * lam
return g
return l1wrap
def l2(lam=.01):
def l2wrap(g,p):
g += p * lam
return g
return l2wrap
def ident(g,*l):
return g