- Graph Developer's Guide for Property Graph
- Using the Graph Server (PGX)
- Using the Machine Learning Library (PgxML) for Graphs
- Model Repository and Model Stores
- Database-Backed Model Repository
17.8.1 Database-Backed Model Repository
In a database-backed model repository, each model store corresponds to a table
in the database. Internally, the tables are prefixed by 'GMLS_'.
The following steps describe the usage of the model repository API with code examples.
- Create a model repository object as shown:
opg4j> var mr = analyst.modelRepository().db().open() mr ==> oracle.pgx.api.mllib.DbModelRepository@5aac6f9f
DbModelRepository mr = analyst.modelRepository().db().open();
>>> mr = analyst.model_repository().db() >>> mr <pypgx.api.mllib._model_repo.ModelRepository object at 0x7f637496df60>
The preceding example assumes that you are creating the model repository from the current logged in database. If you must create the repository in a different database, then refer to the following example:
opg4j> var mr = analyst.modelRepository().db(). ...> username("<username>"). // DB user to use for storing the model ...> password("<password>"). // password of the DB user ...> jdbcUrl("<jdbcUrl>"). // jdbc url to the DB ...> open()
DbModelRepository mr = analyst.modelRepository().db() .username("<username>") // DB user to use for storing the model .password("<password>") // password of the DB user .jdbcUrl("<jdbcUrl>") // jdbc url to the DB .open();
>>> mr = analyst.model_repository().db(username = "<username>", # DB user to use for storing the model ... password = "<password>", # password of the DB user ... jdbc_url = "<jdbc_url>") # jdbc url to the DB
- Create a model store as shown:
- List the model store as shown and verify that the model store is empty:
opg4j> mr.listModelStoresNames() $4 ==> [DW, deepwalk_model, modelstore, modelstoretablename] opg4j> mr.listModelStoresNamesMatching(modelstore) $5 ==> [modelstore, modelstoretablename] opg4j> mr.listModels(modelstore) $6 ==> []
mr.listModelStoresNames(); mr.listModelStoresNamesMatching(modelstore); mr.listModels(modelstore);
>>> mr.list_model_stores_names() >>> mr.list_model_stores_names_matching("modelstore") >>> mr.list_models("modelstore")
- Create and fit a DeepWalk model as shown:
opg4j> var walkLength = 5 opg4j> var walksPerVertex = 4 opg4j> var embeddingSize = 20 opg4j> var batchSize = 128 opg4j> var model = analyst.deepWalkModelBuilder() .setLayerSize(embeddingSize) .setWalkLength(walkLength) .setWalksPerVertex(walksPerVertex) .setBatchSize(batchSize).build() model ==> oracle.pgx.api.mllib.DeepWalkModel@34be7efb opg4j> var smallGraphDeepWalk = session.readGraphByName("<graph_name>",GraphSource.PG_PGQL) smallGraphDeepWalk ==> PgxGraph[name=BANK_GRAPH_2,N=1000,E=5001,created=1649075718843] opg4j> model.fit(smallGraphDeepWalk)
import oracle.pgx.api.mllib.DeepWalkModel; int walkLength = 5; int walksPerVertex = 4; int embeddingSize = 20; int batchSize = 128; DeepWalkModel model = analyst.deepWalkModelBuilder() .setLayerSize(embeddingSize) .setWalkLength(walkLength) .setWalksPerVertex(walksPerVertex) .setBatchSize(batchSize).build(); PgxGraph smallGraphDeepWalk = session.readGraphByName("<graph_name>",GraphSource.PG_PGQL); model.fit(smallGraphDeepWalk);
>>> model = analyst.deepwalk_builder(window_size=3,walks_per_vertex=6,walk_length=4) graph = session.read_graph_by_name("<graph_name>", 'pg_pgql') >>> model.fit(graph)
- Store the trained model in the model store as shown:
opg4j> var modelName = "DeepWalkModel" opg4j> var modelStorer = model.export().db() modelStorer ==> oracle.pgx.api.mllib.DbModelStorer@1e86b2d1 opg4j> modelStorer.modelstore(modelstore) .overwrite(true).modelname(modelName) .description("DeepWalk: model desc") .store()
import oracle.pgx.api.mllib.DbModelStorer; import oracle.pgx.api.mllib.DbModelLoader; String modelName = "DeepWalkModel"; DbModelStorer<DeepWalkModel> modelStorer = model.export().db(); modelStorer.modelstore(modelstore) .overwrite(true).modelname(modelName) .description("DeepWalk: model desc") .store();
>>> model.export().db(model_store = "modelstore", model_name = "DeepWalkModel", ... model_description = "DeepWalk model description")
- Verify that the model is now stored in the model store as shown:
- Load the model from the model store as shown:
opg4j> var modelLoader = analyst.loadDeepWalkModel().db() opg4j> var reloadedModel = modelLoader. modelstore(modelstore). modelname(modelName). load() reloadedModel ==> oracle.pgx.api.mllib.DeepWalkModel@4248608d
DbModelLoader<DeepWalkModel> modelLoader = analyst.loadDeepWalkModel().db(); DeepWalkModel reloadedModel = modelLoader.modelstore(modelstore) .modelname(modelName) .load();
>>> analyst.get_deepwalk_model_loader().db(model_store = "modelstore", model_name = "DeepWalkModel") DeepWalkModel
The preceding example assumes that you are loading the model from the current logged in database. If you must load the model from a different database then refer to the example in Loading a Pre-Trained Model From Another Database. - Get the model description from the model store as shown:
- Delete the model from the model store as shown:
- Delete the model store as shown:
Parent topic: Model Repository and Model Stores