FeatureMatrixStandardScaler.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <FeatureMatrix.h>
  10. #include <FeatureMatrixTransformer.h>
  11. namespace EMotionFX::MotionMatching
  12. {
  13. //! The standard scaler can be used to normalize the feature matrix, the query vector and other data.
  14. //! It standardizes features by subtracting the mean and scaling to the unit variance.
  15. //! This implementation is mimicking the behavior of the standard scaler from scikit-learn (sklearn.preprocessing.StandardScaler).
  16. //! As we use floats by default, our errors are bigger, especially if the variance is small as this leads to a division by a small
  17. //! value. In case the calculated standard deviation for a given feature is smaller than the given s_epsilon value, the standard
  18. //! deviation gets force-set to 1.0 to avoid divisions by infinity and preserve the value when doing the transform -> inverse-transform
  19. //! roundtrip.
  20. class StandardScaler : public FeatureMatrixTransformer
  21. {
  22. public:
  23. AZ_RTTI(StandardScaler, "{A0C7F056-94B0-43A1-8D12-B1A7B67AB92A}", FeatureMatrixTransformer);
  24. AZ_CLASS_ALLOCATOR_DECL;
  25. StandardScaler() = default;
  26. bool Fit(const FeatureMatrix& featureMatrix, const Settings& settings = {}) override;
  27. // Normalize and scale the values.
  28. float Transform(float value, FeatureMatrix::Index column) const override;
  29. AZ::Vector2 Transform(const AZ::Vector2& value, FeatureMatrix::Index column) const override;
  30. AZ::Vector3 Transform(const AZ::Vector3& value, FeatureMatrix::Index column) const override;
  31. void Transform(AZStd::span<float> data) const override;
  32. FeatureMatrix Transform(const FeatureMatrix& featureMatrix) const override;
  33. // From normalized and scaled back to the original values.
  34. FeatureMatrix InverseTransform(const FeatureMatrix& featureMatrix) const override;
  35. AZ::Vector2 InverseTransform(const AZ::Vector2& value, FeatureMatrix::Index column) const override;
  36. AZ::Vector3 InverseTransform(const AZ::Vector3& value, FeatureMatrix::Index column) const override;
  37. float InverseTransform(float value, FeatureMatrix::Index column) const override;
  38. const AZStd::vector<float>& GetMeans() const
  39. {
  40. return m_means;
  41. }
  42. const AZStd::vector<float>& GetStandardDeviations() const
  43. {
  44. return m_standardDeviations;
  45. }
  46. static constexpr float s_epsilon = AZ::Constants::FloatEpsilon;
  47. void SaveAsCsv(const char* filename, const AZStd::vector<AZStd::string>& columnNames = {});
  48. private:
  49. AZStd::vector<float> m_means; //!< The mean value for each feature / column.
  50. AZStd::vector<float> m_standardDeviations; //!< The standard deviation for each feature / column.
  51. };
  52. } // namespace EMotionFX::MotionMatching