Skip to content

Commit

Permalink
updated docs and package infos
Browse files Browse the repository at this point in the history
  • Loading branch information
maniospas committed Aug 24, 2024
1 parent 899cd6d commit e3cdc59
Show file tree
Hide file tree
Showing 383 changed files with 8,793 additions and 5,394 deletions.
2 changes: 1 addition & 1 deletion JGNN/src/examples/classification/LogisticRegression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.nn.loss.Accuracy;
import mklab.JGNN.nn.loss.BinaryCrossEntropy;
import mklab.JGNN.core.Slice;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/classification/MLP.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/APPNP.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
9 changes: 6 additions & 3 deletions JGNN/src/examples/nodeClassification/GAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.adhoc.train.NodeClassification;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down Expand Up @@ -34,7 +35,7 @@ public static void main(String[] args) throws Exception {
.classify()
.assertBackwardValidity();

ModelTraining trainer = new ModelTraining()
ModelTraining trainer = new NodeClassification()
.setOptimizer(new Adam(0.01))
.setEpochs(300)
.setPatience(100)
Expand All @@ -48,7 +49,9 @@ public static void main(String[] args) throws Exception {
.init(new XavierNormal())
.train(trainer,
Tensor.fromRange(nodes.size()).asColumn(),
dataset.labels(), nodes.range(0, 0.6), nodes.range(0.6, 0.8));
dataset.labels(),
nodes.range(0, 0.6),
nodes.range(0.6, 0.8));

System.out.println("Training time "+(System.currentTimeMillis()-tic)/1000.);
Matrix output = model.predict(Tensor.fromRange(0, nodes.size()).asColumn()).get(0).cast(Matrix.class);
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/GCN.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.core.empy.EmptyTensor;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/GCNII.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/HetGCN.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/MessagePassing.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/nodeClassification/Scripting.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.Neuralang;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.core.empy.EmptyTensor;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/tutorial/Learning.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
2 changes: 1 addition & 1 deletion JGNN/src/examples/tutorial/NN.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Citeseer;
import mklab.JGNN.adhoc.parsers.LayeredBuilder;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.nn.initializers.XavierNormal;
Expand Down
64 changes: 36 additions & 28 deletions JGNN/src/examples/tutorial/Quickstart.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import mklab.JGNN.adhoc.Dataset;
import mklab.JGNN.adhoc.ModelBuilder;
import mklab.JGNN.adhoc.ModelTraining;
import mklab.JGNN.adhoc.datasets.Cora;
import mklab.JGNN.adhoc.parsers.FastBuilder;
import mklab.JGNN.adhoc.parsers.Neuralang;
import mklab.JGNN.core.Matrix;
import mklab.JGNN.nn.Model;
import mklab.JGNN.nn.ModelTraining;
import mklab.JGNN.core.Slice;
import mklab.JGNN.core.Tensor;
import mklab.JGNN.core.empy.EmptyTensor;
Expand All @@ -25,42 +26,49 @@ public class Quickstart {
public static void main(String[] args) throws Exception {
Dataset dataset = new Cora();
dataset.graph().setMainDiagonal(1).setToSymmetricNormalization();


String architectures = """
fn classify(nodes, h, epochs: !3000, patience: !100, lr: !0.01) {
return softmax(h[nodes], dim: "row");
}
fn gcnlayer(A, h, hidden: 16, reg: 0.005) {
return A@h@matrix(?, hidden, reg) + vector(hidden);
}
fn gcn(A, h, classes: extern) {
h = gcnlayer(A, h);
h = dropout(relu(h), 0.5);
return gcnlayer(A, h, hidden: classes);
}
""";

long numSamples = dataset.samples().getSlice().size();
long numClasses = dataset.labels().getCols();
ModelBuilder modelBuilder = new FastBuilder(dataset.graph(), dataset.features())
.config("reg", 0.005)
.config("classes", numClasses)
.config("hidden", 64)
.function("gcnlayer", "(A,h){Adrop = dropout(A, 0.5); return Adrop@(h@matrix(?, hidden, reg))+vector(?);}")
.layer("h{l+1}=relu(gcnlayer(A, h{l}))")
.config("hidden", "classes") // reassigns the output gcnlayer's "hidden" to be the number of "classes"
.layer("h{l+1}=gcnlayer(A, h{l})")
.classify()
.autosize(new EmptyTensor(numSamples));
ModelBuilder modelBuilder = new Neuralang()
.parse(architectures)
.constant("A", dataset.graph())
.constant("h", dataset.features())
.var("nodes")
.config("classes", numClasses)
.config("hidden", numClasses+2)
.out("classify(nodes, gcn(A,h))")
.autosize(new EmptyTensor(numSamples));
System.out.println("Preferred learning rate: "+modelBuilder.getConfig("lr"));

ModelTraining trainer = new ModelTraining()
.setOptimizer(new Adam(0.01))
.setEpochs(3000)
.setPatience(100)
.configFrom(modelBuilder)
.setLoss(new CategoricalCrossEntropy())
.setValidationLoss(new VerboseLoss(new Accuracy()).setInterval(10));
.setValidationLoss(new VerboseLoss(new CategoricalCrossEntropy()));

Slice nodes = dataset.samples().getSlice().shuffle(); // a permutation of node identifiers
Matrix inputData = Tensor.fromRange(nodes.size()).asColumn(); // each node has its identifier as an input
long tic = System.currentTimeMillis();
Slice nodes = dataset.samples().getSlice().shuffle(100);
Model model = modelBuilder.getModel()
.init(new XavierNormal())
.train(trainer,
inputData,
dataset.labels(),
nodes.range(0, 0.6), // train slice
nodes.range(0.6, 0.8) // validation slice
);

//modelBuilder.save(Paths.get("gcn_cora.jgnn"));
.train(trainer,
Tensor.fromRange(nodes.size()).asColumn(),
dataset.labels(), nodes.range(0, 0.6), nodes.range(0.6, 0.8));

Model loadedModel = model;//ModelBuilder.load(Paths.get("gcn_cora.jgnn")).getModel();
Matrix output = loadedModel.predict(Tensor.fromRange(0, nodes.size()).asColumn()).get(0).cast(Matrix.class);
System.out.println("Training time "+(System.currentTimeMillis()-tic)/1000.);
Matrix output = model.predict(Tensor.fromRange(0, nodes.size()).asColumn()).get(0).cast(Matrix.class);
double acc = 0;
for(Long node : nodes.range(0.8, 1)) {
Matrix nodeLabels = dataset.labels().accessRow(node).asRow();
Expand Down
Loading

0 comments on commit e3cdc59

Please sign in to comment.