__init__.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import numpy as np
  2. class IdentityActivator(object):
  3. def forward(self, weighted_input):
  4. return weighted_input
  5. def backward(self, output):
  6. return 1
  7. class ReluActivator(object):
  8. """ relu激活函数 """
  9. def forward(self, weighted_input):
  10. return max(0, weighted_input)
  11. def backward(self, output):
  12. return 1 if output > 0 else 0
  13. class SigmoidActivator(object):
  14. """ Sigmoid激活函数类 """
  15. def forward(self, weighted_input):
  16. """ sigmoid函数"""
  17. return 1.0 / (1.0 + np.exp(-weighted_input))
  18. def backward(self, output):
  19. """ sigmoid导数 """
  20. return output * (1 - output)
  21. class TanhActivator(object):
  22. """ Tanh激活函数类 """
  23. def forward(self, weighted_input):
  24. """ Tanh激活函数类 """
  25. return 2.0 / (1.0 + np.exp(-2 * weighted_input)) - 1.0
  26. def backward(self, output):
  27. """ Tanh导数 """
  28. return 1 - output * output
  29. def element_wise_op(array, op):
  30. """对numpy数组进行element wise操作(按元素操作)
  31. :param array: np.array数组
  32. :param op: 激活函数
  33. :return:
  34. """
  35. for i in np.nditer(array, op_flags=['readwrite']): # 修改数组值 readwrite
  36. i[...] = op(i)