From 33bb8222e22171c1f629a293ec645d9060557e26 Mon Sep 17 00:00:00 2001 From: Ki Rill Date: Sun, 24 Mar 2024 08:25:10 +0500 Subject: [PATCH] Tape added --- README.md | 46 +++++++++++++++++++++++++++++++-- source/rk/tautodiff/aux/tape.d | 18 +++++++++---- source/rk/tautodiff/nn/neuron.d | 1 - 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a39f582..b0cdd8b 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,11 @@ Use the `versions` configuration to specify the precision: * `TAUTODIFF_USE_REAL` ``` // dub.sdl -versions "TGRAD_USE_FLOAT" +versions "TAUTODIFF_USE_FLOAT" ``` ``` // dub.json -versions: ["TGRAD_USE_FLOAT"] +versions: ["TAUTODIFF_USE_FLOAT"] ``` ## Example usage @@ -82,6 +82,48 @@ assert(solver.grad == 0); assert(solver.values.length == 4); ``` +### Tape +Create `tapes` of equations and update the resulting value: +```d +// init +auto tape = new Tape(); +assert(tape.values == []); +assert(tape.values.length == 0); +assert(tape.locked == false); +assert(!tape.isLocked); + +// d = a * b - c +auto a = 5.value; +auto b = 10.value; +auto c = 25.value; +auto d = a * b; +auto e = d - c; +assert(e.data == 25); + +// push +tape.pushBack(a); +tape ~= b; +tape ~= [c, d, e]; +assert(tape.values == [a, b, c, d, e]); +assert(tape.values.length == 5); +assert(tape.lastValue.data == 25); + +// lock tape +tape.lock(); +// tape ~= 24.value; // assert error: reset the tape to push new values + +// modify value +a.data = 6; + +// update tape +tape.update(); +assert(tape.lastValue.data == 35); + +// reset tape to push new values +tape.reset(); +tape ~= 35.value; // good +``` + ### Multi-layer perceptron ```d diff --git a/source/rk/tautodiff/aux/tape.d b/source/rk/tautodiff/aux/tape.d index 3ee5ca7..d29a6d6 100644 --- a/source/rk/tautodiff/aux/tape.d +++ b/source/rk/tautodiff/aux/tape.d @@ -16,7 +16,15 @@ class Tape locked = false; } - /// Retrive the last pushed value + /// Retreive the first pushed value + /// Returns: null if empty + Value firstValue() + { + return values.length ? values[0] : null; + } + + /// Retreive the last pushed value + /// Returns: null if empty Value lastValue() { return values.length ? values[$-1] : null; @@ -44,25 +52,25 @@ class Tape } // Push back a single value - void pushBack(Value v) + void pushBack(Value v) in (!isLocked, "Reset the tape to push new values.") { this.values ~= v; } /// Push back values - void pushBack(Value[] vs) + void pushBack(Value[] vs) in (!isLocked, "Reset the tape to push new values.") { this.values ~= vs; } /// Append a single value - void opOpAssign(string op: "~")(Value v) + void opOpAssign(string op: "~")(Value v) in (!isLocked, "Reset the tape to push new values.") { this.pushBack(v); } /// Append values - void opOpAssign(string op: "~")(Value[] vs) + void opOpAssign(string op: "~")(Value[] vs) in (!isLocked, "Reset the tape to push new values.") { this.pushBack(vs); } diff --git a/source/rk/tautodiff/nn/neuron.d b/source/rk/tautodiff/nn/neuron.d index 9c2369f..c7918cd 100644 --- a/source/rk/tautodiff/nn/neuron.d +++ b/source/rk/tautodiff/nn/neuron.d @@ -3,7 +3,6 @@ module rk.tautodiff.nn.neuron; import rk.tautodiff.core.common; import rk.tautodiff.core.value; import rk.tautodiff.aux.activation; -import rk.tautodiff.aux.chainsolver; class Neuron : INeuron {