-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
92 lines (61 loc) · 2.18 KB
/
functions.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
import numpy as np
import uuid
def heconstant(p1, myseed):
def initializer(shape, dtype=None):
a = np.sqrt(2 / np.prod(shape[:-1]))
p2 = 1. - p1
np.random.seed(myseed)
distribution = np.random.choice([1., -1.], shape, p=[p1, p2])
return tf.Variable(a * distribution, dtype=dtype, name=uuid.uuid4().hex)
return initializer
def binary(p1, myseed):
def initializer(shape, dtype=None):
p2 = 1. - p1
np.random.seed(myseed)
distribution = np.random.choice([1., -1.], shape, p=[p1, p2])
return tf.Variable(distribution, dtype=dtype, name=uuid.uuid4().hex)
return initializer
def activate(x, activationtype):
if activationtype is None:
return x
if 'relu' in activationtype:
return tf.keras.activations.relu(x)
if 'softmax' in activationtype:
return tf.keras.activations.softmax(x)
if 'sigmoid' in activationtype:
return tf.keras.activations.sigmoid(x)
if 'swish' in activationtype:
return tf.keras.activations.sigmoid(x) * x
if "elu" in activationtype:
return tf.keras.activations.elu(x)
if "selu" in activationtype:
return tf.keras.activations.selu(x)
return x
@tf.custom_gradient
def mask(x):
y = K.sign(tf.keras.activations.relu(x))
def grad(dy):
return dy
return y, grad
@tf.custom_gradient
def mask_rs(x):
y = K.sign(tf.keras.activations.relu(x))
# some papers (arXiv:1905.01067v4 and arXiv:1911.13299v1) do a
# rescaling of the weights/masks while backpropagating, we can do it here as well
scalefactor = tf.compat.v1.size(y, out_type=tf.dtypes.float32) / (1 + tf.math.count_nonzero(y, dtype=tf.dtypes.float32))
y *= scalefactor
def grad(dy):
return dy
return y, grad
@tf.custom_gradient
def flip(x):
y = K.sign(x)
def grad(dy):
return dy
return y, grad