Skip to content

Commit

Permalink
Tape added
Browse files Browse the repository at this point in the history
  • Loading branch information
rillki committed Mar 24, 2024
1 parent fe1fde3 commit 33bb822
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions source/rk/tautodiff/aux/tape.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion source/rk/tautodiff/nn/neuron.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 33bb822

Please sign in to comment.