您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

74 行
2.3KB

  1. from abc import abstractmethod
  2. import numpy as np
  3. class Layer:
  4. def __init__(self, type, index, input_dim, output_dim):
  5. self.type = type
  6. self.index = index
  7. self.input_dim = input_dim
  8. self.output_dim = output_dim
  9. @abstractmethod
  10. def forward(self, inputs):
  11. raise NotImplementedError("This should be overridden by subclasses")
  12. @abstractmethod
  13. def backward(self, dL_dout, learning_rate):
  14. raise NotImplementedError("This should be overridden by subclasses")
  15. @abstractmethod
  16. def reset(self):
  17. raise NotImplementedError("This should be overridden by subclasses")
  18. class TransformLayer(Layer):
  19. def __init__(self, index, size):
  20. super().__init__('TransformLayer', index, size, size)
  21. def describe(self):
  22. return self.type
  23. def forward(self, inputs):
  24. raise NotImplementedError("This should be overridden by subclasses")
  25. def backward(self, dL_dout, learning_rate):
  26. return dL_dout, None, None, None, None # This is the gradient to propagate to the previous layer
  27. def reset(self):
  28. pass
  29. class NormalizeLayer(TransformLayer):
  30. def __init__(self, index, size):
  31. super().__init__(index, size)
  32. self.type = 'NormalizeLayer'
  33. def forward(self, inputs):
  34. """
  35. Normalizes the input vector.
  36. [1, 5, 5, 3, 6] => [0.05, 0.25, 0.25, 0.15, 0.3]
  37. :param inputs: np.array(float)
  38. :return: np.array(float)
  39. """
  40. return inputs / inputs.sum()
  41. class SoftMaxLayer(TransformLayer):
  42. def __init__(self, index, size):
  43. super().__init__(index, size)
  44. self.type = 'SoftMaxLayer'
  45. def forward(self, inputs):
  46. """
  47. Normalizes the input vector, but "pushes" higher values to dominate the
  48. probability distribution
  49. [1, 5, 5, 3, 6] => [0.02, 0.26, 0.26, 0.10, 0.36]
  50. :param inputs: np.array(float)
  51. :return: np.array(float)
  52. """
  53. input_ex = np.exp(inputs - inputs.max()) # Subtract max for numerical stability
  54. s = np.sum(input_ex, axis=-1, keepdims=True)
  55. # To prevent division by zero, ensure that the sum is not zero
  56. if np.any(s == 0):
  57. return np.ones_like(input_ex) / input_ex.shape[-1] # Return a uniform distribution if sum is 0
  58. return input_ex / s