-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangle_comparison_tensorflow.py
More file actions
33 lines (28 loc) · 1.11 KB
/
angle_comparison_tensorflow.py
File metadata and controls
33 lines (28 loc) · 1.11 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
import tensorflow as tf
from tensorflow.keras.layers import Layer
class AngleComparison(Layer):
def __init__(self, filters=32, kernel_size=3):
super(AngleComparison, self).__init__()
self.filters = filters
self.kernel_size = kernel_size
def build(self, input_shape):
self.conv1 = tf.keras.layers.Conv1D(
filters=self.filters, kernel_size=self.kernel_size, padding="same"
)
self.conv2 = tf.keras.layers.Conv1D(
filters=self.filters, kernel_size=self.kernel_size, padding="same"
)
super(AngleComparison, self).build(input_shape)
def call(self, inputs):
x = inputs
x1 = self.conv1(x)
x2 = self.conv2(x)
dot = tf.matmul(x1, x2, transpose_b=True)
norm1 = tf.norm(x1, ord=2, axis=-1, keepdims=True)
norm2 = tf.norm(x2, ord=2, axis=-1, keepdims=True)
norm = tf.multiply(norm1, norm2)
angle = dot / norm
output = tf.concat([angle, x], axis=-1)
return output
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1] + 1)