Guest User

Untitled

a guest
Apr 9th, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.10 KB | None | 0 0
  1. ## This is working as well
  2. ## Except that I want Node to contain BackProp from float32 to Matrix32 and Matrix32 to Matrix32
  3.  
  4. import linalg
  5.  
  6. type
  7.   BackProp[T, U] = proc (gradient: T): U {.noSideEffect.}
  8.     ## Proc for Backward propagation are typed `BackProp` and are (implicit) closures without side-effects
  9.     # To ease search, backward propagation procedures are prefixed with bp_
  10.  
  11.   Node[T, U] = object
  12.     ## Represent an operation
  13.     ## Stores the gradient transformation for backprop in weights
  14.     ## Stores indices of parent operation in parents
  15.     weights: array[2, BackProp[T, U]]
  16.     parents: array[2, int] #ref indices to parent nodes
  17.  
  18.   Context*[T, U] = object
  19.     ## Tape / Wengert list. Contains the list of applied operations
  20.     nodes: ref seq[Node[T, U]]
  21.  
  22. # Templates in Nim are always inlined. They are used for performance reason to save on function calls costs.
  23.  
  24. proc newContext*[T, U]: Context[T, U] {.noSideEffect.} =
  25.   ## Initialize a context (Tape / Wengert list)
  26.   result.nodes = new seq[Node[T, U]]
  27.   result.nodes[] = @[]
  28.  
  29. let ctx = newContext[float32, Matrix32[2,2]]
Add Comment
Please, Sign In to add comment