Activations.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/VectorN.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <MachineLearning/INeuralNetwork.h>
  12. namespace MachineLearning
  13. {
  14. //! Reflection helper function
  15. AZStd::vector<AZ::Edit::EnumConstant<ActivationFunctions>> GetActivationEnumValues();
  16. //! One-hot encodes the provided value into the resulting vector output, which will have dimensionality maxValue.
  17. void OneHotEncode(AZStd::size_t value, AZStd::size_t maxValue, AZ::VectorN& output);
  18. //! Reverses one-hot encoding, returns the index of the element with the largest value.
  19. AZStd::size_t ArgMaxDecode(const AZ::VectorN& vector);
  20. //! Computes the requested activation function applied to all elements of the source vector.
  21. void Activate(ActivationFunctions activationFunction, const AZ::VectorN& sourceVector, AZ::VectorN& output);
  22. //! Computes the rectified linear unit function (ReLU) applied to all elements of the source vector.
  23. void ReLU(const AZ::VectorN& sourceVector, AZ::VectorN& output);
  24. //! Computes the sigmoid applied to all elements of the source vector.
  25. void Sigmoid(const AZ::VectorN& sourceVector, AZ::VectorN& output);
  26. //! Computes the softmax applied to all elements of the source vector.
  27. void Softmax(const AZ::VectorN& sourceVector, AZ::VectorN& output);
  28. //! Computes the linear activation function applied to all elements of the source vector.
  29. void Linear(const AZ::VectorN& sourceVector, AZ::VectorN& output);
  30. //! Computes the derivative of the requested activation function applied to all elements provided vector.
  31. //! The activationOutput input here is simply the output of calling Activate on the original source vector.
  32. void Activate_Derivative(ActivationFunctions activationFunction, const AZ::VectorN& activationOutput, const AZ::VectorN& backGradients, AZ::VectorN& output);
  33. //! Computes the derivative of the rectified linear unit function (ReLU) applied to all elements of the original source vector.
  34. void ReLU_Derivative(const AZ::VectorN& activationOutput, const AZ::VectorN& backGradients, AZ::VectorN& output);
  35. //! Computes the derivative of the sigmoid activation function applied to all elements of the original source vector.
  36. void Sigmoid_Derivative(const AZ::VectorN& activationOutput, const AZ::VectorN& backGradients, AZ::VectorN& output);
  37. //! Computes the derivative of the sigmoid activation function applied to all elements of the original source vector.
  38. void Softmax_Derivative(const AZ::VectorN& activationOutput, const AZ::VectorN& backGradients, AZ::VectorN& output);
  39. //! Computes the derivative linear activation function applied to all elements of the original source vector.
  40. void Linear_Derivative(const AZ::VectorN& activationOutput, const AZ::VectorN& backGradients, AZ::VectorN& output);
  41. }