Guest User

Untitled

a guest
Sep 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. class SoftMax(Module):
  2. def __init__(self):
  3. super().__init__()
  4.  
  5. def updateOutput(self, input):
  6. exps = np.exp(input - input.max(axis=1, keepdims=True))
  7. self.output = exps / np.sum(exps, axis=1, keepdims=True)
  8. return self.output
  9.  
  10. def updateGradInput(self, input, gradOutput):
  11. self.gradInput = self.output * (1-self.output) * gradOutput \
  12. - self.output * np.sum(self.output * gradOutput, axis=1, keepdims=True) \
  13. + self.output * self.output * gradOutput
  14. return self.gradInput
  15.  
  16. def __repr__(self):
  17. return "SoftMax"
  18.  
  19. class Crossentropy(Criterion):
  20. def __init__(self):
  21. super().__init__()
  22.  
  23. def updateOutput(self, input, target):
  24. eps = 1e-15
  25. input_clamp = np.clip(input, eps, 1 - eps)
  26. self.output = - np.sum(np.log(input) * target) / input.shape[0]
  27. return self.output
  28.  
  29. def updateGradInput(self, input, target):
  30. input_clamp = np.maximum(1e-15, np.minimum(input, 1 - 1e-15) )
  31. self.gradInput = - (target / input) / input.shape[0]
  32. return self.gradInput
  33.  
  34. def __repr__(self):
  35. return "ClassNLLCriterion"
Add Comment
Please, Sign In to add comment