Skip to content

Commit

Permalink
Add examples folder and fix README
Browse files Browse the repository at this point in the history
  • Loading branch information
lewish committed Feb 5, 2024
1 parent fa081e8 commit e75a3fc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .ijwb/.bazelproject
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
directories:
# Add the directories you want added as source here
# By default, we've added your entire workspace ('.')
arrayfire
.

# Automatically includes all relevant targets under the 'directories' above
derive_targets_from_directories: true
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ Currently, the API only supports a subset of functionality of ArrayFire, along w
The following example demonstrates usage of the API, including autograd.

```java
import arrayfire.*;
import arrayfire.af.*;
import arrayfire.optimizers.SGD;

import static arrayfire.af.*;

void main() {
tidy(() -> {
var a = params(randu(F32, shape(5)), SGD.create());
var a = params(() -> randu(F32, shape(5)), SGD.create());
var b = randu(F32, shape(5));
var latestLoss = Float.POSITIVE_INFINITY;
for (int i = 0; i < 50 || latestLoss >= 1E-10; i++) {
latestLoss = tidy(() -> {
var mul = mul(a, b);
var loss = pow(sub(sum(mul), constant(5)), 2);
var loss = pow(sub(sum(mul), 5), 2);
optimize(loss);
return data(loss).get(0);
});
}
assertEquals(0, latestLoss, 1E-10);
System.out.println(latestLoss);
});
}
```
Expand Down
6 changes: 6 additions & 0 deletions examples/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
java_binary(
name = "SimpleSGD",
srcs = ["SimpleSGD.java"],
main_class = "SimpleSGD",
deps = ["//arrayfire"],
)
20 changes: 20 additions & 0 deletions examples/SimpleSGD.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import arrayfire.optimizers.SGD;

import static arrayfire.af.*;

void main() {
tidy(() -> {
var a = params(() -> randu(F32, shape(5)), SGD.create());
var b = randu(F32, shape(5));
var latestLoss = Float.POSITIVE_INFINITY;
for (int i = 0; i < 50 || latestLoss >= 1E-10; i++) {
latestLoss = tidy(() -> {
var mul = mul(a, b);
var loss = pow(sub(sum(mul), 5), 2);
optimize(loss);
return data(loss).get(0);
});
}
System.out.println(latestLoss);
});
}

0 comments on commit e75a3fc

Please sign in to comment.