11.3 Quick Start: Using Graph Machine Learning on PGQL Property Graphs

This tutorial helps you get started on applying the DeepWalk machine learning algorithm on a PGQL property graph.

The instructions assume that the PGQL property graph is already existing in your current database.
Run the following steps to build and work with a Deep Walk model.
  1. Load the PGQL property graph into the graph server (PGX).
    opg4j> var instance = GraphServer.getInstance("https://localhost:7007", "<username>", "<password>".toCharArray())
    instance ==> ServerInstance[embedded=false,baseUrl=https://localhost:7007]
    opg4j> var session=instance.createSession("mySession")
    session ==> PgxSession[ID=5af9c362-10a3-4a7c-953c-602553d4606b,source=mySession]
    opg4j> var graph = session.readGraphByName("BANK_GRAPH",GraphSource.PG_VIEW)
    graph ==> PgxGraph[name=BANK_GRAPH,N=1000,E=4997,created=1684315831352]
    ServerInstance instance = GraphServer.getInstance("https://localhost:7007", "<username>", "<password>".toCharArray());
    PgxSession session = instance.createSession("my-session");
    PgxGraph graph = session.readGraphByName("BANK_GRAPH",GraphSource.PG_VIEW);
    >>> instance = graph_server.get_instance("https://localhost:7007","<username>","<password>")
    >>> session = instance.create_session("my_session")
    >>> graph = session.read_graph_by_name("BANK_GRAPH", "pg_view")
    >>> graph
    PgxGraph(name: BANK_GRAPH, v: 1000, e: 4997, directed: True, memory(Mb): 0)
  2. Build a Deep Walk model using customized hyper-parameters as shown:
    opg4j> var model = session.createAnalyst().deepWalkModelBuilder().
    ...>                 setMinWordFrequency(1).
    ...>                 setBatchSize(512).
    ...>                 setNumEpochs(1).
    ...>                 setLayerSize(100).
    ...>                 setLearningRate(0.05).
    ...>                 setMinLearningRate(0.0001).
    ...>                 setWindowSize(3).
    ...>                 setWalksPerVertex(6).
    ...>                 setWalkLength(4).
    ...>                 setNegativeSample(2).
    ...>                 build()
    model ==> oracle.pgx.api.mllib.DeepWalkModel@6e0f259e
    import oracle.pgx.api.mllib.DeepWalkModel;
    DeepWalkModel model= session.createAnalyst().deepWalkModelBuilder()
            .setMinWordFrequency(1)
            .setBatchSize(512)
            .setNumEpochs(1)
            .setLayerSize(100)
            .setLearningRate(0.05)
            .setMinLearningRate(0.0001)
            .setWindowSize(3)
            .setWalksPerVertex(6)
            .setWalkLength(4)
            .setNegativeSample(2)
            .build();
    >>> model = session.create_analyst().deepwalk_builder(min_word_frequency= 1,
    ...                                 batch_size= 512,
    ...                                 num_epochs= 1,
    ...                                 layer_size= 100,
    ...                                 learning_rate= 0.05,
    ...                                 min_learning_rate= 0.0001,
    ...                                 window_size= 3,
    ...                                 walks_per_vertex= 6,
    ...                                 walk_length= 4,
    ...                                 negative_sample= 2)
  3. Train the Deep Walk model as shown:
    opg4j> model.fit(graph)
    model.fit(graph);
    >>> model.fit(graph)
  4. Get the loss value as shown:
    opg4j> var loss = model.getLoss()
    loss ==> -2.097562355629634E-5
    double loss = model.getLoss();
    >>> loss = model.loss
    >>> loss
    -2.0706271243398078e-05
  5. Compute similar vertices as shown:
    opg4j> var similars = model.computeSimilars("ACCOUNTS(280)",10)
    batchSimilars ==> oracle.pgx.api.frames.internal.PgxFrameImpl@308e465b
    opg4j> batchSimilars.print()
    import oracle.pgx.api.frames.*;
    
    PgxFrame similars = model.computeSimilars("ACCOUNTS(280)", 10);
    similars.print();
    >>> similars = model.compute_similars("ACCOUNTS(280)",10)
    >>> similars.print()

    The example produces a similar output:

    +-------------------------------------+
    | dstVertex     | similarity          |
    +-------------------------------------+
    | ACCOUNTS(280) | 1.0                 |
    | ACCOUNTS(486) | 0.3253505229949951  |
    | ACCOUNTS(615) | 0.2806776463985443  |
    | ACCOUNTS(660) | 0.27348122000694275 |
    | ACCOUNTS(737) | 0.2734076678752899  |
    | ACCOUNTS(368) | 0.2707795202732086  |
    | ACCOUNTS(479) | 0.27019545435905457 |
    | ACCOUNTS(845) | 0.2618815004825592  |
    | ACCOUNTS(834) | 0.2543807625770569  |
    | ACCOUNTS(249) | 0.24260951578617096 |
    +-------------------------------------+
  6. Get all trained vectors and store them in a database table as shown:
    opg4j> var vertexVectors = model.getTrainedVertexVectors().flattenAll()
    vertexVectors ==> oracle.pgx.api.frames.internal.PgxFrameImpl@46cb9794
    opg4j> vertexVectors.write().db().name("deepwalkframe").tablename("vertexVectors").overwrite(true).store()
    PgxFrame vertexVectors = model.getTrainedVertexVectors().flattenAll();
    vertexVectors.write()
      .db()
      .name("vertex vectors")
      .tablename("vertexVectors")
      .overwrite(true)
      .store();
    >>> vertex_vectors = model.trained_vectors.flatten_all()
    >>> vertex_vectors.write().db(). \
    ...         table_name("vertex_vectors"). \
    ...         overwrite(True). \
    ...         store()
  7. Store the trained model in the database as shown:
    opg4j> model.export().db().
    ...>   modelstore("bank_model").
    ...>   modelname("model").
    ...>   description("DeepWalk Model for Bank data").
    ...>   store()
    model.export().db()
      .modelstore("bank_model")  
      .modelname("model2")                 
      .description("DeepWalk Model for Bank data") 
      .store();
    >>> model.export().db(model_store="bank_model",
    ...                   model_name="model",
    ...                   model_description="DeepWalk Model for Bank data")
  8. Load a pre-trained model from the database as shown:
    opg4j> session.createAnalyst().loadDeepWalkModel().db().
    ...>   modelstore("bank_model").
    ...>   modelname("model").
    ...>   load()
    model = session.createAnalyst().loadDeepWalkModel().db()
      .modelstore("bank_model")
      .modelname("model")   
      .load();
    >>> model = session.create_analyst().get_deepwalk_model_loader().db(model_store="bank_model",
    ...                                        model_name="model")
  9. Destroy the model as shown:
    opg4j> model.destroy()
    model.destroy();
    >>> model.destroy()
    See Using the Machine Learning Library (PgxML) for Graphs for more information on the supported machine learning algorithms.