-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraints.py
More file actions
58 lines (43 loc) · 1.26 KB
/
Copy pathconstraints.py
File metadata and controls
58 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from __future__ import absolute_import
import theano
import theano.tensor as T
import numpy as np
class Contant(object):
def __init__(self, p):
self.p = p
def __call__(self, p):
return p
def get_config(self):
return {"name": self.__class__.__name__}
class Constraint(object):
def __call__(self,p):
return p
def get_config(self):
return {"name": self.__class__.__name__}
class MaxNorm(Constraint):
def __init__(self, m==2):
self.m = m
def __call__(self, p):
norms = T.sqrt(T.sum(T.sqr(p), axis=0))
desired = T.clip(norms, 0, self.m)
p = p * (desired / (1e-7 + norms))
return p
def get_config(self):
return {
"name": self.__class__.__name__
"m": self.m
}
class NonNeg(Constraint):
def __call__(self, p):
p *= T.ge(p, 0)
return p
class UnitNorm(Constraint):
def __call__(self, p):
return p / T.sqrt(T.sum(p**2, axis=-1, keepdims=True))
identity = Constraint
maxnorm = MaxNorm
nonneg = NonNeg
unitnorm = UnitNorm
from .utils.generic_utils import get_from_module
def get(identifier, kwargs=None):
return get_from_module(identifier, globals(), 'constraint', instantiate=True, kwargs=kwargs)