Skip to content

Instantly share code, notes, and snippets.

@jknthn
Created September 26, 2017 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jknthn/a417b94b134942ecba3885d132fbf92a to your computer and use it in GitHub Desktop.
Save jknthn/a417b94b134942ecba3885d132fbf92a to your computer and use it in GitHub Desktop.
class Layer {
...
var dW: Matrix!
var db: Matrix!
var dZ: Matrix!
func backward(m: Double, y: Matrix? = nil) {
if let nextLayer = nextLayer, let previousLayer = previousLayer {
dZ = nextLayer.weights.T.dot(nextLayer.dZ) * activation.backward(Z)
dW = (1.0 / m) * (dZ.dot(previousLayer.A.T))
db = (1.0 / m) * (dZ.sum(direction: .rows))
} else if let previousLayer = previousLayer, let Y = y {
dZ = ((Y / A) - ((1 - Y) / (1 - A))).invertSign()
dW = (1.0 / m) * (dZ.dot(previousLayer.A.T))
db = (1.0 / m) * (dZ.sum(direction: .rows))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment