MultilayerPerceptron.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <Assets/ModelAsset.h>
  13. namespace MachineLearning
  14. {
  15. //! This is a basic multilayer perceptron neural network capable of basic training and feed forward operations.
  16. class MultilayerPerceptron
  17. : public INeuralNetwork
  18. {
  19. public:
  20. AZ_RTTI(MultilayerPerceptron, "{E12EF761-41A5-48C3-BF55-7179B280D45F}", INeuralNetwork);
  21. //! AzCore Reflection.
  22. //! @param context reflection context
  23. static void Reflect(AZ::ReflectContext* context);
  24. MultilayerPerceptron();
  25. MultilayerPerceptron(const MultilayerPerceptron&);
  26. MultilayerPerceptron(AZStd::size_t activationCount);
  27. virtual ~MultilayerPerceptron();
  28. MultilayerPerceptron& operator=(const MultilayerPerceptron&);
  29. MultilayerPerceptron& operator=(const ModelAsset&);
  30. //! INeuralNetwork interface
  31. //! @{
  32. AZStd::string GetName() const override;
  33. AZStd::string GetAssetFile(AssetTypes assetType) const override;
  34. AZStd::size_t GetInputDimensionality() const override;
  35. AZStd::size_t GetOutputDimensionality() const override;
  36. AZStd::size_t GetLayerCount() const override;
  37. AZ::MatrixMxN GetLayerWeights(AZStd::size_t layerIndex) const override;
  38. AZ::VectorN GetLayerBiases(AZStd::size_t layerIndex) const override;
  39. AZStd::size_t GetParameterCount() const override;
  40. IInferenceContextPtr CreateInferenceContext() override;
  41. ITrainingContextPtr CreateTrainingContext() override;
  42. const AZ::VectorN* Forward(IInferenceContextPtr context, const AZ::VectorN& activations) override;
  43. void Reverse(ITrainingContextPtr context, LossFunctions lossFunction, const AZ::VectorN& activations, const AZ::VectorN& expected) override;
  44. void GradientDescent(ITrainingContextPtr context, float learningRate) override;
  45. bool LoadModel() override;
  46. bool SaveModel() override;
  47. //! @}
  48. //! Adds a new layer to the model.
  49. void AddLayer(AZStd::size_t layerDimensionality, ActivationFunctions activationFunction = ActivationFunctions::ReLU);
  50. //! Retrieves a specific layer from the model, this is not thread safe and should only be used during unit testing to validate model parameters.
  51. Layer* GetLayer(AZStd::size_t layerIndex);
  52. private:
  53. void OnActivationCountChanged();
  54. //! The model name.
  55. AZStd::string m_name;
  56. //! Optional test and train asset data files.
  57. AZStd::string m_testDataFile;
  58. AZStd::string m_testLabelFile;
  59. AZStd::string m_trainDataFile;
  60. AZStd::string m_trainLabelFile;
  61. //! The number of neurons in the activation layer.
  62. AZStd::size_t m_activationCount = 0;
  63. //! The set of layers in the network.
  64. AZStd::vector<Layer> m_layers;
  65. IAssetPersistenceProxy* m_proxy = nullptr;
  66. friend class MultilayerPerceptronEditorComponent;
  67. };
  68. struct MlpInferenceContext
  69. : public IInferenceContext
  70. {
  71. AZStd::vector<LayerInferenceData> m_layerData;
  72. };
  73. struct MlpTrainingContext
  74. : public ITrainingContext
  75. {
  76. //! Used during the forward pass when calculating loss gradients.
  77. MlpInferenceContext m_forward;
  78. //! The number of accumulated training samples.
  79. AZStd::size_t m_trainingSampleSize = 0;
  80. //! The set of layer training data.
  81. AZStd::vector<LayerTrainingData> m_layerData;
  82. };
  83. }