Created
September 26, 2017 08:22
-
-
Save jknthn/a417b94b134942ecba3885d132fbf92a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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