Layer.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <Models/MultilayerPerceptron.h>
  9. #include <AzCore/RTTI/RTTI.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. namespace MachineLearning
  14. {
  15. void Layer::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<Layer>()
  20. ->Version(1)
  21. ->Field("InputSize", &Layer::m_inputSize)
  22. ->Field("OutputSize", &Layer::m_outputSize)
  23. ->Field("Weights", &Layer::m_weights)
  24. ->Field("Biases", &Layer::m_biases)
  25. ;
  26. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  27. {
  28. editContext->Class<Layer>("A single layer of a neural network", "")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->DataElement(AZ::Edit::UIHandlers::Default, &Layer::m_inputSize, "Input Size", "This value must match the output size of the previous layer, or the number of neurons in the activation layer if this is the first layer")
  31. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &Layer::OnSizesChanged)
  32. ->DataElement(AZ::Edit::UIHandlers::Default, &Layer::m_outputSize, "Output Size", "This value must match the input size of the next layer, if one exists")
  33. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &Layer::OnSizesChanged)
  34. ;
  35. }
  36. }
  37. auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  38. if (behaviorContext)
  39. {
  40. behaviorContext->Class<Layer>()->
  41. Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)->
  42. Attribute(AZ::Script::Attributes::Module, "machineLearning")->
  43. Attribute(AZ::Script::Attributes::ExcludeFrom, AZ::Script::Attributes::ExcludeFlags::ListOnly)->
  44. Constructor<AZStd::size_t, AZStd::size_t>()->
  45. Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  46. ;
  47. }
  48. }
  49. Layer::Layer(AZStd::size_t activationDimensionality, AZStd::size_t layerDimensionality)
  50. : m_inputSize(activationDimensionality)
  51. , m_outputSize(layerDimensionality)
  52. {
  53. OnSizesChanged();
  54. }
  55. const AZ::VectorN& Layer::ActivateLayer(const AZ::VectorN& activations)
  56. {
  57. m_output = m_biases;
  58. AZ::VectorMatrixMultiply(m_weights, activations, m_output);
  59. return m_output;
  60. }
  61. void Layer::OnSizesChanged()
  62. {
  63. m_weights = AZ::MatrixMxN::CreateRandom(m_outputSize, m_inputSize);
  64. m_biases = AZ::VectorN::CreateRandom(m_outputSize);
  65. m_output = AZ::VectorN::CreateZero(m_outputSize);
  66. }
  67. }