Exemple #1
0
    def train_clf(self, graph, L):
        '''
			Train SGC model with updated labeled pool L
			Return new trained model
		'''
        train_targets = self.target_encoding.transform(
            self.df_targets.loc[L].to_dict("records"))
        train_gen = self.generator.flow(L, train_targets)

        sgc = GCN(
            layer_sizes=[train_targets.shape[1]],
            generator=self.generator,
            bias=True,
            dropout=0.5,
            activations=["softmax"],
            kernel_regularizer=regularizers.l2(5e-4),
        )

        x_inp, predictions = sgc.build()

        class_support = dict(Counter(self.df_targets.loc[L]["label"]))
        classes = sorted(self.data.class_labels)
        counts = [
            class_support[c] if c in class_support else 0 for c in classes
        ]
        weights = np.sum(counts) / np.array(counts)
        weighted_loss = self.weighted_categorical_crossentropy(weights)

        model = Model(inputs=x_inp, outputs=predictions)
        model.compile(
            optimizer=optimizers.Adam(lr=0.2),
            # loss=losses.categorical_crossentropy,
            loss=weighted_loss,
            metrics=["acc"],
        )

        # if not os.path.isdir("model_logs"):
        #     os.makedirs("model_logs")
        # es_callback = EarlyStopping(
        #     monitor="acc", patience=50
        # )  # patience is the number of epochs to wait before early stopping in case of no further improvement
        # mc_callback = ModelCheckpoint(
        #     "model_logs/best_model.h5", monitor="acc", save_best_only=True, save_weights_only=True
        # )

        history = model.fit_generator(
            train_gen,
            epochs=50,
            verbose=0,
            shuffle=
            False,  # this should be False, since shuffling data means shuffling the whole graph
            # callbacks=[es_callback, mc_callback],
        )

        # model.load_weights("model_logs/best_model.h5")

        return model
def train(
    train_nodes,
    train_targets,
    val_nodes,
    val_targets,
    generator,
    dropout,
    layer_sizes,
    learning_rate,
    activations,
    num_epochs,
):
    """

    Train a GCN model on the specified graph G with given parameters, evaluate it, and save the model.
    Args:
        train_nodes: A list of train nodes
        train_targets: Labels of train nodes
        val_nodes: A list of validation nodes
        val_targets: Labels of validation nodes
        generator: A FullBatchNodeGenerator
        dropout: The dropout (0->1) Initial Learning rate
        layer_sizes: A list of number of hidden nodes in each layer
        learning_rate: Initial Learning rate
        activations: A list of number of activation functions in each layer
    """

    train_gen = generator.flow(train_nodes, train_targets)
    val_gen = generator.flow(val_nodes, val_targets)
    gcnModel = GCN(
        layer_sizes,
        generator,
        bias=True,
        dropout=dropout,
        kernel_regularizer=regularizers.l2(5e-4),
        activations=activations,
    )

    # Expose the input and output sockets of the model:
    x_inp, x_out = gcnModel.build()

    # Create Keras model for training
    model = keras.Model(inputs=x_inp, outputs=x_out)
    model.compile(
        loss=losses.categorical_crossentropy,
        metrics=[metrics.categorical_accuracy],
        optimizer=optimizers.Adam(lr=learning_rate),
    )

    # Train model
    history = model.fit_generator(train_gen,
                                  epochs=num_epochs,
                                  validation_data=val_gen,
                                  verbose=2,
                                  shuffle=False)

    return model
def create_GCN_model(graph):

    generator = FullBatchNodeGenerator(graph)
    train_gen = generator.flow([1, 2], np.array([[1, 0], [0, 1]]))

    base_model = GCN(
        layer_sizes=[8, 2],
        generator=generator,
        bias=True,
        dropout=0.5,
        activations=["elu", "softmax"],
    )

    x_inp, x_out = base_model.build()

    keras_model = Model(inputs=x_inp, outputs=x_out)

    return base_model, keras_model, generator, train_gen
Exemple #4
0
def create_GCN_model_sparse(graph):
    generator = FullBatchNodeGenerator(graph, sparse=True, method="gcn")
    train_gen = generator.flow([0, 1], np.array([[1, 0], [0, 1]]))

    layer_sizes = [2, 2]
    gcn = GCN(
        layer_sizes=layer_sizes,
        activations=["elu", "elu"],
        generator=generator,
        dropout=0.3,
        kernel_regularizer=regularizers.l2(5e-4),
    )

    for layer in gcn._layers:
        layer._initializer = "ones"
    x_inp, x_out = gcn.build()
    keras_model = Model(inputs=x_inp, outputs=x_out)
    return gcn, keras_model, generator, train_gen