From 47461039780b960360aa84b5c4fba160707fd85a Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 8 Mar 2018 02:40:42 +0100 Subject: [PATCH] add HardClip activation layer --- onn/activation.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/onn/activation.py b/onn/activation.py index e0cbda5..5bbae0d 100644 --- a/onn/activation.py +++ b/onn/activation.py @@ -206,3 +206,17 @@ class Arcsinh(Layer): def backward(self, dY): return dY / np.sqrt(self.X * self.X + 1) + + +class HardClip(Layer): # aka HardTanh when at default settings + def __init__(self, lower=-1.0, upper=1.0): + super().__init__() + self.lower = _f(lower) + self.upper = _f(upper) + + def forward(self, X): + self.X = X + return np.clip(X, self.lower, self.upper) + + def backward(self, dY): + return dY * ((self.X <= self.lower) & (self.X >= self.upper))