def __init__(self, in_channels, out_channels, hiddens=[16], activations=['relu'], dropout=0.5, l2_norm=5e-4, lr=0.01, use_bias=False): x = Input(batch_shape=[None, in_channels], dtype=floatx(), name='attr_matrix') adj = Input(batch_shape=[None, None], dtype=floatx(), sparse=True, name='adj_matrix') index = Input(batch_shape=[None], dtype=intx(), name='node_index') h = x for hidden, activation in zip(hiddens, activations): h = GraphConvAttribute(hidden, use_bias=use_bias, activation=activation, kernel_regularizer=regularizers.l2(l2_norm))([h, adj]) h = Dropout(rate=dropout)(h) h = GraphConvAttribute(out_channels, use_bias=use_bias)([h, adj]) h = Gather()([h, index]) super().__init__(inputs=[x, adj, index], outputs=h) self.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy'])
def build(self, hiddens=[16], activations=['relu'], dropout=0.5, l2_norm=5e-4, lr=0.01, use_bias=False): with tf.device(self.device): n_nodes = self.graph.n_nodes x = Input(batch_shape=[None, self.graph.n_attrs], dtype=self.floatx, name='attr_matrix') wavelet = Input(batch_shape=[n_nodes, n_nodes], dtype=self.floatx, sparse=True, name='wavelet_matrix') inverse_wavelet = Input(batch_shape=[n_nodes, n_nodes], dtype=self.floatx, sparse=True, name='inverse_wavelet_matrix') index = Input(batch_shape=[None], dtype=self.intx, name='node_index') h = x for hidden, activation in zip(hiddens, activations): h = WaveletConvolution(hidden, activation=activation, use_bias=use_bias, kernel_regularizer=regularizers.l2(l2_norm))([h, wavelet, inverse_wavelet]) h = Dropout(rate=dropout)(h) h = WaveletConvolution(self.graph.n_classes, use_bias=use_bias)( [h, wavelet, inverse_wavelet]) h = Gather()([h, index]) model = Model( inputs=[x, wavelet, inverse_wavelet, index], outputs=h) model.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy']) self.model = model
def build(self, hiddens=[16], activations=['relu'], dropout=0.5, lr=0.01, l2_norm=5e-4, use_bias=False, p1=1., p2=1., n_power_iterations=1, epsilon=0.03, xi=1e-6): with tf.device(self.device): x = Input(batch_shape=[None, self.graph.n_attrs], dtype=self.floatx, name='attr_matrix') adj = Input(batch_shape=[None, None], dtype=self.floatx, sparse=True, name='adj_matrix') index = Input(batch_shape=[None], dtype=self.intx, name='node_index') GCN_layers = [] dropout_layers = [] for hidden, activation in zip(hiddens, activations): GCN_layers.append( GraphConvolution( hidden, activation=activation, use_bias=use_bias, kernel_regularizer=regularizers.l2(l2_norm))) dropout_layers.append(Dropout(rate=dropout)) GCN_layers.append( GraphConvolution(self.graph.n_classes, use_bias=use_bias)) self.GCN_layers = GCN_layers self.dropout_layers = dropout_layers logit = self.forward(x, adj) output = Gather()([logit, index]) model = Model(inputs=[x, adj, index], outputs=output) self.model = model self.train_metric = SparseCategoricalAccuracy() self.test_metric = SparseCategoricalAccuracy() self.loss_fn = SparseCategoricalCrossentropy(from_logits=True) self.optimizer = Adam(lr=lr) self.p1 = p1 # Alpha self.p2 = p2 # Beta self.xi = xi # Small constant for finite difference # Norm length for (virtual) adversarial training self.epsilon = epsilon self.n_power_iterations = n_power_iterations # Number of power iterations
def build(self, hiddens=[16], activations=['relu'], dropout=0.5, l2_norm=5e-4, use_bias=False, lr=0.01, p1=1.4, p2=0.7): if self.kind == "P": raise RuntimeError( f"Currently {self.name} only supports for tensorflow backend.") with tf.device(self.device): x = Input(batch_shape=[None, self.graph.n_attrs], dtype=self.floatx, name='attr_matrix') adj = Input(batch_shape=[None, None], dtype=self.floatx, sparse=True, name='adj_matrix') index = Input(batch_shape=[None], dtype=self.intx, name='node_index') GCN_layers = [] for hidden, activation in zip(hiddens, activations): GCN_layers.append( GraphConvolution( hidden, activation=activation, use_bias=use_bias, kernel_regularizer=regularizers.l2(l2_norm))) GCN_layers.append( GraphConvolution(self.graph.n_classes, use_bias=use_bias)) self.GCN_layers = GCN_layers self.dropout = Dropout(rate=dropout) logit = self.forward(x, adj) output = Gather()([logit, index]) model = Model(inputs=[x, adj, index], outputs=output) model.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy']) self.r_vadv = tf.Variable(TruncatedNormal(stddev=0.01)( shape=[self.graph.n_nodes, self.graph.n_attrs]), name="r_vadv") entropy_loss = entropy_y_x(logit) vat_loss = self.virtual_adversarial_loss(x, adj, logit) model.add_loss(p1 * vat_loss + p2 * entropy_loss) self.model = model self.adv_optimizer = Adam(lr=lr / 10)
def build(self, hiddens=[16], activations=['relu'], dropout=0., lr=0.01, l2_norm=5e-4, p1=1.4, p2=0.7, use_bias=False, epsilon=0.01): with tf.device(self.device): x = Input(batch_shape=[None, self.graph.n_attrs], dtype=self.floatx, name='attr_matrix') adj = Input(batch_shape=[None, None], dtype=self.floatx, sparse=True, name='adj_matrix') index = Input(batch_shape=[None], dtype=self.intx, name='node_index') GCN_layers = [] dropout_layers = [] for hidden, activation in zip(hiddens, activations): GCN_layers.append( GraphConvolution( hidden, activation=activation, use_bias=use_bias, kernel_regularizer=regularizers.l2(l2_norm))) dropout_layers.append(Dropout(rate=dropout)) GCN_layers.append( GraphConvolution(self.graph.n_classes, use_bias=use_bias)) self.GCN_layers = GCN_layers self.dropout_layers = dropout_layers logit = self.forward(x, adj) output = Gather()([logit, index]) model = Model(inputs=[x, adj, index], outputs=output) model.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy']) entropy_loss = entropy_y_x(logit) vat_loss = self.virtual_adversarial_loss(x, adj, logit, epsilon) model.add_loss(p1 * vat_loss + p2 * entropy_loss) self.model = model self.adv_optimizer = Adam(lr=lr / 10)
def __init__(self, in_channels, out_channels, hiddens=[64], activations=['relu'], dropout=0.5, l2_norm=5e-4, lr=0.01, kl=5e-4, gamma=1., use_bias=False): _floatx = floatx() x = Input(batch_shape=[None, in_channels], dtype=_floatx, name='attr_matrix') adj = [Input(batch_shape=[None, None], dtype=_floatx, sparse=True, name='adj_matrix_1'), Input(batch_shape=[None, None], dtype=_floatx, sparse=True, name='adj_matrix_2')] index = Input(batch_shape=[None], dtype=intx(), name='node_index') h = x if hiddens: mean, var = GaussionConvolution_F(hiddens[0], gamma=gamma, use_bias=use_bias, activation=activations[0], kernel_regularizer=regularizers.l2(l2_norm))([h, *adj]) if kl: KL_divergence = 0.5 * \ tf.reduce_mean(tf.math.square(mean) + var - tf.math.log(1e-8 + var) - 1, axis=1) KL_divergence = tf.reduce_sum(KL_divergence) # KL loss kl_loss = kl * KL_divergence # additional layers (usually unnecessay) for hidden, activation in zip(hiddens[1:], activations[1:]): mean, var = GaussionConvolution_D( hidden, gamma=gamma, use_bias=use_bias, activation=activation)([mean, var, *adj]) mean = Dropout(rate=dropout)(mean) var = Dropout(rate=dropout)(var) mean, var = GaussionConvolution_D( out_channels, gamma=gamma, use_bias=use_bias)([mean, var, *adj]) h = Sample()([mean, var]) h = Gather()([h, index]) super().__init__(inputs=[x, *adj, index], outputs=h) self.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy']) if hiddens and kl: self.add_loss(kl_loss)
def build(self, hiddens=[64], activations=['relu'], dropout=0.5, l2_norm=5e-3, lr=0.01, use_bias=False): with tf.device(self.device): x = Input(batch_shape=[None, self.graph.n_attrs], dtype=self.floatx, name='attr_matrix') adj = Input(batch_shape=[None, None], dtype=self.floatx, sparse=True, name='adj_matrix') index = Input(batch_shape=[None], dtype=self.intx, name='node_index') h = x for hidden, activation in zip(hiddens, activations): h = Dense(hidden, use_bias=use_bias, activation=activation, kernel_regularizer=regularizers.l2(l2_norm))(h) h = Dropout(dropout)(h) h = Dense(self.graph.n_classes, use_bias=use_bias, activation=activation, kernel_regularizer=regularizers.l2(l2_norm))(h) h = Dropout(dropout)(h) h = PropConvolution( self.K, use_bias=use_bias, activation='sigmoid', kernel_regularizer=regularizers.l2(l2_norm))([h, adj]) h = Gather()([h, index]) model = Model(inputs=[x, adj, index], outputs=h) model.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy']) self.model = model
def build_GCN(x): h = x for hidden, activation in zip(hiddens, activations): h = GraphConvolution(hidden, use_bias=use_bias, activation=activation, kernel_regularizer=regularizers.l2(l2_norm))([h, adj]) h = Dropout(rate=dropout)(h) h = GraphConvolution(self.graph.n_classes, use_bias=use_bias)([h, adj]) h = Gather()([h, index]) model = Model(inputs=[x, adj, index], outputs=h) model.compile(loss=CategoricalCrossentropy(from_logits=True), optimizer=RMSprop(lr=lr), metrics=['accuracy']) return model
def __init__(self, in_channels, out_channels, hiddens=[16], activations=['relu'], dropout=0.5, l2_norm=5e-4, lr=0.01, use_bias=False): _intx = intx() _floatx = floatx() x = Input(batch_shape=[None, in_channels], dtype=_floatx, name='attr_matrix') edge_index = Input(batch_shape=[None, 2], dtype=_intx, name='edge_index') edge_weight = Input(batch_shape=[None], dtype=_floatx, name='edge_weight') index = Input(batch_shape=[None], dtype=_intx, name='node_index') h = x for hidden, activation in zip(hiddens, activations): h = GraphEdgeConvolution( hidden, use_bias=use_bias, activation=activation, kernel_regularizer=regularizers.l2(l2_norm))( [h, edge_index, edge_weight]) h = Dropout(rate=dropout)(h) h = GraphEdgeConvolution( out_channels, use_bias=use_bias)([h, edge_index, edge_weight]) output = Gather()([h, index]) super().__init__(inputs=[x, edge_index, edge_weight, index], outputs=output) self.compile(loss=SparseCategoricalCrossentropy(from_logits=True), optimizer=Adam(lr=lr), metrics=['accuracy'])