Skip to content

Commit

Permalink
[system-a] gradientDescentVelocity
Browse files Browse the repository at this point in the history
- `velocityRepresentation` -- takes `options: { relayFactor }`
  • Loading branch information
xieyuheng committed May 20, 2024
1 parent 1d1e9a6 commit d3e3587
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# system-a

[system-a] `velocityRepresentation` -- takes `options: { velocityAccumulationFactor }`
[system-a] `gradientDescentVelocity`
[system-a] targets test `gradientDescentVelocity`

# question
Expand Down
11 changes: 7 additions & 4 deletions src/system-a/gradient-descent/gradientDescentVelocity.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { tensorZeros, type Tensor } from "../tensor/index.js"
import { mul, sub } from "../toys/index.js"
import { add, mul, sub } from "../toys/index.js"
import type { Representation } from "./Representation.js"
import { gradientDescent } from "./gradientDescent.js"

export function velocityRepresentation(options: {
learningRate: number
velocityAccumulationFactor: number
relayFactor: number
}): Representation<[Tensor, Tensor]> {
return {
inflate: (p) => [p, tensorZeros(p)],
deflate: ([p, _]) => p,
update: ([p], g) => [sub(p, mul(options.learningRate, g))],
update: ([p, w], g) => {
const v = sub(mul(w, options.relayFactor), mul(g, options.learningRate))
return [add(p, v), v]
},
}
}

export function gradientDescentVelocity(options: {
learningRate: number
velocityAccumulationFactor: number
relayFactor: number
}) {
return gradientDescent(velocityRepresentation(options))
}

0 comments on commit d3e3587

Please sign in to comment.