Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import linalg
- type
- Tensor[float32] = float32 or DVector32
- BackProp[T] = proc (gradient: Tensor[T]): Tensor[T] {.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] = object
- ## Represent an operation
- ## Stores the gradient transformation for backprop in weights
- ## Stores indices of parent operation in parents
- weights: array[2, BackProp[T]]
- parents: array[2, int] #ref indices to parent nodes
- Context*[T] = object
- ## Tape / Wengert list. Contains the list of applied operations
- nodes: ref seq[Node[T]]
- # Templates in Nim are always inlined. They are used for performance reason to save on function calls costs.
- proc newContext*(T: typedesc[SomeReal]): Context[T] {.noSideEffect.} =
- ## Initialize a context (Tape / Wengert list)
- result.nodes = new seq[Node[T]]
- result.nodes[] = @[]
- let ctx = newContext(float32)
- # Cannot evaluate at compile time: T
Add Comment
Please, Sign In to add comment