move ActivityRegularizer to regularizer.py
This commit is contained in:
parent
b74e0941dc
commit
843524b80a
2 changed files with 16 additions and 16 deletions
16
onn/layer.py
16
onn/layer.py
|
@ -1,7 +1,6 @@
|
||||||
from .layer_base import *
|
from .layer_base import *
|
||||||
from .initialization import *
|
from .initialization import *
|
||||||
from .float import *
|
from .float import *
|
||||||
from .regularizer import Regularizer
|
|
||||||
|
|
||||||
|
|
||||||
# Nonparametric Layers {{{1
|
# Nonparametric Layers {{{1
|
||||||
|
@ -77,21 +76,6 @@ class Sum(Layer):
|
||||||
return edges[0] # TODO: does this always work?
|
return edges[0] # TODO: does this always work?
|
||||||
|
|
||||||
|
|
||||||
class ActivityRegularizer(Layer):
|
|
||||||
def __init__(self, reg):
|
|
||||||
super().__init__()
|
|
||||||
assert isinstance(reg, Regularizer), reg
|
|
||||||
self.reg = reg
|
|
||||||
|
|
||||||
def forward(self, X):
|
|
||||||
self.X = X
|
|
||||||
self.loss = np.sum(self.reg.forward(X))
|
|
||||||
return X
|
|
||||||
|
|
||||||
def backward(self, dY):
|
|
||||||
return dY + self.reg.backward(self.X)
|
|
||||||
|
|
||||||
|
|
||||||
class Dropout(Layer):
|
class Dropout(Layer):
|
||||||
def __init__(self, dropout=0.0):
|
def __init__(self, dropout=0.0):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
from .layer import Layer
|
||||||
from .float import *
|
from .float import *
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,6 +8,21 @@ class Regularizer:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ActivityRegularizer(Layer):
|
||||||
|
def __init__(self, reg):
|
||||||
|
super().__init__()
|
||||||
|
assert isinstance(reg, Regularizer), reg
|
||||||
|
self.reg = reg
|
||||||
|
|
||||||
|
def forward(self, X):
|
||||||
|
self.X = X
|
||||||
|
self.loss = np.sum(self.reg.forward(X))
|
||||||
|
return X
|
||||||
|
|
||||||
|
def backward(self, dY):
|
||||||
|
return dY + self.reg.backward(self.X)
|
||||||
|
|
||||||
|
|
||||||
class L1L2(Regularizer):
|
class L1L2(Regularizer):
|
||||||
def __init__(self, l1=0.0, l2=0.0):
|
def __init__(self, l1=0.0, l2=0.0):
|
||||||
self.l1 = _f(l1)
|
self.l1 = _f(l1)
|
||||||
|
|
Loading…
Reference in a new issue