MultilayerPerceptron.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <AzNetworking/Serialization/ISerializer.h>
  11. #include <MachineLearning/INeuralNetwork.h>
  12. #include <Models/Layer.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. //! INeuralNetwork interface
  30. //! @{
  31. AZStd::string GetName() const override;
  32. AZStd::string GetAssetFile(AssetTypes assetType) const override;
  33. AZStd::size_t GetInputDimensionality() const override;
  34. AZStd::size_t GetOutputDimensionality() const override;
  35. AZStd::size_t GetLayerCount() const override;
  36. AZStd::size_t GetParameterCount() const override;
  37. IInferenceContextPtr CreateInferenceContext() override;
  38. ITrainingContextPtr CreateTrainingContext() override;
  39. const AZ::VectorN* Forward(IInferenceContextPtr context, const AZ::VectorN& activations) override;
  40. void Reverse(ITrainingContextPtr context, LossFunctions lossFunction, const AZ::VectorN& activations, const AZ::VectorN& expected) override;
  41. void GradientDescent(ITrainingContextPtr context, float learningRate) override;
  42. bool LoadModel() override;
  43. bool SaveModel() override;
  44. //! @}
  45. //! Adds a new layer to the model.
  46. void AddLayer(AZStd::size_t layerDimensionality, ActivationFunctions activationFunction = ActivationFunctions::ReLU);
  47. //! Retrieves a specific layer from the model, this is not thread safe and should only be used during unit testing to validate model parameters.
  48. Layer* GetLayer(AZStd::size_t layerIndex);
  49. //! Base serialize method for all serializable structures or classes to implement.
  50. //! @param serializer ISerializer instance to use for serialization
  51. //! @return boolean true for success, false for serialization failure
  52. bool Serialize(AzNetworking::ISerializer& serializer);
  53. //! Returns the estimated size required to serialize this model.
  54. AZStd::size_t EstimateSerializeSize() const;
  55. private:
  56. void OnActivationCountChanged();
  57. //! The model name.
  58. AZStd::string m_name;
  59. //! The model asset file.
  60. AZStd::string m_modelFile;
  61. //! Optional test and train asset data files.
  62. AZStd::string m_testDataFile;
  63. AZStd::string m_testLabelFile;
  64. AZStd::string m_trainDataFile;
  65. AZStd::string m_trainLabelFile;
  66. //! The number of neurons in the activation layer.
  67. AZStd::size_t m_activationCount = 0;
  68. //! The set of layers in the network.
  69. AZStd::vector<Layer> m_layers;
  70. };
  71. struct MlpInferenceContext
  72. : public IInferenceContext
  73. {
  74. AZStd::vector<LayerInferenceData> m_layerData;
  75. };
  76. struct MlpTrainingContext
  77. : public ITrainingContext
  78. {
  79. //! Used during the forward pass when calculating loss gradients.
  80. MlpInferenceContext m_forward;
  81. //! The number of accumulated training samples.
  82. AZStd::size_t m_trainingSampleSize = 0;
  83. //! The set of layer training data.
  84. AZStd::vector<LayerTrainingData> m_layerData;
  85. };
  86. }