MultilayerPerceptron.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Math/MatrixMxN.h>
  10. #include <MachineLearning/INeuralNetwork.h>
  11. #include <Models/Layer.h>
  12. namespace MachineLearning
  13. {
  14. //! This is a basic multilayer perceptron neural network capable of basic training and feed forward operations.
  15. class MultilayerPerceptron
  16. : public INeuralNetwork
  17. {
  18. public:
  19. AZ_TYPE_INFO(MultilayerPerceptron, "{E12EF761-41A5-48C3-BF55-7179B280D45F}");
  20. //! AzCore Reflection.
  21. //! @param context reflection context
  22. static void Reflect(AZ::ReflectContext* context);
  23. MultilayerPerceptron() = default;
  24. MultilayerPerceptron(AZStd::size_t activationCount);
  25. virtual ~MultilayerPerceptron() = default;
  26. void AddLayer(AZStd::size_t layerDimensionality);
  27. AZStd::size_t GetLayerCount() const;
  28. Layer& GetLayer(AZStd::size_t layerIndex);
  29. AZStd::size_t GetParameterCount() const override;
  30. const AZ::VectorN& FeedForward(const AZ::VectorN& activations) override;
  31. float ComputeCost(const AZ::VectorN& activations, const AZ::VectorN& expectedOutput, CostFunctions costFunction) override;
  32. private:
  33. void OnActivationCountChanged();
  34. float ComputeCost_Quadratic(const AZ::VectorN& activations, const AZ::VectorN& expectedOutput);
  35. //! The number of neurons in the activation layer.
  36. AZStd::size_t m_activationCount = 0;
  37. //! The set of layers in the network.
  38. AZStd::vector<Layer> m_layers;
  39. };
  40. }