Skip to content

Commit

Permalink
[neurons] leakyRelu
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 6, 2024
1 parent 549d7aa commit c93c35a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 59 deletions.
6 changes: 2 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# system-a

[neurons] `leakyRelu`
[neurons] `recuBlock`

[neurons] `recu` -- rectifying correlational unit
[neurons] `correlate`
[neurons] `recu`
[neurons] `recuBlock`

[models] `morse/`
[models] `tenserZip` -- `[d, n] -> [n, d]` -- like transposing a matrix
Expand Down
4 changes: 2 additions & 2 deletions src/system-a/neurons/relu/denseBlock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Block } from "../../block/index.js"
import { relu } from "../../neurons/relu/index.js"
import type { Tensor } from "../../tensor/Tensor.js"
import { randomTensor } from "../../tensor/randomTensor.js"
import type { Shape } from "../../tensor/shape.js"
import { zeroTensor } from "../../tensor/zeroTensor.js"
import { leakyRelu } from "./leakyRelu.js"

export function denseBlock(inputSize: number, layerWidth: number): Block {
return Block(relu, [[layerWidth, inputSize], [layerWidth]])
return Block(leakyRelu, [[layerWidth, inputSize], [layerWidth]])
}

export function denseBlockInitParameters(shapes: Array<Shape>): Array<Tensor> {
Expand Down
1 change: 1 addition & 0 deletions src/system-a/neurons/relu/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./denseBlock.js"
export * from "./leakyRelu.js"
export * from "./relu.js"
13 changes: 13 additions & 0 deletions src/system-a/neurons/relu/leakyRelu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Scalar, Tensor } from "../../tensor/index.js"
import { extend1, lt, mul } from "../../toys/index.js"
import { linear } from "./linear.js"

export function leakyRectifyScalar(s: Scalar): Scalar {
return lt(s, 0) ? (mul(-0.01, s) as Scalar) : s
}

export const leakyRectify = extend1(leakyRectifyScalar, 0)

export function leakyRelu(t: Tensor): (weight: Tensor, bias: Tensor) => Tensor {
return (weight, bias) => leakyRectify(linear(t)(weight, bias))
}
102 changes: 51 additions & 51 deletions src/system-a/neurons/relu/relu.test.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
// import assert from "node:assert"
// import { test } from "node:test"
// import { assertTensorAlmostEqual } from "../../tensor/assertions.js"
// import { rectify, relu } from "./relu.js"
import assert from "node:assert"
import { test } from "node:test"
import { assertTensorAlmostEqual } from "../../tensor/assertions.js"
import { rectify, relu } from "./relu.js"

// test("rectify", () => {
// assert.deepStrictEqual(
// rectify([
// [-1, -2, -3],
// [1, 2, 3],
// ]),
// [
// [0, 0, 0],
// [1, 2, 3],
// ],
// )
// })
test("rectify", () => {
assert.deepStrictEqual(
rectify([
[-1, -2, -3],
[1, 2, 3],
]),
[
[0, 0, 0],
[1, 2, 3],
],
)
})

// test("relu", () => {
// assertTensorAlmostEqual(
// relu([2, 1, 3])(
// [
// [7.1, 4.3, -6.4],
// [7.1, 4.3, -6.4],
// ],
// [0.6, 1],
// ),
// [0, 0.3],
// 0.01,
// )
test("relu", () => {
assertTensorAlmostEqual(
relu([2, 1, 3])(
[
[7.1, 4.3, -6.4],
[7.1, 4.3, -6.4],
],
[0.6, 1],
),
[0, 0.3],
0.01,
)

// assertTensorAlmostEqual(
// relu([2, 1, 3])(
// [
// [7.1, 4.3, -6.4],
// [7.1, 4.3, -6.4],
// ],
// [0.6, 0.6],
// ),
// [0, 0],
// 0,
// )
assertTensorAlmostEqual(
relu([2, 1, 3])(
[
[7.1, 4.3, -6.4],
[7.1, 4.3, -6.4],
],
[0.6, 0.6],
),
[0, 0],
0,
)

// assertTensorAlmostEqual(
// relu([2, 1, 3])(
// [
// [7.1, 4.3, -6.4],
// [7.1, 4.3, -6.4],
// ],
// 0.6,
// ),
// [0, 0],
// 0,
// )
// })
assertTensorAlmostEqual(
relu([2, 1, 3])(
[
[7.1, 4.3, -6.4],
[7.1, 4.3, -6.4],
],
0.6,
),
[0, 0],
0,
)
})
4 changes: 2 additions & 2 deletions src/system-a/neurons/relu/relu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Scalar, Tensor } from "../../tensor/index.js"
import { extend1, lt, mul } from "../../toys/index.js"
import { extend1, lt } from "../../toys/index.js"
import { linear } from "./linear.js"

export function rectifyScalar(s: Scalar): Scalar {
return lt(s, 0) ? (mul(-0.01, s) as Scalar) : s
return lt(s, 0) ? 0 : s
}

export const rectify = extend1(rectifyScalar, 0)
Expand Down

0 comments on commit c93c35a

Please sign in to comment.