Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Created May 23, 2019 21:02
Show Gist options
  • Save matteo-grella/e5bb7e6e5aa262a830cf947911597893 to your computer and use it in GitHub Desktop.
Save matteo-grella/e5bb7e6e5aa262a830cf947911597893 to your computer and use it in GitHub Desktop.
sumWithPrevAndNext
import com.kotlinnlp.simplednn.simplemath.ndarray.dense.DenseNDArray
fun List<DenseNDArray>.sumWithPrevAndNext(): List<DenseNDArray> = this.indices.map { i ->
val cur = this[i].copy()
if (i > 0) cur.assignSum(this[i - 1])
if (i < this.lastIndex) cur.assignSum(this[i + 1])
cur
}
fun List<DenseNDArray>.addBackwardOfSumWithPrevAndNext(gradients: List<DenseNDArray>) = this.indices.map { i ->
this[i].assignSum(gradients[i])
if (i > 0) this[i].assignSum(gradients[i - 1])
if (i < this.lastIndex) this[i].assignSum(gradients[i + 1])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment