-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathlayers.py
179 lines (145 loc) · 5.15 KB
/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright 2019 The TensorFlow Probability Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Base classes for building neural networks."""
import sys
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.experimental.nn.util import utils as nn_util_lib
from tensorflow_probability.python.internal import name_util
__all__ = [
'Layer',
'Sequential',
]
class Layer(tf.Module):
"""A `callable` `tf.Module`."""
def __init__(self, also_track=None, validate_args=False, name=None):
name = name_util.strip_invalid_chars(name or type(self).__name__)
self._also_track = [] if also_track is None else [also_track]
super(Layer, self).__init__(name=name)
self._trace = False
self._validate_args = validate_args
@property
def also_track(self):
return list(self._also_track)
@property
def validate_args(self):
"""Python `bool` indicating possibly expensive checks are enabled."""
return self._validate_args
def summary(self):
return nn_util_lib.variables_summary(self.variables, self.name)
def save(self, filename):
return nn_util_lib.variables_save(filename, self.variables)
def load(self, filename):
return nn_util_lib.variables_load(filename, self.variables)
def __repr__(self):
return '<{}: name={}>'.format(type(self).__name__, self.name)
class Sequential(Layer):
"""A `Layer` characterized by iteratively given functions."""
def __init__(self, layers, also_track=None, validate_args=False, name=None):
layers = tuple(layers)
if not layers:
raise tf.errors.InvalidArgumentError(
'Argument `layers` must contain at least one element.')
name = name or '_'.join([_try_get_name(x) for x in layers])
self._layers = tuple(layers)
super(Sequential, self).__init__(
also_track=also_track, validate_args=validate_args, name=name)
def set_trace(self, trace):
self._trace = bool(trace)
return self
@property
def layers(self):
return self._layers
def __call__(self, inputs, **kwargs):
if callable(inputs):
return Sequential([inputs, self], **kwargs)
x = inputs
if self._trace:
_trace(self, x, -1)
for i, layer in enumerate(self.layers):
x = _try_call(layer, [x], kwargs)
if self._trace:
_trace(layer, x, i)
return x
def __getitem__(self, i):
r = Sequential(self.layers[i], name=self.name)
r._also_track = self._also_track # pylint: disable=protected-access
return r
class KernelBiasLayer(Layer):
"""Linear layer."""
def __init__(self,
kernel,
bias,
apply_kernel_fn,
activation_fn=None,
dtype=tf.float32,
validate_args=False,
name=None):
self._kernel = kernel
self._bias = bias
self._activation_fn = activation_fn
self._apply_kernel_fn = apply_kernel_fn
self._dtype = dtype
super(KernelBiasLayer, self).__init__(
validate_args=validate_args, name=name)
@property
def dtype(self):
return self._dtype
@property
def kernel(self):
return self._kernel
@property
def bias(self):
return self._bias
@property
def activation_fn(self):
return self._activation_fn
def __call__(self, x):
x = tf.convert_to_tensor(x, dtype_hint=self.dtype, name='x')
y = x
if self.kernel is not None:
y = self._apply_kernel_fn(y, self.kernel)
if self.bias is not None:
y = y + self.bias
if self.activation_fn is not None:
y = self.activation_fn(y) # pylint: disable=not-callable
return y
def _try_call(fn, args, kwargs):
"""Convenience function for evaluating argument `fn`."""
try:
if fn is None:
return args[0]
try:
return fn(*args, **kwargs)
except TypeError:
# Don't return from here or else we'll pick up a nested exception.
# Seeing TypeError here isn't really an exception since it only means we
# need to call `fn` differently).
pass
return fn(*args)
except:
print('------ EXCEPTION in {} ------'.format(_try_get_name(fn)))
raise
def _try_get_name(fn, name_fallback='unknown'):
return str(getattr(fn, '__name__', None) or
getattr(fn, 'name', None) or
getattr(type(fn), '__name__', name_fallback))
def _trace(layer, x, i):
name = _try_get_name(layer)
z = tf.nest.map_structure(lambda x_: '{:14} {:<24} {:>10}'.format( # pylint: disable=g-long-lambda
_try_get_name(x_),
str(list(getattr(x_, 'shape', '?'))),
_try_get_name(getattr(x_, 'dtype', x_), '?')), x)
print('--- TRACE{:02}: {:<24} {}'.format(i, name, z))
sys.stdout.flush()