-
Notifications
You must be signed in to change notification settings - Fork 41
/
support_layer.py
64 lines (52 loc) · 2.11 KB
/
support_layer.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
import tensorflow as tf
from tensorflow.keras.layers import Layer
from keras_fsl.models import head_models
class SupportLayer(Layer):
"""
Base class for defining a layer that build a support_set from the input batch and then compute all the pair-wise value of the given
_kernel_.
"""
def __init__(self, kernel, **kwargs):
super().__init__(**kwargs)
self.kernel = kernel
def build(self, input_shape):
embedding_shape = self._normalize_input(input_shape)
if not isinstance(self.kernel, Layer):
kernel_config = self.kernel
if isinstance(kernel_config, str):
kernel_config = {"name": kernel_config}
kernel_config["init"] = {
**kernel_config.get("init", {}),
"input_shape": embedding_shape[1:],
}
self.kernel = getattr(head_models, kernel_config["name"])(**kernel_config["init"])
@staticmethod
def _normalize_input(inputs):
if isinstance(inputs, list):
return inputs[0]
return inputs
def get_config(self):
return {**super().get_config(), "kernel": self.kernel.to_json()}
@classmethod
def from_config(cls, config):
kernel = tf.keras.models.model_from_json(config["kernel"])
config["kernel"] = kernel
return cls(**config)
def compute_output_shape(self, input_shape):
return tf.TensorShape([self._normalize_input(input_shape)[0], None])
def build_support_set(self, inputs):
raise NotImplementedError
@tf.function
def call(self, inputs, **kwargs):
embeddings = self._normalize_input(inputs)
support_tensors = self.build_support_set(inputs)
support_set_size = tf.shape(support_tensors)[0]
return tf.reshape(
self.kernel(
[
tf.reshape(tf.tile(embeddings, [1, support_set_size]), [-1, tf.shape(embeddings)[1]], name="tf.repeat"),
tf.tile(support_tensors, [tf.shape(embeddings)[0], 1]),
]
),
[tf.shape(embeddings)[0], support_set_size],
)