Instantly share code, notes, and snippets.

Embed
What would you like to do?
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