Belle II Software light-2406-ragdoll
EnhancedRelations Class Reference
Inheritance diagram for EnhancedRelations:
Collaboration diagram for EnhancedRelations:

Public Member Functions

def __init__ (self, number_features, hidden_feature_shape=[30, 30, 30, 30], activation=tanh, **kwargs)
 
def build (self, input_shape)
 
def call (self, inputs)
 
def compute_output_shape (self, input_shape)
 
def get_config (self)
 

Public Attributes

 number_features
 Number of features.
 
 number_groups
 Number of groups in input.
 
 hidden_feature_shape
 shape of hidden layers used for extracting relations
 
 activation
 activation used for hidden layer in shared weights.
 
 group_len
 how many neurons has one comparable object
 
 weightvariables
 saves weights for call
 
 combinations
 number of relation combinations
 
 question_len
 size of second input vector
 

Detailed Description

This is a class which implements Relational Layer into Keras.
See Class Relations for details.
EnhanceRelations use an additional input for passing event information to every comparison:
RN(O) = f_phi(sum_phi(g_theta(o_i,o_j,q)))
q is fed in as second one dimensional input.

Definition at line 125 of file keras_relational.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  number_features,
  hidden_feature_shape = [30, 30, 30, 30],
  activation = tanh,
**  kwargs 
)
Init class.

Definition at line 134 of file keras_relational.py.

134 def __init__(self, number_features, hidden_feature_shape=[30, 30, 30, 30], activation=tanh, **kwargs):
135 """
136 Init class.
137 """
138
139
140 self.number_features = number_features
141
142 self.number_groups = 0
143
144 self.hidden_feature_shape = hidden_feature_shape
145
146 self.activation = activations.get(activation)
147
148 self.group_len = 0
149
150 self.weightvariables = []
151
152 self.combinations = 0
153
154 self.question_len = 0
155
156 super().__init__(**kwargs)
157

Member Function Documentation

◆ build()

def build (   self,
  input_shape 
)
Build all weights for Relations Layer
:param input_shape: Input shape of tensor
:return:  Nothing

Definition at line 158 of file keras_relational.py.

158 def build(self, input_shape):
159 """
160 Build all weights for Relations Layer
161 :param input_shape: Input shape of tensor
162 :return: Nothing
163 """
164 # accept only 2 inputs
165 assert(len(input_shape) == 2)
166 # first input should be a 2D layers
167 assert(len(input_shape[0]) == 3)
168 # second input should be a 1D layers
169 assert(len(input_shape[1]) == 2)
170
171 self.number_groups = input_shape[0][1]
172
173 self.group_len = input_shape[0][2]
174
175 self.question_len = input_shape[1][1]
176
177 self.combinations = np.int32(np.math.factorial(self.number_groups) / (2 * np.math.factorial(self.number_groups - 2)))
178
179 dense_shape = [2 * self.group_len + self.question_len] + self.hidden_feature_shape + [self.number_features]
180
181 for i in range(len(dense_shape[:-1])):
182 weights = self.add_weight(name=f'relation_weights_{i}',
183 shape=list(dense_shape[i:i + 2]), initializer='glorot_uniform', trainable=True)
184 bias = self.add_weight(name=f'relation_weights_{i}',
185 shape=(dense_shape[i + 1],), initializer='zeros', trainable=True)
186
187 self.weightvariables.append([weights, bias])
188
189 super().build(input_shape)
190

◆ call()

def call (   self,
  inputs 
)
Compute Relational Layer
:param inputs: input tensor
:return: output tensor

Definition at line 191 of file keras_relational.py.

191 def call(self, inputs):
192 """
193 Compute Relational Layer
194 :param inputs: input tensor
195 :return: output tensor
196 """
197 input_groups = [inputs[0][:, i, :] for i in range(self.number_groups)]
198 questions = inputs[1]
199 outputs = []
200 for index, group1 in enumerate(input_groups[:-1]):
201 for group2 in input_groups[index + 1:]:
202 net = K.dot(K.concatenate([group1, group2, questions]), self.weightvariables[0][0])
203 net = K.bias_add(net, self.weightvariables[0][1])
204 for variables in self.weightvariables[1:]:
205 net = self.activation(net)
206 net = K.dot(net, variables[0])
207 net = K.bias_add(net, variables[1])
208 outputs.append(sigmoid(net))
209
210 flat_result = K.concatenate(outputs)
211 return Reshape((self.combinations, self.number_features,))(flat_result)
212

◆ compute_output_shape()

def compute_output_shape (   self,
  input_shape 
)
Compute Output shape
:return: Output shape

Definition at line 213 of file keras_relational.py.

213 def compute_output_shape(self, input_shape):
214 """
215 Compute Output shape
216 :return: Output shape
217 """
218 # accept only 2 inputs
219 assert(len(input_shape) == 2)
220 # first input should be a 2D layers
221 assert(len(input_shape[0]) == 3)
222 # second input should be a 1D layers
223 assert(len(input_shape[1]) == 2)
224
225 self.combinations = np.int32(np.math.factorial(self.number_groups) / (2 * np.math.factorial(self.number_groups - 2)))
226
227 return (input_shape[0][0], self.combinations, self.number_features)
228

◆ get_config()

def get_config (   self)
Config required for saving parameters in keras model.

Definition at line 229 of file keras_relational.py.

229 def get_config(self):
230 """
231 Config required for saving parameters in keras model.
232 """
233 config = {
234 'number_features': self.number_features,
235 'hidden_feature_shape': self.hidden_feature_shape,
236 'activation': activations.serialize(self.activation)
237 }
238 base_config = super().get_config()
239 return dict(list(base_config.items()) + list(config.items()))

Member Data Documentation

◆ activation

activation

activation used for hidden layer in shared weights.

For output sigmoid will always be used.

Definition at line 146 of file keras_relational.py.

◆ combinations

combinations

number of relation combinations

Definition at line 152 of file keras_relational.py.

◆ group_len

group_len

how many neurons has one comparable object

Definition at line 148 of file keras_relational.py.

◆ hidden_feature_shape

hidden_feature_shape

shape of hidden layers used for extracting relations

Definition at line 144 of file keras_relational.py.

◆ number_features

number_features

Number of features.

Number of different shared weights used for comparison for each relation.

Definition at line 140 of file keras_relational.py.

◆ number_groups

number_groups

Number of groups in input.

Definition at line 142 of file keras_relational.py.

◆ question_len

question_len

size of second input vector

Definition at line 154 of file keras_relational.py.

◆ weightvariables

weightvariables

saves weights for call

Definition at line 150 of file keras_relational.py.


The documentation for this class was generated from the following file: