-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmodules.py
126 lines (96 loc) · 3.32 KB
/
modules.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
import keras as ks
from keras import ops
class Embedding(ks.layers.Layer):
def __init__(self,
input_dim,
output_dim,
embeddings_initializer='uniform',
embeddings_regularizer=None,
activity_regularizer=None,
embeddings_constraint=None,
mask_zero=False,
input_length=None,
sparse=False,
**kwargs):
super().__init__(**kwargs)
self.input_dim = input_dim
self.output_dim = output_dim
self.embeddings_initializer = ks.initializers.get(embeddings_initializer)
self.embeddings_regularizer = ks.regularizers.get(embeddings_regularizer)
self.activity_regularizer = ks.regularizers.get(activity_regularizer)
self.embeddings_constraint = ks.constraints.get(embeddings_constraint)
self.embeddings = self.add_weight(
name="embeddings",
shape=(input_dim, output_dim),
dtype=self.dtype,
initializer=self.embeddings_initializer,
regularizer=self.embeddings_regularizer,
constraint=self.embeddings_constraint,
trainable=True,
)
def build(self, input_shape):
super(Embedding, self).build(input_shape)
def call(self, inputs):
return ops.take(self.embeddings, inputs, axis=0)
def get_config(self):
return super(Embedding, self).get_config()
class ExpandDims(ks.layers.Layer):
def __init__(self, axis, **kwargs):
super(ExpandDims, self).__init__(**kwargs)
self.axis = axis
def build(self, input_shape):
self.built = True
def call(self, inputs):
return ops.expand_dims(inputs, axis=self.axis)
def get_config(self):
config = super(ExpandDims, self).get_config()
config.update({"axis": self.axis})
return config
class SqueezeDims(ks.layers.Layer):
def __init__(self, axis, **kwargs):
super(SqueezeDims, self).__init__(**kwargs)
self.axis = axis
def build(self, input_shape):
self.built = True
def call(self, inputs):
return ops.squeeze(inputs, axis=self.axis)
def get_config(self):
config = super(SqueezeDims, self).get_config()
config.update({"axis": self.axis})
return config
def Input(
shape=None,
batch_size=None,
dtype=None,
sparse=None,
batch_shape=None,
name=None,
tensor=None,
ragged=None
):
layer = ks.layers.InputLayer(
shape=shape,
batch_size=batch_size,
dtype=dtype,
sparse=sparse,
batch_shape=batch_shape,
name=name,
input_tensor=tensor,
)
return layer.output
class ZerosLike(ks.layers.Layer):
r"""Layer to make a zero tensor"""
def __init__(self, **kwargs):
"""Initialize layer."""
super(ZerosLike, self).__init__(**kwargs)
def build(self, input_shape):
"""Build layer."""
super(ZerosLike, self).build(input_shape)
def call(self, inputs, **kwargs):
"""Forward pass.
Args:
inputs (Tensor): Tensor of node or edge embeddings of shape ([N], F, ...)
Returns:
Tensor: Zero-like tensor of input.
"""
return ops.zeros_like(inputs)