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

35 行
1.2KB

  1. import numpy as np
  2. from neural_net.activation_layers.relu_layer import ReluLayer
  3. from neural_net.functions.loss import cross_entropy_loss, cross_entropy_derivative_loss
  4. from neural_net.neural_net import NeuralNet
  5. from neural_net.transform_layer import SoftMaxLayer
  6. class MNISTNeuralNet(NeuralNet):
  7. def __init__(self):
  8. super().__init__(layers=[
  9. ReluLayer(0, 784, 121),
  10. ReluLayer(1, 121, 10),
  11. SoftMaxLayer(2, 10)
  12. ])
  13. def backward(self, dL_dout, epoch):
  14. return super().backward(dL_dout, epoch)
  15. def loss(self, y_pred: np.array, y_actual: np.array):
  16. return cross_entropy_loss(y_pred, y_actual)
  17. def loss_derivative(self, y_pred: np.array, targets: np.array):
  18. return cross_entropy_derivative_loss(y_pred, targets)
  19. def describe(self):
  20. """Return a human-readable string of the model architecture."""
  21. architecture_info = ""
  22. for layer in self.layers:
  23. architecture_info += f"{layer.describe()}\n"
  24. return architecture_info.strip()
  25. def predict(self, inputs):
  26. raw_outputs = super().predict(inputs)
  27. return raw_outputs, raw_outputs.argmax(axis=1)