Last active
April 11, 2018 13:47
-
-
Save darden1/8214bb39bb4f208e4f41bc260d95b691 to your computer and use it in GitHub Desktop.
softmax.py
This file contains hidden or 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 SoftMax(): | |
| def __init__(self, name="softmax"): | |
| self.name = name | |
| def forward_prop(self, Z): | |
| self.Z = Z | |
| return self.softmax(Z) | |
| def back_prop(self, Z, Y): | |
| return self.grad_softmax_with_loss(Z, Y) | |
| def softmax(self, Z): | |
| maxZ = np.max(Z, axis=1)[:, np.newaxis] | |
| expZ = np.exp(Z - maxZ) # np.expはZが少し大きくなるとすぐinfになってしまう。maxZを引くのはオーバーフローの防止。 | |
| return expZ/np.sum(expZ, axis=1)[:, np.newaxis] | |
| def grad_softmax_with_loss(self, Z, Y): | |
| return - (Y - self.softmax(Z)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment