Skip to content

Commit 7ce9b64

Browse files
committed
neuralang default broadcasting
1 parent cc092e2 commit 7ce9b64

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

JGNN/src/examples/nodeClassification/Scripting.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public static void main(String[] args) throws Exception {
3737
.autosize(new EmptyTensor(dataset.samples().getSlice().size()));
3838

3939
ModelTraining trainer = new ModelTraining()
40-
.setEpochs(300)
41-
.setPatience(100)
42-
.setOptimizer(new Adam(0.01))
40+
.configFrom(modelBuilder)
4341
.setVerbose(true)
4442
.setLoss(new CategoricalCrossEntropy())
4543
.setValidationLoss(new CategoricalCrossEntropy());

JGNN/src/main/java/mklab/JGNN/adhoc/ModelBuilder.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -941,23 +941,33 @@ else if(functions.containsKey(splt[2])) {
941941
if(i<args.length) {
942942
String config = args[i].substring(0, args[i].indexOf(":")).trim();
943943
String value = args[i].substring(args[i].indexOf(":")+1).trim();
944+
boolean isFinal = value.startsWith("!");
945+
if(isFinal)
946+
value = value.substring(1);
944947
if(value.equals("extern")) {
945948
if(!this.configurations.containsKey(config))
946949
throw new RuntimeException("Required external config: "+config);
947950
}
948-
if(!this.configurations.containsKey(config))
951+
if(!this.configurations.containsKey(config)) {
952+
if(isFinal)
953+
configStack.put(config, parseConfigValue(value));
949954
this.config(config, parseConfigValue(value));
955+
}
950956
}
951957
// these are parsed in the attempt to create an intermediate variable for the argument
952958
if(i<splt.length-3) {
953959
String config = splt[i+3].substring(0, splt[i+3].indexOf(":")).trim();
954960
String value = splt[i+3].substring(splt[i+3].indexOf(":")+1).trim();
961+
boolean isFinal = value.startsWith("!");
962+
if(isFinal)
963+
value = value.substring(1);
955964
if(value.equals("extern")) {
956965
if(!this.configurations.containsKey(config))
957966
throw new RuntimeException("Required external config: "+config);
958967
}
959-
else
960-
this.config(config, parseConfigValue(value));
968+
if(isFinal)
969+
configStack.put(config, parseConfigValue(value));
970+
this.config(config, parseConfigValue(value));
961971
}
962972
}
963973
List<String> tokens = extractTokens(functions.get(splt[2]));

architectures.nn

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn classify(nodes, h, epochs: 3000, patience: 100, lr: 0.01) {
1+
fn classify(nodes, h, epochs: !3000, patience: !100, lr: !0.01) {
22
return softmax(h[nodes], dim: "row");
33
}
44

tutorials/Neuralang.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ using a syntax inspired by Mojo. Use a Rust highlighter, which covers
1515
all keywords. Below are examples of function declarations:
1616

1717
```rust
18-
fn classify(nodes, h, epochs: 3000, patience: 100, lr: 0.01) {
18+
fn classify(nodes, h, epochs: !3000, patience: !100, lr: !0.01) {
1919
return softmax(h[nodes], dim: "row");
2020
}
2121
```
2222

23-
The classify function takes several parameters: nodes, which are the input nodes for classification; h, the feature matrix; epochs, which defaults to 3000 and represents the number of training epochs; patience, which defaults to 100 and denotes the early stopping patience; and lr, the learning rate, which defaults to 0.01. The function returns the softmax output for the specified nodes. Configuration defaults are indicated by a colon (:) in the function signatures. The same notation is used to set/overwrite them when calling functions, as we do for softmax to apply it row-wise. Configuration values have the priority:
23+
The classify function takes two inputs: nodes are the input nodes for classification; h is the feature matrix. The function returns a softmax output for the specified nodes. It also considers several configuration values, whose defaults are indicated by a colon (:) in the function signatures. The same notation is used to set/overwrite them when calling functions, as we do for softmax to apply it row-wise. Think of them as keyword arguments. These defaults for the classify function are: epochs, which defaults to 3000 and represents the number of training epochs; patience, which defaults to 100 and denotes the early stopping patience; and lr the learning rate that defaults to 0.01.
24+
25+
Exclamation marks (!) before numbers broadcast them to all subsequent function calls as new defaults for the same configurations. Broadcasted configurations are retrievable from JGNN's Neuralang model builder, which is useful for Java integration later. Configuration values have the priority:
2426
1. function call arguments
25-
2. Java configurations
27+
2. broacasted configurations (last value, includes configurations set by Java)
2628
3. function signature defaults
2729

2830
```rust
@@ -70,6 +72,6 @@ ModelTraining trainer = new ModelTraining()
7072
.setValidationLoss(new CategoricalCrossEntropy());
7173
```
7274

73-
In this example, a dataset (Cora) is loaded, and its graph is prepared by adding self-loops (this is known as the renormalization trick) and normalizing symmetrically. A Neuralang instance is created, which is a ModelBuilder that can parse scripts as either file Paths or pure text. Constants like the adjacency matrix A and feature matrix h are set, along with variables (nodes) and configurations (classes, hidden). The model and its output is defined with a Neuralang statement. Finally, dimension names and sizes for ? found model declaration are filled by calling autosize. In the example we use empty tensors to avoid unecessary computations while determining the dimensions.
75+
In the above example, a dataset (Cora) is loaded, and its graph is prepared by adding self-loops (the renormalization trick) and performing symmetric normalization. A Neuralang instance is ten created; this is a ModelBuilder that can parse scripts as either file Paths or pure text. Constants like the adjacency matrix A and feature matrix h are set, along with variables (nodes) and configurations (classes, hidden). The model and its output is defined with a Neuralang statement. Finally, dimension names and sizes for ? found model declaration are filled by calling autosize. In the example we use empty tensors to avoid unecessary computations while determining the dimensions.
7476

75-
A ModelTraining instance is then configured using parameters from the ModelBuilder, utilizing the configurations found in the classification method.
77+
A ModelTraining instance is finally configured using parameters from the ModelBuilder, utilizing the configurations found in the classification method. Don't forget to broadcast configuration values that you need to access from Java code later.

0 commit comments

Comments
 (0)