Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed Jun 6, 2024
1 parent e146808 commit 135d7cb
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 82 deletions.
18 changes: 8 additions & 10 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
已知鸢尾花数据的性质,
可以尝试利用这些性质调整参数。

# the-book

[the-book] 15: …But It Is Correlated!
[the-book] Appendix B: I Could Have Raced All Day

# system-a

`neurons/relu` -- vs `neurons/recu`
move `denseBlock` to `neurons/relu` -- maybe rename to `reluBlock`
[system-a] `neurons/relu` -- because there will also be `neurons/recu`
[system-a] move `denseBlock` to `neurons/relu` -- maybe rename to `reluBlock`
[system-a] move `gradient` to `gradient/`

[neurons] `leakyRelu`
[neurons] `recuBlock`
Expand All @@ -20,6 +13,11 @@ move `denseBlock` to `neurons/relu` -- maybe rename to `reluBlock`
[models] `morse/`
[models] `tenserZip` -- `[d, n] -> [n, d]` -- like transposing a matrix

# the-book

[the-book] 15: …But It Is Correlated!
[the-book] Appendix B: I Could Have Raced All Day

# later

[models] `accurateEnoughIrisTrainParameters`
Expand Down
4 changes: 2 additions & 2 deletions src/mos/model/line.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function line(x: number): (theta: [number, number]) => number {
return (theta) => x * theta[0] + theta[1]
}
return (theta) => x * theta[0] + theta[1]
}
8 changes: 4 additions & 4 deletions src/mos/model/test/line.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { line } from "../line.js";
import assert from "node:assert"
import { test } from "node:test"
import { line } from "../line.js"

test("mos -- line", () =>{
assert.deepEqual(line(1)([2, 3]), 5)
})
test("mos -- line", () => {
assert.deepEqual(line(1)([2, 3]), 5)
})
32 changes: 20 additions & 12 deletions src/mos/tensor/operator.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { assertTensor1, isScalar, isTensor1, type Tensor } from "./tensor.js";
import { assertTensor1, isScalar, isTensor1, type Tensor } from "./tensor.js"

export function sum1(t: Tensor): number {
assertTensor1(t)
return summed(t, t.length-1, 0)
function summed(t: Array<number>, i: number, a: number): number {
if (i === 0) {return a + t[0]}
else {return summed(t, i-1, a + t[i])}
assertTensor1(t)
return summed(t, t.length - 1, 0)
function summed(t: Array<number>, i: number, a: number): number {
if (i === 0) {
return a + t[0]
} else {
return summed(t, i - 1, a + t[i])
}
}
}

export function sum(t: Tensor): Tensor {
return summed(t)
function summed(t: Tensor): Tensor {
if (isScalar(t)) {return t}
if (isTensor1(t)) {return sum1(t)}
else {return t.map(summed)}
return summed(t)
function summed(t: Tensor): Tensor {
if (isScalar(t)) {
return t
}
}
if (isTensor1(t)) {
return sum1(t)
} else {
return t.map(summed)
}
}
}
41 changes: 21 additions & 20 deletions src/mos/tensor/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ export type Tensor = number | Array<Tensor>

export type Scalar = number


export function isScalar(t: Tensor): t is number {
return typeof(t) === "number"
return typeof t === "number"
}

export function isTensor1(t: Tensor): t is Array<number> {
return rank(t) == 1
return rank(t) == 1
}

export function assertScalar(t: Tensor): asserts t is number {
if (!isScalar(t)) {
throw new Error(`[assertScalar] ${t}`)
}
if (!isScalar(t)) {
throw new Error(`[assertScalar] ${t}`)
}
}

export function assertTensor1(t: Tensor): asserts t is Array<number> {
if (!isTensor1(t)) {
throw new Error(`[assertTensor1] ${t}`)
}
if (!isTensor1(t)) {
throw new Error(`[assertTensor1] ${t}`)
}
}

export function shape(t: Tensor): Array<number> {
if (isScalar(t)) {
return []
}
else {
return [t.length].concat(shape(t[0]))
}
if (isScalar(t)) {
return []
} else {
return [t.length].concat(shape(t[0]))
}
}

export function rank(t: Tensor): number {
return ranked(t, 0)
function ranked(t: Tensor, a: number): number{
if (isScalar(t)) {return a}
else {return ranked(t[0], a + 1)}
return ranked(t, 0)
function ranked(t: Tensor, a: number): number {
if (isScalar(t)) {
return a
} else {
return ranked(t[0], a + 1)
}
}
}
}
24 changes: 15 additions & 9 deletions src/mos/tensor/test/operator.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import test from "node:test";
import assert from "node:assert";
import { sum, sum1 } from "../operator.js";
import assert from "node:assert"
import test from "node:test"
import { sum, sum1 } from "../operator.js"

test("mos -- sum1", () => {
assert.equal(sum1([1, 2, 3, 4, 5, 6]), 21)
assert.equal(sum1([1, 2, 3, 4, 5, 6]), 21)
})

test ("mos -- sum", () => {
assert.deepEqual(sum(1), 1)
assert.deepEqual(sum([1, 2, 3]), 6)
assert.deepEqual(sum([[1, 2, 3], [4, 5, 6]]), [6, 15])
})
test("mos -- sum", () => {
assert.deepEqual(sum(1), 1)
assert.deepEqual(sum([1, 2, 3]), 6)
assert.deepEqual(
sum([
[1, 2, 3],
[4, 5, 6],
]),
[6, 15],
)
})
23 changes: 11 additions & 12 deletions src/mos/tensor/test/tensor.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import test from "node:test";
import assert from "node:assert";
import { shape, rank } from "../tensor.js";
import assert from "node:assert"
import test from "node:test"
import { rank, shape } from "../tensor.js"


test("mos -- shape", () =>{
assert.deepEqual(shape(1), [])
assert.deepEqual(shape([1, 2, 3]), [3])
assert.deepEqual(shape([[1], [2], [3]]), [3, 1])
test("mos -- shape", () => {
assert.deepEqual(shape(1), [])
assert.deepEqual(shape([1, 2, 3]), [3])
assert.deepEqual(shape([[1], [2], [3]]), [3, 1])
})

test("mos -- rank", () => {
assert.deepEqual(rank(1), 0)
assert.deepEqual(rank([1]), 1)
assert.deepEqual(rank([[1], [2], [3]]), 2)
})
assert.deepEqual(rank(1), 0)
assert.deepEqual(rank([1]), 1)
assert.deepEqual(rank([[1], [2], [3]]), 2)
})
10 changes: 1 addition & 9 deletions src/system-a/gradient-descent/gradientDescent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { inspect } from "../../utils/inspect.js"
import { zip } from "../../utils/zip.js"
import { assertTensorArray, shape, type Tensor } from "../tensor/index.js"
import { assertTensorArray, type Tensor } from "../tensor/index.js"
import type { Representation } from "./Representation.js"
import { gradient } from "./index.js"

Expand Down Expand Up @@ -31,13 +30,6 @@ export function gradientDescent<R>(
revs--
}

inspect({
who: "GradientDescentFn",
shapes: ps.map(shape),
initialPs: ps,
finalPs: rs.map(representation.deflate),
})

return rs.map(representation.deflate)
}
}
9 changes: 6 additions & 3 deletions src/system-a/models/iris/irisModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { irisTestXs, irisTestYs } from "./irisDataset.js"
import { irisModel } from "./irisModel.js"

test("irisModel", () => {
console.log(irisModel(irisTestXs))
console.log(irisTestYs)
console.log(modelAccuracy(irisModel, irisTestXs, irisTestYs))
// console.log(irisModel(irisTestXs))
// console.log(irisTestYs)
console.log(
"iris model accuracy:",
modelAccuracy(irisModel, irisTestXs, irisTestYs),
)
})
2 changes: 1 addition & 1 deletion src/system-a/models/iris/irisModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function irisTrainParameters(): Array<Tensor> {

gradientDescentAdam
const gradientDescentFn = gradientDescentAdam({
learningRate: 0.01,
learningRate: 0.06,
decayRate: 0.9,
relayFactor: 0.85,
})
Expand Down

0 comments on commit 135d7cb

Please sign in to comment.