laser icon indicating copy to clipboard operation
laser copied to clipboard

Fused assignation shortcut

Open mratsim opened this issue 6 years ago • 0 comments

Currently the way to implement fast sigmoid would be:

var x = randomTensor([1000, 1000], 1.0)
var output = newTensor[float64](x.shape)
forEach o in output, xi in x:
  o = 1 / (1 + exp(-x))

which is quite wordy.

Reusing the Arraymancer syntax for broacasting would be:

let output = 1 ./ (1 .+ exp(-x))

but this would allocate for:

  • x0 = -x
  • x1 = exp(x0)
  • x2 = 1 .+ x1
  • x3 = 1/x2

Unfortunately we cannot use anything over than `=` in a let/var statement like `let x .=  1 / (1 + exp(-x))`
But we can use `let x = fuse: 1 / (1 + exp(-x))` to request the code to generate `forEach` automatically.

mratsim avatar Jan 28 '19 12:01 mratsim