Skip to content

Commit e167b05

Browse files
committed
For more illustrative CI logs, add a moons_demo_parallel test run logging to stdout
1 parent 1b287c8 commit e167b05

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

test/dune

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
(test
2+
(name moons_demo_parallel_run)
3+
(package neural_nets_lib)
4+
(modules moons_demo_parallel_run)
5+
(deps ocannl_config)
6+
(libraries ocannl)
7+
(preprocess
8+
(pps ppx_jane ppx_ocannl)))
9+
110
(library
211
(name tutorials)
312
(package neural_nets_lib)

test/moons_demo_parallel.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module CDSL = Train.CDSL
88
module Utils = Arrayjit.Utils
99
module Rand = Arrayjit.Rand.Lib
1010

11-
let%expect_test "Half-moons data parallel" =
11+
let main () =
1212
let seed = 1 in
1313
let hid_dim = 16 in
1414
(* let hid_dim = 4 in *)
@@ -75,7 +75,10 @@ let%expect_test "Half-moons data parallel" =
7575
]
7676
in
7777
Stdio.printf "\nHalf-moons scatterplot and decision boundary:\n";
78-
PrintBox_text.output Stdio.stdout plot_moons;
78+
PrintBox_text.output Stdio.stdout plot_moons
79+
80+
let%expect_test "Half-moons data parallel" =
81+
main ();
7982
(* NOTE: as of OCANNL 0.4, moons_demo_parallel, while deterministic on a single machine, gives
8083
slightly different results on machines with a different hardware, e.g. arm64, ppc. Here we list
8184
the results from the various CI targets. The first result is the one typically observed, the

test/moons_demo_parallel_run.ml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
open Base
2+
open Ocannl
3+
module Tn = Arrayjit.Tnode
4+
module IDX = Train.IDX
5+
module TDSL = Operation.TDSL
6+
module NTDSL = Operation.NTDSL
7+
module CDSL = Train.CDSL
8+
module Utils = Arrayjit.Utils
9+
module Rand = Arrayjit.Rand.Lib
10+
11+
let main () =
12+
let seed = 1 in
13+
let hid_dim = 16 in
14+
(* let hid_dim = 4 in *)
15+
let batch_size = 120 in
16+
(* let batch_size = 60 in *)
17+
(* let batch_size = 20 in *)
18+
let len = batch_size * 20 in
19+
let init_lr = 0.1 in
20+
(* let epochs = 10 in *)
21+
let epochs = 20 in
22+
(* let epochs = 1 in *)
23+
let noise () = Rand.float_range (-0.1) 0.1 in
24+
let moons_flat =
25+
Array.concat_map (Array.create ~len ())
26+
~f:
27+
Float.(
28+
fun () ->
29+
let i = Rand.int len in
30+
let v = of_int i * pi / of_int len in
31+
let c = cos v and s = sin v in
32+
[| c + noise (); s + noise (); 1.0 - c + noise (); 0.5 - s + noise () |])
33+
in
34+
let moons_flat ~b = TDSL.init_const ~l:"moons_flat" ~b ~o:[ 2 ] moons_flat in
35+
let moons_classes = Array.init (len * 2) ~f:(fun i -> if i % 2 = 0 then 1. else -1.) in
36+
let moons_classes ~b = TDSL.init_const ~l:"moons_classes" ~b ~o:[ 1 ] moons_classes in
37+
let%op mlp x = "b3" + ("w3" * ?/("b2" hid_dim + ("w2" * ?/("b1" hid_dim + ("w1" * x))))) in
38+
(* let%op mlp x = "b" + ("w" * x) in *)
39+
let%op loss_fn ~output ~expectation = ?/(!..1 - (expectation *. output)) in
40+
(* We don't need a regression loss formula thanks to weight_decay built into the sgd_update
41+
computation. *)
42+
let weight_decay = 0.0002 in
43+
(* So that we can inspect them. *)
44+
let backend = Arrayjit.Backends.fresh_backend () in
45+
let per_batch_callback ~at_batch ~at_step ~learning_rate ~batch_loss ~epoch_loss =
46+
if (at_batch + 1) % 20 = 0 then
47+
Stdio.printf "Batch=%d, step=%d, lr=%f, batch loss=%f, epoch loss=%f\n%!" at_batch at_step
48+
learning_rate batch_loss epoch_loss
49+
in
50+
(* Tn.print_accessible_headers (); *)
51+
let per_epoch_callback ~at_step ~at_epoch ~learning_rate ~epoch_loss =
52+
Stdio.printf "Epoch=%d, step=%d, lr=%f, epoch loss=%f\n%!" at_epoch at_step learning_rate
53+
epoch_loss
54+
in
55+
let module Backend = (val backend) in
56+
let inputs, outputs, _model_result, infer_callback, _batch_losses, _epoch_losses, _learning_rates
57+
=
58+
Train.example_train_loop ~seed ~batch_size ~max_num_devices:(batch_size / 2) ~init_lr
59+
~data_len:len ~epochs ~inputs:moons_flat ~outputs:moons_classes ~model:mlp ~loss_fn
60+
~weight_decay ~per_batch_callback ~per_epoch_callback
61+
(module Backend)
62+
()
63+
in
64+
let points = Tensor.value_2d_points ~xdim:0 ~ydim:1 inputs in
65+
let classes = Tensor.value_1d_points ~xdim:0 outputs in
66+
let points1, points2 = Array.partitioni_tf points ~f:Float.(fun i _ -> classes.(i) > 0.) in
67+
let callback (x, y) = Float.((infer_callback [| x; y |]).(0) >= 0.) in
68+
let plot_moons =
69+
let open PrintBox_utils in
70+
plot ~no_axes:true ~size:(120, 40)
71+
[
72+
Scatterplot { points = points1; pixel = "#" };
73+
Scatterplot { points = points2; pixel = "%" };
74+
Boundary_map { pixel_false = "."; pixel_true = "*"; callback };
75+
]
76+
in
77+
Stdio.printf "\nHalf-moons scatterplot and decision boundary:\n";
78+
PrintBox_text.output Stdio.stdout plot_moons
79+
80+
81+
let () =
82+
(* Get some insights. *)
83+
Utils.set_log_level 1;
84+
Exn.protect ~f:main ~finally:Utils.restore_settings

0 commit comments

Comments
 (0)