-
Notifications
You must be signed in to change notification settings - Fork 25
/
masked_layers.py
148 lines (112 loc) · 5.92 KB
/
masked_layers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright (c) 2019 Uber Technologies, Inc.
# Licensed under the Uber Non-Commercial License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at the root directory of this project.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from keras import backend as K
import numpy as np
from tf_plus import Conv2D, Dense
class MaskedDense(Dense):
def __init__(self, units, mask_weight, mask_bias,
*args, **kwargs):
super(MaskedDense, self).__init__(units, *args, **kwargs)
self.mask_weight = mask_weight
self.mask_bias = mask_bias
def build(self, input_shape):
super(MaskedDense, self).build(input_shape)
self._underlying_kernel = self.kernel
self.kernel_mask = K.variable(value=self.mask_weight)
self._non_trainable_weights.append(self.kernel_mask)
self.kernel = self._underlying_kernel * self.kernel_mask
if self.use_bias:
self._underlying_bias = self.bias
self.bias_mask = K.variable(value=self.mask_bias)
self._non_trainable_weights.append(self.bias_mask)
self.bias = self._underlying_bias * self.bias_mask
else:
self.bias = None
class MaskedConv2D(Conv2D):
def __init__(self, filters, kernel_size, mask_weight, mask_bias,
*args, **kwargs):
super(MaskedConv2D, self).__init__(filters, kernel_size, *args, **kwargs)
self.mask_weight = mask_weight
self.mask_bias = mask_bias
def build(self, input_shape):
super(MaskedConv2D, self).build(input_shape)
self._underlying_kernel = self.kernel
self.kernel_mask = K.variable(value = self.mask_weight)
self._non_trainable_weights.append(self.kernel_mask)
self.kernel = self._underlying_kernel * self.kernel_mask
if self.use_bias:
self._underlying_bias = self.bias
self.bias_mask = K.variable(value=self.mask_bias)
self._non_trainable_weights.append(self.bias_mask)
self.bias = self._underlying_bias * self.bias_mask
else:
self.bias = None
class FreezeDense(Dense):
def __init__(self, units, init_weight, init_bias, mask_weight, mask_bias,
*args, **kwargs):
super(FreezeDense, self).__init__(units, *args, **kwargs)
self.init_weight = init_weight
self.init_bias = init_bias
self.mask_weight = mask_weight
self.mask_weight_rev = np.abs(mask_weight - 1)
self.mask_bias = mask_bias
self.mask_bias_rev = np.abs(mask_bias - 1)
def build(self, input_shape):
super(FreezeDense, self).build(input_shape)
self._underlying_kernel = self.kernel
self.kernel_init = K.variable(value = self.init_weight)
self.kernel_mask = K.variable(value = self.mask_weight)
self.kernel_mask_rev = K.variable(value = self.mask_weight_rev)
self._non_trainable_weights.append(self.kernel_init)
self._non_trainable_weights.append(self.kernel_mask)
self._non_trainable_weights.append(self.kernel_mask_rev)
self.kernel = self._underlying_kernel * self.kernel_mask + self.kernel_mask_rev * self.kernel_init
if self.use_bias:
self._underlying_bias = self.bias
self.bias_init = K.variable(value = self.init_bias)
self.bias_mask = K.variable(value=self.mask_bias)
self.bias_mask_rev = K.variable(value = self.mask_bias_rev)
self._non_trainable_weights.append(self.bias_init)
self._non_trainable_weights.append(self.bias_mask)
self._non_trainable_weights.append(self.bias_mask_rev)
self.bias = self._underlying_bias * self.bias_mask + self.bias_mask_rev * self.bias_init
else:
self.bias = None
class FreezeConv2D(Conv2D):
def __init__(self, filters, kernel_size, init_weight, init_bias, mask_weight, mask_bias,
*args, **kwargs):
super(FreezeConv2D, self).__init__(filters, kernel_size, *args, **kwargs)
self.init_weight = init_weight
self.init_bias = init_bias
self.mask_weight = mask_weight
self.mask_weight_rev = np.abs(mask_weight - 1)
self.mask_bias = mask_bias
self.mask_bias_rev = np.abs(mask_bias - 1)
def build(self, input_shape):
super(FreezeConv2D, self).build(input_shape)
self._underlying_kernel = self.kernel
self.kernel_init = K.variable(value = self.init_weight)
self.kernel_mask = K.variable(value = self.mask_weight)
self.kernel_mask_rev = K.variable(value = self.mask_weight_rev)
self._non_trainable_weights.append(self.kernel_init)
self._non_trainable_weights.append(self.kernel_mask)
self._non_trainable_weights.append(self.kernel_mask_rev)
self.kernel = self._underlying_kernel * self.kernel_mask + self.kernel_mask_rev * self.kernel_init
if self.use_bias:
self._underlying_bias = self.bias
self.bias_init = K.variable(value = self.init_bias)
self.bias_mask = K.variable(value=self.mask_bias)
self.bias_mask_rev = K.variable(value = self.mask_bias_rev)
self._non_trainable_weights.append(self.bias_init)
self._non_trainable_weights.append(self.bias_mask)
self._non_trainable_weights.append(self.bias_mask_rev)
self.bias = self._underlying_bias * self.bias_mask + self.bias_mask_rev * self.bias_init
else:
self.bias = None