From 3f9c7a2db3274985fa44c89bea2087b81d7d52e7 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 25 Jan 2024 13:42:45 +0100 Subject: [PATCH] Include sfw if it's an sfw build. --- SConstruct | 4 + activation/activation.cpp | 2 - activation/activation.h | 6 + ann/ann.cpp | 5 + ann/ann.h | 6 + auto_encoder/auto_encoder.cpp | 4 + auto_encoder/auto_encoder.h | 5 +- bernoulli_nb/bernoulli_nb.h | 4 + c_log_log_reg/c_log_log_reg.h | 5 + convolutions/convolutions.cpp | 5 + convolutions/convolutions.h | 8 +- cost/cost.h | 4 + data/data.cpp | 4 + data/data.h | 4 + dual_svc/dual_svc.h | 4 + exp_reg/exp_reg.h | 4 + gan/gan.cpp | 4 + gan/gan.h | 4 + gauss_markov_checker/gauss_markov_checker.h | 4 + gaussian_nb/gaussian_nb.cpp | 4 + gaussian_nb/gaussian_nb.h | 4 + hidden_layer/hidden_layer.h | 4 + hypothesis_testing/hypothesis_testing.h | 4 + kmeans/kmeans.cpp | 4 + kmeans/kmeans.h | 4 + knn/knn.cpp | 4 + knn/knn.h | 4 + lin_alg/lin_alg.cpp | 4 + lin_alg/lin_alg.h | 13 +- lin_alg/mlpp_matrix.cpp | 4 + lin_alg/mlpp_matrix.h | 521 ++- lin_alg/mlpp_tensor3.cpp | 3301 ++++++++++--------- lin_alg/mlpp_tensor3.h | 431 +-- lin_alg/mlpp_vector.h | 14 +- lin_reg/lin_reg.h | 4 + log_reg/log_reg.h | 4 + mann/mann.cpp | 4 + mann/mann.h | 4 + mlp/mlp.cpp | 4 + mlp/mlp.h | 4 + multi_output_layer/multi_output_layer.cpp | 1 + multi_output_layer/multi_output_layer.h | 4 + multinomial_nb/multinomial_nb.cpp | 4 + multinomial_nb/multinomial_nb.h | 4 + numerical_analysis/numerical_analysis.cpp | 1 + numerical_analysis/numerical_analysis.h | 4 + outlier_finder/outlier_finder.h | 4 + output_layer/output_layer.cpp | 1 + output_layer/output_layer.h | 4 + pca/pca.cpp | 1 + pca/pca.h | 4 + probit_reg/probit_reg.h | 4 + regularization/reg.cpp | 4 + regularization/reg.h | 4 + softmax_net/softmax_net.cpp | 4 + softmax_net/softmax_net.h | 4 + softmax_reg/softmax_reg.h | 4 + stat/stat.cpp | 6 + stat/stat.h | 4 + svc/svc.h | 4 + tanh_reg/tanh_reg.h | 4 + test/mlpp_matrix_tests.cpp | 4 + test/mlpp_matrix_tests.h | 4 + test/mlpp_tests.cpp | 4 + test/mlpp_tests.h | 4 + transforms/transforms.cpp | 5 + transforms/transforms.h | 4 + uni_lin_reg/uni_lin_reg.h | 4 + utilities/utilities.cpp | 4 + utilities/utilities.h | 4 + wgan/wgan.cpp | 686 ++-- wgan/wgan.h | 4 + 72 files changed, 2872 insertions(+), 2359 deletions(-) diff --git a/SConstruct b/SConstruct index e0077cd..5eee659 100644 --- a/SConstruct +++ b/SConstruct @@ -267,6 +267,10 @@ Help(opts.GenerateHelpText(env_base)) # add default include paths env_base.Prepend(CPPPATH=["#"]) +env_base.Prepend(CPPPATH=["#platform"]) + +# USE SFWL +env_base.Prepend(CPPDEFINES=["USING_SFW"]) # configure ENV for platform env_base.platform_exporters = platform_exporters diff --git a/activation/activation.cpp b/activation/activation.cpp index cdadcd9..d61f091 100644 --- a/activation/activation.cpp +++ b/activation/activation.cpp @@ -31,8 +31,6 @@ #include "activation.h" #include "../lin_alg/lin_alg.h" -#include "core/math/math_defs.h" - #include #include #include diff --git a/activation/activation.h b/activation/activation.h index 2f1914e..77f28a3 100644 --- a/activation/activation.h +++ b/activation/activation.h @@ -31,11 +31,17 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else + #include "core/math/math_defs.h" #include "core/object/func_ref.h" #include "core/object/reference.h" +#endif + #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/ann/ann.cpp b/ann/ann.cpp index f3fa84b..0ed8c18 100644 --- a/ann/ann.cpp +++ b/ann/ann.cpp @@ -35,7 +35,12 @@ #include "../lin_alg/lin_alg.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" + +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include diff --git a/ann/ann.h b/ann/ann.h index e9d133b..6338c50 100644 --- a/ann/ann.h +++ b/ann/ann.h @@ -31,10 +31,16 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else + #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif + #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_tensor3.h" #include "../lin_alg/mlpp_vector.h" diff --git a/auto_encoder/auto_encoder.cpp b/auto_encoder/auto_encoder.cpp index d979ebe..edb1258 100644 --- a/auto_encoder/auto_encoder.cpp +++ b/auto_encoder/auto_encoder.cpp @@ -34,7 +34,11 @@ #include "../cost/cost.h" #include "../utilities/utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include diff --git a/auto_encoder/auto_encoder.h b/auto_encoder/auto_encoder.h index 85092cc..9e280fe 100644 --- a/auto_encoder/auto_encoder.h +++ b/auto_encoder/auto_encoder.h @@ -31,9 +31,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" - #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/bernoulli_nb/bernoulli_nb.h b/bernoulli_nb/bernoulli_nb.h index e22fd2e..5b6b36f 100644 --- a/bernoulli_nb/bernoulli_nb.h +++ b/bernoulli_nb/bernoulli_nb.h @@ -31,11 +31,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/hash_map.h" #include "core/containers/vector.h" #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/c_log_log_reg/c_log_log_reg.h b/c_log_log_reg/c_log_log_reg.h index cad606c..1fd7e24 100644 --- a/c_log_log_reg/c_log_log_reg.h +++ b/c_log_log_reg/c_log_log_reg.h @@ -31,10 +31,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif + #include "../regularization/reg.h" #include "../lin_alg/mlpp_matrix.h" diff --git a/convolutions/convolutions.cpp b/convolutions/convolutions.cpp index 1592b05..851b66e 100644 --- a/convolutions/convolutions.cpp +++ b/convolutions/convolutions.cpp @@ -31,7 +31,12 @@ #include "convolutions.h" #include "../lin_alg/lin_alg.h" #include "../stat/stat.h" + +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_funcs.h" +#endif #include diff --git a/convolutions/convolutions.h b/convolutions/convolutions.h index 4fffd10..bfe961f 100644 --- a/convolutions/convolutions.h +++ b/convolutions/convolutions.h @@ -31,16 +31,22 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/vector.h" #include "core/string/ustring.h" #include "core/math/math_defs.h" +#include "core/object/reference.h" +#endif + #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_tensor3.h" #include "../lin_alg/mlpp_vector.h" -#include "core/object/reference.h" + class MLPPConvolutions : public Reference { GDCLASS(MLPPConvolutions, Reference); diff --git a/cost/cost.h b/cost/cost.h index 4db52b2..7018b7a 100644 --- a/cost/cost.h +++ b/cost/cost.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include diff --git a/data/data.cpp b/data/data.cpp index ac82be7..d7a8daf 100644 --- a/data/data.cpp +++ b/data/data.cpp @@ -30,7 +30,11 @@ #include "data.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/os/file_access.h" +#endif #include "../lin_alg/lin_alg.h" #include "../stat/stat.h" diff --git a/data/data.h b/data/data.h index 33eafb8..d82d96e 100644 --- a/data/data.h +++ b/data/data.h @@ -31,12 +31,16 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/variant/array.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/dual_svc/dual_svc.h b/dual_svc/dual_svc.h index 12e31b7..6b8354e 100644 --- a/dual_svc/dual_svc.h +++ b/dual_svc/dual_svc.h @@ -35,9 +35,13 @@ // http://ciml.info/dl/v0_99/ciml-v0_99-ch11.pdf // Were excellent for the practical intution behind the dual formulation. +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/exp_reg/exp_reg.h b/exp_reg/exp_reg.h index 7de8717..13566f7 100644 --- a/exp_reg/exp_reg.h +++ b/exp_reg/exp_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../regularization/reg.h" diff --git a/gan/gan.cpp b/gan/gan.cpp index 7073e8c..c0ae8dc 100644 --- a/gan/gan.cpp +++ b/gan/gan.cpp @@ -34,7 +34,11 @@ #include "../regularization/reg.h" #include "../utilities/utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include #include diff --git a/gan/gan.h b/gan/gan.h index 53f556f..114e9a6 100644 --- a/gan/gan.h +++ b/gan/gan.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../hidden_layer/hidden_layer.h" #include "../output_layer/output_layer.h" diff --git a/gauss_markov_checker/gauss_markov_checker.h b/gauss_markov_checker/gauss_markov_checker.h index 59fa946..13c05b5 100644 --- a/gauss_markov_checker/gauss_markov_checker.h +++ b/gauss_markov_checker/gauss_markov_checker.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include #include diff --git a/gaussian_nb/gaussian_nb.cpp b/gaussian_nb/gaussian_nb.cpp index 53207e7..fd1e437 100644 --- a/gaussian_nb/gaussian_nb.cpp +++ b/gaussian_nb/gaussian_nb.cpp @@ -30,7 +30,11 @@ #include "gaussian_nb.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" +#endif #include "../stat/stat.h" #include "../utilities/utilities.h" diff --git a/gaussian_nb/gaussian_nb.h b/gaussian_nb/gaussian_nb.h index 9c0b438..db2c178 100644 --- a/gaussian_nb/gaussian_nb.h +++ b/gaussian_nb/gaussian_nb.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/hidden_layer/hidden_layer.h b/hidden_layer/hidden_layer.h index 6d03b2f..44c9607 100644 --- a/hidden_layer/hidden_layer.h +++ b/hidden_layer/hidden_layer.h @@ -31,10 +31,14 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/object/reference.h" +#endif #include "../activation/activation.h" #include "../regularization/reg.h" diff --git a/hypothesis_testing/hypothesis_testing.h b/hypothesis_testing/hypothesis_testing.h index 67fa02e..a377732 100644 --- a/hypothesis_testing/hypothesis_testing.h +++ b/hypothesis_testing/hypothesis_testing.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include #include diff --git a/kmeans/kmeans.cpp b/kmeans/kmeans.cpp index f14658a..05b23d6 100644 --- a/kmeans/kmeans.cpp +++ b/kmeans/kmeans.cpp @@ -31,7 +31,11 @@ #include "kmeans.h" #include "../utilities/utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/random_pcg.h" +#endif #include #include diff --git a/kmeans/kmeans.h b/kmeans/kmeans.h index a35384d..9068bd9 100644 --- a/kmeans/kmeans.h +++ b/kmeans/kmeans.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/knn/knn.cpp b/knn/knn.cpp index 976afdd..761ad15 100644 --- a/knn/knn.cpp +++ b/knn/knn.cpp @@ -31,8 +31,12 @@ #include "knn.h" #include "../utilities/utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/hash_map.h" #include "core/containers/vector.h" +#endif Ref MLPPKNN::get_input_set() { return _input_set; diff --git a/knn/knn.h b/knn/knn.h index cf14953..d698d10 100644 --- a/knn/knn.h +++ b/knn/knn.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/lin_alg/lin_alg.cpp b/lin_alg/lin_alg.cpp index fc029b9..7b1a5cd 100644 --- a/lin_alg/lin_alg.cpp +++ b/lin_alg/lin_alg.cpp @@ -30,7 +30,11 @@ #include "lin_alg.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_funcs.h" +#endif #include "../stat/stat.h" #include diff --git a/lin_alg/lin_alg.h b/lin_alg/lin_alg.h index 43de03e..960c62b 100644 --- a/lin_alg/lin_alg.h +++ b/lin_alg/lin_alg.h @@ -33,19 +33,12 @@ //TODO Methods here should probably use error macros in a way where they get disabled in non-tools(?) (maybe release?) builds -#ifndef GDNATIVE - +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" - -#else - -#include "core/defs.h" -#include "core/math_funcs.h" - -#include "gen/resource.h" - #endif #include "../lin_alg/mlpp_matrix.h" diff --git a/lin_alg/mlpp_matrix.cpp b/lin_alg/mlpp_matrix.cpp index 08ea4fa..2e7f8e7 100644 --- a/lin_alg/mlpp_matrix.cpp +++ b/lin_alg/mlpp_matrix.cpp @@ -30,7 +30,11 @@ #include "mlpp_matrix.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/io/image.h" +#endif #include "../stat/stat.h" #include diff --git a/lin_alg/mlpp_matrix.h b/lin_alg/mlpp_matrix.h index 6f4d762..f6ff009 100644 --- a/lin_alg/mlpp_matrix.h +++ b/lin_alg/mlpp_matrix.h @@ -1,6 +1,5 @@ #ifndef MLPP_MATRIX_H #define MLPP_MATRIX_H -#ifndef GDNATIVE /*************************************************************************/ /* mlpp_matrix.h */ @@ -32,6 +31,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/containers/pool_vector.h" @@ -42,17 +44,6 @@ #include "core/os/memory.h" #include "core/object/resource.h" - -#else - -#include "core/containers/vector.h" -#include "core/defs.h" -#include "core/math_funcs.h" -#include "core/os/memory.h" -#include "core/pool_arrays.h" - -#include "gen/resource.h" - #endif #include "mlpp_vector.h" @@ -60,355 +51,359 @@ class Image; class MLPPMatrix : public Resource { - GDCLASS(MLPPMatrix, Resource); + GDCLASS(MLPPMatrix, Resource); public: - Array get_data(); - void set_data(const Array &p_from); + Array get_data(); + void set_data(const Array &p_from); - _FORCE_INLINE_ real_t *ptrw() { - return _data; - } + _FORCE_INLINE_ real_t *ptrw() { return _data; } - _FORCE_INLINE_ const real_t *ptr() const { - return _data; - } + _FORCE_INLINE_ const real_t *ptr() const { return _data; } - void row_add(const Vector &p_row); - void row_add_pool_vector(const PoolRealArray &p_row); - void row_add_mlpp_vector(const Ref &p_row); - void rows_add_mlpp_matrix(const Ref &p_other); + void row_add(const Vector &p_row); + void row_add_pool_vector(const PoolRealArray &p_row); + void row_add_mlpp_vector(const Ref &p_row); + void rows_add_mlpp_matrix(const Ref &p_other); - void row_remove(int p_index); + void row_remove(int p_index); - // Removes the item copying the last value into the position of the one to - // remove. It's generally faster than `remove`. - void row_remove_unordered(int p_index); + // Removes the item copying the last value into the position of the one to + // remove. It's generally faster than `remove`. + void row_remove_unordered(int p_index); - void row_swap(int p_index_1, int p_index_2); + void row_swap(int p_index_1, int p_index_2); - _FORCE_INLINE_ void clear() { resize(Size2i()); } - _FORCE_INLINE_ void reset() { - if (_data) { - memfree(_data); - _data = NULL; - _size = Vector2i(); - } - } + _FORCE_INLINE_ void clear() { resize(Size2i()); } + _FORCE_INLINE_ void reset() { + if (_data) { + memfree(_data); + _data = NULL; + _size = Vector2i(); + } + } - _FORCE_INLINE_ bool empty() const { return data_size() == 0; } - _FORCE_INLINE_ int data_size() const { return _size.x * _size.y; } - _FORCE_INLINE_ Size2i size() const { return _size; } + _FORCE_INLINE_ bool empty() const { return data_size() == 0; } + _FORCE_INLINE_ int data_size() const { return _size.x * _size.y; } + _FORCE_INLINE_ Size2i size() const { return _size; } - void resize(const Size2i &p_size); + void resize(const Size2i &p_size); - _FORCE_INLINE_ int calculate_index(int p_index_y, int p_index_x) const { - return p_index_y * _size.x + p_index_x; - } + _FORCE_INLINE_ int calculate_index(int p_index_y, int p_index_x) const { + return p_index_y * _size.x + p_index_x; + } - _FORCE_INLINE_ const real_t &operator[](int p_index) const { - CRASH_BAD_INDEX(p_index, data_size()); - return _data[p_index]; - } - _FORCE_INLINE_ real_t &operator[](int p_index) { - CRASH_BAD_INDEX(p_index, data_size()); - return _data[p_index]; - } + _FORCE_INLINE_ const real_t &operator[](int p_index) const { + CRASH_BAD_INDEX(p_index, data_size()); + return _data[p_index]; + } + _FORCE_INLINE_ real_t &operator[](int p_index) { + CRASH_BAD_INDEX(p_index, data_size()); + return _data[p_index]; + } - _FORCE_INLINE_ real_t element_get_index(int p_index) const { - ERR_FAIL_INDEX_V(p_index, data_size(), 0); + _FORCE_INLINE_ real_t element_get_index(int p_index) const { + ERR_FAIL_INDEX_V(p_index, data_size(), 0); - return _data[p_index]; - } + return _data[p_index]; + } - _FORCE_INLINE_ void element_set_index(int p_index, real_t p_val) { - ERR_FAIL_INDEX(p_index, data_size()); + _FORCE_INLINE_ void element_set_index(int p_index, real_t p_val) { + ERR_FAIL_INDEX(p_index, data_size()); - _data[p_index] = p_val; - } + _data[p_index] = p_val; + } - _FORCE_INLINE_ real_t element_get(int p_index_y, int p_index_x) const { - ERR_FAIL_INDEX_V(p_index_x, _size.x, 0); - ERR_FAIL_INDEX_V(p_index_y, _size.y, 0); + _FORCE_INLINE_ real_t element_get(int p_index_y, int p_index_x) const { + ERR_FAIL_INDEX_V(p_index_x, _size.x, 0); + ERR_FAIL_INDEX_V(p_index_y, _size.y, 0); - return _data[p_index_y * _size.x + p_index_x]; - } + return _data[p_index_y * _size.x + p_index_x]; + } - _FORCE_INLINE_ void element_set(int p_index_y, int p_index_x, real_t p_val) { - ERR_FAIL_INDEX(p_index_x, _size.x); - ERR_FAIL_INDEX(p_index_y, _size.y); + _FORCE_INLINE_ void element_set(int p_index_y, int p_index_x, real_t p_val) { + ERR_FAIL_INDEX(p_index_x, _size.x); + ERR_FAIL_INDEX(p_index_y, _size.y); - _data[p_index_y * _size.x + p_index_x] = p_val; - } + _data[p_index_y * _size.x + p_index_x] = p_val; + } - Vector row_get_vector(int p_index_y) const; - PoolRealArray row_get_pool_vector(int p_index_y) const; - Ref row_get_mlpp_vector(int p_index_y) const; - void row_get_into_mlpp_vector(int p_index_y, Ref target) const; + Vector row_get_vector(int p_index_y) const; + PoolRealArray row_get_pool_vector(int p_index_y) const; + Ref row_get_mlpp_vector(int p_index_y) const; + void row_get_into_mlpp_vector(int p_index_y, Ref target) const; - void row_set_vector(int p_index_y, const Vector &p_row); - void row_set_pool_vector(int p_index_y, const PoolRealArray &p_row); - void row_set_mlpp_vector(int p_index_y, const Ref &p_row); + void row_set_vector(int p_index_y, const Vector &p_row); + void row_set_pool_vector(int p_index_y, const PoolRealArray &p_row); + void row_set_mlpp_vector(int p_index_y, const Ref &p_row); - void fill(real_t p_val); + void fill(real_t p_val); - Vector to_flat_vector() const; - PoolRealArray to_flat_pool_vector() const; - Vector to_flat_byte_array() const; + Vector to_flat_vector() const; + PoolRealArray to_flat_pool_vector() const; + Vector to_flat_byte_array() const; - Ref duplicate_fast() const; + Ref duplicate_fast() const; - void set_from_mlpp_matrix(const Ref &p_from); - void set_from_mlpp_matrixr(const MLPPMatrix &p_from); - void set_from_mlpp_vectors(const Vector> &p_from); - void set_from_mlpp_vectors_array(const Array &p_from); - void set_from_vectors(const Vector> &p_from); - void set_from_arrays(const Array &p_from); - void set_from_ptr(const real_t *p_from, const int p_size_y, const int p_size_x); + void set_from_mlpp_matrix(const Ref &p_from); + void set_from_mlpp_matrixr(const MLPPMatrix &p_from); + void set_from_mlpp_vectors(const Vector> &p_from); + void set_from_mlpp_vectors_array(const Array &p_from); + void set_from_vectors(const Vector> &p_from); + void set_from_arrays(const Array &p_from); + void set_from_ptr(const real_t *p_from, const int p_size_y, + const int p_size_x); - //std::vector> gramMatrix(std::vector> A); - //bool linearIndependenceChecker(std::vector> A); + // std::vector> + // gramMatrix(std::vector> A); bool + // linearIndependenceChecker(std::vector> A); - Ref gaussian_noise(int n, int m) const; - void gaussian_noise_fill(); + Ref gaussian_noise(int n, int m) const; + void gaussian_noise_fill(); - static Ref create_gaussian_noise(int n, int m); + static Ref create_gaussian_noise(int n, int m); - void add(const Ref &B); - Ref addn(const Ref &B) const; - void addb(const Ref &A, const Ref &B); + void add(const Ref &B); + Ref addn(const Ref &B) const; + void addb(const Ref &A, const Ref &B); - void sub(const Ref &B); - Ref subn(const Ref &B) const; - void subb(const Ref &A, const Ref &B); + void sub(const Ref &B); + Ref subn(const Ref &B) const; + void subb(const Ref &A, const Ref &B); - void mult(const Ref &B); - Ref multn(const Ref &B) const; - void multb(const Ref &A, const Ref &B); + void mult(const Ref &B); + Ref multn(const Ref &B) const; + void multb(const Ref &A, const Ref &B); - void hadamard_product(const Ref &B); - Ref hadamard_productn(const Ref &B) const; - void hadamard_productb(const Ref &A, const Ref &B); + void hadamard_product(const Ref &B); + Ref hadamard_productn(const Ref &B) const; + void hadamard_productb(const Ref &A, const Ref &B); - void kronecker_product(const Ref &B); - Ref kronecker_productn(const Ref &B) const; - void kronecker_productb(const Ref &A, const Ref &B); + void kronecker_product(const Ref &B); + Ref kronecker_productn(const Ref &B) const; + void kronecker_productb(const Ref &A, const Ref &B); - void division_element_wise(const Ref &B); - Ref division_element_wisen(const Ref &B) const; - void division_element_wiseb(const Ref &A, const Ref &B); + void division_element_wise(const Ref &B); + Ref division_element_wisen(const Ref &B) const; + void division_element_wiseb(const Ref &A, + const Ref &B); - void transpose(); - Ref transposen() const; - void transposeb(const Ref &A); + void transpose(); + Ref transposen() const; + void transposeb(const Ref &A); - void scalar_multiply(const real_t scalar); - Ref scalar_multiplyn(const real_t scalar) const; - void scalar_multiplyb(const real_t scalar, const Ref &A); + void scalar_multiply(const real_t scalar); + Ref scalar_multiplyn(const real_t scalar) const; + void scalar_multiplyb(const real_t scalar, const Ref &A); - void scalar_add(const real_t scalar); - Ref scalar_addn(const real_t scalar) const; - void scalar_addb(const real_t scalar, const Ref &A); + void scalar_add(const real_t scalar); + Ref scalar_addn(const real_t scalar) const; + void scalar_addb(const real_t scalar, const Ref &A); - void log(); - Ref logn() const; - void logb(const Ref &A); + void log(); + Ref logn() const; + void logb(const Ref &A); - void log10(); - Ref log10n() const; - void log10b(const Ref &A); + void log10(); + Ref log10n() const; + void log10b(const Ref &A); - void exp(); - Ref expn() const; - void expb(const Ref &A); + void exp(); + Ref expn() const; + void expb(const Ref &A); - void erf(); - Ref erfn() const; - void erfb(const Ref &A); + void erf(); + Ref erfn() const; + void erfb(const Ref &A); - void exponentiate(real_t p); - Ref exponentiaten(real_t p) const; - void exponentiateb(const Ref &A, real_t p); + void exponentiate(real_t p); + Ref exponentiaten(real_t p) const; + void exponentiateb(const Ref &A, real_t p); - void sqrt(); - Ref sqrtn() const; - void sqrtb(const Ref &A); + void sqrt(); + Ref sqrtn() const; + void sqrtb(const Ref &A); - void cbrt(); - Ref cbrtn() const; - void cbrtb(const Ref &A); + void cbrt(); + Ref cbrtn() const; + void cbrtb(const Ref &A); - Ref matrix_powern(const int n) const; + Ref matrix_powern(const int n) const; - void abs(); - Ref absn() const; - void absb(const Ref &A); + void abs(); + Ref absn() const; + void absb(const Ref &A); - real_t det(int d = -1) const; - real_t detb(const Ref &A, int d) const; + real_t det(int d = -1) const; + real_t detb(const Ref &A, int d) const; - real_t trace() const; + real_t trace() const; - Ref cofactor(int n, int i, int j) const; - void cofactoro(int n, int i, int j, Ref out) const; + Ref cofactor(int n, int i, int j) const; + void cofactoro(int n, int i, int j, Ref out) const; - Ref adjoint() const; - void adjointo(Ref out) const; + Ref adjoint() const; + void adjointo(Ref out) const; - Ref inverse() const; - void inverseo(Ref out) const; + Ref inverse() const; + void inverseo(Ref out) const; - Ref pinverse() const; - void pinverseo(Ref out) const; + Ref pinverse() const; + void pinverseo(Ref out) const; - Ref matn_zero(int n, int m) const; - Ref matn_one(int n, int m) const; - Ref matn_full(int n, int m, int k) const; + Ref matn_zero(int n, int m) const; + Ref matn_one(int n, int m) const; + Ref matn_full(int n, int m, int k) const; - void sin(); - Ref sinn() const; - void sinb(const Ref &A); + void sin(); + Ref sinn() const; + void sinb(const Ref &A); - void cos(); - Ref cosn() const; - void cosb(const Ref &A); + void cos(); + Ref cosn() const; + void cosb(const Ref &A); - Ref create_rotation_matrix(real_t theta, int axis = -1); + Ref create_rotation_matrix(real_t theta, int axis = -1); - void rotate(real_t theta, int axis = -1); - Ref rotaten(real_t theta, int axis = -1); - void rotateb(const Ref &A, real_t theta, int axis = -1); + void rotate(real_t theta, int axis = -1); + Ref rotaten(real_t theta, int axis = -1); + void rotateb(const Ref &A, real_t theta, int axis = -1); - void max(const Ref &B); - Ref maxn(const Ref &B) const; - void maxb(const Ref &A, const Ref &B); + void max(const Ref &B); + Ref maxn(const Ref &B) const; + void maxb(const Ref &A, const Ref &B); - void min(const Ref &B); - Ref minn(const Ref &B) const; - void minb(const Ref &A, const Ref &B); + void min(const Ref &B); + Ref minn(const Ref &B) const; + void minb(const Ref &A, const Ref &B); - //real_t max(std::vector> A); - //real_t min(std::vector> A); + // real_t max(std::vector> A); + // real_t min(std::vector> A); - //std::vector> round(std::vector> A); + // std::vector> round(std::vector> A); - //real_t norm_2(std::vector> A); + // real_t norm_2(std::vector> A); - void identity(); - Ref identityn() const; - Ref identity_mat(int d) const; + void identity(); + Ref identityn() const; + Ref identity_mat(int d) const; - static Ref create_identity_mat(int d); + static Ref create_identity_mat(int d); - Ref cov() const; - void covo(Ref out) const; + Ref cov() const; + void covo(Ref out) const; - struct EigenResult { - Ref eigen_vectors; - Ref eigen_values; - }; + struct EigenResult { + Ref eigen_vectors; + Ref eigen_values; + }; - EigenResult eigen() const; - EigenResult eigenb(const Ref &A) const; - Array eigen_bind(); - Array eigenb_bind(const Ref &A); + EigenResult eigen() const; + EigenResult eigenb(const Ref &A) const; + Array eigen_bind(); + Array eigenb_bind(const Ref &A); - struct SVDResult { - Ref U; - Ref S; - Ref Vt; - }; + struct SVDResult { + Ref U; + Ref S; + Ref Vt; + }; - SVDResult svd() const; - SVDResult svdb(const Ref &A) const; - Array svd_bind(); - Array svdb_bind(const Ref &A); + SVDResult svd() const; + SVDResult svdb(const Ref &A) const; + Array svd_bind(); + Array svdb_bind(const Ref &A); - //std::vector vectorProjection(std::vector a, std::vector b); + // std::vector vectorProjection(std::vector a, + // std::vector b); - //std::vector> gramSchmidtProcess(std::vector> A); + // std::vector> + // gramSchmidtProcess(std::vector> A); - /* - struct QRDResult { - std::vector> Q; - std::vector> R; - }; - */ + /* + struct QRDResult { + std::vector> Q; + std::vector> R; + }; + */ - //QRDResult qrd(std::vector> A); + // QRDResult qrd(std::vector> A); - /* - struct CholeskyResult { - std::vector> L; - std::vector> Lt; - }; + /* + struct CholeskyResult { + std::vector> L; + std::vector> Lt; + }; - CholeskyResult cholesky(std::vector> A); - */ + CholeskyResult cholesky(std::vector> A); + */ - //real_t sum_elements(std::vector> A); + // real_t sum_elements(std::vector> A); - Ref flatten() const; - void flatteno(Ref out) const; + Ref flatten() const; + void flatteno(Ref out) const; - Ref solve(const Ref &b) const; + Ref solve(const Ref &b) const; - /* - bool positiveDefiniteChecker(std::vector> A); + /* + bool positiveDefiniteChecker(std::vector> A); - bool negativeDefiniteChecker(std::vector> A); + bool negativeDefiniteChecker(std::vector> A); - bool zeroEigenvalue(std::vector> A); - */ + bool zeroEigenvalue(std::vector> A); + */ - Ref mult_vec(const Ref &b) const; - void mult_veco(const Ref &b, Ref out); + Ref mult_vec(const Ref &b) const; + void mult_veco(const Ref &b, Ref out); - void add_vec(const Ref &b); - Ref add_vecn(const Ref &b) const; - void add_vecb(const Ref &A, const Ref &b); + void add_vec(const Ref &b); + Ref add_vecn(const Ref &b) const; + void add_vecb(const Ref &A, const Ref &b); - // This multiplies a, bT - void outer_product(const Ref &a, const Ref &b); - Ref outer_productn(const Ref &a, const Ref &b) const; + // This multiplies a, bT + void outer_product(const Ref &a, const Ref &b); + Ref outer_productn(const Ref &a, + const Ref &b) const; - // Just sets the diagonal - void diagonal_set(const Ref &a); - Ref diagonal_setn(const Ref &a) const; + // Just sets the diagonal + void diagonal_set(const Ref &a); + Ref diagonal_setn(const Ref &a) const; - // Sets the diagonals, everythign else will get zeroed - void diagonal_zeroed(const Ref &a); - Ref diagonal_zeroedn(const Ref &a) const; + // Sets the diagonals, everythign else will get zeroed + void diagonal_zeroed(const Ref &a); + Ref diagonal_zeroedn(const Ref &a) const; - bool is_equal_approx(const Ref &p_with, real_t tolerance = static_cast(CMP_EPSILON)) const; + bool + is_equal_approx(const Ref &p_with, + real_t tolerance = static_cast(CMP_EPSILON)) const; - Ref get_as_image() const; - void get_into_image(Ref out) const; - void set_from_image(const Ref &p_img, const int p_image_channel); + Ref get_as_image() const; + void get_into_image(Ref out) const; + void set_from_image(const Ref &p_img, const int p_image_channel); - String to_string(); + String to_string(); - MLPPMatrix(); - MLPPMatrix(const MLPPMatrix &p_from); - MLPPMatrix(const Vector> &p_from); - MLPPMatrix(const Array &p_from); - MLPPMatrix(const real_t *p_from, const int p_size_y, const int p_size_x); + MLPPMatrix(); + MLPPMatrix(const MLPPMatrix &p_from); + MLPPMatrix(const Vector> &p_from); + MLPPMatrix(const Array &p_from); + MLPPMatrix(const real_t *p_from, const int p_size_y, const int p_size_x); - ~MLPPMatrix(); + ~MLPPMatrix(); - // TODO: These are temporary - std::vector to_flat_std_vector() const; - void set_from_std_vectors(const std::vector> &p_from); - std::vector> to_std_vector(); - void set_row_std_vector(int p_index_y, const std::vector &p_row); - MLPPMatrix(const std::vector> &p_from); + // TODO: These are temporary + std::vector to_flat_std_vector() const; + void set_from_std_vectors(const std::vector> &p_from); + std::vector> to_std_vector(); + void set_row_std_vector(int p_index_y, const std::vector &p_row); + MLPPMatrix(const std::vector> &p_from); protected: - static void _bind_methods(); + static void _bind_methods(); protected: - Size2i _size; - real_t *_data; + Size2i _size; + real_t *_data; }; #endif diff --git a/lin_alg/mlpp_tensor3.cpp b/lin_alg/mlpp_tensor3.cpp index 47cfb45..87a6e71 100644 --- a/lin_alg/mlpp_tensor3.cpp +++ b/lin_alg/mlpp_tensor3.cpp @@ -30,2430 +30,2571 @@ #include "mlpp_tensor3.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/io/image.h" +#endif Array MLPPTensor3::get_data() { - PoolRealArray pl; + PoolRealArray pl; - int ds = data_size(); + int ds = data_size(); - if (ds) { - pl.resize(ds); - PoolRealArray::Write w = pl.write(); - real_t *dest = w.ptr(); + if (ds) { + pl.resize(ds); + PoolRealArray::Write w = pl.write(); + real_t *dest = w.ptr(); - for (int i = 0; i < ds; ++i) { - dest[i] = _data[i]; - } - } + for (int i = 0; i < ds; ++i) { + dest[i] = _data[i]; + } + } - Array arr; - arr.push_back(size()); - arr.push_back(pl); + Array arr; + arr.push_back(size()); + arr.push_back(pl); - return arr; + return arr; } void MLPPTensor3::set_data(const Array &p_from) { - if (p_from.size() != 2) { - return; - } + if (p_from.size() != 2) { + return; + } - Size3i s = p_from[0]; - PoolRealArray pl = p_from[1]; + Size3i s = p_from[0]; + PoolRealArray pl = p_from[1]; - int ds = s.x * s.y * s.z; + int ds = s.x * s.y * s.z; - if (ds != pl.size()) { - return; - } + if (ds != pl.size()) { + return; + } - if (_size != s) { - resize(s); - } + if (_size != s) { + resize(s); + } - PoolRealArray::Read r = pl.read(); - for (int i = 0; i < ds; ++i) { - _data[i] = r[i]; - } + PoolRealArray::Read r = pl.read(); + for (int i = 0; i < ds; ++i) { + _data[i] = r[i]; + } } void MLPPTensor3::z_slice_add(const Vector &p_row) { - if (p_row.size() == 0) { - return; - } + if (p_row.size() == 0) { + return; + } - int fms = z_slice_data_size(); + int fms = z_slice_data_size(); - ERR_FAIL_COND(fms != p_row.size()); + ERR_FAIL_COND(fms != p_row.size()); - int ci = data_size(); + int ci = data_size(); - ++_size.z; + ++_size.z; - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); - const real_t *row_arr = p_row.ptr(); + const real_t *row_arr = p_row.ptr(); - for (int i = 0; i < p_row.size(); ++i) { - _data[ci + i] = row_arr[i]; - } + for (int i = 0; i < p_row.size(); ++i) { + _data[ci + i] = row_arr[i]; + } } void MLPPTensor3::z_slice_add_pool_vector(const PoolRealArray &p_row) { - if (p_row.size() == 0) { - return; - } + if (p_row.size() == 0) { + return; + } - int fms = z_slice_data_size(); + int fms = z_slice_data_size(); - ERR_FAIL_COND(fms != p_row.size()); + ERR_FAIL_COND(fms != p_row.size()); - int ci = data_size(); + int ci = data_size(); - ++_size.z; + ++_size.z; - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); - PoolRealArray::Read rread = p_row.read(); - const real_t *row_arr = rread.ptr(); + PoolRealArray::Read rread = p_row.read(); + const real_t *row_arr = rread.ptr(); - for (int i = 0; i < p_row.size(); ++i) { - _data[ci + i] = row_arr[i]; - } + for (int i = 0; i < p_row.size(); ++i) { + _data[ci + i] = row_arr[i]; + } } void MLPPTensor3::z_slice_add_mlpp_vector(const Ref &p_row) { - ERR_FAIL_COND(!p_row.is_valid()); + ERR_FAIL_COND(!p_row.is_valid()); - int p_row_size = p_row->size(); + int p_row_size = p_row->size(); - if (p_row_size == 0) { - return; - } + if (p_row_size == 0) { + return; + } - int fms = z_slice_data_size(); + int fms = z_slice_data_size(); - ERR_FAIL_COND(fms != p_row_size); + ERR_FAIL_COND(fms != p_row_size); - int ci = data_size(); + int ci = data_size(); - ++_size.z; + ++_size.z; - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); - const real_t *row_ptr = p_row->ptr(); + const real_t *row_ptr = p_row->ptr(); - for (int i = 0; i < p_row_size; ++i) { - _data[ci + i] = row_ptr[i]; - } + for (int i = 0; i < p_row_size; ++i) { + _data[ci + i] = row_ptr[i]; + } } void MLPPTensor3::z_slice_add_mlpp_matrix(const Ref &p_matrix) { - ERR_FAIL_COND(!p_matrix.is_valid()); + ERR_FAIL_COND(!p_matrix.is_valid()); - int other_data_size = p_matrix->data_size(); + int other_data_size = p_matrix->data_size(); - if (other_data_size == 0) { - return; - } + if (other_data_size == 0) { + return; + } - Size2i matrix_size = p_matrix->size(); - Size2i fms = z_slice_size(); + Size2i matrix_size = p_matrix->size(); + Size2i fms = z_slice_size(); - ERR_FAIL_COND(fms != matrix_size); + ERR_FAIL_COND(fms != matrix_size); - int start_offset = data_size(); + int start_offset = data_size(); - ++_size.z; + ++_size.z; - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); - const real_t *other_ptr = p_matrix->ptr(); + const real_t *other_ptr = p_matrix->ptr(); - for (int i = 0; i < other_data_size; ++i) { - _data[start_offset + i] = other_ptr[i]; - } + for (int i = 0; i < other_data_size; ++i) { + _data[start_offset + i] = other_ptr[i]; + } } void MLPPTensor3::z_slice_remove(int p_index) { - ERR_FAIL_INDEX(p_index, _size.z); + ERR_FAIL_INDEX(p_index, _size.z); - --_size.z; + --_size.z; - int ds = data_size(); + int ds = data_size(); - if (ds == 0) { - memfree(_data); - _data = NULL; - return; - } + if (ds == 0) { + memfree(_data); + _data = NULL; + return; + } - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - for (int i = calculate_z_slice_index(p_index); i < ds; ++i) { - _data[i] = _data[i + fmds]; - } + for (int i = calculate_z_slice_index(p_index); i < ds; ++i) { + _data[i] = _data[i + fmds]; + } - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); } // Removes the item copying the last value into the position of the one to // remove. It's generally faster than `remove`. void MLPPTensor3::z_slice_remove_unordered(int p_index) { - ERR_FAIL_INDEX(p_index, _size.z); + ERR_FAIL_INDEX(p_index, _size.z); - --_size.z; + --_size.z; - int ds = data_size(); + int ds = data_size(); - if (ds == 0) { - memfree(_data); - _data = NULL; - return; - } + if (ds == 0) { + memfree(_data); + _data = NULL; + return; + } - int start_ind = calculate_z_slice_index(p_index); - int end_ind = calculate_z_slice_index(p_index + 1); + int start_ind = calculate_z_slice_index(p_index); + int end_ind = calculate_z_slice_index(p_index + 1); - for (int i = start_ind; i < end_ind; ++i) { - _data[i] = _data[ds + i]; - } + for (int i = start_ind; i < end_ind; ++i) { + _data[i] = _data[ds + i]; + } - _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, data_size() * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); } void MLPPTensor3::z_slice_swap(int p_index_1, int p_index_2) { - ERR_FAIL_INDEX(p_index_1, _size.z); - ERR_FAIL_INDEX(p_index_2, _size.z); + ERR_FAIL_INDEX(p_index_1, _size.z); + ERR_FAIL_INDEX(p_index_2, _size.z); - int ind1_start = calculate_z_slice_index(p_index_1); - int ind2_start = calculate_z_slice_index(p_index_2); + int ind1_start = calculate_z_slice_index(p_index_1); + int ind2_start = calculate_z_slice_index(p_index_2); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - for (int i = 0; i < fmds; ++i) { - SWAP(_data[ind1_start + i], _data[ind2_start + i]); - } + for (int i = 0; i < fmds; ++i) { + SWAP(_data[ind1_start + i], _data[ind2_start + i]); + } } void MLPPTensor3::resize(const Size3i &p_size) { - _size = p_size; + _size = p_size; - int ds = data_size(); + int ds = data_size(); - if (ds == 0) { - if (_data) { - memfree(_data); - _data = NULL; - } + if (ds == 0) { + if (_data) { + memfree(_data); + _data = NULL; + } - return; - } + return; + } - _data = (real_t *)memrealloc(_data, ds * sizeof(real_t)); - CRASH_COND_MSG(!_data, "Out of memory"); + _data = (real_t *)memrealloc(_data, ds * sizeof(real_t)); + CRASH_COND_MSG(!_data, "Out of memory"); } void MLPPTensor3::shape_set(const Size3i &p_size) { - int ds = data_size(); - int new_data_size = p_size.x * p_size.y * p_size.z; + int ds = data_size(); + int new_data_size = p_size.x * p_size.y * p_size.z; - ERR_FAIL_COND_MSG(ds != new_data_size, "The new size has a different volume than the old. If this is intended use resize()!"); + ERR_FAIL_COND_MSG(ds != new_data_size, + "The new size has a different volume than the old. If this " + "is intended use resize()!"); - _size = p_size; + _size = p_size; } Vector MLPPTensor3::row_get_vector(int p_index_z, int p_index_y) const { - ERR_FAIL_INDEX_V(p_index_y, _size.y, Vector()); - ERR_FAIL_INDEX_V(p_index_z, _size.z, Vector()); + ERR_FAIL_INDEX_V(p_index_y, _size.y, Vector()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Vector()); - Vector ret; + Vector ret; - if (unlikely(_size.x == 0)) { - return ret; - } + if (unlikely(_size.x == 0)) { + return ret; + } - ret.resize(_size.x); + ret.resize(_size.x); - int ind_start = p_index_y * _size.x; + int ind_start = p_index_y * _size.x; - real_t *row_ptr = ret.ptrw(); + real_t *row_ptr = ret.ptrw(); - for (int i = 0; i < _size.x; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < _size.x; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } -PoolRealArray MLPPTensor3::row_get_pool_vector(int p_index_z, int p_index_y) const { - ERR_FAIL_INDEX_V(p_index_y, _size.y, PoolRealArray()); - ERR_FAIL_INDEX_V(p_index_z, _size.z, PoolRealArray()); +PoolRealArray MLPPTensor3::row_get_pool_vector(int p_index_z, + int p_index_y) const { + ERR_FAIL_INDEX_V(p_index_y, _size.y, PoolRealArray()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, PoolRealArray()); - PoolRealArray ret; + PoolRealArray ret; - if (unlikely(_size.x == 0)) { - return ret; - } + if (unlikely(_size.x == 0)) { + return ret; + } - ret.resize(_size.x); + ret.resize(_size.x); - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - PoolRealArray::Write w = ret.write(); - real_t *row_ptr = w.ptr(); + PoolRealArray::Write w = ret.write(); + real_t *row_ptr = w.ptr(); - for (int i = 0; i < _size.x; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < _size.x; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } -Ref MLPPTensor3::row_get_mlpp_vector(int p_index_z, int p_index_y) const { - ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); - ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); +Ref MLPPTensor3::row_get_mlpp_vector(int p_index_z, + int p_index_y) const { + ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); - Ref ret; - ret.instance(); + Ref ret; + ret.instance(); - if (unlikely(_size.x == 0)) { - return ret; - } + if (unlikely(_size.x == 0)) { + return ret; + } - ret->resize(_size.x); + ret->resize(_size.x); - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - real_t *row_ptr = ret->ptrw(); + real_t *row_ptr = ret->ptrw(); - for (int i = 0; i < _size.x; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < _size.x; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } -void MLPPTensor3::row_get_into_mlpp_vector(int p_index_z, int p_index_y, Ref target) const { - ERR_FAIL_COND(!target.is_valid()); - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::row_get_into_mlpp_vector(int p_index_z, int p_index_y, + Ref target) const { + ERR_FAIL_COND(!target.is_valid()); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_INDEX(p_index_z, _size.z); - if (unlikely(target->size() != _size.x)) { - target->resize(_size.x); - } + if (unlikely(target->size() != _size.x)) { + target->resize(_size.x); + } - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - real_t *row_ptr = target->ptrw(); + real_t *row_ptr = target->ptrw(); - for (int i = 0; i < _size.x; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < _size.x; ++i) { + row_ptr[i] = _data[ind_start + i]; + } } -void MLPPTensor3::row_set_vector(int p_index_z, int p_index_y, const Vector &p_row) { - ERR_FAIL_COND(p_row.size() != _size.x); - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::row_set_vector(int p_index_z, int p_index_y, + const Vector &p_row) { + ERR_FAIL_COND(p_row.size() != _size.x); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_INDEX(p_index_z, _size.z); - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - const real_t *row_ptr = p_row.ptr(); + const real_t *row_ptr = p_row.ptr(); - for (int i = 0; i < _size.x; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < _size.x; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::row_set_pool_vector(int p_index_z, int p_index_y, const PoolRealArray &p_row) { - ERR_FAIL_COND(p_row.size() != _size.x); - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::row_set_pool_vector(int p_index_z, int p_index_y, + const PoolRealArray &p_row) { + ERR_FAIL_COND(p_row.size() != _size.x); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_INDEX(p_index_z, _size.z); - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - PoolRealArray::Read r = p_row.read(); - const real_t *row_ptr = r.ptr(); + PoolRealArray::Read r = p_row.read(); + const real_t *row_ptr = r.ptr(); - for (int i = 0; i < _size.x; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < _size.x; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::row_set_mlpp_vector(int p_index_z, int p_index_y, const Ref &p_row) { - ERR_FAIL_COND(!p_row.is_valid()); - ERR_FAIL_COND(p_row->size() != _size.x); - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::row_set_mlpp_vector(int p_index_z, int p_index_y, + const Ref &p_row) { + ERR_FAIL_COND(!p_row.is_valid()); + ERR_FAIL_COND(p_row->size() != _size.x); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_INDEX(p_index_z, _size.z); - int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; + int ind_start = p_index_y * _size.x + _size.x * _size.y * p_index_z; - const real_t *row_ptr = p_row->ptr(); + const real_t *row_ptr = p_row->ptr(); - for (int i = 0; i < _size.x; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < _size.x; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } Vector MLPPTensor3::z_slice_get_vector(int p_index_z) const { - ERR_FAIL_INDEX_V(p_index_z, _size.z, Vector()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Vector()); - Vector ret; + Vector ret; - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - if (unlikely(fmds == 0)) { - return ret; - } + if (unlikely(fmds == 0)) { + return ret; + } - ret.resize(fmds); + ret.resize(fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - real_t *row_ptr = ret.ptrw(); + real_t *row_ptr = ret.ptrw(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } PoolRealArray MLPPTensor3::z_slice_get_pool_vector(int p_index_z) const { - ERR_FAIL_INDEX_V(p_index_z, _size.z, PoolRealArray()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, PoolRealArray()); - PoolRealArray ret; + PoolRealArray ret; - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - if (unlikely(fmds == 0)) { - return ret; - } + if (unlikely(fmds == 0)) { + return ret; + } - ret.resize(fmds); + ret.resize(fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - PoolRealArray::Write w = ret.write(); - real_t *row_ptr = w.ptr(); + PoolRealArray::Write w = ret.write(); + real_t *row_ptr = w.ptr(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } Ref MLPPTensor3::z_slice_get_mlpp_vector(int p_index_z) const { - ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); - Ref ret; - ret.instance(); + Ref ret; + ret.instance(); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - if (unlikely(fmds == 0)) { - return ret; - } + if (unlikely(fmds == 0)) { + return ret; + } - ret->resize(fmds); + ret->resize(fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - real_t *row_ptr = ret->ptrw(); + real_t *row_ptr = ret->ptrw(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } -void MLPPTensor3::z_slice_get_into_mlpp_vector(int p_index_z, Ref target) const { - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::z_slice_get_into_mlpp_vector(int p_index_z, + Ref target) const { + ERR_FAIL_INDEX(p_index_z, _size.z); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - if (unlikely(target->size() != fmds)) { - target->resize(fmds); - } + if (unlikely(target->size() != fmds)) { + target->resize(fmds); + } - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - real_t *row_ptr = target->ptrw(); + real_t *row_ptr = target->ptrw(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } } Ref MLPPTensor3::z_slice_get_mlpp_matrix(int p_index_z) const { - ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); - Ref ret; - ret.instance(); + Ref ret; + ret.instance(); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - if (unlikely(fmds == 0)) { - return ret; - } + if (unlikely(fmds == 0)) { + return ret; + } - ret->resize(z_slice_size()); + ret->resize(z_slice_size()); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - real_t *row_ptr = ret->ptrw(); + real_t *row_ptr = ret->ptrw(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } - return ret; + return ret; } -void MLPPTensor3::z_slice_get_into_mlpp_matrix(int p_index_z, Ref target) const { - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::z_slice_get_into_mlpp_matrix(int p_index_z, + Ref target) const { + ERR_FAIL_INDEX(p_index_z, _size.z); - int fmds = z_slice_data_size(); - Size2i fms = z_slice_size(); + int fmds = z_slice_data_size(); + Size2i fms = z_slice_size(); - if (unlikely(target->size() != fms)) { - target->resize(fms); - } + if (unlikely(target->size() != fms)) { + target->resize(fms); + } - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - real_t *row_ptr = target->ptrw(); + real_t *row_ptr = target->ptrw(); - for (int i = 0; i < fmds; ++i) { - row_ptr[i] = _data[ind_start + i]; - } + for (int i = 0; i < fmds; ++i) { + row_ptr[i] = _data[ind_start + i]; + } } -void MLPPTensor3::z_slice_set_vector(int p_index_z, const Vector &p_row) { - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::z_slice_set_vector(int p_index_z, + const Vector &p_row) { + ERR_FAIL_INDEX(p_index_z, _size.z); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - ERR_FAIL_COND(p_row.size() != fmds); + ERR_FAIL_COND(p_row.size() != fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - const real_t *row_ptr = p_row.ptr(); + const real_t *row_ptr = p_row.ptr(); - for (int i = 0; i < fmds; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < fmds; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::z_slice_set_pool_vector(int p_index_z, const PoolRealArray &p_row) { - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::z_slice_set_pool_vector(int p_index_z, + const PoolRealArray &p_row) { + ERR_FAIL_INDEX(p_index_z, _size.z); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - ERR_FAIL_COND(p_row.size() != fmds); + ERR_FAIL_COND(p_row.size() != fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - PoolRealArray::Read r = p_row.read(); - const real_t *row_ptr = r.ptr(); + PoolRealArray::Read r = p_row.read(); + const real_t *row_ptr = r.ptr(); - for (int i = 0; i < fmds; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < fmds; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::z_slice_set_mlpp_vector(int p_index_z, const Ref &p_row) { - ERR_FAIL_INDEX(p_index_z, _size.z); - ERR_FAIL_COND(!p_row.is_valid()); +void MLPPTensor3::z_slice_set_mlpp_vector(int p_index_z, + const Ref &p_row) { + ERR_FAIL_INDEX(p_index_z, _size.z); + ERR_FAIL_COND(!p_row.is_valid()); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - ERR_FAIL_COND(p_row->size() != fmds); + ERR_FAIL_COND(p_row->size() != fmds); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - const real_t *row_ptr = p_row->ptr(); + const real_t *row_ptr = p_row->ptr(); - for (int i = 0; i < fmds; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < fmds; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::z_slice_set_mlpp_matrix(int p_index_z, const Ref &p_mat) { - ERR_FAIL_INDEX(p_index_z, _size.z); - ERR_FAIL_COND(!p_mat.is_valid()); +void MLPPTensor3::z_slice_set_mlpp_matrix(int p_index_z, + const Ref &p_mat) { + ERR_FAIL_INDEX(p_index_z, _size.z); + ERR_FAIL_COND(!p_mat.is_valid()); - int fmds = z_slice_data_size(); + int fmds = z_slice_data_size(); - ERR_FAIL_COND(p_mat->size() != z_slice_size()); + ERR_FAIL_COND(p_mat->size() != z_slice_size()); - int ind_start = calculate_z_slice_index(p_index_z); + int ind_start = calculate_z_slice_index(p_index_z); - const real_t *row_ptr = p_mat->ptr(); + const real_t *row_ptr = p_mat->ptr(); - for (int i = 0; i < fmds; ++i) { - _data[ind_start + i] = row_ptr[i]; - } + for (int i = 0; i < fmds; ++i) { + _data[ind_start + i] = row_ptr[i]; + } } -void MLPPTensor3::x_slice_get_into(int p_index_x, Ref target) const { - ERR_FAIL_INDEX(p_index_x, _size.x); - ERR_FAIL_COND(!target.is_valid()); +void MLPPTensor3::x_slice_get_into(int p_index_x, + Ref target) const { + ERR_FAIL_INDEX(p_index_x, _size.x); + ERR_FAIL_COND(!target.is_valid()); - if (unlikely(target->size() != Size2i(_size.y, _size.z))) { - target->resize(Size2i(_size.y, _size.z)); - } + if (unlikely(target->size() != Size2i(_size.y, _size.z))) { + target->resize(Size2i(_size.y, _size.z)); + } - for (int z = 0; z < _size.z; ++z) { - for (int y = 0; y < _size.y; ++y) { - target->element_set(z, y, element_get(z, p_index_x, y)); - } - } + for (int z = 0; z < _size.z; ++z) { + for (int y = 0; y < _size.y; ++y) { + target->element_set(z, y, element_get(z, p_index_x, y)); + } + } } Ref MLPPTensor3::x_slice_get(int p_index_x) const { - ERR_FAIL_INDEX_V(p_index_x, _size.x, Ref()); + ERR_FAIL_INDEX_V(p_index_x, _size.x, Ref()); - Ref m; - m.instance(); + Ref m; + m.instance(); - x_slice_get_into(p_index_x, m); + x_slice_get_into(p_index_x, m); - return m; + return m; } void MLPPTensor3::x_slice_set(int p_index_x, const Ref &p_mat) { - ERR_FAIL_INDEX(p_index_x, _size.x); - ERR_FAIL_COND(!p_mat.is_valid()); - ERR_FAIL_COND(p_mat->size() != Size2i(_size.y, _size.z)); + ERR_FAIL_INDEX(p_index_x, _size.x); + ERR_FAIL_COND(!p_mat.is_valid()); + ERR_FAIL_COND(p_mat->size() != Size2i(_size.y, _size.z)); - for (int z = 0; z < _size.z; ++z) { - for (int y = 0; y < _size.y; ++y) { - element_set(z, p_index_x, y, p_mat->element_get(z, y)); - } - } + for (int z = 0; z < _size.z; ++z) { + for (int y = 0; y < _size.y; ++y) { + element_set(z, p_index_x, y, p_mat->element_get(z, y)); + } + } } -void MLPPTensor3::y_slice_get_into(int p_index_y, Ref target) const { - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_COND(!target.is_valid()); +void MLPPTensor3::y_slice_get_into(int p_index_y, + Ref target) const { + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_COND(!target.is_valid()); - if (unlikely(target->size() != Size2i(_size.y, _size.z))) { - target->resize(Size2i(_size.x, _size.z)); - } + if (unlikely(target->size() != Size2i(_size.y, _size.z))) { + target->resize(Size2i(_size.x, _size.z)); + } - for (int z = 0; z < _size.z; ++z) { - for (int x = 0; x < _size.x; ++x) { - target->element_set(z, x, element_get(z, x, p_index_y)); - } - } + for (int z = 0; z < _size.z; ++z) { + for (int x = 0; x < _size.x; ++x) { + target->element_set(z, x, element_get(z, x, p_index_y)); + } + } } Ref MLPPTensor3::y_slice_get(int p_index_y) const { - ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); + ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); - Ref m; - m.instance(); + Ref m; + m.instance(); - y_slice_get_into(p_index_y, m); + y_slice_get_into(p_index_y, m); - return m; + return m; } void MLPPTensor3::y_slice_set(int p_index_y, const Ref &p_mat) { - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_COND(!p_mat.is_valid()); - ERR_FAIL_COND(p_mat->size() != Size2i(_size.y, _size.z)); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_COND(!p_mat.is_valid()); + ERR_FAIL_COND(p_mat->size() != Size2i(_size.y, _size.z)); - for (int z = 0; z < _size.z; ++z) { - for (int x = 0; x < _size.x; ++x) { - element_set(z, x, p_index_y, p_mat->element_get(z, x)); - } - } + for (int z = 0; z < _size.z; ++z) { + for (int x = 0; x < _size.x; ++x) { + element_set(z, x, p_index_y, p_mat->element_get(z, x)); + } + } } -void MLPPTensor3::z_slices_add_image(const Ref &p_img, const int p_channels) { - ERR_FAIL_COND(!p_img.is_valid()); +void MLPPTensor3::z_slices_add_image(const Ref &p_img, + const int p_channels) { + ERR_FAIL_COND(!p_img.is_valid()); - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - int channel_count = 0; - int channels[4]; + int channel_count = 0; + int channels[4]; - if (p_channels & IMAGE_CHANNEL_FLAG_R) { - channels[channel_count] = 0; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_R) { + channels[channel_count] = 0; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_G) { - channels[channel_count] = 1; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_G) { + channels[channel_count] = 1; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_B) { - channels[channel_count] = 2; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_B) { + channels[channel_count] = 2; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_A) { - channels[channel_count] = 3; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_A) { + channels[channel_count] = 3; + ++channel_count; + } - ERR_FAIL_COND(channel_count == 0); + ERR_FAIL_COND(channel_count == 0); - if (unlikely(_size == Size3i())) { - resize(Size3i(img_size.x, img_size.y, channel_count)); - } + if (unlikely(_size == Size3i())) { + resize(Size3i(img_size.x, img_size.y, channel_count)); + } - Size2i fms = z_slice_size(); + Size2i fms = z_slice_size(); - ERR_FAIL_COND(img_size != fms); + ERR_FAIL_COND(img_size != fms); - int start_channel = _size.y; + int start_channel = _size.y; - _size.y += channel_count; + _size.y += channel_count; - resize(_size); + resize(_size); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c = img->get_pixel(x, y); + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, y); - for (int i = 0; i < channel_count; ++i) { - element_set(start_channel + i, y, x, c[channels[i]]); - } - } - } + for (int i = 0; i < channel_count; ++i) { + element_set(start_channel + i, y, x, c[channels[i]]); + } + } + } - img->unlock(); + img->unlock(); } Ref MLPPTensor3::z_slice_get_image(const int p_index_z) const { - ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); + ERR_FAIL_INDEX_V(p_index_z, _size.z, Ref()); - Ref image; - image.instance(); + Ref image; + image.instance(); - if (data_size() == 0) { - return image; - } + if (data_size() == 0) { + return image; + } - PoolByteArray arr; + PoolByteArray arr; - int fmsi = calculate_z_slice_index(p_index_z); - int fms = z_slice_data_size(); + int fmsi = calculate_z_slice_index(p_index_z); + int fms = z_slice_data_size(); - arr.resize(fms); + arr.resize(fms); - PoolByteArray::Write w = arr.write(); - uint8_t *wptr = w.ptr(); + PoolByteArray::Write w = arr.write(); + uint8_t *wptr = w.ptr(); - for (int i = 0; i < fms; ++i) { - wptr[i] = static_cast(_data[fmsi + i] * 255.0); - } + for (int i = 0; i < fms; ++i) { + wptr[i] = static_cast(_data[fmsi + i] * 255.0); + } - image->create(_size.x, _size.y, false, Image::FORMAT_L8, arr); + image->create(_size.x, _size.y, false, Image::FORMAT_L8, arr); - return image; + return image; } -Ref MLPPTensor3::z_slices_get_image(const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) const { - if (p_index_r != -1) { - ERR_FAIL_INDEX_V(p_index_r, _size.z, Ref()); - } +Ref MLPPTensor3::z_slices_get_image(const int p_index_r, + const int p_index_g, + const int p_index_b, + const int p_index_a) const { + if (p_index_r != -1) { + ERR_FAIL_INDEX_V(p_index_r, _size.z, Ref()); + } - if (p_index_g != -1) { - ERR_FAIL_INDEX_V(p_index_g, _size.z, Ref()); - } + if (p_index_g != -1) { + ERR_FAIL_INDEX_V(p_index_g, _size.z, Ref()); + } - if (p_index_b != -1) { - ERR_FAIL_INDEX_V(p_index_b, _size.z, Ref()); - } + if (p_index_b != -1) { + ERR_FAIL_INDEX_V(p_index_b, _size.z, Ref()); + } - if (p_index_a != -1) { - ERR_FAIL_INDEX_V(p_index_a, _size.z, Ref()); - } + if (p_index_a != -1) { + ERR_FAIL_INDEX_V(p_index_a, _size.z, Ref()); + } - Ref image; - image.instance(); + Ref image; + image.instance(); - if (data_size() == 0) { - return image; - } + if (data_size() == 0) { + return image; + } - Size2i fms = z_slice_size(); + Size2i fms = z_slice_size(); - image->create(_size.x, _size.y, false, Image::FORMAT_RGBA8); + image->create(_size.x, _size.y, false, Image::FORMAT_RGBA8); - image->lock(); + image->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c; + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c; - if (p_index_r != -1) { - c.r = element_get(p_index_r, y, x); - } + if (p_index_r != -1) { + c.r = element_get(p_index_r, y, x); + } - if (p_index_g != -1) { - c.g = element_get(p_index_g, y, x); - } + if (p_index_g != -1) { + c.g = element_get(p_index_g, y, x); + } - if (p_index_b != -1) { - c.b = element_get(p_index_b, y, x); - } + if (p_index_b != -1) { + c.b = element_get(p_index_b, y, x); + } - if (p_index_a != -1) { - c.a = element_get(p_index_a, y, x); - } + if (p_index_a != -1) { + c.a = element_get(p_index_a, y, x); + } - image->set_pixel(x, y, c); - } - } + image->set_pixel(x, y, c); + } + } - image->unlock(); + image->unlock(); - return image; + return image; } -void MLPPTensor3::z_slice_get_into_image(Ref p_target, const int p_index_z, const int p_target_channels) const { - ERR_FAIL_INDEX(p_index_z, _size.z); - ERR_FAIL_COND(!p_target.is_valid()); +void MLPPTensor3::z_slice_get_into_image(Ref p_target, + const int p_index_z, + const int p_target_channels) const { + ERR_FAIL_INDEX(p_index_z, _size.z); + ERR_FAIL_COND(!p_target.is_valid()); - int channel_count = 0; - int channels[4]; + int channel_count = 0; + int channels[4]; - if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { - channels[channel_count] = 0; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { + channels[channel_count] = 0; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { - channels[channel_count] = 1; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { + channels[channel_count] = 1; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { - channels[channel_count] = 2; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { + channels[channel_count] = 2; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { - channels[channel_count] = 3; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { + channels[channel_count] = 3; + ++channel_count; + } - ERR_FAIL_COND(channel_count == 0); + ERR_FAIL_COND(channel_count == 0); - if (data_size() == 0) { - p_target->clear(); - return; - } + if (data_size() == 0) { + p_target->clear(); + return; + } - Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); - Size2i fms = z_slice_size(); - if (img_size != fms) { - bool mip_maps = p_target->has_mipmaps(); - p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); + Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); + Size2i fms = z_slice_size(); + if (img_size != fms) { + bool mip_maps = p_target->has_mipmaps(); + p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); - if (p_target->has_mipmaps() != mip_maps) { - if (mip_maps) { - p_target->generate_mipmaps(); - } else { - p_target->clear_mipmaps(); - } - } - } + if (p_target->has_mipmaps() != mip_maps) { + if (mip_maps) { + p_target->generate_mipmaps(); + } else { + p_target->clear_mipmaps(); + } + } + } - p_target->lock(); + p_target->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c; + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c; - float e = element_get(p_index_z, y, x); + float e = element_get(p_index_z, y, x); - for (int i = 0; i < channel_count; ++i) { - c[channels[i]] = e; - } + for (int i = 0; i < channel_count; ++i) { + c[channels[i]] = e; + } - p_target->set_pixel(x, y, c); - } - } + p_target->set_pixel(x, y, c); + } + } - p_target->unlock(); + p_target->unlock(); } -void MLPPTensor3::z_slices_get_into_image(Ref p_target, const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) const { - ERR_FAIL_COND(!p_target.is_valid()); +void MLPPTensor3::z_slices_get_into_image(Ref p_target, + const int p_index_r, + const int p_index_g, + const int p_index_b, + const int p_index_a) const { + ERR_FAIL_COND(!p_target.is_valid()); - if (p_index_r != -1) { - ERR_FAIL_INDEX(p_index_r, _size.z); - } + if (p_index_r != -1) { + ERR_FAIL_INDEX(p_index_r, _size.z); + } - if (p_index_g != -1) { - ERR_FAIL_INDEX(p_index_g, _size.z); - } + if (p_index_g != -1) { + ERR_FAIL_INDEX(p_index_g, _size.z); + } - if (p_index_b != -1) { - ERR_FAIL_INDEX(p_index_b, _size.z); - } + if (p_index_b != -1) { + ERR_FAIL_INDEX(p_index_b, _size.z); + } - if (p_index_a != -1) { - ERR_FAIL_INDEX(p_index_a, _size.z); - } + if (p_index_a != -1) { + ERR_FAIL_INDEX(p_index_a, _size.z); + } - if (data_size() == 0) { - p_target->clear(); - return; - } + if (data_size() == 0) { + p_target->clear(); + return; + } - Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); - Size2i fms = z_slice_size(); - if (img_size != fms) { - bool mip_maps = p_target->has_mipmaps(); - p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); + Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); + Size2i fms = z_slice_size(); + if (img_size != fms) { + bool mip_maps = p_target->has_mipmaps(); + p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); - if (p_target->has_mipmaps() != mip_maps) { - if (mip_maps) { - p_target->generate_mipmaps(); - } else { - p_target->clear_mipmaps(); - } - } - } + if (p_target->has_mipmaps() != mip_maps) { + if (mip_maps) { + p_target->generate_mipmaps(); + } else { + p_target->clear_mipmaps(); + } + } + } - p_target->lock(); + p_target->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c; + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c; - if (p_index_r != -1) { - c.r = element_get(p_index_r, y, x); - } + if (p_index_r != -1) { + c.r = element_get(p_index_r, y, x); + } - if (p_index_g != -1) { - c.g = element_get(p_index_g, y, x); - } + if (p_index_g != -1) { + c.g = element_get(p_index_g, y, x); + } - if (p_index_b != -1) { - c.b = element_get(p_index_b, y, x); - } + if (p_index_b != -1) { + c.b = element_get(p_index_b, y, x); + } - if (p_index_a != -1) { - c.a = element_get(p_index_a, y, x); - } + if (p_index_a != -1) { + c.a = element_get(p_index_a, y, x); + } - p_target->set_pixel(x, y, c); - } - } + p_target->set_pixel(x, y, c); + } + } - p_target->unlock(); + p_target->unlock(); } -void MLPPTensor3::z_slice_set_image(const Ref &p_img, const int p_index_z, const int p_image_channel_flag) { - ERR_FAIL_COND(!p_img.is_valid()); - ERR_FAIL_INDEX(p_index_z, _size.z); +void MLPPTensor3::z_slice_set_image(const Ref &p_img, + const int p_index_z, + const int p_image_channel_flag) { + ERR_FAIL_COND(!p_img.is_valid()); + ERR_FAIL_INDEX(p_index_z, _size.z); - int channel_index = -1; + int channel_index = -1; - for (int i = 0; i < 4; ++i) { - if (((p_image_channel_flag & (1 << i)) != 0)) { - channel_index = i; - break; - } - } + for (int i = 0; i < 4; ++i) { + if (((p_image_channel_flag & (1 << i)) != 0)) { + channel_index = i; + break; + } + } - ERR_FAIL_INDEX(channel_index, 4); + ERR_FAIL_INDEX(channel_index, 4); - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - Size2i fms = z_slice_size(); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i fms = z_slice_size(); - ERR_FAIL_COND(img_size != fms); + ERR_FAIL_COND(img_size != fms); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c = img->get_pixel(x, y); + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, y); - element_set(p_index_z, y, x, c[channel_index]); - } - } + element_set(p_index_z, y, x, c[channel_index]); + } + } - img->unlock(); + img->unlock(); } -void MLPPTensor3::z_slices_set_image(const Ref &p_img, const int p_index_r, const int p_index_g, const int p_index_b, const int p_index_a) { - ERR_FAIL_COND(!p_img.is_valid()); +void MLPPTensor3::z_slices_set_image(const Ref &p_img, + const int p_index_r, const int p_index_g, + const int p_index_b, const int p_index_a) { + ERR_FAIL_COND(!p_img.is_valid()); - if (p_index_r != -1) { - ERR_FAIL_INDEX(p_index_r, _size.z); - } + if (p_index_r != -1) { + ERR_FAIL_INDEX(p_index_r, _size.z); + } - if (p_index_g != -1) { - ERR_FAIL_INDEX(p_index_g, _size.z); - } + if (p_index_g != -1) { + ERR_FAIL_INDEX(p_index_g, _size.z); + } - if (p_index_b != -1) { - ERR_FAIL_INDEX(p_index_b, _size.z); - } + if (p_index_b != -1) { + ERR_FAIL_INDEX(p_index_b, _size.z); + } - if (p_index_a != -1) { - ERR_FAIL_INDEX(p_index_a, _size.z); - } + if (p_index_a != -1) { + ERR_FAIL_INDEX(p_index_a, _size.z); + } - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - Size2i fms = z_slice_size(); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i fms = z_slice_size(); - ERR_FAIL_COND(img_size != fms); + ERR_FAIL_COND(img_size != fms); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c = img->get_pixel(x, y); + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, y); - if (p_index_r != -1) { - element_set(p_index_r, y, x, c.r); - } + if (p_index_r != -1) { + element_set(p_index_r, y, x, c.r); + } - if (p_index_g != -1) { - element_set(p_index_g, y, x, c.g); - } + if (p_index_g != -1) { + element_set(p_index_g, y, x, c.g); + } - if (p_index_b != -1) { - element_set(p_index_b, y, x, c.b); - } + if (p_index_b != -1) { + element_set(p_index_b, y, x, c.b); + } - if (p_index_a != -1) { - element_set(p_index_a, y, x, c.a); - } - } - } + if (p_index_a != -1) { + element_set(p_index_a, y, x, c.a); + } + } + } - img->unlock(); + img->unlock(); } -void MLPPTensor3::set_from_image(const Ref &p_img, const int p_channels) { - ERR_FAIL_COND(!p_img.is_valid()); +void MLPPTensor3::set_from_image(const Ref &p_img, + const int p_channels) { + ERR_FAIL_COND(!p_img.is_valid()); - int channel_count = 0; - int channels[4]; + int channel_count = 0; + int channels[4]; - if (p_channels & IMAGE_CHANNEL_FLAG_R) { - channels[channel_count] = 0; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_R) { + channels[channel_count] = 0; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_G) { - channels[channel_count] = 1; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_G) { + channels[channel_count] = 1; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_B) { - channels[channel_count] = 2; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_B) { + channels[channel_count] = 2; + ++channel_count; + } - if (p_channels & IMAGE_CHANNEL_FLAG_A) { - channels[channel_count] = 3; - ++channel_count; - } + if (p_channels & IMAGE_CHANNEL_FLAG_A) { + channels[channel_count] = 3; + ++channel_count; + } - ERR_FAIL_COND(channel_count == 0); + ERR_FAIL_COND(channel_count == 0); - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - resize(Size3i(img_size.x, img_size.y, channel_count)); + resize(Size3i(img_size.x, img_size.y, channel_count)); - Size2i fms = z_slice_size(); + Size2i fms = z_slice_size(); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int x = 0; x < fms.x; ++x) { - Color c = img->get_pixel(x, y); + for (int y = 0; y < fms.y; ++y) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, y); - for (int i = 0; i < channel_count; ++i) { - element_set(i, y, x, c[channels[i]]); - } - } - } + for (int i = 0; i < channel_count; ++i) { + element_set(i, y, x, c[channels[i]]); + } + } + } - img->unlock(); + img->unlock(); } Ref MLPPTensor3::x_slice_get_image(const int p_index_x) const { - ERR_FAIL_INDEX_V(p_index_x, _size.x, Ref()); + ERR_FAIL_INDEX_V(p_index_x, _size.x, Ref()); - Ref image; - image.instance(); + Ref image; + image.instance(); - if (data_size() == 0) { - return image; - } + if (data_size() == 0) { + return image; + } - PoolByteArray arr; - arr.resize(_size.y * _size.z); + PoolByteArray arr; + arr.resize(_size.y * _size.z); - PoolByteArray::Write w = arr.write(); - uint8_t *wptr = w.ptr(); - int i = 0; + PoolByteArray::Write w = arr.write(); + uint8_t *wptr = w.ptr(); + int i = 0; - for (int z = 0; z < _size.z; ++z) { - for (int y = 0; y < _size.y; ++y) { - wptr[i] = static_cast(element_get(z, p_index_x, y) * 255.0); + for (int z = 0; z < _size.z; ++z) { + for (int y = 0; y < _size.y; ++y) { + wptr[i] = static_cast(element_get(z, p_index_x, y) * 255.0); - ++i; - } - } + ++i; + } + } - image->create(_size.y, _size.z, false, Image::FORMAT_L8, arr); + image->create(_size.y, _size.z, false, Image::FORMAT_L8, arr); - return image; + return image; } -void MLPPTensor3::x_slice_get_into_image(Ref p_target, const int p_index_x, const int p_target_channels) const { - ERR_FAIL_INDEX(p_index_x, _size.x); - ERR_FAIL_COND(!p_target.is_valid()); +void MLPPTensor3::x_slice_get_into_image(Ref p_target, + const int p_index_x, + const int p_target_channels) const { + ERR_FAIL_INDEX(p_index_x, _size.x); + ERR_FAIL_COND(!p_target.is_valid()); - int channel_count = 0; - int channels[4]; + int channel_count = 0; + int channels[4]; - if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { - channels[channel_count] = 0; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { + channels[channel_count] = 0; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { - channels[channel_count] = 1; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { + channels[channel_count] = 1; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { - channels[channel_count] = 2; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { + channels[channel_count] = 2; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { - channels[channel_count] = 3; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { + channels[channel_count] = 3; + ++channel_count; + } - ERR_FAIL_COND(channel_count == 0); + ERR_FAIL_COND(channel_count == 0); - if (data_size() == 0) { - p_target->clear(); - return; - } + if (data_size() == 0) { + p_target->clear(); + return; + } - Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); - Size2i fms = Size2i(_size.y, _size.z); - if (img_size != fms) { - bool mip_maps = p_target->has_mipmaps(); - p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); + Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); + Size2i fms = Size2i(_size.y, _size.z); + if (img_size != fms) { + bool mip_maps = p_target->has_mipmaps(); + p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); - if (p_target->has_mipmaps() != mip_maps) { - if (mip_maps) { - p_target->generate_mipmaps(); - } else { - p_target->clear_mipmaps(); - } - } - } + if (p_target->has_mipmaps() != mip_maps) { + if (mip_maps) { + p_target->generate_mipmaps(); + } else { + p_target->clear_mipmaps(); + } + } + } - p_target->lock(); + p_target->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int z = 0; z < fms.x; ++z) { - Color c; + for (int y = 0; y < fms.y; ++y) { + for (int z = 0; z < fms.x; ++z) { + Color c; - float e = element_get(z, y, p_index_x); + float e = element_get(z, y, p_index_x); - for (int i = 0; i < channel_count; ++i) { - c[channels[i]] = e; - } + for (int i = 0; i < channel_count; ++i) { + c[channels[i]] = e; + } - p_target->set_pixel(z, y, c); - } - } + p_target->set_pixel(z, y, c); + } + } - p_target->unlock(); + p_target->unlock(); } -void MLPPTensor3::x_slice_set_image(const Ref &p_img, const int p_index_x, const int p_image_channel_flag) { - ERR_FAIL_COND(!p_img.is_valid()); - ERR_FAIL_INDEX(p_index_x, _size.x); +void MLPPTensor3::x_slice_set_image(const Ref &p_img, + const int p_index_x, + const int p_image_channel_flag) { + ERR_FAIL_COND(!p_img.is_valid()); + ERR_FAIL_INDEX(p_index_x, _size.x); - int channel_index = -1; + int channel_index = -1; - for (int i = 0; i < 4; ++i) { - if (((p_image_channel_flag & (1 << i)) != 0)) { - channel_index = i; - break; - } - } + for (int i = 0; i < 4; ++i) { + if (((p_image_channel_flag & (1 << i)) != 0)) { + channel_index = i; + break; + } + } - ERR_FAIL_INDEX(channel_index, 4); + ERR_FAIL_INDEX(channel_index, 4); - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - Size2i fms = Size2i(_size.y, _size.z); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i fms = Size2i(_size.y, _size.z); - ERR_FAIL_COND(img_size != fms); + ERR_FAIL_COND(img_size != fms); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int y = 0; y < fms.y; ++y) { - for (int z = 0; z < fms.x; ++z) { - Color c = img->get_pixel(z, y); + for (int y = 0; y < fms.y; ++y) { + for (int z = 0; z < fms.x; ++z) { + Color c = img->get_pixel(z, y); - element_set(z, y, p_index_x, c[channel_index]); - } - } + element_set(z, y, p_index_x, c[channel_index]); + } + } - img->unlock(); + img->unlock(); } Ref MLPPTensor3::y_slice_get_image(const int p_index_y) const { - ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); + ERR_FAIL_INDEX_V(p_index_y, _size.y, Ref()); - Ref image; - image.instance(); + Ref image; + image.instance(); - if (data_size() == 0) { - return image; - } + if (data_size() == 0) { + return image; + } - PoolByteArray arr; - arr.resize(_size.x * _size.z); + PoolByteArray arr; + arr.resize(_size.x * _size.z); - PoolByteArray::Write w = arr.write(); - uint8_t *wptr = w.ptr(); - int i = 0; + PoolByteArray::Write w = arr.write(); + uint8_t *wptr = w.ptr(); + int i = 0; - for (int z = 0; z < _size.z; ++z) { - for (int x = 0; x < _size.x; ++x) { - wptr[i] = static_cast(element_get(z, x, p_index_y) * 255.0); + for (int z = 0; z < _size.z; ++z) { + for (int x = 0; x < _size.x; ++x) { + wptr[i] = static_cast(element_get(z, x, p_index_y) * 255.0); - ++i; - } - } + ++i; + } + } - image->create(_size.x, _size.z, false, Image::FORMAT_L8, arr); + image->create(_size.x, _size.z, false, Image::FORMAT_L8, arr); - return image; + return image; } -void MLPPTensor3::y_slice_get_into_image(Ref p_target, const int p_index_y, const int p_target_channels) const { - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_COND(!p_target.is_valid()); +void MLPPTensor3::y_slice_get_into_image(Ref p_target, + const int p_index_y, + const int p_target_channels) const { + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_COND(!p_target.is_valid()); - int channel_count = 0; - int channels[4]; + int channel_count = 0; + int channels[4]; - if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { - channels[channel_count] = 0; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_R) { + channels[channel_count] = 0; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { - channels[channel_count] = 1; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_G) { + channels[channel_count] = 1; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { - channels[channel_count] = 2; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_B) { + channels[channel_count] = 2; + ++channel_count; + } - if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { - channels[channel_count] = 3; - ++channel_count; - } + if (p_target_channels & IMAGE_CHANNEL_FLAG_A) { + channels[channel_count] = 3; + ++channel_count; + } - ERR_FAIL_COND(channel_count == 0); + ERR_FAIL_COND(channel_count == 0); - if (data_size() == 0) { - p_target->clear(); - return; - } + if (data_size() == 0) { + p_target->clear(); + return; + } - Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); - Size2i fms = Size2i(_size.x, _size.z); - if (img_size != fms) { - bool mip_maps = p_target->has_mipmaps(); - p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); + Size2i img_size = Size2i(p_target->get_width(), p_target->get_height()); + Size2i fms = Size2i(_size.x, _size.z); + if (img_size != fms) { + bool mip_maps = p_target->has_mipmaps(); + p_target->resize(fms.x, fms.y, Image::INTERPOLATE_NEAREST); - if (p_target->has_mipmaps() != mip_maps) { - if (mip_maps) { - p_target->generate_mipmaps(); - } else { - p_target->clear_mipmaps(); - } - } - } + if (p_target->has_mipmaps() != mip_maps) { + if (mip_maps) { + p_target->generate_mipmaps(); + } else { + p_target->clear_mipmaps(); + } + } + } - p_target->lock(); + p_target->lock(); - for (int x = 0; x < fms.y; ++x) { - for (int z = 0; z < fms.x; ++z) { - Color c; + for (int x = 0; x < fms.y; ++x) { + for (int z = 0; z < fms.x; ++z) { + Color c; - float e = element_get(z, p_index_y, x); + float e = element_get(z, p_index_y, x); - for (int i = 0; i < channel_count; ++i) { - c[channels[i]] = e; - } + for (int i = 0; i < channel_count; ++i) { + c[channels[i]] = e; + } - p_target->set_pixel(z, x, c); - } - } + p_target->set_pixel(z, x, c); + } + } - p_target->unlock(); + p_target->unlock(); } -void MLPPTensor3::y_slice_set_image(const Ref &p_img, const int p_index_y, const int p_image_channel_flag) { - ERR_FAIL_COND(!p_img.is_valid()); - ERR_FAIL_INDEX(p_index_y, _size.y); +void MLPPTensor3::y_slice_set_image(const Ref &p_img, + const int p_index_y, + const int p_image_channel_flag) { + ERR_FAIL_COND(!p_img.is_valid()); + ERR_FAIL_INDEX(p_index_y, _size.y); - int channel_index = -1; + int channel_index = -1; - for (int i = 0; i < 4; ++i) { - if (((p_image_channel_flag & (1 << i)) != 0)) { - channel_index = i; - break; - } - } + for (int i = 0; i < 4; ++i) { + if (((p_image_channel_flag & (1 << i)) != 0)) { + channel_index = i; + break; + } + } - ERR_FAIL_INDEX(channel_index, 4); + ERR_FAIL_INDEX(channel_index, 4); - Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); - Size2i fms = Size2i(_size.x, _size.z); + Size2i img_size = Size2i(p_img->get_width(), p_img->get_height()); + Size2i fms = Size2i(_size.x, _size.z); - ERR_FAIL_COND(img_size != fms); + ERR_FAIL_COND(img_size != fms); - Ref img = p_img; + Ref img = p_img; - img->lock(); + img->lock(); - for (int z = 0; z < fms.y; ++z) { - for (int x = 0; x < fms.x; ++x) { - Color c = img->get_pixel(x, z); + for (int z = 0; z < fms.y; ++z) { + for (int x = 0; x < fms.x; ++x) { + Color c = img->get_pixel(x, z); - element_set(z, p_index_y, x, c[channel_index]); - } - } + element_set(z, p_index_y, x, c[channel_index]); + } + } - img->unlock(); + img->unlock(); } void MLPPTensor3::add(const Ref &B) { - ERR_FAIL_COND(!B.is_valid()); - ERR_FAIL_COND(_size != B->size()); + ERR_FAIL_COND(!B.is_valid()); + ERR_FAIL_COND(_size != B->size()); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] += b_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] += b_ptr[i]; + } } Ref MLPPTensor3::addn(const Ref &B) const { - ERR_FAIL_COND_V(!B.is_valid(), Ref()); - ERR_FAIL_COND_V(_size != B->size(), Ref()); + ERR_FAIL_COND_V(!B.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != B->size(), Ref()); - Ref C; - C.instance(); - C->resize(_size); + Ref C; + C.instance(); + C->resize(_size); - const real_t *a_ptr = ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] = a_ptr[i] + b_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] = a_ptr[i] + b_ptr[i]; + } - return C; + return C; } void MLPPTensor3::addb(const Ref &A, const Ref &B) { - ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); - Size3i a_size = A->size(); - ERR_FAIL_COND(a_size != B->size()); + ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); + Size3i a_size = A->size(); + ERR_FAIL_COND(a_size != B->size()); - if (_size != a_size) { - resize(a_size); - } + if (_size != a_size) { + resize(a_size); + } - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int data_size = A->data_size(); + int data_size = A->data_size(); - for (int i = 0; i < data_size; ++i) { - c_ptr[i] = a_ptr[i] + b_ptr[i]; - } + for (int i = 0; i < data_size; ++i) { + c_ptr[i] = a_ptr[i] + b_ptr[i]; + } } void MLPPTensor3::sub(const Ref &B) { - ERR_FAIL_COND(!B.is_valid()); - ERR_FAIL_COND(_size != B->size()); + ERR_FAIL_COND(!B.is_valid()); + ERR_FAIL_COND(_size != B->size()); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] -= b_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] -= b_ptr[i]; + } } Ref MLPPTensor3::subn(const Ref &B) const { - ERR_FAIL_COND_V(!B.is_valid(), Ref()); - ERR_FAIL_COND_V(_size != B->size(), Ref()); + ERR_FAIL_COND_V(!B.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != B->size(), Ref()); - Ref C; - C.instance(); - C->resize(_size); + Ref C; + C.instance(); + C->resize(_size); - const real_t *a_ptr = ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] = a_ptr[i] - b_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] = a_ptr[i] - b_ptr[i]; + } - return C; + return C; } void MLPPTensor3::subb(const Ref &A, const Ref &B) { - ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); - Size3i a_size = A->size(); - ERR_FAIL_COND(a_size != B->size()); + ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); + Size3i a_size = A->size(); + ERR_FAIL_COND(a_size != B->size()); - if (_size != a_size) { - resize(a_size); - } + if (_size != a_size) { + resize(a_size); + } - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int data_size = A->data_size(); + int data_size = A->data_size(); - for (int i = 0; i < data_size; ++i) { - c_ptr[i] = a_ptr[i] - b_ptr[i]; - } + for (int i = 0; i < data_size; ++i) { + c_ptr[i] = a_ptr[i] - b_ptr[i]; + } } void MLPPTensor3::division_element_wise(const Ref &B) { - ERR_FAIL_COND(!B.is_valid()); - ERR_FAIL_COND(_size != B->size()); + ERR_FAIL_COND(!B.is_valid()); + ERR_FAIL_COND(_size != B->size()); - int ds = data_size(); + int ds = data_size(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] /= b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] /= b_ptr[i]; + } } -Ref MLPPTensor3::division_element_wisen(const Ref &B) const { - ERR_FAIL_COND_V(!B.is_valid(), Ref()); - ERR_FAIL_COND_V(_size != B->size(), Ref()); +Ref +MLPPTensor3::division_element_wisen(const Ref &B) const { + ERR_FAIL_COND_V(!B.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != B->size(), Ref()); - int ds = data_size(); + int ds = data_size(); - Ref C; - C.instance(); - C->resize(_size); + Ref C; + C.instance(); + C->resize(_size); - const real_t *a_ptr = ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] = a_ptr[i] / b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] / b_ptr[i]; + } - return C; + return C; } -void MLPPTensor3::division_element_wiseb(const Ref &A, const Ref &B) { - ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); - Size3i a_size = A->size(); - ERR_FAIL_COND(a_size != B->size()); +void MLPPTensor3::division_element_wiseb(const Ref &A, + const Ref &B) { + ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); + Size3i a_size = A->size(); + ERR_FAIL_COND(a_size != B->size()); - if (a_size != _size) { - resize(a_size); - } + if (a_size != _size) { + resize(a_size); + } - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] = a_ptr[i] / b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] / b_ptr[i]; + } } void MLPPTensor3::sqrt() { - int ds = data_size(); + int ds = data_size(); - real_t *out_ptr = ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::sqrt(out_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::sqrt(out_ptr[i]); + } } Ref MLPPTensor3::sqrtn() const { - Ref out; - out.instance(); - out->resize(size()); + Ref out; + out.instance(); + out->resize(size()); - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = ptr(); - real_t *out_ptr = out->ptrw(); + const real_t *a_ptr = ptr(); + real_t *out_ptr = out->ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::sqrt(a_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::sqrt(a_ptr[i]); + } - return out; + return out; } void MLPPTensor3::sqrtb(const Ref &A) { - ERR_FAIL_COND(!A.is_valid()); + ERR_FAIL_COND(!A.is_valid()); - Size3i a_size = A->size(); + Size3i a_size = A->size(); - if (a_size != size()) { - resize(a_size); - } + if (a_size != size()) { + resize(a_size); + } - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::sqrt(a_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::sqrt(a_ptr[i]); + } } void MLPPTensor3::exponentiate(real_t p) { - int ds = data_size(); + int ds = data_size(); - real_t *out_ptr = ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::pow(out_ptr[i], p); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::pow(out_ptr[i], p); + } } Ref MLPPTensor3::exponentiaten(real_t p) const { - Ref out; - out.instance(); - out->resize(size()); + Ref out; + out.instance(); + out->resize(size()); - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = ptr(); - real_t *out_ptr = out->ptrw(); + const real_t *a_ptr = ptr(); + real_t *out_ptr = out->ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::pow(a_ptr[i], p); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::pow(a_ptr[i], p); + } - return out; + return out; } void MLPPTensor3::exponentiateb(const Ref &A, real_t p) { - ERR_FAIL_COND(!A.is_valid()); + ERR_FAIL_COND(!A.is_valid()); - Size3i a_size = A->size(); + Size3i a_size = A->size(); - if (a_size != size()) { - resize(a_size); - } + if (a_size != size()) { + resize(a_size); + } - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = Math::pow(a_ptr[i], p); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = Math::pow(a_ptr[i], p); + } } void MLPPTensor3::scalar_multiply(const real_t scalar) { - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - _data[i] *= scalar; - } + for (int i = 0; i < ds; ++i) { + _data[i] *= scalar; + } } Ref MLPPTensor3::scalar_multiplyn(const real_t scalar) const { - Ref AN = duplicate_fast(); - int ds = AN->data_size(); - real_t *an_ptr = AN->ptrw(); + Ref AN = duplicate_fast(); + int ds = AN->data_size(); + real_t *an_ptr = AN->ptrw(); - for (int i = 0; i < ds; ++i) { - an_ptr[i] *= scalar; - } + for (int i = 0; i < ds; ++i) { + an_ptr[i] *= scalar; + } - return AN; + return AN; } -void MLPPTensor3::scalar_multiplyb(const real_t scalar, const Ref &A) { - ERR_FAIL_COND(!A.is_valid()); +void MLPPTensor3::scalar_multiplyb(const real_t scalar, + const Ref &A) { + ERR_FAIL_COND(!A.is_valid()); - if (A->size() != _size) { - resize(A->size()); - } + if (A->size() != _size) { + resize(A->size()); + } - int ds = data_size(); - real_t *an_ptr = ptrw(); + int ds = data_size(); + real_t *an_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - _data[i] = an_ptr[i] * scalar; - } + for (int i = 0; i < ds; ++i) { + _data[i] = an_ptr[i] * scalar; + } } void MLPPTensor3::scalar_add(const real_t scalar) { - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - _data[i] += scalar; - } + for (int i = 0; i < ds; ++i) { + _data[i] += scalar; + } } Ref MLPPTensor3::scalar_addn(const real_t scalar) const { - Ref AN = duplicate_fast(); - int ds = AN->data_size(); - real_t *an_ptr = AN->ptrw(); + Ref AN = duplicate_fast(); + int ds = AN->data_size(); + real_t *an_ptr = AN->ptrw(); - for (int i = 0; i < ds; ++i) { - an_ptr[i] += scalar; - } + for (int i = 0; i < ds; ++i) { + an_ptr[i] += scalar; + } - return AN; + return AN; } void MLPPTensor3::scalar_addb(const real_t scalar, const Ref &A) { - ERR_FAIL_COND(!A.is_valid()); + ERR_FAIL_COND(!A.is_valid()); - if (A->size() != _size) { - resize(A->size()); - } + if (A->size() != _size) { + resize(A->size()); + } - int ds = data_size(); - real_t *an_ptr = ptrw(); + int ds = data_size(); + real_t *an_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - _data[i] = an_ptr[i] + scalar; - } + for (int i = 0; i < ds; ++i) { + _data[i] = an_ptr[i] + scalar; + } } void MLPPTensor3::hadamard_product(const Ref &B) { - ERR_FAIL_COND(!B.is_valid()); - ERR_FAIL_COND(_size != B->size()); + ERR_FAIL_COND(!B.is_valid()); + ERR_FAIL_COND(_size != B->size()); - int ds = data_size(); + int ds = data_size(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] = c_ptr[i] * b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = c_ptr[i] * b_ptr[i]; + } } -Ref MLPPTensor3::hadamard_productn(const Ref &B) const { - ERR_FAIL_COND_V(!B.is_valid(), Ref()); - ERR_FAIL_COND_V(_size != B->size(), Ref()); +Ref +MLPPTensor3::hadamard_productn(const Ref &B) const { + ERR_FAIL_COND_V(!B.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != B->size(), Ref()); - int ds = data_size(); + int ds = data_size(); - Ref C; - C.instance(); - C->resize(_size); + Ref C; + C.instance(); + C->resize(_size); - const real_t *a_ptr = ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] = a_ptr[i] * b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] * b_ptr[i]; + } - return C; + return C; } -void MLPPTensor3::hadamard_productb(const Ref &A, const Ref &B) { - ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); - Size3i a_size = A->size(); - ERR_FAIL_COND(a_size != B->size()); +void MLPPTensor3::hadamard_productb(const Ref &A, + const Ref &B) { + ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); + Size3i a_size = A->size(); + ERR_FAIL_COND(a_size != B->size()); - if (a_size != _size) { - resize(a_size); - } + if (a_size != _size) { + resize(a_size); + } - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - for (int i = 0; i < ds; i++) { - c_ptr[i] = a_ptr[i] * b_ptr[i]; - } + for (int i = 0; i < ds; i++) { + c_ptr[i] = a_ptr[i] * b_ptr[i]; + } } void MLPPTensor3::max(const Ref &B) { - ERR_FAIL_COND(!B.is_valid()); - ERR_FAIL_COND(_size != B->size()); + ERR_FAIL_COND(!B.is_valid()); + ERR_FAIL_COND(_size != B->size()); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] = MAX(c_ptr[i], b_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] = MAX(c_ptr[i], b_ptr[i]); + } } Ref MLPPTensor3::maxn(const Ref &B) const { - ERR_FAIL_COND_V(!B.is_valid(), Ref()); - ERR_FAIL_COND_V(_size != B->size(), Ref()); + ERR_FAIL_COND_V(!B.is_valid(), Ref()); + ERR_FAIL_COND_V(_size != B->size(), Ref()); - Ref C; - C.instance(); - C->resize(_size); + Ref C; + C.instance(); + C->resize(_size); - const real_t *a_ptr = ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = C->ptrw(); + const real_t *a_ptr = ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = C->ptrw(); - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - c_ptr[i] = MAX(a_ptr[i], b_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + c_ptr[i] = MAX(a_ptr[i], b_ptr[i]); + } - return C; + return C; } void MLPPTensor3::maxb(const Ref &A, const Ref &B) { - ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); - Size3i a_size = A->size(); - ERR_FAIL_COND(a_size != B->size()); + ERR_FAIL_COND(!A.is_valid() || !B.is_valid()); + Size3i a_size = A->size(); + ERR_FAIL_COND(a_size != B->size()); - if (_size != a_size) { - resize(a_size); - } + if (_size != a_size) { + resize(a_size); + } - const real_t *a_ptr = A->ptr(); - const real_t *b_ptr = B->ptr(); - real_t *c_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + const real_t *b_ptr = B->ptr(); + real_t *c_ptr = ptrw(); - int data_size = A->data_size(); + int data_size = A->data_size(); - for (int i = 0; i < data_size; ++i) { - c_ptr[i] = MAX(a_ptr[i], b_ptr[i]); - } + for (int i = 0; i < data_size; ++i) { + c_ptr[i] = MAX(a_ptr[i], b_ptr[i]); + } } void MLPPTensor3::abs() { - int ds = data_size(); + int ds = data_size(); - real_t *out_ptr = ptrw(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = ABS(out_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = ABS(out_ptr[i]); + } } Ref MLPPTensor3::absn() const { - Ref out; - out.instance(); - out->resize(size()); + Ref out; + out.instance(); + out->resize(size()); - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = ptr(); - real_t *out_ptr = out->ptrw(); + const real_t *a_ptr = ptr(); + real_t *out_ptr = out->ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = ABS(a_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = ABS(a_ptr[i]); + } - return out; + return out; } void MLPPTensor3::absb(const Ref &A) { - ERR_FAIL_COND(!A.is_valid()); + ERR_FAIL_COND(!A.is_valid()); - Size3i a_size = A->size(); + Size3i a_size = A->size(); - if (a_size != size()) { - resize(a_size); - } + if (a_size != size()) { + resize(a_size); + } - int ds = data_size(); + int ds = data_size(); - const real_t *a_ptr = A->ptr(); - real_t *out_ptr = ptrw(); + const real_t *a_ptr = A->ptr(); + real_t *out_ptr = ptrw(); - for (int i = 0; i < ds; ++i) { - out_ptr[i] = ABS(a_ptr[i]); - } + for (int i = 0; i < ds; ++i) { + out_ptr[i] = ABS(a_ptr[i]); + } } Ref MLPPTensor3::flatten() const { - int ds = data_size(); + int ds = data_size(); - Ref res; - res.instance(); - res->resize(ds); + Ref res; + res.instance(); + res->resize(ds); - real_t *res_ptr = res->ptrw(); - const real_t *a_ptr = ptr(); + real_t *res_ptr = res->ptrw(); + const real_t *a_ptr = ptr(); - for (int i = 0; i < ds; ++i) { - res_ptr[i] = a_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + res_ptr[i] = a_ptr[i]; + } - return res; + return res; } void MLPPTensor3::flatteno(Ref out) const { - ERR_FAIL_COND(!out.is_valid()); + ERR_FAIL_COND(!out.is_valid()); - int ds = data_size(); + int ds = data_size(); - if (unlikely(out->size() != ds)) { - out->resize(ds); - } + if (unlikely(out->size() != ds)) { + out->resize(ds); + } - real_t *res_ptr = out->ptrw(); - const real_t *a_ptr = ptr(); + real_t *res_ptr = out->ptrw(); + const real_t *a_ptr = ptr(); - for (int i = 0; i < ds; ++i) { - res_ptr[i] = a_ptr[i]; - } + for (int i = 0; i < ds; ++i) { + res_ptr[i] = a_ptr[i]; + } } /* real_t MLPPTensor3::norm_2(std::vector>> A) { - real_t sum = 0; - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - for (uint32_t k = 0; k < A[i][j].size(); k++) { - sum += A[i][j][k] * A[i][j][k]; - } - } - } - return Math::sqrt(sum); + real_t sum = 0; + for (uint32_t i = 0; i < A.size(); i++) { + for (uint32_t j = 0; j < A[i].size(); j++) { + for (uint32_t k = 0; k < A[i][j].size(); k++) { + sum += A[i][j][k] * A[i][j][k]; + } + } + } + return Math::sqrt(sum); } */ Ref MLPPTensor3::tensor_vec_mult(const Ref &b) { - Ref C; - C.instance(); - C->resize(Size2i(_size.y, _size.z)); + Ref C; + C.instance(); + C->resize(Size2i(_size.y, _size.z)); - Ref row_tmp; - row_tmp.instance(); - row_tmp->resize(_size.x); + Ref row_tmp; + row_tmp.instance(); + row_tmp->resize(_size.x); - for (int i = 0; i < _size.z; i++) { - for (int j = 0; j < _size.y; j++) { - row_get_into_mlpp_vector(i, j, row_tmp); + for (int i = 0; i < _size.z; i++) { + for (int j = 0; j < _size.y; j++) { + row_get_into_mlpp_vector(i, j, row_tmp); - C->element_set(i, j, row_tmp->dot(b)); - } - } + C->element_set(i, j, row_tmp->dot(b)); + } + } - return C; + return C; } /* // Bad implementation. Change this later. -std::vector>> MLPPTensor3::vector_wise_tensor_product(std::vector>> A, std::vector> B) { - std::vector>> C; - C = resize(C, A); - for (uint32_t i = 0; i < A[0].size(); i++) { - for (uint32_t j = 0; j < A[0][i].size(); j++) { - std::vector currentVector; - currentVector.resize(A.size()); +std::vector>> +MLPPTensor3::vector_wise_tensor_product(std::vector>> +A, std::vector> B) { + std::vector>> C; + C = resize(C, A); + for (uint32_t i = 0; i < A[0].size(); i++) { + for (uint32_t j = 0; j < A[0][i].size(); j++) { + std::vector currentVector; + currentVector.resize(A.size()); - for (uint32_t k = 0; k < C.size(); k++) { - currentVector[k] = A[k][i][j]; - } + for (uint32_t k = 0; k < C.size(); k++) { + currentVector[k] = A[k][i][j]; + } - currentVector = mat_vec_mult(B, currentVector); + currentVector = mat_vec_mult(B, currentVector); - for (uint32_t k = 0; k < C.size(); k++) { - C[k][i][j] = currentVector[k]; - } - } - } - return C; + for (uint32_t k = 0; k < C.size(); k++) { + C[k][i][j] = currentVector[k]; + } + } + } + return C; } */ void MLPPTensor3::fill(real_t p_val) { - if (!_data) { - return; - } + if (!_data) { + return; + } - int ds = data_size(); - for (int i = 0; i < ds; ++i) { - _data[i] = p_val; - } + int ds = data_size(); + for (int i = 0; i < ds; ++i) { + _data[i] = p_val; + } } Vector MLPPTensor3::to_flat_vector() const { - Vector ret; - ret.resize(data_size()); - real_t *w = ret.ptrw(); - memcpy(w, _data, sizeof(real_t) * data_size()); - return ret; + Vector ret; + ret.resize(data_size()); + real_t *w = ret.ptrw(); + memcpy(w, _data, sizeof(real_t) * data_size()); + return ret; } PoolRealArray MLPPTensor3::to_flat_pool_vector() const { - PoolRealArray pl; - if (data_size()) { - pl.resize(data_size()); - typename PoolRealArray::Write w = pl.write(); - real_t *dest = w.ptr(); + PoolRealArray pl; + if (data_size()) { + pl.resize(data_size()); + typename PoolRealArray::Write w = pl.write(); + real_t *dest = w.ptr(); - for (int i = 0; i < data_size(); ++i) { - dest[i] = static_cast(_data[i]); - } - } - return pl; + for (int i = 0; i < data_size(); ++i) { + dest[i] = static_cast(_data[i]); + } + } + return pl; } Vector MLPPTensor3::to_flat_byte_array() const { - Vector ret; - ret.resize(data_size() * sizeof(real_t)); - uint8_t *w = ret.ptrw(); - memcpy(w, _data, sizeof(real_t) * data_size()); - return ret; + Vector ret; + ret.resize(data_size() * sizeof(real_t)); + uint8_t *w = ret.ptrw(); + memcpy(w, _data, sizeof(real_t) * data_size()); + return ret; } Ref MLPPTensor3::duplicate_fast() const { - Ref ret; - ret.instance(); + Ref ret; + ret.instance(); - ret->set_from_mlpp_tensor3r(*this); + ret->set_from_mlpp_tensor3r(*this); - return ret; + return ret; } void MLPPTensor3::set_from_mlpp_tensor3(const Ref &p_from) { - ERR_FAIL_COND(!p_from.is_valid()); + ERR_FAIL_COND(!p_from.is_valid()); - resize(p_from->size()); + resize(p_from->size()); - int ds = p_from->data_size(); - const real_t *ptr = p_from->ptr(); + int ds = p_from->data_size(); + const real_t *ptr = p_from->ptr(); - for (int i = 0; i < ds; ++i) { - _data[i] = ptr[i]; - } + for (int i = 0; i < ds; ++i) { + _data[i] = ptr[i]; + } } void MLPPTensor3::set_from_mlpp_tensor3r(const MLPPTensor3 &p_from) { - resize(p_from.size()); + resize(p_from.size()); - int ds = p_from.data_size(); - const real_t *ptr = p_from.ptr(); + int ds = p_from.data_size(); + const real_t *ptr = p_from.ptr(); - for (int i = 0; i < ds; ++i) { - _data[i] = ptr[i]; - } + for (int i = 0; i < ds; ++i) { + _data[i] = ptr[i]; + } } void MLPPTensor3::set_from_mlpp_matrix(const Ref &p_from) { - ERR_FAIL_COND(!p_from.is_valid()); + ERR_FAIL_COND(!p_from.is_valid()); - Size2i mat_size = p_from->size(); - resize(Size3i(mat_size.x, mat_size.y, 1)); + Size2i mat_size = p_from->size(); + resize(Size3i(mat_size.x, mat_size.y, 1)); - int ds = p_from->data_size(); - const real_t *ptr = p_from->ptr(); + int ds = p_from->data_size(); + const real_t *ptr = p_from->ptr(); - for (int i = 0; i < ds; ++i) { - _data[i] = ptr[i]; - } + for (int i = 0; i < ds; ++i) { + _data[i] = ptr[i]; + } } void MLPPTensor3::set_from_mlpp_matrixr(const MLPPMatrix &p_from) { - Size2i mat_size = p_from.size(); - resize(Size3i(mat_size.x, mat_size.y, 1)); + Size2i mat_size = p_from.size(); + resize(Size3i(mat_size.x, mat_size.y, 1)); - int ds = p_from.data_size(); - const real_t *ptr = p_from.ptr(); + int ds = p_from.data_size(); + const real_t *ptr = p_from.ptr(); - for (int i = 0; i < ds; ++i) { - _data[i] = ptr[i]; - } + for (int i = 0; i < ds; ++i) { + _data[i] = ptr[i]; + } } void MLPPTensor3::set_from_mlpp_vectors(const Vector> &p_from) { - if (p_from.size() == 0) { - reset(); - return; - } + if (p_from.size() == 0) { + reset(); + return; + } - if (!p_from[0].is_valid()) { - reset(); - return; - } + if (!p_from[0].is_valid()) { + reset(); + return; + } - resize(Size3i(p_from[0]->size(), p_from.size(), 1)); + resize(Size3i(p_from[0]->size(), p_from.size(), 1)); - if (data_size() == 0) { - reset(); - return; - } + if (data_size() == 0) { + reset(); + return; + } - for (int i = 0; i < p_from.size(); ++i) { - const Ref &r = p_from[i]; + for (int i = 0; i < p_from.size(); ++i) { + const Ref &r = p_from[i]; - ERR_CONTINUE(!r.is_valid()); - ERR_CONTINUE(r->size() != _size.x); + ERR_CONTINUE(!r.is_valid()); + ERR_CONTINUE(r->size() != _size.x); - int start_index = i * _size.x; + int start_index = i * _size.x; - const real_t *from_ptr = r->ptr(); - for (int j = 0; j < _size.x; j++) { - _data[start_index + j] = from_ptr[j]; - } - } + const real_t *from_ptr = r->ptr(); + for (int j = 0; j < _size.x; j++) { + _data[start_index + j] = from_ptr[j]; + } + } } -void MLPPTensor3::set_from_mlpp_matricess(const Vector> &p_from) { - if (p_from.size() == 0) { - reset(); - return; - } +void MLPPTensor3::set_from_mlpp_matricess( + const Vector> &p_from) { + if (p_from.size() == 0) { + reset(); + return; + } - if (!p_from[0].is_valid()) { - reset(); - return; - } + if (!p_from[0].is_valid()) { + reset(); + return; + } - resize(Size3i(p_from[0]->size().x, p_from[0]->size().y, p_from.size())); + resize(Size3i(p_from[0]->size().x, p_from[0]->size().y, p_from.size())); - if (data_size() == 0) { - reset(); - return; - } + if (data_size() == 0) { + reset(); + return; + } - Size2i fms = z_slice_size(); - int fmds = z_slice_data_size(); + Size2i fms = z_slice_size(); + int fmds = z_slice_data_size(); - for (int i = 0; i < p_from.size(); ++i) { - const Ref &r = p_from[i]; + for (int i = 0; i < p_from.size(); ++i) { + const Ref &r = p_from[i]; - ERR_CONTINUE(!r.is_valid()); - ERR_CONTINUE(r->size() != fms); + ERR_CONTINUE(!r.is_valid()); + ERR_CONTINUE(r->size() != fms); - int start_index = calculate_z_slice_index(i); + int start_index = calculate_z_slice_index(i); - const real_t *from_ptr = r->ptr(); - for (int j = 0; j < fmds; j++) { - _data[start_index + j] = from_ptr[j]; - } - } + const real_t *from_ptr = r->ptr(); + for (int j = 0; j < fmds; j++) { + _data[start_index + j] = from_ptr[j]; + } + } } void MLPPTensor3::set_from_mlpp_vectors_array(const Array &p_from) { - if (p_from.size() == 0) { - reset(); - return; - } + if (p_from.size() == 0) { + reset(); + return; + } - Ref v0 = p_from[0]; + Ref v0 = p_from[0]; - if (!v0.is_valid()) { - reset(); - return; - } + if (!v0.is_valid()) { + reset(); + return; + } - resize(Size3i(v0->size(), p_from.size(), 1)); + resize(Size3i(v0->size(), p_from.size(), 1)); - if (data_size() == 0) { - reset(); - return; - } + if (data_size() == 0) { + reset(); + return; + } - for (int i = 0; i < p_from.size(); ++i) { - Ref r = p_from[i]; + for (int i = 0; i < p_from.size(); ++i) { + Ref r = p_from[i]; - ERR_CONTINUE(!r.is_valid()); - ERR_CONTINUE(r->size() != _size.x); + ERR_CONTINUE(!r.is_valid()); + ERR_CONTINUE(r->size() != _size.x); - int start_index = i * _size.x; + int start_index = i * _size.x; - const real_t *from_ptr = r->ptr(); - for (int j = 0; j < _size.x; j++) { - _data[start_index + j] = from_ptr[j]; - } - } + const real_t *from_ptr = r->ptr(); + for (int j = 0; j < _size.x; j++) { + _data[start_index + j] = from_ptr[j]; + } + } } void MLPPTensor3::set_from_mlpp_matrices_array(const Array &p_from) { - if (p_from.size() == 0) { - reset(); - return; - } + if (p_from.size() == 0) { + reset(); + return; + } - Ref v0 = p_from[0]; + Ref v0 = p_from[0]; - if (!v0.is_valid()) { - reset(); - return; - } + if (!v0.is_valid()) { + reset(); + return; + } - resize(Size3i(v0->size().x, v0->size().y, p_from.size())); + resize(Size3i(v0->size().x, v0->size().y, p_from.size())); - if (data_size() == 0) { - reset(); - return; - } + if (data_size() == 0) { + reset(); + return; + } - Size2i fms = z_slice_size(); - int fmds = z_slice_data_size(); + Size2i fms = z_slice_size(); + int fmds = z_slice_data_size(); - for (int i = 0; i < p_from.size(); ++i) { - Ref r = p_from[i]; + for (int i = 0; i < p_from.size(); ++i) { + Ref r = p_from[i]; - ERR_CONTINUE(!r.is_valid()); - ERR_CONTINUE(r->size() != fms); + ERR_CONTINUE(!r.is_valid()); + ERR_CONTINUE(r->size() != fms); - int start_index = calculate_z_slice_index(i); + int start_index = calculate_z_slice_index(i); - const real_t *from_ptr = r->ptr(); - for (int j = 0; j < fmds; j++) { - _data[start_index + j] = from_ptr[j]; - } - } + const real_t *from_ptr = r->ptr(); + for (int j = 0; j < fmds; j++) { + _data[start_index + j] = from_ptr[j]; + } + } } -bool MLPPTensor3::is_equal_approx(const Ref &p_with, real_t tolerance) const { - ERR_FAIL_COND_V(!p_with.is_valid(), false); +bool MLPPTensor3::is_equal_approx(const Ref &p_with, + real_t tolerance) const { + ERR_FAIL_COND_V(!p_with.is_valid(), false); - if (unlikely(this == p_with.ptr())) { - return true; - } + if (unlikely(this == p_with.ptr())) { + return true; + } - if (_size != p_with->size()) { - return false; - } + if (_size != p_with->size()) { + return false; + } - int ds = data_size(); + int ds = data_size(); - for (int i = 0; i < ds; ++i) { - if (!Math::is_equal_approx(_data[i], p_with->_data[i], tolerance)) { - return false; - } - } + for (int i = 0; i < ds; ++i) { + if (!Math::is_equal_approx(_data[i], p_with->_data[i], tolerance)) { + return false; + } + } - return true; + return true; } String MLPPTensor3::to_string() { - String str; + String str; - str += "[MLPPTensor3: \n"; + str += "[MLPPTensor3: \n"; - for (int z = 0; z < _size.z; ++z) { - int z_ofs = _size.x * _size.y * z; + for (int z = 0; z < _size.z; ++z) { + int z_ofs = _size.x * _size.y * z; - str += " [ "; + str += " [ "; - for (int y = 0; y < _size.y; ++y) { - str += " [ "; + for (int y = 0; y < _size.y; ++y) { + str += " [ "; - for (int x = 0; x < _size.x; ++x) { - str += String::num(_data[_size.x * y + x + z_ofs]); - str += " "; - } + for (int x = 0; x < _size.x; ++x) { + str += String::num(_data[_size.x * y + x + z_ofs]); + str += " "; + } - str += " ]\n"; - } + str += " ]\n"; + } - str += "],\n"; - } + str += "],\n"; + } - str += "]\n"; + str += "]\n"; - return str; + return str; } -MLPPTensor3::MLPPTensor3() { - _data = NULL; -} +MLPPTensor3::MLPPTensor3() { _data = NULL; } MLPPTensor3::MLPPTensor3(const MLPPMatrix &p_from) { - _data = NULL; + _data = NULL; - Size2i mat_size = p_from.size(); - resize(Size3i(mat_size.x, mat_size.y, 1)); + Size2i mat_size = p_from.size(); + resize(Size3i(mat_size.x, mat_size.y, 1)); - int ds = p_from.data_size(); - const real_t *ptr = p_from.ptr(); + int ds = p_from.data_size(); + const real_t *ptr = p_from.ptr(); - for (int i = 0; i < ds; ++i) { - _data[i] = ptr[i]; - } + for (int i = 0; i < ds; ++i) { + _data[i] = ptr[i]; + } } MLPPTensor3::MLPPTensor3(const Array &p_from) { - _data = NULL; + _data = NULL; - set_from_mlpp_matrices_array(p_from); + set_from_mlpp_matrices_array(p_from); } MLPPTensor3::~MLPPTensor3() { - if (_data) { - reset(); - } + if (_data) { + reset(); + } } std::vector MLPPTensor3::to_flat_std_vector() const { - std::vector ret; - ret.resize(data_size()); - real_t *w = &ret[0]; - memcpy(w, _data, sizeof(real_t) * data_size()); - return ret; + std::vector ret; + ret.resize(data_size()); + real_t *w = &ret[0]; + memcpy(w, _data, sizeof(real_t) * data_size()); + return ret; } -void MLPPTensor3::set_from_std_vectors(const std::vector>> &p_from) { - if (p_from.size() == 0) { - reset(); - return; - } +void MLPPTensor3::set_from_std_vectors( + const std::vector>> &p_from) { + if (p_from.size() == 0) { + reset(); + return; + } - resize(Size3i(p_from[0][0].size(), p_from[0].size(), p_from.size())); + resize(Size3i(p_from[0][0].size(), p_from[0].size(), p_from.size())); - if (data_size() == 0) { - reset(); - return; - } + if (data_size() == 0) { + reset(); + return; + } - for (uint32_t z = 0; z < p_from.size(); ++z) { - const std::vector> &vxy = p_from[z]; + for (uint32_t z = 0; z < p_from.size(); ++z) { + const std::vector> &vxy = p_from[z]; - for (uint32_t y = 0; y < vxy.size(); ++y) { - ERR_CONTINUE(vxy.size() != static_cast(_size.y)); + for (uint32_t y = 0; y < vxy.size(); ++y) { + ERR_CONTINUE(vxy.size() != static_cast(_size.y)); - const std::vector &vx = vxy[y]; + const std::vector &vx = vxy[y]; - ERR_CONTINUE(vx.size() != static_cast(_size.x)); + ERR_CONTINUE(vx.size() != static_cast(_size.x)); - for (uint32_t x = 0; x < vx.size(); ++x) { - element_set(z, y, x, vx[x]); - } - } - } + for (uint32_t x = 0; x < vx.size(); ++x) { + element_set(z, y, x, vx[x]); + } + } + } } std::vector>> MLPPTensor3::to_std_vector() { - std::vector>> ret; + std::vector>> ret; - ret.resize(_size.z); + ret.resize(_size.z); - for (int k = 0; k < _size.z; ++k) { - ret[k].resize(_size.y); + for (int k = 0; k < _size.z; ++k) { + ret[k].resize(_size.y); - for (int i = 0; i < _size.y; ++i) { - std::vector row; + for (int i = 0; i < _size.y; ++i) { + std::vector row; - for (int j = 0; j < _size.x; ++j) { - row.push_back(_data[calculate_index(k, i, j)]); - } + for (int j = 0; j < _size.x; ++j) { + row.push_back(_data[calculate_index(k, i, j)]); + } - ret[k][i] = row; - } - } + ret[k][i] = row; + } + } - return ret; + return ret; } -MLPPTensor3::MLPPTensor3(const std::vector>> &p_from) { - _data = NULL; +MLPPTensor3::MLPPTensor3( + const std::vector>> &p_from) { + _data = NULL; - set_from_std_vectors(p_from); + set_from_std_vectors(p_from); } void MLPPTensor3::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_data"), &MLPPTensor3::get_data); - ClassDB::bind_method(D_METHOD("set_data", "data"), &MLPPTensor3::set_data); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "data"), "set_data", "get_data"); + ClassDB::bind_method(D_METHOD("get_data"), &MLPPTensor3::get_data); + ClassDB::bind_method(D_METHOD("set_data", "data"), &MLPPTensor3::set_data); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "data"), "set_data", "get_data"); - ClassDB::bind_method(D_METHOD("z_slice_add_pool_vector", "row"), &MLPPTensor3::z_slice_add_pool_vector); - ClassDB::bind_method(D_METHOD("z_slice_add_mlpp_vector", "row"), &MLPPTensor3::z_slice_add_mlpp_vector); - ClassDB::bind_method(D_METHOD("z_slice_add_mlpp_matrix", "matrix"), &MLPPTensor3::z_slice_add_mlpp_matrix); + ClassDB::bind_method(D_METHOD("z_slice_add_pool_vector", "row"), + &MLPPTensor3::z_slice_add_pool_vector); + ClassDB::bind_method(D_METHOD("z_slice_add_mlpp_vector", "row"), + &MLPPTensor3::z_slice_add_mlpp_vector); + ClassDB::bind_method(D_METHOD("z_slice_add_mlpp_matrix", "matrix"), + &MLPPTensor3::z_slice_add_mlpp_matrix); - ClassDB::bind_method(D_METHOD("z_slice_remove", "index"), &MLPPTensor3::z_slice_remove); - ClassDB::bind_method(D_METHOD("z_slice_remove_unordered", "index"), &MLPPTensor3::z_slice_remove_unordered); + ClassDB::bind_method(D_METHOD("z_slice_remove", "index"), + &MLPPTensor3::z_slice_remove); + ClassDB::bind_method(D_METHOD("z_slice_remove_unordered", "index"), + &MLPPTensor3::z_slice_remove_unordered); - ClassDB::bind_method(D_METHOD("z_slice_swap", "index_1", "index_2"), &MLPPTensor3::z_slice_swap); + ClassDB::bind_method(D_METHOD("z_slice_swap", "index_1", "index_2"), + &MLPPTensor3::z_slice_swap); - ClassDB::bind_method(D_METHOD("clear"), &MLPPTensor3::clear); - ClassDB::bind_method(D_METHOD("reset"), &MLPPTensor3::reset); - ClassDB::bind_method(D_METHOD("empty"), &MLPPTensor3::empty); + ClassDB::bind_method(D_METHOD("clear"), &MLPPTensor3::clear); + ClassDB::bind_method(D_METHOD("reset"), &MLPPTensor3::reset); + ClassDB::bind_method(D_METHOD("empty"), &MLPPTensor3::empty); - ClassDB::bind_method(D_METHOD("z_slice_data_size"), &MLPPTensor3::z_slice_data_size); - ClassDB::bind_method(D_METHOD("z_slice_size"), &MLPPTensor3::z_slice_size); + ClassDB::bind_method(D_METHOD("z_slice_data_size"), + &MLPPTensor3::z_slice_data_size); + ClassDB::bind_method(D_METHOD("z_slice_size"), &MLPPTensor3::z_slice_size); - ClassDB::bind_method(D_METHOD("data_size"), &MLPPTensor3::data_size); - ClassDB::bind_method(D_METHOD("size"), &MLPPTensor3::size); + ClassDB::bind_method(D_METHOD("data_size"), &MLPPTensor3::data_size); + ClassDB::bind_method(D_METHOD("size"), &MLPPTensor3::size); - ClassDB::bind_method(D_METHOD("resize", "size"), &MLPPTensor3::resize); + ClassDB::bind_method(D_METHOD("resize", "size"), &MLPPTensor3::resize); - ClassDB::bind_method(D_METHOD("shape_set", "size"), &MLPPTensor3::shape_set); - ClassDB::bind_method(D_METHOD("calculate_index", "index_y", "index_x", "index_z"), &MLPPTensor3::calculate_index); - ClassDB::bind_method(D_METHOD("calculate_z_slice_index", "index_z"), &MLPPTensor3::calculate_z_slice_index); + ClassDB::bind_method(D_METHOD("shape_set", "size"), &MLPPTensor3::shape_set); + ClassDB::bind_method( + D_METHOD("calculate_index", "index_y", "index_x", "index_z"), + &MLPPTensor3::calculate_index); + ClassDB::bind_method(D_METHOD("calculate_z_slice_index", "index_z"), + &MLPPTensor3::calculate_z_slice_index); - ClassDB::bind_method(D_METHOD("element_get_index", "index"), &MLPPTensor3::element_get_index); - ClassDB::bind_method(D_METHOD("element_set_index", "index", "val"), &MLPPTensor3::element_set_index); + ClassDB::bind_method(D_METHOD("element_get_index", "index"), + &MLPPTensor3::element_get_index); + ClassDB::bind_method(D_METHOD("element_set_index", "index", "val"), + &MLPPTensor3::element_set_index); - ClassDB::bind_method(D_METHOD("element_get", "index_y", "index_x", "index_z"), &MLPPTensor3::element_get); - ClassDB::bind_method(D_METHOD("element_set", "index_y", "index_x", "index_z", "val"), &MLPPTensor3::element_set); + ClassDB::bind_method(D_METHOD("element_get", "index_y", "index_x", "index_z"), + &MLPPTensor3::element_get); + ClassDB::bind_method( + D_METHOD("element_set", "index_y", "index_x", "index_z", "val"), + &MLPPTensor3::element_set); - ClassDB::bind_method(D_METHOD("row_get_pool_vector", "index_y", "index_z"), &MLPPTensor3::row_get_pool_vector); - ClassDB::bind_method(D_METHOD("row_get_mlpp_vector", "index_y", "index_z"), &MLPPTensor3::row_get_mlpp_vector); - ClassDB::bind_method(D_METHOD("row_get_into_mlpp_vector", "index_y", "index_z", "target"), &MLPPTensor3::row_get_into_mlpp_vector); + ClassDB::bind_method(D_METHOD("row_get_pool_vector", "index_y", "index_z"), + &MLPPTensor3::row_get_pool_vector); + ClassDB::bind_method(D_METHOD("row_get_mlpp_vector", "index_y", "index_z"), + &MLPPTensor3::row_get_mlpp_vector); + ClassDB::bind_method( + D_METHOD("row_get_into_mlpp_vector", "index_y", "index_z", "target"), + &MLPPTensor3::row_get_into_mlpp_vector); - ClassDB::bind_method(D_METHOD("row_set_pool_vector", "index_y", "index_z", "row"), &MLPPTensor3::row_set_pool_vector); - ClassDB::bind_method(D_METHOD("row_set_mlpp_vector", "index_y", "index_z", "row"), &MLPPTensor3::row_set_mlpp_vector); + ClassDB::bind_method( + D_METHOD("row_set_pool_vector", "index_y", "index_z", "row"), + &MLPPTensor3::row_set_pool_vector); + ClassDB::bind_method( + D_METHOD("row_set_mlpp_vector", "index_y", "index_z", "row"), + &MLPPTensor3::row_set_mlpp_vector); - ClassDB::bind_method(D_METHOD("z_slice_get_pool_vector", "index_z"), &MLPPTensor3::z_slice_get_pool_vector); - ClassDB::bind_method(D_METHOD("z_slice_get_mlpp_vector", "index_z"), &MLPPTensor3::z_slice_get_mlpp_vector); - ClassDB::bind_method(D_METHOD("z_slice_get_into_mlpp_vector", "index_z", "target"), &MLPPTensor3::z_slice_get_into_mlpp_vector); + ClassDB::bind_method(D_METHOD("z_slice_get_pool_vector", "index_z"), + &MLPPTensor3::z_slice_get_pool_vector); + ClassDB::bind_method(D_METHOD("z_slice_get_mlpp_vector", "index_z"), + &MLPPTensor3::z_slice_get_mlpp_vector); + ClassDB::bind_method( + D_METHOD("z_slice_get_into_mlpp_vector", "index_z", "target"), + &MLPPTensor3::z_slice_get_into_mlpp_vector); - ClassDB::bind_method(D_METHOD("z_slice_get_mlpp_matrix", "index_z"), &MLPPTensor3::z_slice_get_mlpp_matrix); - ClassDB::bind_method(D_METHOD("z_slice_get_into_mlpp_matrix", "index_z", "target"), &MLPPTensor3::z_slice_get_into_mlpp_matrix); + ClassDB::bind_method(D_METHOD("z_slice_get_mlpp_matrix", "index_z"), + &MLPPTensor3::z_slice_get_mlpp_matrix); + ClassDB::bind_method( + D_METHOD("z_slice_get_into_mlpp_matrix", "index_z", "target"), + &MLPPTensor3::z_slice_get_into_mlpp_matrix); - ClassDB::bind_method(D_METHOD("z_slice_set_pool_vector", "index_z", "row"), &MLPPTensor3::z_slice_set_pool_vector); - ClassDB::bind_method(D_METHOD("z_slice_set_mlpp_vector", "index_z", "row"), &MLPPTensor3::z_slice_set_mlpp_vector); - ClassDB::bind_method(D_METHOD("z_slice_set_mlpp_matrix", "index_z", "mat"), &MLPPTensor3::z_slice_set_mlpp_matrix); + ClassDB::bind_method(D_METHOD("z_slice_set_pool_vector", "index_z", "row"), + &MLPPTensor3::z_slice_set_pool_vector); + ClassDB::bind_method(D_METHOD("z_slice_set_mlpp_vector", "index_z", "row"), + &MLPPTensor3::z_slice_set_mlpp_vector); + ClassDB::bind_method(D_METHOD("z_slice_set_mlpp_matrix", "index_z", "mat"), + &MLPPTensor3::z_slice_set_mlpp_matrix); - ClassDB::bind_method(D_METHOD("x_slice_get_into", "index_x", "target"), &MLPPTensor3::x_slice_get_into); - ClassDB::bind_method(D_METHOD("x_slice_get", "index_x"), &MLPPTensor3::x_slice_get); - ClassDB::bind_method(D_METHOD("x_slice_set", "index_x", "mat"), &MLPPTensor3::x_slice_set); + ClassDB::bind_method(D_METHOD("x_slice_get_into", "index_x", "target"), + &MLPPTensor3::x_slice_get_into); + ClassDB::bind_method(D_METHOD("x_slice_get", "index_x"), + &MLPPTensor3::x_slice_get); + ClassDB::bind_method(D_METHOD("x_slice_set", "index_x", "mat"), + &MLPPTensor3::x_slice_set); - ClassDB::bind_method(D_METHOD("y_slice_get_into", "index_y", "target"), &MLPPTensor3::y_slice_get_into); - ClassDB::bind_method(D_METHOD("y_slice_get", "index_y"), &MLPPTensor3::y_slice_get); - ClassDB::bind_method(D_METHOD("y_slice_set", "index_y", "mat"), &MLPPTensor3::y_slice_set); + ClassDB::bind_method(D_METHOD("y_slice_get_into", "index_y", "target"), + &MLPPTensor3::y_slice_get_into); + ClassDB::bind_method(D_METHOD("y_slice_get", "index_y"), + &MLPPTensor3::y_slice_get); + ClassDB::bind_method(D_METHOD("y_slice_set", "index_y", "mat"), + &MLPPTensor3::y_slice_set); - ClassDB::bind_method(D_METHOD("z_slices_add_image", "img", "channels"), &MLPPTensor3::z_slices_add_image, IMAGE_CHANNEL_FLAG_RGBA); + ClassDB::bind_method(D_METHOD("z_slices_add_image", "img", "channels"), + &MLPPTensor3::z_slices_add_image, + IMAGE_CHANNEL_FLAG_RGBA); - ClassDB::bind_method(D_METHOD("z_slice_get_image", "index_z"), &MLPPTensor3::z_slice_get_image); - ClassDB::bind_method(D_METHOD("z_slices_get_image", "index_r", "index_g", "index_b", "index_a"), &MLPPTensor3::z_slices_get_image, -1, -1, -1, -1); + ClassDB::bind_method(D_METHOD("z_slice_get_image", "index_z"), + &MLPPTensor3::z_slice_get_image); + ClassDB::bind_method(D_METHOD("z_slices_get_image", "index_r", "index_g", + "index_b", "index_a"), + &MLPPTensor3::z_slices_get_image, -1, -1, -1, -1); - ClassDB::bind_method(D_METHOD("z_slice_get_into_image", "target", "index_z", "target_channels"), &MLPPTensor3::z_slice_get_into_image, IMAGE_CHANNEL_FLAG_RGB); - ClassDB::bind_method(D_METHOD("z_slices_get_into_image", "target", "index_r", "index_g", "index_b", "index_a"), &MLPPTensor3::z_slices_get_into_image, -1, -1, -1, -1); + ClassDB::bind_method(D_METHOD("z_slice_get_into_image", "target", "index_z", + "target_channels"), + &MLPPTensor3::z_slice_get_into_image, + IMAGE_CHANNEL_FLAG_RGB); + ClassDB::bind_method(D_METHOD("z_slices_get_into_image", "target", "index_r", + "index_g", "index_b", "index_a"), + &MLPPTensor3::z_slices_get_into_image, -1, -1, -1, -1); - ClassDB::bind_method(D_METHOD("z_slice_set_image", "img", "index_z", "image_channel_flag"), &MLPPTensor3::z_slice_set_image, IMAGE_CHANNEL_FLAG_R); - ClassDB::bind_method(D_METHOD("z_slices_set_image", "img", "index_r", "index_g", "index_b", "index_a"), &MLPPTensor3::z_slices_set_image); + ClassDB::bind_method( + D_METHOD("z_slice_set_image", "img", "index_z", "image_channel_flag"), + &MLPPTensor3::z_slice_set_image, IMAGE_CHANNEL_FLAG_R); + ClassDB::bind_method(D_METHOD("z_slices_set_image", "img", "index_r", + "index_g", "index_b", "index_a"), + &MLPPTensor3::z_slices_set_image); - ClassDB::bind_method(D_METHOD("set_from_image", "img", "channels"), &MLPPTensor3::set_from_image, IMAGE_CHANNEL_FLAG_RGBA); + ClassDB::bind_method(D_METHOD("set_from_image", "img", "channels"), + &MLPPTensor3::set_from_image, IMAGE_CHANNEL_FLAG_RGBA); - ClassDB::bind_method(D_METHOD("x_slice_get_image", "index_x"), &MLPPTensor3::x_slice_get_image); - ClassDB::bind_method(D_METHOD("x_slice_get_into_image", "target", "index_x", "target_channels"), &MLPPTensor3::x_slice_get_into_image, IMAGE_CHANNEL_FLAG_RGB); - ClassDB::bind_method(D_METHOD("x_slice_set_image", "img", "index_x", "image_channel_flag"), &MLPPTensor3::x_slice_set_image, IMAGE_CHANNEL_FLAG_R); + ClassDB::bind_method(D_METHOD("x_slice_get_image", "index_x"), + &MLPPTensor3::x_slice_get_image); + ClassDB::bind_method(D_METHOD("x_slice_get_into_image", "target", "index_x", + "target_channels"), + &MLPPTensor3::x_slice_get_into_image, + IMAGE_CHANNEL_FLAG_RGB); + ClassDB::bind_method( + D_METHOD("x_slice_set_image", "img", "index_x", "image_channel_flag"), + &MLPPTensor3::x_slice_set_image, IMAGE_CHANNEL_FLAG_R); - ClassDB::bind_method(D_METHOD("y_slice_get_image", "index_x"), &MLPPTensor3::y_slice_get_image); - ClassDB::bind_method(D_METHOD("y_slice_get_into_image", "target", "index_x", "target_channels"), &MLPPTensor3::y_slice_get_into_image, IMAGE_CHANNEL_FLAG_RGB); - ClassDB::bind_method(D_METHOD("y_slice_set_image", "img", "index_x", "image_channel_flag"), &MLPPTensor3::y_slice_set_image, IMAGE_CHANNEL_FLAG_R); + ClassDB::bind_method(D_METHOD("y_slice_get_image", "index_x"), + &MLPPTensor3::y_slice_get_image); + ClassDB::bind_method(D_METHOD("y_slice_get_into_image", "target", "index_x", + "target_channels"), + &MLPPTensor3::y_slice_get_into_image, + IMAGE_CHANNEL_FLAG_RGB); + ClassDB::bind_method( + D_METHOD("y_slice_set_image", "img", "index_x", "image_channel_flag"), + &MLPPTensor3::y_slice_set_image, IMAGE_CHANNEL_FLAG_R); - ClassDB::bind_method(D_METHOD("fill", "val"), &MLPPTensor3::fill); + ClassDB::bind_method(D_METHOD("fill", "val"), &MLPPTensor3::fill); - ClassDB::bind_method(D_METHOD("to_flat_pool_vector"), &MLPPTensor3::to_flat_pool_vector); - ClassDB::bind_method(D_METHOD("to_flat_byte_array"), &MLPPTensor3::to_flat_byte_array); + ClassDB::bind_method(D_METHOD("to_flat_pool_vector"), + &MLPPTensor3::to_flat_pool_vector); + ClassDB::bind_method(D_METHOD("to_flat_byte_array"), + &MLPPTensor3::to_flat_byte_array); - ClassDB::bind_method(D_METHOD("duplicate_fast"), &MLPPTensor3::duplicate_fast); + ClassDB::bind_method(D_METHOD("duplicate_fast"), + &MLPPTensor3::duplicate_fast); - ClassDB::bind_method(D_METHOD("set_from_mlpp_tensor3", "from"), &MLPPTensor3::set_from_mlpp_tensor3); - ClassDB::bind_method(D_METHOD("set_from_mlpp_matrix", "from"), &MLPPTensor3::set_from_mlpp_matrix); - ClassDB::bind_method(D_METHOD("set_from_mlpp_vectors_array", "from"), &MLPPTensor3::set_from_mlpp_vectors_array); - ClassDB::bind_method(D_METHOD("set_from_mlpp_matrices_array", "from"), &MLPPTensor3::set_from_mlpp_matrices_array); + ClassDB::bind_method(D_METHOD("set_from_mlpp_tensor3", "from"), + &MLPPTensor3::set_from_mlpp_tensor3); + ClassDB::bind_method(D_METHOD("set_from_mlpp_matrix", "from"), + &MLPPTensor3::set_from_mlpp_matrix); + ClassDB::bind_method(D_METHOD("set_from_mlpp_vectors_array", "from"), + &MLPPTensor3::set_from_mlpp_vectors_array); + ClassDB::bind_method(D_METHOD("set_from_mlpp_matrices_array", "from"), + &MLPPTensor3::set_from_mlpp_matrices_array); - ClassDB::bind_method(D_METHOD("is_equal_approx", "with", "tolerance"), &MLPPTensor3::is_equal_approx, CMP_EPSILON); + ClassDB::bind_method(D_METHOD("is_equal_approx", "with", "tolerance"), + &MLPPTensor3::is_equal_approx, CMP_EPSILON); - ClassDB::bind_method(D_METHOD("add", "B"), &MLPPTensor3::add); - ClassDB::bind_method(D_METHOD("addn", "B"), &MLPPTensor3::addn); - ClassDB::bind_method(D_METHOD("addb", "A", "B"), &MLPPTensor3::addb); + ClassDB::bind_method(D_METHOD("add", "B"), &MLPPTensor3::add); + ClassDB::bind_method(D_METHOD("addn", "B"), &MLPPTensor3::addn); + ClassDB::bind_method(D_METHOD("addb", "A", "B"), &MLPPTensor3::addb); - ClassDB::bind_method(D_METHOD("sub", "B"), &MLPPTensor3::sub); - ClassDB::bind_method(D_METHOD("subn", "B"), &MLPPTensor3::subn); - ClassDB::bind_method(D_METHOD("subb", "A", "B"), &MLPPTensor3::subb); + ClassDB::bind_method(D_METHOD("sub", "B"), &MLPPTensor3::sub); + ClassDB::bind_method(D_METHOD("subn", "B"), &MLPPTensor3::subn); + ClassDB::bind_method(D_METHOD("subb", "A", "B"), &MLPPTensor3::subb); - ClassDB::bind_method(D_METHOD("hadamard_product", "B"), &MLPPTensor3::hadamard_product); - ClassDB::bind_method(D_METHOD("hadamard_productn", "B"), &MLPPTensor3::hadamard_productn); - ClassDB::bind_method(D_METHOD("hadamard_productb", "A", "B"), &MLPPTensor3::hadamard_productb); + ClassDB::bind_method(D_METHOD("hadamard_product", "B"), + &MLPPTensor3::hadamard_product); + ClassDB::bind_method(D_METHOD("hadamard_productn", "B"), + &MLPPTensor3::hadamard_productn); + ClassDB::bind_method(D_METHOD("hadamard_productb", "A", "B"), + &MLPPTensor3::hadamard_productb); - ClassDB::bind_method(D_METHOD("division_element_wise", "B"), &MLPPTensor3::division_element_wise); - ClassDB::bind_method(D_METHOD("division_element_wisen", "B"), &MLPPTensor3::division_element_wisen); - ClassDB::bind_method(D_METHOD("division_element_wiseb", "A", "B"), &MLPPTensor3::division_element_wiseb); + ClassDB::bind_method(D_METHOD("division_element_wise", "B"), + &MLPPTensor3::division_element_wise); + ClassDB::bind_method(D_METHOD("division_element_wisen", "B"), + &MLPPTensor3::division_element_wisen); + ClassDB::bind_method(D_METHOD("division_element_wiseb", "A", "B"), + &MLPPTensor3::division_element_wiseb); - ClassDB::bind_method(D_METHOD("scalar_multiply", "scalar"), &MLPPTensor3::scalar_multiply); - ClassDB::bind_method(D_METHOD("scalar_multiplyn", "scalar"), &MLPPTensor3::scalar_multiplyn); - ClassDB::bind_method(D_METHOD("scalar_multiplyb", "scalar", "A"), &MLPPTensor3::scalar_multiplyb); + ClassDB::bind_method(D_METHOD("scalar_multiply", "scalar"), + &MLPPTensor3::scalar_multiply); + ClassDB::bind_method(D_METHOD("scalar_multiplyn", "scalar"), + &MLPPTensor3::scalar_multiplyn); + ClassDB::bind_method(D_METHOD("scalar_multiplyb", "scalar", "A"), + &MLPPTensor3::scalar_multiplyb); - ClassDB::bind_method(D_METHOD("scalar_add", "scalar"), &MLPPTensor3::scalar_add); - ClassDB::bind_method(D_METHOD("scalar_addn", "scalar"), &MLPPTensor3::scalar_addn); - ClassDB::bind_method(D_METHOD("scalar_addb", "scalar", "A"), &MLPPTensor3::scalar_addb); + ClassDB::bind_method(D_METHOD("scalar_add", "scalar"), + &MLPPTensor3::scalar_add); + ClassDB::bind_method(D_METHOD("scalar_addn", "scalar"), + &MLPPTensor3::scalar_addn); + ClassDB::bind_method(D_METHOD("scalar_addb", "scalar", "A"), + &MLPPTensor3::scalar_addb); - ClassDB::bind_method(D_METHOD("exponentiate", "p"), &MLPPTensor3::exponentiate); - ClassDB::bind_method(D_METHOD("exponentiaten", "p"), &MLPPTensor3::exponentiaten); - ClassDB::bind_method(D_METHOD("exponentiateb", "A", "p"), &MLPPTensor3::exponentiateb); + ClassDB::bind_method(D_METHOD("exponentiate", "p"), + &MLPPTensor3::exponentiate); + ClassDB::bind_method(D_METHOD("exponentiaten", "p"), + &MLPPTensor3::exponentiaten); + ClassDB::bind_method(D_METHOD("exponentiateb", "A", "p"), + &MLPPTensor3::exponentiateb); - ClassDB::bind_method(D_METHOD("sqrt"), &MLPPTensor3::sqrt); - ClassDB::bind_method(D_METHOD("sqrtn"), &MLPPTensor3::sqrtn); - ClassDB::bind_method(D_METHOD("sqrtb", "A"), &MLPPTensor3::sqrtb); + ClassDB::bind_method(D_METHOD("sqrt"), &MLPPTensor3::sqrt); + ClassDB::bind_method(D_METHOD("sqrtn"), &MLPPTensor3::sqrtn); + ClassDB::bind_method(D_METHOD("sqrtb", "A"), &MLPPTensor3::sqrtb); - ClassDB::bind_method(D_METHOD("abs"), &MLPPTensor3::abs); - ClassDB::bind_method(D_METHOD("absn"), &MLPPTensor3::absn); - ClassDB::bind_method(D_METHOD("absb", "A"), &MLPPTensor3::absb); + ClassDB::bind_method(D_METHOD("abs"), &MLPPTensor3::abs); + ClassDB::bind_method(D_METHOD("absn"), &MLPPTensor3::absn); + ClassDB::bind_method(D_METHOD("absb", "A"), &MLPPTensor3::absb); - ClassDB::bind_method(D_METHOD("max", "B"), &MLPPTensor3::max); - ClassDB::bind_method(D_METHOD("maxn", "B"), &MLPPTensor3::maxn); - ClassDB::bind_method(D_METHOD("maxb", "A", "B"), &MLPPTensor3::maxb); + ClassDB::bind_method(D_METHOD("max", "B"), &MLPPTensor3::max); + ClassDB::bind_method(D_METHOD("maxn", "B"), &MLPPTensor3::maxn); + ClassDB::bind_method(D_METHOD("maxb", "A", "B"), &MLPPTensor3::maxb); - ClassDB::bind_method(D_METHOD("flatten"), &MLPPTensor3::flatten); - ClassDB::bind_method(D_METHOD("flatteno", "out"), &MLPPTensor3::flatteno); + ClassDB::bind_method(D_METHOD("flatten"), &MLPPTensor3::flatten); + ClassDB::bind_method(D_METHOD("flatteno", "out"), &MLPPTensor3::flatteno); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_R); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_G); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_B); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_A); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_R); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_G); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_B); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_A); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_NONE); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RG); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RGB); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_GB); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_GBA); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_BA); - BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RGBA); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_NONE); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RG); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RGB); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_GB); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_GBA); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_BA); + BIND_ENUM_CONSTANT(IMAGE_CHANNEL_FLAG_RGBA); } diff --git a/lin_alg/mlpp_tensor3.h b/lin_alg/mlpp_tensor3.h index 45910d8..9c2264e 100644 --- a/lin_alg/mlpp_tensor3.h +++ b/lin_alg/mlpp_tensor3.h @@ -1,6 +1,5 @@ #ifndef MLPP_TENSOR3_H #define MLPP_TENSOR3_H -#ifndef GDNATIVE /*************************************************************************/ /* mlpp_tensor3.h */ @@ -32,6 +31,10 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else + #include "core/math/math_defs.h" #include "core/containers/pool_vector.h" @@ -42,17 +45,6 @@ #include "core/os/memory.h" #include "core/object/resource.h" - -#else - -#include "core/containers/vector.h" -#include "core/defs.h" -#include "core/math_funcs.h" -#include "core/os/memory.h" -#include "core/pool_arrays.h" - -#include "gen/resource.h" - #endif #include "mlpp_matrix.h" @@ -61,262 +53,301 @@ class Image; class MLPPTensor3 : public Resource { - GDCLASS(MLPPTensor3, Resource); + GDCLASS(MLPPTensor3, Resource); public: - Array get_data(); - void set_data(const Array &p_from); + Array get_data(); + void set_data(const Array &p_from); - _FORCE_INLINE_ real_t *ptrw() { - return _data; - } + _FORCE_INLINE_ real_t *ptrw() { return _data; } - _FORCE_INLINE_ const real_t *ptr() const { - return _data; - } + _FORCE_INLINE_ const real_t *ptr() const { return _data; } - void z_slice_add(const Vector &p_row); - void z_slice_add_pool_vector(const PoolRealArray &p_row); - void z_slice_add_mlpp_vector(const Ref &p_row); - void z_slice_add_mlpp_matrix(const Ref &p_matrix); - void z_slice_remove(int p_index); + void z_slice_add(const Vector &p_row); + void z_slice_add_pool_vector(const PoolRealArray &p_row); + void z_slice_add_mlpp_vector(const Ref &p_row); + void z_slice_add_mlpp_matrix(const Ref &p_matrix); + void z_slice_remove(int p_index); - // Removes the item copying the last value into the position of the one to - // remove. It's generally faster than `remove`. - void z_slice_remove_unordered(int p_index); + // Removes the item copying the last value into the position of the one to + // remove. It's generally faster than `remove`. + void z_slice_remove_unordered(int p_index); - void z_slice_swap(int p_index_1, int p_index_2); + void z_slice_swap(int p_index_1, int p_index_2); - _FORCE_INLINE_ void clear() { resize(Size3i()); } - _FORCE_INLINE_ void reset() { - if (_data) { - memfree(_data); - _data = NULL; - _size = Size3i(); - } - } + _FORCE_INLINE_ void clear() { resize(Size3i()); } + _FORCE_INLINE_ void reset() { + if (_data) { + memfree(_data); + _data = NULL; + _size = Size3i(); + } + } - _FORCE_INLINE_ bool empty() const { return _size == Size3i(); } - _FORCE_INLINE_ int z_slice_data_size() const { return _size.x * _size.y; } - _FORCE_INLINE_ Size2i z_slice_size() const { return Size2i(_size.x, _size.y); } - _FORCE_INLINE_ int data_size() const { return _size.x * _size.y * _size.z; } - _FORCE_INLINE_ Size3i size() const { return _size; } + _FORCE_INLINE_ bool empty() const { return _size == Size3i(); } + _FORCE_INLINE_ int z_slice_data_size() const { return _size.x * _size.y; } + _FORCE_INLINE_ Size2i z_slice_size() const { + return Size2i(_size.x, _size.y); + } + _FORCE_INLINE_ int data_size() const { return _size.x * _size.y * _size.z; } + _FORCE_INLINE_ Size3i size() const { return _size; } - void resize(const Size3i &p_size); - void shape_set(const Size3i &p_size); + void resize(const Size3i &p_size); + void shape_set(const Size3i &p_size); - _FORCE_INLINE_ int calculate_index(int p_index_z, int p_index_y, int p_index_x) const { - return p_index_y * _size.x + p_index_x + _size.x * _size.y * p_index_z; - } + _FORCE_INLINE_ int calculate_index(int p_index_z, int p_index_y, + int p_index_x) const { + return p_index_y * _size.x + p_index_x + _size.x * _size.y * p_index_z; + } - _FORCE_INLINE_ int calculate_z_slice_index(int p_index_z) const { - return _size.x * _size.y * p_index_z; - } + _FORCE_INLINE_ int calculate_z_slice_index(int p_index_z) const { + return _size.x * _size.y * p_index_z; + } - _FORCE_INLINE_ const real_t &operator[](int p_index) const { - CRASH_BAD_INDEX(p_index, data_size()); - return _data[p_index]; - } - _FORCE_INLINE_ real_t &operator[](int p_index) { - CRASH_BAD_INDEX(p_index, data_size()); - return _data[p_index]; - } + _FORCE_INLINE_ const real_t &operator[](int p_index) const { + CRASH_BAD_INDEX(p_index, data_size()); + return _data[p_index]; + } + _FORCE_INLINE_ real_t &operator[](int p_index) { + CRASH_BAD_INDEX(p_index, data_size()); + return _data[p_index]; + } - _FORCE_INLINE_ real_t element_get_index(int p_index) const { - ERR_FAIL_INDEX_V(p_index, data_size(), 0); + _FORCE_INLINE_ real_t element_get_index(int p_index) const { + ERR_FAIL_INDEX_V(p_index, data_size(), 0); - return _data[p_index]; - } + return _data[p_index]; + } - _FORCE_INLINE_ void element_set_index(int p_index, real_t p_val) { - ERR_FAIL_INDEX(p_index, data_size()); + _FORCE_INLINE_ void element_set_index(int p_index, real_t p_val) { + ERR_FAIL_INDEX(p_index, data_size()); - _data[p_index] = p_val; - } + _data[p_index] = p_val; + } - _FORCE_INLINE_ real_t element_get(int p_index_z, int p_index_y, int p_index_x) const { - ERR_FAIL_INDEX_V(p_index_x, _size.x, 0); - ERR_FAIL_INDEX_V(p_index_y, _size.y, 0); - ERR_FAIL_INDEX_V(p_index_z, _size.z, 0); + _FORCE_INLINE_ real_t element_get(int p_index_z, int p_index_y, + int p_index_x) const { + ERR_FAIL_INDEX_V(p_index_x, _size.x, 0); + ERR_FAIL_INDEX_V(p_index_y, _size.y, 0); + ERR_FAIL_INDEX_V(p_index_z, _size.z, 0); - return _data[p_index_y * _size.x + p_index_x + _size.x * _size.y * p_index_z]; - } + return _data[p_index_y * _size.x + p_index_x + + _size.x * _size.y * p_index_z]; + } - _FORCE_INLINE_ void element_set(int p_index_z, int p_index_y, int p_index_x, real_t p_val) { - ERR_FAIL_INDEX(p_index_x, _size.x); - ERR_FAIL_INDEX(p_index_y, _size.y); - ERR_FAIL_INDEX(p_index_z, _size.z); + _FORCE_INLINE_ void element_set(int p_index_z, int p_index_y, int p_index_x, + real_t p_val) { + ERR_FAIL_INDEX(p_index_x, _size.x); + ERR_FAIL_INDEX(p_index_y, _size.y); + ERR_FAIL_INDEX(p_index_z, _size.z); - _data[p_index_y * _size.x + p_index_x + _size.x * _size.y * p_index_z] = p_val; - } + _data[p_index_y * _size.x + p_index_x + _size.x * _size.y * p_index_z] = + p_val; + } - Vector row_get_vector(int p_index_z, int p_index_y) const; - PoolRealArray row_get_pool_vector(int p_index_z, int p_index_y) const; - Ref row_get_mlpp_vector(int p_index_z, int p_index_y) const; - void row_get_into_mlpp_vector(int p_index_z, int p_index_y, Ref target) const; + Vector row_get_vector(int p_index_z, int p_index_y) const; + PoolRealArray row_get_pool_vector(int p_index_z, int p_index_y) const; + Ref row_get_mlpp_vector(int p_index_z, int p_index_y) const; + void row_get_into_mlpp_vector(int p_index_z, int p_index_y, + Ref target) const; - void row_set_vector(int p_index_z, int p_index_y, const Vector &p_row); - void row_set_pool_vector(int p_index_z, int p_index_y, const PoolRealArray &p_row); - void row_set_mlpp_vector(int p_index_z, int p_index_y, const Ref &p_row); + void row_set_vector(int p_index_z, int p_index_y, + const Vector &p_row); + void row_set_pool_vector(int p_index_z, int p_index_y, + const PoolRealArray &p_row); + void row_set_mlpp_vector(int p_index_z, int p_index_y, + const Ref &p_row); - Vector z_slice_get_vector(int p_index_z) const; - PoolRealArray z_slice_get_pool_vector(int p_index_z) const; - Ref z_slice_get_mlpp_vector(int p_index_z) const; - void z_slice_get_into_mlpp_vector(int p_index_z, Ref target) const; - Ref z_slice_get_mlpp_matrix(int p_index_z) const; - void z_slice_get_into_mlpp_matrix(int p_index_z, Ref target) const; + Vector z_slice_get_vector(int p_index_z) const; + PoolRealArray z_slice_get_pool_vector(int p_index_z) const; + Ref z_slice_get_mlpp_vector(int p_index_z) const; + void z_slice_get_into_mlpp_vector(int p_index_z, + Ref target) const; + Ref z_slice_get_mlpp_matrix(int p_index_z) const; + void z_slice_get_into_mlpp_matrix(int p_index_z, + Ref target) const; - void z_slice_set_vector(int p_index_z, const Vector &p_row); - void z_slice_set_pool_vector(int p_index_z, const PoolRealArray &p_row); - void z_slice_set_mlpp_vector(int p_index_z, const Ref &p_row); - void z_slice_set_mlpp_matrix(int p_index_z, const Ref &p_mat); + void z_slice_set_vector(int p_index_z, const Vector &p_row); + void z_slice_set_pool_vector(int p_index_z, const PoolRealArray &p_row); + void z_slice_set_mlpp_vector(int p_index_z, const Ref &p_row); + void z_slice_set_mlpp_matrix(int p_index_z, const Ref &p_mat); - //TODO resize() need to be reworked for add and remove to work, in any other direction than z - //void x_slice_add(const Ref &p_matrix); - //void x_slice_remove(int p_index); - void x_slice_get_into(int p_index_x, Ref target) const; - Ref x_slice_get(int p_index_x) const; - void x_slice_set(int p_index_x, const Ref &p_mat); + // TODO resize() need to be reworked for add and remove to work, in any other + // direction than z void x_slice_add(const Ref &p_matrix); void + // x_slice_remove(int p_index); + void x_slice_get_into(int p_index_x, Ref target) const; + Ref x_slice_get(int p_index_x) const; + void x_slice_set(int p_index_x, const Ref &p_mat); - //void y_slice_add(const Ref &p_matrix); - //void y_slice_remove(int p_index); - void y_slice_get_into(int p_index_y, Ref target) const; - Ref y_slice_get(int p_index_y) const; - void y_slice_set(int p_index_y, const Ref &p_mat); + // void y_slice_add(const Ref &p_matrix); + // void y_slice_remove(int p_index); + void y_slice_get_into(int p_index_y, Ref target) const; + Ref y_slice_get(int p_index_y) const; + void y_slice_set(int p_index_y, const Ref &p_mat); public: - //Image api + // Image api - enum ImageChannelFlags { - IMAGE_CHANNEL_FLAG_R = 1 << 0, - IMAGE_CHANNEL_FLAG_G = 1 << 1, - IMAGE_CHANNEL_FLAG_B = 1 << 2, - IMAGE_CHANNEL_FLAG_A = 1 << 3, + enum ImageChannelFlags { + IMAGE_CHANNEL_FLAG_R = 1 << 0, + IMAGE_CHANNEL_FLAG_G = 1 << 1, + IMAGE_CHANNEL_FLAG_B = 1 << 2, + IMAGE_CHANNEL_FLAG_A = 1 << 3, - IMAGE_CHANNEL_FLAG_NONE = 0, - IMAGE_CHANNEL_FLAG_RG = IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G, - IMAGE_CHANNEL_FLAG_RGB = IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B, - IMAGE_CHANNEL_FLAG_GB = IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B, - IMAGE_CHANNEL_FLAG_GBA = IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, - IMAGE_CHANNEL_FLAG_BA = IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, - IMAGE_CHANNEL_FLAG_RGBA = IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, - }; + IMAGE_CHANNEL_FLAG_NONE = 0, + IMAGE_CHANNEL_FLAG_RG = IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G, + IMAGE_CHANNEL_FLAG_RGB = + IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B, + IMAGE_CHANNEL_FLAG_GB = IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B, + IMAGE_CHANNEL_FLAG_GBA = + IMAGE_CHANNEL_FLAG_G | IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, + IMAGE_CHANNEL_FLAG_BA = IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, + IMAGE_CHANNEL_FLAG_RGBA = IMAGE_CHANNEL_FLAG_R | IMAGE_CHANNEL_FLAG_G | + IMAGE_CHANNEL_FLAG_B | IMAGE_CHANNEL_FLAG_A, + }; - void z_slices_add_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); + void z_slices_add_image(const Ref &p_img, + const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); - Ref z_slice_get_image(const int p_index_z) const; - Ref z_slices_get_image(const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1) const; + Ref z_slice_get_image(const int p_index_z) const; + Ref z_slices_get_image(const int p_index_r = -1, + const int p_index_g = -1, + const int p_index_b = -1, + const int p_index_a = -1) const; - void z_slice_get_into_image(Ref p_target, const int p_index_z, const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; - void z_slices_get_into_image(Ref p_target, const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1) const; + void z_slice_get_into_image( + Ref p_target, const int p_index_z, + const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; + void z_slices_get_into_image(Ref p_target, const int p_index_r = -1, + const int p_index_g = -1, + const int p_index_b = -1, + const int p_index_a = -1) const; - void z_slice_set_image(const Ref &p_img, const int p_index_z, const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); - void z_slices_set_image(const Ref &p_img, const int p_index_r = -1, const int p_index_g = -1, const int p_index_b = -1, const int p_index_a = -1); + void z_slice_set_image(const Ref &p_img, const int p_index_z, + const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); + void z_slices_set_image(const Ref &p_img, const int p_index_r = -1, + const int p_index_g = -1, const int p_index_b = -1, + const int p_index_a = -1); - void set_from_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); + void set_from_image(const Ref &p_img, + const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); - //void x_slices_add_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); - Ref x_slice_get_image(const int p_index_x) const; - void x_slice_get_into_image(Ref p_target, const int p_index_x, const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; - void x_slice_set_image(const Ref &p_img, const int p_index_x, const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); + // void x_slices_add_image(const Ref &p_img, const int p_channels = + // IMAGE_CHANNEL_FLAG_RGBA); + Ref x_slice_get_image(const int p_index_x) const; + void x_slice_get_into_image( + Ref p_target, const int p_index_x, + const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; + void x_slice_set_image(const Ref &p_img, const int p_index_x, + const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); - //void y_slices_add_image(const Ref &p_img, const int p_channels = IMAGE_CHANNEL_FLAG_RGBA); - Ref y_slice_get_image(const int p_index_y) const; - void y_slice_get_into_image(Ref p_target, const int p_index_y, const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; - void y_slice_set_image(const Ref &p_img, const int p_index_y, const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); + // void y_slices_add_image(const Ref &p_img, const int p_channels = + // IMAGE_CHANNEL_FLAG_RGBA); + Ref y_slice_get_image(const int p_index_y) const; + void y_slice_get_into_image( + Ref p_target, const int p_index_y, + const int p_target_channels = IMAGE_CHANNEL_FLAG_RGB) const; + void y_slice_set_image(const Ref &p_img, const int p_index_y, + const int p_image_channel_flag = IMAGE_CHANNEL_FLAG_R); public: - //math api + // math api - void add(const Ref &B); - Ref addn(const Ref &B) const; - void addb(const Ref &A, const Ref &B); + void add(const Ref &B); + Ref addn(const Ref &B) const; + void addb(const Ref &A, const Ref &B); - void sub(const Ref &B); - Ref subn(const Ref &B) const; - void subb(const Ref &A, const Ref &B); + void sub(const Ref &B); + Ref subn(const Ref &B) const; + void subb(const Ref &A, const Ref &B); - void division_element_wise(const Ref &B); - Ref division_element_wisen(const Ref &B) const; - void division_element_wiseb(const Ref &A, const Ref &B); + void division_element_wise(const Ref &B); + Ref division_element_wisen(const Ref &B) const; + void division_element_wiseb(const Ref &A, + const Ref &B); - void sqrt(); - Ref sqrtn() const; - void sqrtb(const Ref &A); + void sqrt(); + Ref sqrtn() const; + void sqrtb(const Ref &A); - void exponentiate(real_t p); - Ref exponentiaten(real_t p) const; - void exponentiateb(const Ref &A, real_t p); + void exponentiate(real_t p); + Ref exponentiaten(real_t p) const; + void exponentiateb(const Ref &A, real_t p); - void scalar_multiply(const real_t scalar); - Ref scalar_multiplyn(const real_t scalar) const; - void scalar_multiplyb(const real_t scalar, const Ref &A); + void scalar_multiply(const real_t scalar); + Ref scalar_multiplyn(const real_t scalar) const; + void scalar_multiplyb(const real_t scalar, const Ref &A); - void scalar_add(const real_t scalar); - Ref scalar_addn(const real_t scalar) const; - void scalar_addb(const real_t scalar, const Ref &A); + void scalar_add(const real_t scalar); + Ref scalar_addn(const real_t scalar) const; + void scalar_addb(const real_t scalar, const Ref &A); - void hadamard_product(const Ref &B); - Ref hadamard_productn(const Ref &B) const; - void hadamard_productb(const Ref &A, const Ref &B); + void hadamard_product(const Ref &B); + Ref hadamard_productn(const Ref &B) const; + void hadamard_productb(const Ref &A, const Ref &B); - void max(const Ref &B); - Ref maxn(const Ref &B) const; - void maxb(const Ref &A, const Ref &B); + void max(const Ref &B); + Ref maxn(const Ref &B) const; + void maxb(const Ref &A, const Ref &B); - void abs(); - Ref absn() const; - void absb(const Ref &A); + void abs(); + Ref absn() const; + void absb(const Ref &A); - Ref flatten() const; - void flatteno(Ref out) const; + Ref flatten() const; + void flatteno(Ref out) const; - //real_t norm_2(std::vector>> A); + // real_t norm_2(std::vector>> A); - Ref tensor_vec_mult(const Ref &b); - //std::vector>> vector_wise_tensor_product(std::vector>> A, std::vector> B); + Ref tensor_vec_mult(const Ref &b); + // std::vector>> + // vector_wise_tensor_product(std::vector>> A, + // std::vector> B); public: - void fill(real_t p_val); + void fill(real_t p_val); - Vector to_flat_vector() const; - PoolRealArray to_flat_pool_vector() const; - Vector to_flat_byte_array() const; + Vector to_flat_vector() const; + PoolRealArray to_flat_pool_vector() const; + Vector to_flat_byte_array() const; - Ref duplicate_fast() const; + Ref duplicate_fast() const; - void set_from_mlpp_tensor3(const Ref &p_from); - void set_from_mlpp_tensor3r(const MLPPTensor3 &p_from); + void set_from_mlpp_tensor3(const Ref &p_from); + void set_from_mlpp_tensor3r(const MLPPTensor3 &p_from); - void set_from_mlpp_matrix(const Ref &p_from); - void set_from_mlpp_matrixr(const MLPPMatrix &p_from); - void set_from_mlpp_vectors(const Vector> &p_from); - void set_from_mlpp_matricess(const Vector> &p_from); + void set_from_mlpp_matrix(const Ref &p_from); + void set_from_mlpp_matrixr(const MLPPMatrix &p_from); + void set_from_mlpp_vectors(const Vector> &p_from); + void set_from_mlpp_matricess(const Vector> &p_from); - void set_from_mlpp_vectors_array(const Array &p_from); - void set_from_mlpp_matrices_array(const Array &p_from); + void set_from_mlpp_vectors_array(const Array &p_from); + void set_from_mlpp_matrices_array(const Array &p_from); - bool is_equal_approx(const Ref &p_with, real_t tolerance = static_cast(CMP_EPSILON)) const; + bool + is_equal_approx(const Ref &p_with, + real_t tolerance = static_cast(CMP_EPSILON)) const; - String to_string(); + String to_string(); - MLPPTensor3(); - MLPPTensor3(const MLPPMatrix &p_from); - MLPPTensor3(const Array &p_from); - ~MLPPTensor3(); + MLPPTensor3(); + MLPPTensor3(const MLPPMatrix &p_from); + MLPPTensor3(const Array &p_from); + ~MLPPTensor3(); - // TODO: These are temporary - std::vector to_flat_std_vector() const; - void set_from_std_vectors(const std::vector>> &p_from); - std::vector>> to_std_vector(); - MLPPTensor3(const std::vector>> &p_from); + // TODO: These are temporary + std::vector to_flat_std_vector() const; + void set_from_std_vectors( + const std::vector>> &p_from); + std::vector>> to_std_vector(); + MLPPTensor3(const std::vector>> &p_from); protected: - static void _bind_methods(); + static void _bind_methods(); protected: - Size3i _size; - real_t *_data; + Size3i _size; + real_t *_data; }; VARIANT_ENUM_CAST(MLPPTensor3::ImageChannelFlags); diff --git a/lin_alg/mlpp_vector.h b/lin_alg/mlpp_vector.h index f654d28..d307eb3 100644 --- a/lin_alg/mlpp_vector.h +++ b/lin_alg/mlpp_vector.h @@ -1,6 +1,5 @@ #ifndef MLPP_VECTOR_H #define MLPP_VECTOR_H -#ifndef GDNATIVE /*************************************************************************/ /* mlpp_vector.h */ @@ -32,6 +31,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/math/math_funcs.h" @@ -43,16 +45,6 @@ #include "core/object/resource.h" -#else - -#include "core/containers/vector.h" -#include "core/defs.h" -#include "core/math_funcs.h" -#include "core/os/memory.h" -#include "core/pool_arrays.h" - -#include "gen/resource.h" - #endif //REMOVE diff --git a/lin_reg/lin_reg.h b/lin_reg/lin_reg.h index c68fac0..98effb6 100644 --- a/lin_reg/lin_reg.h +++ b/lin_reg/lin_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/log_reg/log_reg.h b/log_reg/log_reg.h index e32342a..df46ac3 100644 --- a/log_reg/log_reg.h +++ b/log_reg/log_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/mann/mann.cpp b/mann/mann.cpp index 04d6d6d..73a0f1d 100644 --- a/mann/mann.cpp +++ b/mann/mann.cpp @@ -30,7 +30,11 @@ #include "mann.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include "../activation/activation.h" #include "../cost/cost.h" diff --git a/mann/mann.h b/mann/mann.h index 1ea895b..9420c47 100644 --- a/mann/mann.h +++ b/mann/mann.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../regularization/reg.h" diff --git a/mlp/mlp.cpp b/mlp/mlp.cpp index 6b07579..e54177e 100644 --- a/mlp/mlp.cpp +++ b/mlp/mlp.cpp @@ -30,7 +30,11 @@ #include "mlp.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include "../activation/activation.h" #include "../cost/cost.h" diff --git a/mlp/mlp.h b/mlp/mlp.h index 5048161..bd0916d 100644 --- a/mlp/mlp.h +++ b/mlp/mlp.h @@ -31,12 +31,16 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/vector.h" #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/variant/variant.h" #include "core/object/reference.h" +#endif #include "../regularization/reg.h" diff --git a/multi_output_layer/multi_output_layer.cpp b/multi_output_layer/multi_output_layer.cpp index 6cb941d..5d9c54b 100644 --- a/multi_output_layer/multi_output_layer.cpp +++ b/multi_output_layer/multi_output_layer.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "multi_output_layer.h" + #include "../utilities/utilities.h" int MLPPMultiOutputLayer::get_n_output() { diff --git a/multi_output_layer/multi_output_layer.h b/multi_output_layer/multi_output_layer.h index 0c63fc3..52842a2 100644 --- a/multi_output_layer/multi_output_layer.h +++ b/multi_output_layer/multi_output_layer.h @@ -31,10 +31,14 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/object/reference.h" +#endif #include "../activation/activation.h" #include "../cost/cost.h" diff --git a/multinomial_nb/multinomial_nb.cpp b/multinomial_nb/multinomial_nb.cpp index ae83edb..e2e27f3 100644 --- a/multinomial_nb/multinomial_nb.cpp +++ b/multinomial_nb/multinomial_nb.cpp @@ -30,7 +30,11 @@ #include "multinomial_nb.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/local_vector.h" +#endif #include "../utilities/utilities.h" diff --git a/multinomial_nb/multinomial_nb.h b/multinomial_nb/multinomial_nb.h index 3c3c0c0..a7035f7 100644 --- a/multinomial_nb/multinomial_nb.h +++ b/multinomial_nb/multinomial_nb.h @@ -31,11 +31,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/hash_map.h" #include "core/containers/vector.h" #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/numerical_analysis/numerical_analysis.cpp b/numerical_analysis/numerical_analysis.cpp index 55f1629..a6c20a9 100644 --- a/numerical_analysis/numerical_analysis.cpp +++ b/numerical_analysis/numerical_analysis.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "numerical_analysis.h" + #include "../lin_alg/lin_alg.h" #include "../lin_alg/mlpp_matrix.h" diff --git a/numerical_analysis/numerical_analysis.h b/numerical_analysis/numerical_analysis.h index 682f663..abd0c57 100644 --- a/numerical_analysis/numerical_analysis.h +++ b/numerical_analysis/numerical_analysis.h @@ -36,12 +36,16 @@ // // +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" #include "core/containers/vector.h" #include "core/string/ustring.h" +#endif class MLPPVector; class MLPPMatrix; diff --git a/outlier_finder/outlier_finder.h b/outlier_finder/outlier_finder.h index 66654ac..9dd2bbd 100644 --- a/outlier_finder/outlier_finder.h +++ b/outlier_finder/outlier_finder.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/output_layer/output_layer.cpp b/output_layer/output_layer.cpp index 2e6fee0..f7b419b 100644 --- a/output_layer/output_layer.cpp +++ b/output_layer/output_layer.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "output_layer.h" + #include "../utilities/utilities.h" int MLPPOutputLayer::get_n_hidden() { diff --git a/output_layer/output_layer.h b/output_layer/output_layer.h index a8227ae..7ae5595 100644 --- a/output_layer/output_layer.h +++ b/output_layer/output_layer.h @@ -31,10 +31,14 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/object/reference.h" +#endif #include "../activation/activation.h" #include "../cost/cost.h" diff --git a/pca/pca.cpp b/pca/pca.cpp index 3dbe851..2ed746b 100644 --- a/pca/pca.cpp +++ b/pca/pca.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "pca.h" + #include "../data/data.h" Ref MLPPPCA::get_input_set() { diff --git a/pca/pca.h b/pca/pca.h index f081a8e..d8aa9ea 100644 --- a/pca/pca.h +++ b/pca/pca.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/probit_reg/probit_reg.h b/probit_reg/probit_reg.h index f064a85..1bc7fe8 100644 --- a/probit_reg/probit_reg.h +++ b/probit_reg/probit_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/regularization/reg.cpp b/regularization/reg.cpp index c55e0bf..d652efb 100644 --- a/regularization/reg.cpp +++ b/regularization/reg.cpp @@ -30,7 +30,11 @@ #include "reg.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" +#endif #include "../activation/activation.h" #include "../lin_alg/lin_alg.h" diff --git a/regularization/reg.h b/regularization/reg.h index 3d4f6e7..84aded9 100644 --- a/regularization/reg.h +++ b/regularization/reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/softmax_net/softmax_net.cpp b/softmax_net/softmax_net.cpp index 87b241f..2bae7ce 100644 --- a/softmax_net/softmax_net.cpp +++ b/softmax_net/softmax_net.cpp @@ -36,7 +36,11 @@ #include "../regularization/reg.h" #include "../utilities/utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include diff --git a/softmax_net/softmax_net.h b/softmax_net/softmax_net.h index b39da29..62177af 100644 --- a/softmax_net/softmax_net.h +++ b/softmax_net/softmax_net.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/softmax_reg/softmax_reg.h b/softmax_reg/softmax_reg.h index 8678d53..3b4a5cb 100644 --- a/softmax_reg/softmax_reg.h +++ b/softmax_reg/softmax_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/stat/stat.cpp b/stat/stat.cpp index 0535835..aa8d6b9 100644 --- a/stat/stat.cpp +++ b/stat/stat.cpp @@ -29,10 +29,16 @@ /*************************************************************************/ #include "stat.h" + #include "../activation/activation.h" #include "../data/data.h" #include "../lin_alg/lin_alg.h" + +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/hash_map.h" +#endif #include #include diff --git a/stat/stat.h b/stat/stat.h index f22e445..fd64805 100644 --- a/stat/stat.h +++ b/stat/stat.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/svc/svc.h b/svc/svc.h index 9777db7..aff57cb 100644 --- a/svc/svc.h +++ b/svc/svc.h @@ -34,9 +34,13 @@ // https://towardsdatascience.com/svm-implementation-from-scratch-python-2db2fc52e5c2 // Illustratd a practical definition of the Hinge Loss function and its gradient when optimizing with SGD. +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/tanh_reg/tanh_reg.h b/tanh_reg/tanh_reg.h index 8112975..0360b59 100644 --- a/tanh_reg/tanh_reg.h +++ b/tanh_reg/tanh_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/test/mlpp_matrix_tests.cpp b/test/mlpp_matrix_tests.cpp index f21185b..6f26ad1 100644 --- a/test/mlpp_matrix_tests.cpp +++ b/test/mlpp_matrix_tests.cpp @@ -30,7 +30,11 @@ #include "mlpp_matrix_tests.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#endif #include "../lin_alg/mlpp_matrix.h" diff --git a/test/mlpp_matrix_tests.h b/test/mlpp_matrix_tests.h index 4781415..cc3490a 100644 --- a/test/mlpp_matrix_tests.h +++ b/test/mlpp_matrix_tests.h @@ -31,6 +31,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/containers/vector.h" @@ -38,6 +41,7 @@ #include "core/object/reference.h" #include "core/string/ustring.h" +#endif #include "mlpp_tests.h" diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index f978913..a6dddd3 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -30,9 +30,13 @@ #include "mlpp_tests.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_funcs.h" #include "core/log/logger.h" +#endif //TODO remove #include diff --git a/test/mlpp_tests.h b/test/mlpp_tests.h index 2f822c3..ab150cb 100644 --- a/test/mlpp_tests.h +++ b/test/mlpp_tests.h @@ -31,6 +31,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/containers/vector.h" @@ -38,6 +41,7 @@ #include "core/object/reference.h" #include "core/string/ustring.h" +#endif // TODO port this class to use the test module once it's working // Also don't forget to remove it's bindings diff --git a/transforms/transforms.cpp b/transforms/transforms.cpp index 81b6999..e6d7a39 100644 --- a/transforms/transforms.cpp +++ b/transforms/transforms.cpp @@ -29,9 +29,14 @@ /*************************************************************************/ #include "transforms.h" + #include "../lin_alg/lin_alg.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_funcs.h" +#endif // DCT ii. // https://www.mathworks.com/help/images/discrete-cosine-transform.html diff --git a/transforms/transforms.h b/transforms/transforms.h index f06e7f3..59a3661 100644 --- a/transforms/transforms.h +++ b/transforms/transforms.h @@ -36,9 +36,13 @@ // // +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" diff --git a/uni_lin_reg/uni_lin_reg.h b/uni_lin_reg/uni_lin_reg.h index d0a7a80..7676f6d 100644 --- a/uni_lin_reg/uni_lin_reg.h +++ b/uni_lin_reg/uni_lin_reg.h @@ -31,9 +31,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/math/math_defs.h" #include "core/object/resource.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/utilities/utilities.cpp b/utilities/utilities.cpp index 9dff10f..288ba48 100644 --- a/utilities/utilities.cpp +++ b/utilities/utilities.cpp @@ -30,9 +30,13 @@ #include "utilities.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" #include "core/math/math_funcs.h" #include "core/math/random_pcg.h" +#endif #include #include diff --git a/utilities/utilities.h b/utilities/utilities.h index 8aff3f5..e4d31cc 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -31,12 +31,16 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/vector.h" #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/variant/variant.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_vector.h" diff --git a/wgan/wgan.cpp b/wgan/wgan.cpp index 66a29bd..339aadd 100644 --- a/wgan/wgan.cpp +++ b/wgan/wgan.cpp @@ -30,451 +30,555 @@ #include "wgan.h" +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/log/logger.h" +#include "core/object/method_bind_ext.gen.inc" +#endif + #include "../activation/activation.h" #include "../cost/cost.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" -#include "core/object/method_bind_ext.gen.inc" +Ref MLPPWGAN::get_output_set() { return _output_set; } +void MLPPWGAN::set_output_set(const Ref &val) { _output_set = val; } -Ref MLPPWGAN::get_output_set() { - return _output_set; -} -void MLPPWGAN::set_output_set(const Ref &val) { - _output_set = val; -} - -int MLPPWGAN::get_k() const { - return _k; -} -void MLPPWGAN::set_k(const int val) { - _k = val; -} +int MLPPWGAN::get_k() const { return _k; } +void MLPPWGAN::set_k(const int val) { _k = val; } Ref MLPPWGAN::generate_example(int n) { - return model_set_test_generator(MLPPMatrix::create_gaussian_noise(n, _k)); + return model_set_test_generator(MLPPMatrix::create_gaussian_noise(n, _k)); } void MLPPWGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { - //MLPPCost mlpp_cost; - real_t cost_prev = 0; - int epoch = 1; - int n = _output_set->size().y; + // MLPPCost mlpp_cost; + real_t cost_prev = 0; + int epoch = 1; + int n = _output_set->size().y; - forward_pass(); + forward_pass(); - const int CRITIC_INTERATIONS = 5; // Wasserstein GAN specific parameter. + const int CRITIC_INTERATIONS = 5; // Wasserstein GAN specific parameter. - while (true) { - cost_prev = cost(_y_hat, MLPPVector::create_vec_one(n)); + while (true) { + cost_prev = cost(_y_hat, MLPPVector::create_vec_one(n)); - Ref generator_input_set; - Ref discriminator_input_set; - discriminator_input_set.instance(); + Ref generator_input_set; + Ref discriminator_input_set; + discriminator_input_set.instance(); - Ref ly_hat; - Ref loutput_set; + Ref ly_hat; + Ref loutput_set; - // Training of the discriminator. - for (int i = 0; i < CRITIC_INTERATIONS; i++) { - generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k); - discriminator_input_set->set_from_mlpp_matrix(model_set_test_generator(generator_input_set)); - discriminator_input_set->rows_add_mlpp_matrix(_output_set); // Fake + real inputs. + // Training of the discriminator. + for (int i = 0; i < CRITIC_INTERATIONS; i++) { + generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k); + discriminator_input_set->set_from_mlpp_matrix( + model_set_test_generator(generator_input_set)); + discriminator_input_set->rows_add_mlpp_matrix( + _output_set); // Fake + real inputs. - ly_hat = model_set_test_discriminator(discriminator_input_set); - loutput_set = MLPPVector::create_vec_one(n)->scalar_multiplyn(-1); // WGAN changes y_i = 1 and y_i = 0 to y_i = 1 and y_i = -1 - Ref output_set_real = MLPPVector::create_vec_one(n); - loutput_set->append_mlpp_vector(output_set_real); // Fake + real output scores. + ly_hat = model_set_test_discriminator(discriminator_input_set); + loutput_set = MLPPVector::create_vec_one(n)->scalar_multiplyn( + -1); // WGAN changes y_i = 1 and y_i = 0 to y_i = 1 and y_i = -1 + Ref output_set_real = MLPPVector::create_vec_one(n); + loutput_set->append_mlpp_vector( + output_set_real); // Fake + real output scores. - DiscriminatorGradientResult discriminator_gradient_results = compute_discriminator_gradients(ly_hat, loutput_set); - Vector> cumulative_discriminator_hidden_layer_w_grad = discriminator_gradient_results.cumulative_hidden_layer_w_grad; - Ref output_discriminator_w_grad = discriminator_gradient_results.output_w_grad; + DiscriminatorGradientResult discriminator_gradient_results = + compute_discriminator_gradients(ly_hat, loutput_set); + Vector> cumulative_discriminator_hidden_layer_w_grad = + discriminator_gradient_results.cumulative_hidden_layer_w_grad; + Ref output_discriminator_w_grad = + discriminator_gradient_results.output_w_grad; - real_t lrpn = learning_rate / n; + real_t lrpn = learning_rate / n; - for (int j = 0; j < cumulative_discriminator_hidden_layer_w_grad.size(); ++j) { - cumulative_discriminator_hidden_layer_w_grad.write[j]->scalar_multiply(lrpn); - } + for (int j = 0; j < cumulative_discriminator_hidden_layer_w_grad.size(); + ++j) { + cumulative_discriminator_hidden_layer_w_grad.write[j]->scalar_multiply( + lrpn); + } - output_discriminator_w_grad->scalar_multiply(learning_rate / n); - update_discriminator_parameters(cumulative_discriminator_hidden_layer_w_grad, output_discriminator_w_grad, learning_rate); - } + output_discriminator_w_grad->scalar_multiply(learning_rate / n); + update_discriminator_parameters( + cumulative_discriminator_hidden_layer_w_grad, + output_discriminator_w_grad, learning_rate); + } - // Training of the generator. - generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k); - discriminator_input_set->set_from_mlpp_matrix(model_set_test_generator(generator_input_set)); - ly_hat = model_set_test_discriminator(discriminator_input_set); - loutput_set = MLPPVector::create_vec_one(n); + // Training of the generator. + generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k); + discriminator_input_set->set_from_mlpp_matrix( + model_set_test_generator(generator_input_set)); + ly_hat = model_set_test_discriminator(discriminator_input_set); + loutput_set = MLPPVector::create_vec_one(n); - Vector> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(_y_hat, loutput_set); + Vector> cumulative_generator_hidden_layer_w_grad = + compute_generator_gradients(_y_hat, loutput_set); - real_t lrpn = learning_rate / n; + real_t lrpn = learning_rate / n; - for (int i = 0; i < cumulative_generator_hidden_layer_w_grad.size(); ++i) { - cumulative_generator_hidden_layer_w_grad.write[i]->scalar_multiply(lrpn); - } + for (int i = 0; i < cumulative_generator_hidden_layer_w_grad.size(); ++i) { + cumulative_generator_hidden_layer_w_grad.write[i]->scalar_multiply(lrpn); + } - update_generator_parameters(cumulative_generator_hidden_layer_w_grad, learning_rate); + update_generator_parameters(cumulative_generator_hidden_layer_w_grad, + learning_rate); - forward_pass(); + forward_pass(); - if (ui) { - handle_ui(epoch, cost_prev, _y_hat, MLPPVector::create_vec_one(n)); - } + if (ui) { + handle_ui(epoch, cost_prev, _y_hat, MLPPVector::create_vec_one(n)); + } - epoch++; - if (epoch > max_epoch) { - break; - } - } + epoch++; + if (epoch > max_epoch) { + break; + } + } } real_t MLPPWGAN::score() { - MLPPUtilities util; - forward_pass(); - int n = _output_set->size().y; + MLPPUtilities util; + forward_pass(); + int n = _output_set->size().y; - return util.performance_vec(_y_hat, MLPPVector::create_vec_one(n)); + return util.performance_vec(_y_hat, MLPPVector::create_vec_one(n)); } void MLPPWGAN::save(const String &file_name) { - MLPPUtilities util; + MLPPUtilities util; - /* - if (!network.empty()) { - util.saveParameters(file_name, network[0].weights, network[0].bias, 0, 1); - for (uint32_t i = 1; i < network.size(); i++) { - util.saveParameters(fileName, network[i].weights, network[i].bias, 1, i + 1); - } - util.saveParameters(file_name, outputLayer->weights, outputLayer->bias, 1, network.size() + 1); - } else { - util.saveParameters(file_name, outputLayer->weights, outputLayer->bias, 0, network.size() + 1); - } - */ + /* + if (!network.empty()) { + util.saveParameters(file_name, network[0].weights, network[0].bias, 0, + 1); for (uint32_t i = 1; i < network.size(); i++) { + util.saveParameters(fileName, network[i].weights, + network[i].bias, 1, i + 1); + } + util.saveParameters(file_name, outputLayer->weights, + outputLayer->bias, 1, network.size() + 1); } else { + util.saveParameters(file_name, outputLayer->weights, + outputLayer->bias, 0, network.size() + 1); + } + */ } -void MLPPWGAN::create_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) { - Ref layer; - layer.instance(); +void MLPPWGAN::create_layer(int n_hidden, + MLPPActivation::ActivationFunction activation, + MLPPUtilities::WeightDistributionType weight_init, + MLPPReg::RegularizationType reg, real_t lambda, + real_t alpha) { + Ref layer; + layer.instance(); - layer->set_n_hidden(n_hidden); - layer->set_activation(activation); - layer->set_weight_init(weight_init); - layer->set_reg(reg); - layer->set_lambda(lambda); - layer->set_alpha(alpha); + layer->set_n_hidden(n_hidden); + layer->set_activation(activation); + layer->set_weight_init(weight_init); + layer->set_reg(reg); + layer->set_lambda(lambda); + layer->set_alpha(alpha); - int n = _output_set->size().y; + int n = _output_set->size().y; - if (_network.empty()) { - layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); - } else { - layer->set_input(_network.write[_network.size() - 1]->get_a()); - } + if (_network.empty()) { + layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); + } else { + layer->set_input(_network.write[_network.size() - 1]->get_a()); + } - _network.push_back(layer); - layer->forward_pass(); + _network.push_back(layer); + layer->forward_pass(); } void MLPPWGAN::add_layer(Ref layer) { - if (!layer.is_valid()) { - return; - } + if (!layer.is_valid()) { + return; + } - if (_network.empty()) { - int n = _output_set->size().y; + if (_network.empty()) { + int n = _output_set->size().y; - layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); - } else { - layer->set_input(_network.write[_network.size() - 1]->get_a()); - } + layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); + } else { + layer->set_input(_network.write[_network.size() - 1]->get_a()); + } - _network.push_back(layer); - layer->forward_pass(); + _network.push_back(layer); + layer->forward_pass(); } Ref MLPPWGAN::get_layer(const int index) { - ERR_FAIL_INDEX_V(index, _network.size(), Ref()); + ERR_FAIL_INDEX_V(index, _network.size(), Ref()); - return _network[index]; + return _network[index]; } void MLPPWGAN::remove_layer(const int index) { - ERR_FAIL_INDEX(index, _network.size()); + ERR_FAIL_INDEX(index, _network.size()); - _network.remove(index); -} -int MLPPWGAN::get_layer_count() const { - return _network.size(); + _network.remove(index); } +int MLPPWGAN::get_layer_count() const { return _network.size(); } -void MLPPWGAN::add_output_layer(MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) { - ERR_FAIL_COND(_network.empty()); +void MLPPWGAN::add_output_layer( + MLPPUtilities::WeightDistributionType weight_init, + MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) { + ERR_FAIL_COND(_network.empty()); - if (!_output_layer.is_valid()) { - _output_layer.instance(); - } + if (!_output_layer.is_valid()) { + _output_layer.instance(); + } - _output_layer->set_n_hidden(_network[_network.size() - 1]->get_n_hidden()); - _output_layer->set_activation(MLPPActivation::ACTIVATION_FUNCTION_LINEAR); - _output_layer->set_cost(MLPPCost::COST_TYPE_WASSERSTEIN_LOSS); - _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); - _output_layer->set_weight_init(weight_init); - _output_layer->set_lambda(lambda); - _output_layer->set_alpha(alpha); + _output_layer->set_n_hidden(_network[_network.size() - 1]->get_n_hidden()); + _output_layer->set_activation(MLPPActivation::ACTIVATION_FUNCTION_LINEAR); + _output_layer->set_cost(MLPPCost::COST_TYPE_WASSERSTEIN_LOSS); + _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); + _output_layer->set_weight_init(weight_init); + _output_layer->set_lambda(lambda); + _output_layer->set_alpha(alpha); } MLPPWGAN::MLPPWGAN(int p_k, const Ref &p_output_set) { - _output_set = p_output_set; - _k = p_k; + _output_set = p_output_set; + _k = p_k; - _y_hat.instance(); + _y_hat.instance(); } MLPPWGAN::MLPPWGAN() { - _k = 0; + _k = 0; - _y_hat.instance(); + _y_hat.instance(); } -MLPPWGAN::~MLPPWGAN() { -} +MLPPWGAN::~MLPPWGAN() {} Ref MLPPWGAN::model_set_test_generator(const Ref &X) { - if (!_network.empty()) { - _network.write[0]->set_input(X); - _network.write[0]->forward_pass(); + if (!_network.empty()) { + _network.write[0]->set_input(X); + _network.write[0]->forward_pass(); - for (int i = 1; i <= _network.size() / 2; ++i) { - _network.write[i]->set_input(_network.write[i - 1]->get_a()); - _network.write[i]->forward_pass(); - } - } + for (int i = 1; i <= _network.size() / 2; ++i) { + _network.write[i]->set_input(_network.write[i - 1]->get_a()); + _network.write[i]->forward_pass(); + } + } - return _network.write[_network.size() / 2]->get_a(); + return _network.write[_network.size() / 2]->get_a(); } -Ref MLPPWGAN::model_set_test_discriminator(const Ref &X) { - if (!_network.empty()) { - for (int i = _network.size() / 2 + 1; i < _network.size(); i++) { - if (i == _network.size() / 2 + 1) { - _network.write[i]->set_input(X); - } else { - _network.write[i]->set_input(_network.write[i - 1]->get_a()); - } - _network.write[i]->forward_pass(); - } +Ref +MLPPWGAN::model_set_test_discriminator(const Ref &X) { + if (!_network.empty()) { + for (int i = _network.size() / 2 + 1; i < _network.size(); i++) { + if (i == _network.size() / 2 + 1) { + _network.write[i]->set_input(X); + } else { + _network.write[i]->set_input(_network.write[i - 1]->get_a()); + } + _network.write[i]->forward_pass(); + } - _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); - } + _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); + } - _output_layer->forward_pass(); + _output_layer->forward_pass(); - return _output_layer->get_a(); + return _output_layer->get_a(); } real_t MLPPWGAN::cost(const Ref &y_hat, const Ref &y) { - MLPPReg regularization; - MLPPCost mlpp_cost; + MLPPReg regularization; + MLPPCost mlpp_cost; - real_t total_reg_term = 0; + real_t total_reg_term = 0; - for (int i = 0; i < _network.size() - 1; ++i) { - Ref layer = _network[i]; + for (int i = 0; i < _network.size() - 1; ++i) { + Ref layer = _network[i]; - total_reg_term += regularization.reg_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()); - } + total_reg_term += + regularization.reg_termm(layer->get_weights(), layer->get_lambda(), + layer->get_alpha(), layer->get_reg()); + } - total_reg_term += regularization.reg_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()); + total_reg_term += regularization.reg_termv( + _output_layer->get_weights(), _output_layer->get_lambda(), + _output_layer->get_alpha(), _output_layer->get_reg()); - return mlpp_cost.run_cost_norm_vector(_output_layer->get_cost(), y_hat, y) + total_reg_term; + return mlpp_cost.run_cost_norm_vector(_output_layer->get_cost(), y_hat, y) + + total_reg_term; } void MLPPWGAN::forward_pass() { - int n = _output_set->size().y; + int n = _output_set->size().y; - if (!_network.empty()) { - Ref layer = _network[0]; + if (!_network.empty()) { + Ref layer = _network[0]; - layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); - layer->forward_pass(); + layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); + layer->forward_pass(); - for (int i = 1; i < _network.size(); i++) { - layer = _network[i]; + for (int i = 1; i < _network.size(); i++) { + layer = _network[i]; - layer->set_input(_network.write[i - 1]->get_a()); - layer->forward_pass(); - } + layer->set_input(_network.write[i - 1]->get_a()); + layer->forward_pass(); + } - _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); - } else { // Should never happen, though. - _output_layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); - } + _output_layer->set_input(_network.write[_network.size() - 1]->get_a()); + } else { // Should never happen, though. + _output_layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k)); + } - _output_layer->forward_pass(); + _output_layer->forward_pass(); - _y_hat->set_from_mlpp_vector(_output_layer->get_a()); + _y_hat->set_from_mlpp_vector(_output_layer->get_a()); } -void MLPPWGAN::update_discriminator_parameters(const Vector> &hidden_layer_updations, const Ref &output_layer_updation, real_t learning_rate) { - int n = _output_set->size().y; +void MLPPWGAN::update_discriminator_parameters( + const Vector> &hidden_layer_updations, + const Ref &output_layer_updation, real_t learning_rate) { + int n = _output_set->size().y; - _output_layer->set_weights(_output_layer->get_weights()->subn(output_layer_updation)); - _output_layer->set_bias(_output_layer->get_bias() - learning_rate * _output_layer->get_delta()->sum_elements() / n); + _output_layer->set_weights( + _output_layer->get_weights()->subn(output_layer_updation)); + _output_layer->set_bias(_output_layer->get_bias() - + learning_rate * + _output_layer->get_delta()->sum_elements() / n); - if (!_network.empty()) { - Ref layer = _network[_network.size() - 1]; + if (!_network.empty()) { + Ref layer = _network[_network.size() - 1]; - Ref slice = hidden_layer_updations[0]; + Ref slice = hidden_layer_updations[0]; - layer->set_weights(layer->get_weights()->subn(slice)); - layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n))); + layer->set_weights(layer->get_weights()->subn(slice)); + layer->set_bias(layer->get_bias()->subtract_matrix_rowsn( + layer->get_delta()->scalar_multiplyn(learning_rate / n))); - for (int i = _network.size() - 2; i > _network.size() / 2; i--) { - layer = _network[i]; + for (int i = _network.size() - 2; i > _network.size() / 2; i--) { + layer = _network[i]; - slice = hidden_layer_updations[(_network.size() - 2) - i + 1]; + slice = hidden_layer_updations[(_network.size() - 2) - i + 1]; - layer->set_weights(layer->get_weights()->subn(slice)); - layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n))); - } - } + layer->set_weights(layer->get_weights()->subn(slice)); + layer->set_bias(layer->get_bias()->subtract_matrix_rowsn( + layer->get_delta()->scalar_multiplyn(learning_rate / n))); + } + } } -void MLPPWGAN::update_generator_parameters(const Vector> &hidden_layer_updations, real_t learning_rate) { - if (!_network.empty()) { - int n = _output_set->size().y; +void MLPPWGAN::update_generator_parameters( + const Vector> &hidden_layer_updations, + real_t learning_rate) { + if (!_network.empty()) { + int n = _output_set->size().y; - Ref slice; + Ref slice; - for (int i = _network.size() / 2; i >= 0; i--) { - Ref layer = _network[i]; + for (int i = _network.size() / 2; i >= 0; i--) { + Ref layer = _network[i]; - slice = hidden_layer_updations[(_network.size() - 2) - i + 1]; + slice = hidden_layer_updations[(_network.size() - 2) - i + 1]; - //std::cout << network[i].weights.size() << "x" << network[i].weights[0].size() << std::endl; - //std::cout << hiddenLayerUpdations[(network.size() - 2) - i + 1].size() << "x" << hiddenLayerUpdations[(network.size() - 2) - i + 1][0].size() << std::endl; - layer->set_weights(layer->get_weights()->subn(slice)); - layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n))); - } - } + // std::cout << network[i].weights.size() << "x" << + // network[i].weights[0].size() << std::endl; std::cout << + // hiddenLayerUpdations[(network.size() - 2) - i + 1].size() << "x" << + // hiddenLayerUpdations[(network.size() - 2) - i + 1][0].size() << + // std::endl; + layer->set_weights(layer->get_weights()->subn(slice)); + layer->set_bias(layer->get_bias()->subtract_matrix_rowsn( + layer->get_delta()->scalar_multiplyn(learning_rate / n))); + } + } } -MLPPWGAN::DiscriminatorGradientResult MLPPWGAN::compute_discriminator_gradients(const Ref &y_hat, const Ref &output_set) { - MLPPCost mlpp_cost; - MLPPActivation avn; - MLPPReg regularization; +MLPPWGAN::DiscriminatorGradientResult +MLPPWGAN::compute_discriminator_gradients(const Ref &y_hat, + const Ref &output_set) { + MLPPCost mlpp_cost; + MLPPActivation avn; + MLPPReg regularization; - DiscriminatorGradientResult data; + DiscriminatorGradientResult data; - _output_layer->set_delta(mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set)->hadamard_productn(avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z()))); + _output_layer->set_delta( + mlpp_cost + .run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set) + ->hadamard_productn(avn.run_activation_deriv_vector( + _output_layer->get_activation(), _output_layer->get_z()))); - data.output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta()); - data.output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg())); + data.output_w_grad = _output_layer->get_input()->transposen()->mult_vec( + _output_layer->get_delta()); + data.output_w_grad->add(regularization.reg_deriv_termv( + _output_layer->get_weights(), _output_layer->get_lambda(), + _output_layer->get_alpha(), _output_layer->get_reg())); - if (!_network.empty()) { - Ref layer = _network[_network.size() - 1]; + if (!_network.empty()) { + Ref layer = _network[_network.size() - 1]; - layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()))); + layer->set_delta(_output_layer->get_delta() + ->outer_product(_output_layer->get_weights()) + ->hadamard_productn(avn.run_activation_deriv_matrix( + layer->get_activation(), layer->get_z()))); - Ref hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); + Ref hidden_layer_w_grad = + layer->get_input()->transposen()->multn(layer->get_delta()); - data.cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. + data.cumulative_hidden_layer_w_grad.push_back( + hidden_layer_w_grad->addn(regularization.reg_deriv_termm( + layer->get_weights(), layer->get_lambda(), layer->get_alpha(), + layer->get_reg()))); // Adding to our cumulative hidden layer grads. + // Maintain reg terms as well. - //std::cout << "HIDDENLAYER FIRST:" << hiddenLayerWGrad.size() << "x" << hiddenLayerWGrad[0].size() << std::endl; - //std::cout << "WEIGHTS SECOND:" << layer.weights.size() << "x" << layer.weights[0].size() << std::endl; + // std::cout << "HIDDENLAYER FIRST:" << hiddenLayerWGrad.size() << "x" << + // hiddenLayerWGrad[0].size() << std::endl; std::cout << "WEIGHTS SECOND:" + // << layer.weights.size() << "x" << layer.weights[0].size() << std::endl; - for (int i = _network.size() - 2; i > _network.size() / 2; i--) { - layer = _network[i]; - Ref next_layer = _network[i + 1]; + for (int i = _network.size() - 2; i > _network.size() / 2; i--) { + layer = _network[i]; + Ref next_layer = _network[i + 1]; - layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()))); + layer->set_delta(next_layer->get_delta() + ->multn(next_layer->get_weights()->transposen()) + ->hadamard_productn(avn.run_activation_deriv_matrix( + layer->get_activation(), layer->get_z()))); - hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); - data.cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. - } - } + hidden_layer_w_grad = + layer->get_input()->transposen()->multn(layer->get_delta()); + data.cumulative_hidden_layer_w_grad.push_back( + hidden_layer_w_grad->addn(regularization.reg_deriv_termm( + layer->get_weights(), layer->get_lambda(), layer->get_alpha(), + layer->get_reg()))); // Adding to our cumulative hidden layer + // grads. Maintain reg terms as well. + } + } - return data; + return data; } -Vector> MLPPWGAN::compute_generator_gradients(const Ref &y_hat, const Ref &output_set) { - class MLPPCost cost; - MLPPActivation avn; - MLPPReg regularization; +Vector> +MLPPWGAN::compute_generator_gradients(const Ref &y_hat, + const Ref &output_set) { + class MLPPCost cost; + MLPPActivation avn; + MLPPReg regularization; - Vector> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads. + Vector> + cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads. - Ref cost_deriv_vector = cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set); - Ref activation_deriv_vector = avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z()); + Ref cost_deriv_vector = + cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set); + Ref activation_deriv_vector = avn.run_activation_deriv_vector( + _output_layer->get_activation(), _output_layer->get_z()); - _output_layer->set_delta(cost_deriv_vector->hadamard_productn(activation_deriv_vector)); + _output_layer->set_delta( + cost_deriv_vector->hadamard_productn(activation_deriv_vector)); - Ref output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta()); - output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg())); + Ref output_w_grad = + _output_layer->get_input()->transposen()->mult_vec( + _output_layer->get_delta()); + output_w_grad->add(regularization.reg_deriv_termv( + _output_layer->get_weights(), _output_layer->get_lambda(), + _output_layer->get_alpha(), _output_layer->get_reg())); - if (!_network.empty()) { - Ref layer = _network[_network.size() - 1]; + if (!_network.empty()) { + Ref layer = _network[_network.size() - 1]; - Ref activation_deriv_matrix = avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()); - layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(activation_deriv_matrix)); + Ref activation_deriv_matrix = avn.run_activation_deriv_matrix( + layer->get_activation(), layer->get_z()); + layer->set_delta(_output_layer->get_delta() + ->outer_product(_output_layer->get_weights()) + ->hadamard_productn(activation_deriv_matrix)); - Ref hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); + Ref hidden_layer_w_grad = + layer->get_input()->transposen()->multn(layer->get_delta()); - cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. + cumulative_hidden_layer_w_grad.push_back( + hidden_layer_w_grad->addn(regularization.reg_deriv_termm( + layer->get_weights(), layer->get_lambda(), layer->get_alpha(), + layer->get_reg()))); // Adding to our cumulative hidden layer grads. + // Maintain reg terms as well. - for (int i = _network.size() - 2; i >= 0; i--) { - layer = _network[i]; - Ref next_layer = _network[i + 1]; + for (int i = _network.size() - 2; i >= 0; i--) { + layer = _network[i]; + Ref next_layer = _network[i + 1]; - activation_deriv_matrix = avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()); + activation_deriv_matrix = avn.run_activation_deriv_matrix( + layer->get_activation(), layer->get_z()); - layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(activation_deriv_matrix)); - hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); + layer->set_delta(next_layer->get_delta() + ->multn(next_layer->get_weights()->transposen()) + ->hadamard_productn(activation_deriv_matrix)); + hidden_layer_w_grad = + layer->get_input()->transposen()->multn(layer->get_delta()); - cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well. - } - } + cumulative_hidden_layer_w_grad.push_back( + hidden_layer_w_grad->addn(regularization.reg_deriv_termm( + layer->get_weights(), layer->get_lambda(), layer->get_alpha(), + layer->get_reg()))); // Adding to our cumulative hidden layer + // grads. Maintain reg terms as well. + } + } - return cumulative_hidden_layer_w_grad; + return cumulative_hidden_layer_w_grad; } -void MLPPWGAN::handle_ui(int epoch, real_t cost_prev, const Ref &y_hat, const Ref &output_set) { - MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, output_set)); +void MLPPWGAN::handle_ui(int epoch, real_t cost_prev, + const Ref &y_hat, + const Ref &output_set) { + MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, output_set)); - PLOG_MSG("Layer " + itos(_network.size() + 1) + ":"); + PLOG_MSG("Layer " + itos(_network.size() + 1) + ":"); - MLPPUtilities::print_ui_vb(_output_layer->get_weights(), _output_layer->get_bias()); + MLPPUtilities::print_ui_vb(_output_layer->get_weights(), + _output_layer->get_bias()); - if (!_network.empty()) { - for (int i = _network.size() - 1; i >= 0; i--) { - Ref layer = _network[i]; + if (!_network.empty()) { + for (int i = _network.size() - 1; i >= 0; i--) { + Ref layer = _network[i]; - PLOG_MSG("Layer " + itos(i + 1) + ":"); + PLOG_MSG("Layer " + itos(i + 1) + ":"); - MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias()); - } - } + MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias()); + } + } } void MLPPWGAN::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPWGAN::get_output_set); - ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPWGAN::set_output_set); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_output_set", "get_output_set"); + ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPWGAN::get_output_set); + ClassDB::bind_method(D_METHOD("set_output_set", "val"), + &MLPPWGAN::set_output_set); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", + PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), + "set_output_set", "get_output_set"); - ClassDB::bind_method(D_METHOD("get_k"), &MLPPWGAN::get_k); - ClassDB::bind_method(D_METHOD("set_k", "val"), &MLPPWGAN::set_k); - ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k"); + ClassDB::bind_method(D_METHOD("get_k"), &MLPPWGAN::get_k); + ClassDB::bind_method(D_METHOD("set_k", "val"), &MLPPWGAN::set_k); + ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k"); - ClassDB::bind_method(D_METHOD("generate_example", "n"), &MLPPWGAN::generate_example); - ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPWGAN::gradient_descent, false); - ClassDB::bind_method(D_METHOD("score"), &MLPPWGAN::score); - ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPWGAN::save); + ClassDB::bind_method(D_METHOD("generate_example", "n"), + &MLPPWGAN::generate_example); + ClassDB::bind_method( + D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), + &MLPPWGAN::gradient_descent, false); + ClassDB::bind_method(D_METHOD("score"), &MLPPWGAN::score); + ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPWGAN::save); - ClassDB::bind_method(D_METHOD("create_layer", "activation", "weight_init", "reg", "lambda", "alpha"), &MLPPWGAN::create_layer, MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5); - ClassDB::bind_method(D_METHOD("add_layer", "layer"), &MLPPWGAN::add_layer); - ClassDB::bind_method(D_METHOD("get_layer", "index"), &MLPPWGAN::get_layer); - ClassDB::bind_method(D_METHOD("remove_layer", "index"), &MLPPWGAN::remove_layer); - ClassDB::bind_method(D_METHOD("get_layer_count"), &MLPPWGAN::score); + ClassDB::bind_method(D_METHOD("create_layer", "activation", "weight_init", + "reg", "lambda", "alpha"), + &MLPPWGAN::create_layer, + MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, + MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5); + ClassDB::bind_method(D_METHOD("add_layer", "layer"), &MLPPWGAN::add_layer); + ClassDB::bind_method(D_METHOD("get_layer", "index"), &MLPPWGAN::get_layer); + ClassDB::bind_method(D_METHOD("remove_layer", "index"), + &MLPPWGAN::remove_layer); + ClassDB::bind_method(D_METHOD("get_layer_count"), &MLPPWGAN::score); - ClassDB::bind_method(D_METHOD("add_output_layer", "weight_init", "reg", "lambda", "alpha"), &MLPPWGAN::add_output_layer, MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5); + ClassDB::bind_method( + D_METHOD("add_output_layer", "weight_init", "reg", "lambda", "alpha"), + &MLPPWGAN::add_output_layer, + MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, + MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5); } diff --git a/wgan/wgan.h b/wgan/wgan.h index de76aaa..e606f84 100644 --- a/wgan/wgan.h +++ b/wgan/wgan.h @@ -31,11 +31,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifdef USING_SFW +#include "sfw.h" +#else #include "core/containers/vector.h" #include "core/math/math_defs.h" #include "core/string/ustring.h" #include "core/object/reference.h" +#endif #include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_tensor3.h"