Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## This is working as well
- ## Except that I want Node to contain BackProp from float32 to Matrix32 and Matrix32 to Matrix32
- import linalg
- type
- BackProp[T, U] = proc (gradient: T): U {.noSideEffect.}
- ## Proc for Backward propagation are typed `BackProp` and are (implicit) closures without side-effects
- # To ease search, backward propagation procedures are prefixed with bp_
- Node[T, U] = object
- ## Represent an operation
- ## Stores the gradient transformation for backprop in weights
- ## Stores indices of parent operation in parents
- weights: array[2, BackProp[T, U]]
- parents: array[2, int] #ref indices to parent nodes
- Context*[T, U] = object
- ## Tape / Wengert list. Contains the list of applied operations
- nodes: ref seq[Node[T, U]]
- # Templates in Nim are always inlined. They are used for performance reason to save on function calls costs.
- proc newContext*[T, U]: Context[T, U] {.noSideEffect.} =
- ## Initialize a context (Tape / Wengert list)
- result.nodes = new seq[Node[T, U]]
- result.nodes[] = @[]
- let ctx = newContext[float32, Matrix32[2,2]]
Add Comment
Please, Sign In to add comment