Guest User

Untitled

a guest
Apr 9th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.06 KB | None | 0 0
  1. import linalg
  2.  
  3. type
  4.  
  5.   Tensor[float32] = float32 or DVector32
  6.  
  7.   BackProp[T] = proc (gradient: Tensor[T]): Tensor[T] {.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] = 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]]
  16.     parents: array[2, int] #ref indices to parent nodes
  17.  
  18.   Context*[T] = object
  19.     ## Tape / Wengert list. Contains the list of applied operations
  20.     nodes: ref seq[Node[T]]
  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: typedesc[SomeReal]): Context[T] {.noSideEffect.} =
  25.   ## Initialize a context (Tape / Wengert list)
  26.   result.nodes = new seq[Node[T]]
  27.   result.nodes[] = @[]
  28.  
  29. let ctx = newContext(float32)
  30.  
  31. # Cannot evaluate at compile time: T
Add Comment
Please, Sign In to add comment