Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.add_loss(cut_loss)
# Orthogonality regularization
SS = ops.matmul_AT_B(S, S)
I_S = tf.eye(self.k)
ortho_loss = tf.norm(
SS / tf.norm(SS, axis=(-1, -2)) - I_S / tf.norm(I_S), axis=(-1, -2)
)
if batch_mode:
ortho_loss = K.mean(cut_loss)
self.add_loss(ortho_loss)
# Pooling
X_pooled = ops.matmul_AT_B(S, X)
A_pooled = tf.linalg.set_diag(A_pooled, tf.zeros(K.shape(A_pooled)[:-1])) # Remove diagonal
A_pooled = ops.normalize_A(A_pooled)
output = [X_pooled, A_pooled]
if I is not None:
I_mean = tf.segment_mean(I, I)
I_pooled = ops.repeat(I_mean, tf.ones_like(I_mean) * self.k)
output.append(I_pooled)
if self.return_mask:
output.append(S)
return output
def call(self, inputs):
# Note that I is useless, because thee layer cannot be used in graph
# batch mode.
if len(inputs) == 3:
X, A, I = inputs
if K.ndim(I) == 2:
I = I[:, 0]
else:
X, A = inputs
I = None
N = K.shape(A)[-1]
# Check if the layer is operating in batch mode (X and A have rank 3)
mode = ops.autodetect_mode(A, X)
self.reduce_loss = mode in (ops.modes['M'], ops.modes['B'])
# Get normalized adjacency
if K.is_sparse(A):
I_ = tf.sparse.eye(N, dtype=A.dtype)
A_ = tf.sparse.add(A, I_)
else:
I_ = tf.eye(N, dtype=A.dtype)
A_ = A + I_
fltr = ops.normalize_A(A_)
# Node embeddings
Z = K.dot(X, self.kernel_emb)
Z = ops.filter_dot(fltr, Z)
if self.activation is not None:
Z = self.activation(Z)
def call(self, inputs):
# Note that I is useless, because thee layer cannot be used in graph
# batch mode.
if len(inputs) == 3:
X, A, I = inputs
if K.ndim(I) == 2:
I = I[:, 0]
else:
X, A = inputs
I = None
N = K.shape(A)[-1]
# Check if the layer is operating in batch mode (X and A have rank 3)
mode = ops.autodetect_mode(A, X)
self.reduce_loss = mode in (ops.modes['M'], ops.modes['B'])
# Get normalized adjacency
if K.is_sparse(A):
I_ = tf.sparse.eye(N, dtype=A.dtype)
A_ = tf.sparse.add(A, I_)
else:
I_ = tf.eye(N, dtype=A.dtype)
A_ = A + I_
fltr = ops.normalize_A(A_)
# Node embeddings
Z = K.dot(X, self.kernel_emb)
Z = ops.filter_dot(fltr, Z)
if self.activation is not None:
Z = self.activation(Z)
def call(self, inputs):
X = inputs[0] # (batch_size, N, F)
A = inputs[1] # (batch_size, N, N)
E = inputs[2] # (batch_size, N, N, S)
mode = ops.autodetect_mode(A, X)
# Parameters
N = K.shape(X)[-2]
F = K.int_shape(X)[-1]
F_ = self.channels
# Normalize adjacency matrix
A = ops.normalize_A(A)
# Filter network
kernel_network = E
if self.kernel_network is not None:
for i, l in enumerate(self.kernel_network):
kernel_network = self.dense_layer(kernel_network, l,
'FGN_{}'.format(i),
activation='relu',
SS / tf.norm(SS, axis=(-1, -2)) - I_S / tf.norm(I_S), axis=(-1, -2)
)
if batch_mode:
ortho_loss = K.mean(cut_loss)
self.add_loss(ortho_loss)
# Pooling
X_pooled = ops.matmul_AT_B(S, X)
A_pooled = tf.linalg.set_diag(A_pooled, tf.zeros(K.shape(A_pooled)[:-1])) # Remove diagonal
A_pooled = ops.normalize_A(A_pooled)
output = [X_pooled, A_pooled]
if I is not None:
I_mean = tf.segment_mean(I, I)
I_pooled = ops.repeat(I_mean, tf.ones_like(I_mean) * self.k)
output.append(I_pooled)
if self.return_mask:
output.append(S)
return output
def call(self, inputs):
X = inputs[0] # (batch_size, N, F)
A = inputs[1] # (batch_size, N, N)
E = inputs[2] # (batch_size, N, N, S)
mode = ops.autodetect_mode(A, X)
# Parameters
N = K.shape(X)[-2]
F = K.int_shape(X)[-1]
F_ = self.channels
# Normalize adjacency matrix
A = ops.normalize_A(A)
# Filter network
kernel_network = E
if self.kernel_network is not None:
for i, l in enumerate(self.kernel_network):
kernel_network = self.dense_layer(kernel_network, l,
'FGN_{}'.format(i),
activation='relu',
use_bias=self.use_bias,
kernel_initializer=self.kernel_initializer,
bias_initializer=self.bias_initializer,
kernel_regularizer=self.kernel_regularizer,
bias_regularizer=self.bias_regularizer,
kernel_constraint=self.kernel_constraint,
bias_constraint=self.bias_constraint)
kernel_network = self.dense_layer(kernel_network, F_ * F, 'FGN_out')
if self.use_bias:
Hid = K.bias_add(Hid, self.bias_in)
if self.activation is not None:
Hid = self.activation(Hid)
# Compute cluster assignment matrix
S = K.dot(Hid, self.kernel_out)
if self.use_bias:
S = K.bias_add(S, self.bias_out)
S = activations.softmax(S, axis=-1) # Apply softmax to get cluster assignments
# MinCut regularization
A_pooled = ops.matmul_AT_B_A(S, A)
num = tf.trace(A_pooled)
D = ops.degree_matrix(A)
den = tf.trace(ops.matmul_AT_B_A(S, D))
cut_loss = -(num / den)
if batch_mode:
cut_loss = K.mean(cut_loss)
self.add_loss(cut_loss)
# Orthogonality regularization
SS = ops.matmul_AT_B(S, S)
I_S = tf.eye(self.k)
ortho_loss = tf.norm(
SS / tf.norm(SS, axis=(-1, -2)) - I_S / tf.norm(I_S), axis=(-1, -2)
)
if batch_mode:
ortho_loss = K.mean(cut_loss)
self.add_loss(ortho_loss)