Merge branch 'master' into cli-lsvm
This commit is contained in:
+19
-13
@@ -11,9 +11,8 @@ environment:
|
||||
boost_serialization-vc140.1.60.0.0/lib/native/address-model-64/lib/*.*"
|
||||
BOOST_UNIT_TEST : "C:/projects/mlpack/\
|
||||
boost_unit_test_framework-vc140.1.60.0.0/lib/native/address-model-64/lib/*.*"
|
||||
ARMADILLO_DOWNLOAD : "http://sourceforge.net/projects/arma/files/\
|
||||
armadillo-7.800.2.tar.xz"
|
||||
ARMADILLO_LIBRARY : "C:/projects/mlpack/armadillo-7.800.2/\
|
||||
ARMADILLO_DOWNLOAD : "http://ftp.fau.de/macports/distfiles/armadillo/armadillo-8.400.0.tar.xz"
|
||||
ARMADILLO_LIBRARY : "C:/projects/mlpack/armadillo-8.400.0/\
|
||||
build/Debug/armadillo.lib"
|
||||
BLAS_LIBRARY : "%APPVEYOR_BUILD_FOLDER%/OpenBLAS.0.2.14.1/lib/native/lib/x64/\
|
||||
libopenblas.dll.a"
|
||||
@@ -25,8 +24,15 @@ environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
VSVER: Visual Studio 14 2015 Win64
|
||||
MSBUILD: C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
VSVER: Visual Studio 15 2017 Win64
|
||||
MSBUILD: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
|
||||
# Currently, the VS2019 build seems to always time out. This seems to be an
|
||||
# AppVeyor issue.
|
||||
# - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
|
||||
# VSVER: Visual Studio 16 2019
|
||||
# MSBUILD: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe
|
||||
|
||||
|
||||
configuration: Release
|
||||
@@ -62,28 +68,26 @@ build_script:
|
||||
- ps: cp ${env:BOOST_UNIT_TEST} C:\projects\mlpack\boost_libs\
|
||||
- echo TEST_ARMA is %ARMADILLO_DOWNLOAD%
|
||||
- >
|
||||
if not exist armadillo.tar.xz
|
||||
appveyor DownloadFile %ARMADILLO_DOWNLOAD%
|
||||
-FileName armadillo.tar.xz
|
||||
- 7z x armadillo.tar.xz -so | 7z x -si -ttar > nul
|
||||
- cd armadillo-7.800.2 && mkdir build && cd build
|
||||
- 7z x armadillo.tar.xz -so -txz | 7z x -si -ttar > nul
|
||||
- cd armadillo-8.400.0 && mkdir build && cd build
|
||||
- >
|
||||
cmake -G "Visual Studio 14 2015 Win64"
|
||||
cmake -G "%VSVER%"
|
||||
-DBLAS_LIBRARY:FILEPATH=%BLAS_LIBRARY%
|
||||
-DLAPACK_LIBRARY:FILEPATH=%BLAS_LIBRARY%
|
||||
-DCMAKE_PREFIX:FILEPATH="%APPVEYOR_BUILD_FOLDER%/armadillo"
|
||||
-DBUILD_SHARED_LIBS=OFF
|
||||
-DCMAKE_BUILD_TYPE=Release ..
|
||||
- >
|
||||
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
|
||||
"C:\projects\mlpack\armadillo-7.800.2\build\armadillo.sln"
|
||||
"%MSBUILD%" "C:\projects\mlpack\armadillo-8.400.0\build\armadillo.sln"
|
||||
/m /verbosity:quiet /p:Configuration=Release;Platform=x64
|
||||
- cd C:\projects\mlpack && mkdir build && cd build
|
||||
- >
|
||||
cmake -G "Visual Studio 14 2015 Win64"
|
||||
cmake -G "%VSVER%"
|
||||
-DBLAS_LIBRARY:FILEPATH=%BLAS_LIBRARY%
|
||||
-DLAPACK_LIBRARY:FILEPATH=%BLAS_LIBRARY%
|
||||
-DARMADILLO_INCLUDE_DIR="C:/projects/mlpack/armadillo-7.800.2/include"
|
||||
-DARMADILLO_INCLUDE_DIR="C:/projects/mlpack/armadillo-8.400.0/include"
|
||||
-DARMADILLO_LIBRARY:FILEPATH=%ARMADILLO_LIBRARY%
|
||||
-DBOOST_INCLUDEDIR:PATH=%BOOST_INCLUDE%
|
||||
-DBOOST_LIBRARYDIR:PATH="C:/projects/mlpack/boost_libs"
|
||||
@@ -92,8 +96,7 @@ build_script:
|
||||
-DBUILD_PYTHON_BINDINGS=OFF
|
||||
-DCMAKE_BUILD_TYPE=Release ..
|
||||
- >
|
||||
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
|
||||
"C:\projects\mlpack\build\mlpack.sln"
|
||||
"%MSBUILD%" "C:\projects\mlpack\build\mlpack.sln"
|
||||
/m /verbosity:minimal /nologo /p:BuildInParallel=true
|
||||
/p:Configuration=Release;Platform=x64
|
||||
|
||||
@@ -143,6 +146,9 @@ build_script:
|
||||
- ps: >
|
||||
cp C:\projects\mlpack\doc\examples
|
||||
C:\projects\mlpack\dist\win-installer\staging -recurse
|
||||
- ps: >
|
||||
cp C:\projects\mlpack\src\mlpack\tests\data\german.csv
|
||||
C:\projects\mlpack\dist\win-installer\staging\examples\sample-ml-app\sample-ml-app\data\
|
||||
|
||||
# Checking current gitversion or mlpack version.
|
||||
- ps: >
|
||||
|
||||
@@ -6,3 +6,4 @@ src/mlpack/core/util/arma_config.hpp
|
||||
.idea
|
||||
cmake-build-*
|
||||
*.pyc
|
||||
Testing/
|
||||
|
||||
+7
-8
@@ -1,4 +1,3 @@
|
||||
sudo: required
|
||||
language: cpp
|
||||
|
||||
matrix:
|
||||
@@ -8,9 +7,9 @@ matrix:
|
||||
env: CMAKE_OPTIONS="-DDEBUG=OFF -DPROFILE=OFF -DPYTHON_EXECUTABLE=/usr/bin/python"
|
||||
before_install:
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev python-pip cython python-numpy python-pandas
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev python-pip cython python-numpy python-pandas xz-utils
|
||||
- sudo pip install --upgrade --ignore-installed setuptools cython
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-6.500.5.tar.gz | tar xvz && cd armadillo*
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-8.400.0.tar.xz | tar -xvJ && cd armadillo*
|
||||
- cmake . && make && sudo make install && cd ..
|
||||
- sudo cp .travis/config.hpp /usr/include/armadillo_bits/config.hpp
|
||||
|
||||
@@ -19,9 +18,9 @@ matrix:
|
||||
env: CMAKE_OPTIONS="-DDEBUG=OFF -DPROFILE=OFF -DPYTHON_EXECUTABLE=/usr/bin/python3"
|
||||
before_install:
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev python3-pip cython3 python3-numpy
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev python3-pip cython3 python3-numpy xz-utils
|
||||
- sudo pip3 install --upgrade --ignore-installed setuptools cython pandas
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-6.500.5.tar.gz | tar xvz && cd armadillo*
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-8.400.0.tar.xz | tar -xvJ && cd armadillo*
|
||||
- cmake . && make && sudo make install && cd ..
|
||||
- sudo cp .travis/config.hpp /usr/include/armadillo_bits/config.hpp
|
||||
|
||||
@@ -30,8 +29,8 @@ matrix:
|
||||
env: CMAKE_OPTIONS="-DDEBUG=ON -DPROFILE=OFF -DBUILD_PYTHON_BINDINGS=OFF"
|
||||
before_install:
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-6.500.5.tar.gz | tar xvz && cd armadillo*
|
||||
- sudo apt-get install -y --allow-unauthenticated libopenblas-dev liblapack-dev g++ libboost-all-dev xz-utils
|
||||
- curl https://ftp.fau.de/macports/distfiles/armadillo/armadillo-8.400.0.tar.xz | tar -xvJ && cd armadillo*
|
||||
- cmake . && make && sudo make install && cd ..
|
||||
- sudo cp .travis/config.hpp /usr/include/armadillo_bits/config.hpp
|
||||
|
||||
@@ -42,7 +41,7 @@ matrix:
|
||||
- brew install openblas armadillo || brew install openblas armadillo
|
||||
|
||||
install:
|
||||
- mkdir build && cd build && cmake $CMAKE_OPTIONS .. && travis_wait 60 make -j2
|
||||
- mkdir build && cd build && cmake $CMAKE_OPTIONS .. && travis_wait 75 make -j2
|
||||
|
||||
script:
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 travis_wait 30 ctest -j2
|
||||
|
||||
+177
-49
@@ -1,35 +1,65 @@
|
||||
// Copyright (C) 2008-2012 NICTA (www.nicta.com.au)
|
||||
// Copyright (C) 2008-2012 Conrad Sanderson
|
||||
//
|
||||
// This file is part of the Armadillo C++ library.
|
||||
// It is provided without any warranty of fitness
|
||||
// for any purpose. You can redistribute this file
|
||||
// and/or modify it under the terms of the GNU
|
||||
// Lesser General Public License (LGPL) as published
|
||||
// by the Free Software Foundation, either version 3
|
||||
// of the License or (at your option) any later version.
|
||||
// (see http://www.opensource.org/licenses for more info)
|
||||
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
|
||||
// Copyright 2008-2016 National ICT Australia (NICTA)
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#if !defined(ARMA_USE_LAPACK)
|
||||
#define ARMA_USE_LAPACK
|
||||
//// Uncomment the above line if you have LAPACK or a high-speed replacement for LAPACK,
|
||||
//// such as Intel's MKL, AMD's ACML, or the Accelerate framework.
|
||||
//// Comment out the above line if you don't have LAPACK or a high-speed replacement for LAPACK,
|
||||
//// such as Intel MKL, AMD ACML, or the Accelerate framework.
|
||||
//// LAPACK is required for matrix decompositions (eg. SVD) and matrix inverse.
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_BLAS)
|
||||
#define ARMA_USE_BLAS
|
||||
//// Uncomment the above line if you have BLAS or a high-speed replacement for BLAS,
|
||||
//// such as GotoBLAS, Intel's MKL, AMD's ACML, or the Accelerate framework.
|
||||
//// Comment out the above line if you don't have BLAS or a high-speed replacement for BLAS,
|
||||
//// such as OpenBLAS, GotoBLAS, Intel MKL, AMD ACML, or the Accelerate framework.
|
||||
//// BLAS is used for matrix multiplication.
|
||||
//// Without BLAS, matrix multiplication will still work, but might be slower.
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_NEWARP)
|
||||
#define ARMA_USE_NEWARP
|
||||
//// Uncomment the above line to enable the built-in partial emulation of ARPACK.
|
||||
//// This is used for eigen decompositions of real (non-complex) sparse matrices, eg. eigs_sym(), svds()
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_ARPACK)
|
||||
// #define ARMA_USE_ARPACK
|
||||
//// Uncomment the above line if you have ARPACK or a high-speed replacement for ARPACK.
|
||||
//// ARPACK is required for eigen decompositions of complex sparse matrices
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_SUPERLU)
|
||||
// #define ARMA_USE_SUPERLU
|
||||
//// Uncomment the above line if you have SuperLU.
|
||||
//// SuperLU is used for solving sparse linear systems via spsolve()
|
||||
//// Caveat: only SuperLU version 5.2 can be used!
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_SUPERLU_INCLUDE_DIR)
|
||||
// #define ARMA_SUPERLU_INCLUDE_DIR /usr/include/
|
||||
//// If you're using SuperLU and want to explicitly include the SuperLU headers,
|
||||
//// uncomment the above define and specify the appropriate include directory.
|
||||
//// Make sure the directory has a trailing /
|
||||
#endif
|
||||
|
||||
#define ARMA_USE_WRAPPER
|
||||
//// Comment out the above line if you prefer to directly link with LAPACK and/or BLAS (eg. -llapack -lblas)
|
||||
//// instead of linking indirectly with LAPACK and/or BLAS via Armadillo's run-time wrapper library.
|
||||
//// Comment out the above line if you're getting linking errors when compiling your programs,
|
||||
//// or if you prefer to directly link with LAPACK, BLAS + etc instead of the Armadillo runtime library.
|
||||
//// You will then need to link your programs directly with -llapack -lblas instead of -larmadillo
|
||||
|
||||
// #define ARMA_BLAS_CAPITALS
|
||||
//// Uncomment the above line if your BLAS and LAPACK libraries have capitalised function names (eg. ACML on 64-bit Windows)
|
||||
@@ -45,31 +75,49 @@
|
||||
//// Uncomment the above line if your BLAS and LAPACK libraries use "long long" instead of "int"
|
||||
|
||||
// #define ARMA_USE_TBB_ALLOC
|
||||
//// Uncomment the above line if you want to use Intel TBB scalable_malloc() and scalable_free() instead of standard new[] and delete[]
|
||||
//// Uncomment the above line if you want to use Intel TBB scalable_malloc() and scalable_free() instead of standard malloc() and free()
|
||||
|
||||
// #define ARMA_USE_MKL_ALLOC
|
||||
//// Uncomment the above line if you want to use Intel MKL mkl_malloc() and mkl_free() instead of standard new[] and delete[]
|
||||
//// Uncomment the above line if you want to use Intel MKL mkl_malloc() and mkl_free() instead of standard malloc() and free()
|
||||
|
||||
/* #undef ARMA_USE_ATLAS */
|
||||
#define ARMA_ATLAS_INCLUDE_DIR /
|
||||
// #define ARMA_USE_ATLAS
|
||||
// #define ARMA_ATLAS_INCLUDE_DIR /usr/include/
|
||||
//// If you're using ATLAS and the compiler can't find cblas.h and/or clapack.h
|
||||
//// uncomment the above define and specify the appropriate include directory.
|
||||
//// Make sure the directory has a trailing /
|
||||
|
||||
#define ARMA_64BIT_WORD
|
||||
//// Uncomment the above line if you require matrices/vectors capable of holding more than 4 billion elements.
|
||||
//// Your machine and compiler must have support for 64 bit integers (eg. via "long" or "long long")
|
||||
|
||||
#if !defined(ARMA_USE_CXX11)
|
||||
#define ARMA_USE_CXX11
|
||||
//// Uncomment the above line if you have a C++ compiler that supports the C++11 standard
|
||||
//// This will enable additional features, such as use of initialiser lists
|
||||
//// Uncomment the above line to forcefully enable use of C++11 features (eg. initialiser lists).
|
||||
//// Note that ARMA_USE_CXX11 is automatically enabled when a C++11 compiler is detected.
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_OPENMP)
|
||||
// #define ARMA_USE_OPENMP
|
||||
//// Uncomment the above line to forcefully enable use of OpenMP for parallelisation.
|
||||
//// Note that ARMA_USE_OPENMP is automatically enabled when a compiler supporting OpenMP 3.1 is detected.
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_64BIT_WORD)
|
||||
#define ARMA_64BIT_WORD
|
||||
//// Uncomment the above line if you require matrices/vectors capable of holding more than 4 billion elements.
|
||||
//// Your machine and compiler must have support for 64 bit integers (eg. via "long" or "long long").
|
||||
//// Note that ARMA_64BIT_WORD is automatically enabled when a C++11 compiler is detected.
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_USE_HDF5)
|
||||
/* #undef ARMA_USE_HDF5 */
|
||||
//// Uncomment the above line if you want the ability to save and load matrices stored in the HDF5 format;
|
||||
//// the hdf5.h header file must be available on your system and you will need to link with the hdf5 library (eg. -lhdf5)
|
||||
// #define ARMA_USE_HDF5
|
||||
//// Uncomment the above line to allow the ability to save and load matrices stored in HDF5 format;
|
||||
//// the hdf5.h header file must be available on your system,
|
||||
//// and you will need to link with the hdf5 library (eg. -lhdf5)
|
||||
#endif
|
||||
|
||||
// #define ARMA_USE_HDF5_ALT
|
||||
#if defined(ARMA_USE_HDF5_ALT) && defined(ARMA_USE_WRAPPER)
|
||||
#undef ARMA_USE_HDF5
|
||||
#define ARMA_USE_HDF5
|
||||
|
||||
// #define ARMA_HDF5_INCLUDE_DIR /usr/include/
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_MAT_PREALLOC)
|
||||
@@ -80,6 +128,18 @@
|
||||
//// If you mainly use lots of very small vectors (eg. <= 4 elements),
|
||||
//// change the number to the size of your vectors.
|
||||
|
||||
#if !defined(ARMA_OPENMP_THRESHOLD)
|
||||
#define ARMA_OPENMP_THRESHOLD 320
|
||||
#endif
|
||||
//// The minimum number of elements in a matrix to allow OpenMP based parallelisation;
|
||||
//// it must be an integer that is at least 1.
|
||||
|
||||
#if !defined(ARMA_OPENMP_THREADS)
|
||||
#define ARMA_OPENMP_THREADS 10
|
||||
#endif
|
||||
//// The maximum number of threads to use for OpenMP based parallelisation;
|
||||
//// it must be an integer that is at least 1.
|
||||
|
||||
#if !defined(ARMA_SPMAT_CHUNKSIZE)
|
||||
#define ARMA_SPMAT_CHUNKSIZE 256
|
||||
#endif
|
||||
@@ -99,27 +159,39 @@
|
||||
//// This is mainly useful for debugging of the library.
|
||||
|
||||
|
||||
// #define ARMA_USE_BOOST
|
||||
// #define ARMA_USE_BOOST_DATE
|
||||
|
||||
|
||||
#if !defined(ARMA_DEFAULT_OSTREAM)
|
||||
#define ARMA_DEFAULT_OSTREAM std::cout
|
||||
#if defined(ARMA_DEFAULT_OSTREAM)
|
||||
#pragma message ("WARNING: support for ARMA_DEFAULT_OSTREAM is deprecated and will be removed;")
|
||||
#pragma message ("WARNING: use ARMA_COUT_STREAM and ARMA_CERR_STREAM instead")
|
||||
#endif
|
||||
|
||||
#define ARMA_PRINT_LOGIC_ERRORS
|
||||
#define ARMA_PRINT_RUNTIME_ERRORS
|
||||
//#define ARMA_PRINT_HDF5_ERRORS
|
||||
|
||||
#define ARMA_HAVE_STD_ISFINITE
|
||||
#define ARMA_HAVE_STD_ISINF
|
||||
#define ARMA_HAVE_STD_ISNAN
|
||||
#define ARMA_HAVE_STD_SNPRINTF
|
||||
#if !defined(ARMA_COUT_STREAM)
|
||||
#if defined(ARMA_DEFAULT_OSTREAM)
|
||||
// for compatibility with earlier versions of Armadillo
|
||||
#define ARMA_COUT_STREAM ARMA_DEFAULT_OSTREAM
|
||||
#else
|
||||
#define ARMA_COUT_STREAM std::cout
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define ARMA_HAVE_LOG1P
|
||||
#define ARMA_HAVE_GETTIMEOFDAY
|
||||
#if !defined(ARMA_CERR_STREAM)
|
||||
#if defined(ARMA_DEFAULT_OSTREAM)
|
||||
// for compatibility with earlier versions of Armadillo
|
||||
#define ARMA_CERR_STREAM ARMA_DEFAULT_OSTREAM
|
||||
#else
|
||||
#define ARMA_CERR_STREAM std::cerr
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(ARMA_PRINT_ERRORS)
|
||||
#define ARMA_PRINT_ERRORS
|
||||
//// Comment out the above line if you don't want errors and warnings printed (eg. failed decompositions)
|
||||
#endif
|
||||
|
||||
#if !defined(ARMA_PRINT_HDF5_ERRORS)
|
||||
// #define ARMA_PRINT_HDF5_ERRORS
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_LAPACK)
|
||||
#undef ARMA_USE_LAPACK
|
||||
@@ -129,15 +201,71 @@
|
||||
#undef ARMA_USE_BLAS
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_NEWARP) || !defined(ARMA_USE_LAPACK)
|
||||
#undef ARMA_USE_NEWARP
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_ARPACK)
|
||||
#undef ARMA_USE_ARPACK
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_SUPERLU)
|
||||
#undef ARMA_USE_SUPERLU
|
||||
#undef ARMA_SUPERLU_INCLUDE_DIR
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_ATLAS)
|
||||
#undef ARMA_USE_ATLAS
|
||||
#undef ARMA_ATLAS_INCLUDE_DIR
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_PRINT_LOGIC_ERRORS)
|
||||
#undef ARMA_PRINT_LOGIC_ERRORS
|
||||
#if defined(ARMA_DONT_USE_WRAPPER)
|
||||
#undef ARMA_USE_WRAPPER
|
||||
#undef ARMA_USE_HDF5_ALT
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_PRINT_RUNTIME_ERRORS)
|
||||
#undef ARMA_PRINT_RUNTIME_ERRORS
|
||||
#if defined(ARMA_DONT_USE_CXX11)
|
||||
#undef ARMA_USE_CXX11
|
||||
#undef ARMA_USE_EXTERN_CXX11_RNG
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_OPENMP)
|
||||
#undef ARMA_USE_OPENMP
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_USE_WRAPPER)
|
||||
#if defined(ARMA_USE_CXX11)
|
||||
#if !defined(ARMA_USE_EXTERN_CXX11_RNG)
|
||||
// #define ARMA_USE_EXTERN_CXX11_RNG
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_EXTERN_CXX11_RNG)
|
||||
#undef ARMA_USE_EXTERN_CXX11_RNG
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_32BIT_WORD)
|
||||
#undef ARMA_64BIT_WORD
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_USE_HDF5)
|
||||
#undef ARMA_USE_HDF5
|
||||
#undef ARMA_USE_HDF5_ALT
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_PRINT_ERRORS)
|
||||
#undef ARMA_PRINT_ERRORS
|
||||
#endif
|
||||
|
||||
#if defined(ARMA_DONT_PRINT_HDF5_ERRORS)
|
||||
#undef ARMA_PRINT_HDF5_ERRORS
|
||||
#endif
|
||||
|
||||
|
||||
// if Armadillo was installed on this system via CMake and ARMA_USE_WRAPPER is not defined,
|
||||
// ARMA_AUX_LIBS lists the libraries required by Armadillo on this system, and
|
||||
// ARMA_AUX_INCDIRS lists the include directories required by Armadillo on this system.
|
||||
// Do not use these unless you know what you are doing.
|
||||
#define ARMA_AUX_LIBS
|
||||
#define ARMA_AUX_INCDIRS
|
||||
|
||||
@@ -52,7 +52,7 @@ endif ()
|
||||
|
||||
# Checks 'REQUIRED', 'QUIET' and versions.
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(ensmallen
|
||||
find_package_handle_standard_args(Ensmallen
|
||||
REQUIRED_VARS ENSMALLEN_INCLUDE_DIR
|
||||
VERSION_VAR ENSMALLEN_VERSION_STRING)
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# - Find STB_IMAGE
|
||||
# Find the STB_IMAGE C++ library
|
||||
#
|
||||
# This module sets the following variables:
|
||||
# STB_IMAGE_FOUND - set to true if the library is found
|
||||
# STB_IMAGE_INCLUDE_DIR - list of required include directories
|
||||
|
||||
file(GLOB STB_IMAGE_SEARCH_PATHS
|
||||
${CMAKE_BINARY_DIR}/deps/stb)
|
||||
find_path(STB_IMAGE_INCLUDE_DIR
|
||||
NAMES stb_image.h stb_image_write.h
|
||||
PATHS ${STB_IMAGE_SEARCH_PATHS})
|
||||
|
||||
if(STB_IMAGE_INCLUDE_DIR)
|
||||
set(STB_IMAGE_FOUND YES)
|
||||
endif ()
|
||||
|
||||
# Checks 'REQUIRED'.
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(STB_IMAGE
|
||||
REQUIRED_VARS STB_IMAGE_INCLUDE_DIR)
|
||||
|
||||
mark_as_advanced(STB_IMAGE_INCLUDE_DIR)
|
||||
@@ -5,4 +5,9 @@
|
||||
#
|
||||
# PROGRAM: the program to run to.
|
||||
# OUTPUT_FILE: the file to store the output in.
|
||||
execute_process(COMMAND ${PROGRAM} OUTPUT_FILE ${OUTPUT_FILE})
|
||||
execute_process(COMMAND ${PROGRAM} OUTPUT_FILE ${OUTPUT_FILE}
|
||||
ERROR_VARIABLE err)
|
||||
|
||||
if (err)
|
||||
message(FATAL_ERROR "Fatal error running ${PROGRAM}: ${err}!")
|
||||
endif ()
|
||||
|
||||
+56
-3
@@ -13,6 +13,7 @@ option(TEST_VERBOSE "Run test cases with verbose output." OFF)
|
||||
option(BUILD_TESTS "Build tests." ON)
|
||||
option(BUILD_CLI_EXECUTABLES "Build command-line executables." ON)
|
||||
option(DOWNLOAD_ENSMALLEN "If ensmallen is not found, download it." ON)
|
||||
option(DOWNLOAD_STB_IMAGE "Download stb_image for image loading." ON)
|
||||
|
||||
# Currently Python bindings aren't known to build successfully on Windows, so
|
||||
# set BUILD_PYTHON_BINDINGS to OFF when the platform is Windows.
|
||||
@@ -75,6 +76,10 @@ if(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -ftemplate-depth=1000")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
|
||||
|
||||
# To remove unused functions warnings.
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-function")
|
||||
endif()
|
||||
|
||||
# These support libraries are used if we need to link against something
|
||||
@@ -233,7 +238,7 @@ endif()
|
||||
# ENSMALLEN_INCLUDE_DIR - include directory for ensmallen
|
||||
# MATHJAX_ROOT - root of MathJax installation
|
||||
|
||||
find_package(Armadillo 6.500.0 REQUIRED)
|
||||
find_package(Armadillo 8.400.0 REQUIRED)
|
||||
|
||||
# If Armadillo was compiled without ARMA_64BIT_WORD and we are on a 64-bit
|
||||
# system (where size_t will be 64 bits), suggest to the user that they should
|
||||
@@ -310,15 +315,62 @@ endif ()
|
||||
set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${ARMADILLO_INCLUDE_DIRS})
|
||||
set(MLPACK_LIBRARIES ${MLPACK_LIBRARIES} ${ARMADILLO_LIBRARIES})
|
||||
|
||||
# Find stb_image.h and stb_image_write.h.
|
||||
find_package(StbImage)
|
||||
# Download stb_image for image loading.
|
||||
if (NOT STB_IMAGE_FOUND)
|
||||
if (DOWNLOAD_STB_IMAGE)
|
||||
set(STB_DIR "stb")
|
||||
install(DIRECTORY DESTINATION ${CMAKE_BINARY_DIR}/deps/${STB_DIR})
|
||||
file(DOWNLOAD http://mlpack.org/files/stb/stb_image.h
|
||||
"${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h"
|
||||
STATUS STB_IMAGE_DOWNLOAD_STATUS_LIST LOG STB_IMAGE_DOWNLOAD_LOG
|
||||
SHOW_PROGRESS)
|
||||
list(GET STB_IMAGE_DOWNLOAD_STATUS_LIST 0 STB_IMAGE_DOWNLOAD_STATUS)
|
||||
file(DOWNLOAD http://mlpack.org/files/stb/stb_image_write.h
|
||||
"${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h"
|
||||
STATUS STB_IMAGE_WRITE_DOWNLOAD_STATUS_LIST
|
||||
LOG STB_IMAGE_WRITE_DOWNLOAD_LOG
|
||||
SHOW_PROGRESS)
|
||||
list(GET STB_IMAGE_WRITE_DOWNLOAD_STATUS_LIST 0
|
||||
STB_IMAGE_WRITE_DOWNLOAD_STATUS)
|
||||
if (STB_IMAGE_DOWNLOAD_STATUS EQUAL 0 AND
|
||||
STB_IMAGE_WRITE_DOWNLOAD_STATUS EQUAL 0)
|
||||
set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS}
|
||||
"${CMAKE_BINARY_DIR}/deps/${STB_DIR}/")
|
||||
message(STATUS
|
||||
"Successfully downloaded stb into ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/")
|
||||
# Now we have to also ensure these header files get installed.
|
||||
install(FILES ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
install(FILES ${CMAKE_BINARY_DIR}/deps/${STB_DIR}/stb_image_write.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
add_definitions(-DHAS_STB)
|
||||
else ()
|
||||
list(GET STB_IMAGE_DOWNLOAD_STATUS_LIST 1 STB_DOWNLOAD_ERROR)
|
||||
message(WARNING
|
||||
"Could not download stb! Error code ${STB_DOWNLOAD_STATUS}: ${STB_DOWNLOAD_ERROR}! Error log: ${STB_DOWNLOAD_LOG}")
|
||||
message(WARNING
|
||||
"stb/stb_image.h is not installed. Image utilities will not be available!")
|
||||
endif ()
|
||||
else ()
|
||||
message(WARNING
|
||||
"stb/stb_image.h is not installed. Image utilities will not be available!")
|
||||
endif ()
|
||||
else ()
|
||||
# Already has STB installed.
|
||||
add_definitions(-DHAS_STB)
|
||||
set(MLPACK_INCLUDE_DIRS ${MLPACK_INCLUDE_DIRS} ${STB_IMAGE_INCLUDE_DIR})
|
||||
endif ()
|
||||
|
||||
|
||||
# Find ensmallen.
|
||||
# Once ensmallen is readily available in package repos, the automatic downloader
|
||||
# here can be removed.
|
||||
find_package(Ensmallen 1.10.0)
|
||||
find_package(Ensmallen 2.10.0)
|
||||
if (NOT ENSMALLEN_FOUND)
|
||||
if (DOWNLOAD_ENSMALLEN)
|
||||
file(DOWNLOAD http://www.ensmallen.org/files/ensmallen-latest.tar.gz
|
||||
"${CMAKE_BINARY_DIR}/deps/ensmallen-latest.tar.gz"
|
||||
STATUS ENS_DOWNLOAD_STATUS_LIST LOG ENS_DOWBLOAD_LOG
|
||||
STATUS ENS_DOWNLOAD_STATUS_LIST LOG ENS_DOWNLOAD_LOG
|
||||
SHOW_PROGRESS)
|
||||
list(GET ENS_DOWNLOAD_STATUS_LIST 0 ENS_DOWNLOAD_STATUS)
|
||||
if (ENS_DOWNLOAD_STATUS EQUAL 0)
|
||||
@@ -329,6 +381,7 @@ if (NOT ENSMALLEN_FOUND)
|
||||
# Get the name of the directory.
|
||||
file (GLOB ENS_DIRECTORIES RELATIVE "${CMAKE_BINARY_DIR}/deps/"
|
||||
"${CMAKE_BINARY_DIR}/deps/ensmallen-[0-9]*.[0-9]*.[0-9]*")
|
||||
list(FILTER ENS_DIRECTORIES EXCLUDE REGEX "ensmallen-.*\.tar\.gz")
|
||||
list(LENGTH ENS_DIRECTORIES ENS_DIRECTORIES_LEN)
|
||||
if (ENS_DIRECTORIES_LEN EQUAL 1)
|
||||
list(GET ENS_DIRECTORIES 0 ENSMALLEN_INCLUDE_DIR)
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
# mlpack Code of Conduct
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, sex characteristics, gender identity and
|
||||
expression, level of experience, education, socio-economic status, nationality,
|
||||
personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies within all project spaces, and it also applies when
|
||||
an individual is representing the project or its community in public spaces.
|
||||
Examples of representing a project or community include using an official
|
||||
project e-mail address, posting via an official social media account, or acting
|
||||
as an appointed representative at an online or offline event. Representation of
|
||||
a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at conduct@mlpack.org. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Reporting
|
||||
|
||||
If you believe someone is violating the code of conduct we ask that you report
|
||||
it by emailing conduct@mlpack.org. All reports will be kept confidential. In
|
||||
some cases we may determine that a public statement will need to be made. If
|
||||
that's the case, the identities of all victims and reporters will remain
|
||||
confidential unless those individuals instruct us otherwise.
|
||||
|
||||
If you are unsure whether the incident is a violation, or whether the space
|
||||
where it happened is covered by this Code of Conduct, we encourage you to still
|
||||
report it. We would much rather have a few extra reports where we decide to take
|
||||
no action, rather than miss a report of an actual violation. We do not look
|
||||
negatively on you if we find the incident is not a violation. And knowing about
|
||||
incidents that are not violations, or happen outside our spaces, can also help
|
||||
us to improve the Code of Conduct or the processes surrounding it.
|
||||
|
||||
In your report please include:
|
||||
|
||||
* Your contact info (so we can get in touch with you if we need to follow up)
|
||||
* Names (real, nicknames, or pseudonyms) of any individuals involved. If there
|
||||
were other witnesses besides you, please try to include them as well.
|
||||
* When and where the incident occurred. Please be as specific as possible.
|
||||
* Your account of what occurred. If there is a publicly available record
|
||||
(e.g. a mailing list archive or a public IRC logger) please include a link.
|
||||
* Any extra context you believe existed for the incident.
|
||||
* If you believe this incident is ongoing.
|
||||
* Any other information you believe we should have.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||
version 1.4, available at
|
||||
https://www.contributor-covenant.org/version/1/4/code-of-conduct.html, and
|
||||
includes some aspects of the Drupal Code of Conduct.
|
||||
@@ -113,6 +113,10 @@ Copyright:
|
||||
Copyright 2019, Dan Timson
|
||||
Copyright 2019, Miguel Canteras <mcanteras@gmail.com>
|
||||
Copyright 2019, Bishwa Karki <karkeebishwa1@gmail.com>
|
||||
Copyright 2019, Mehul Kumar Nirala <mehulkumarnirala@gmail.com>
|
||||
Copyright 2019, Yashwant Singh Parihar <yashwantsingh.sngh@gmail.com>
|
||||
Copyright 2019, Heet Sankesara <heetsankesara3@gmail.com>
|
||||
Copyright 2019, Jeffin Sam <sam.jeffin@gmail.com>
|
||||
|
||||
License: BSD-3-clause
|
||||
All rights reserved.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
# Project related configuration options
|
||||
#---------------------------------------------------------------------------
|
||||
PROJECT_NAME = mlpack
|
||||
PROJECT_NUMBER = 3.1.0
|
||||
PROJECT_NUMBER = 3.1.1
|
||||
OUTPUT_DIRECTORY = ./doc
|
||||
CREATE_SUBDIRS = NO
|
||||
OUTPUT_LANGUAGE = English
|
||||
|
||||
+1
-2
@@ -666,5 +666,4 @@
|
||||
* Initial release. See any resolved tickets numbered less than #196 or
|
||||
execute this query:
|
||||
http://www.mlpack.org/trac/query?status=closed&milestone=mlpack+1.0.0
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,10 @@ licensed under the Boost Software License, version 1.0. This code is found in
|
||||
src/mlpack/core/boost_backport/ and more details on the licensing are available
|
||||
there.
|
||||
|
||||
mlpack may contain some usage of the source code of stb, which is licensed
|
||||
under the MIT License and the Public Domain (www.unlicense.org). This code
|
||||
is used in src/mlpack/core/data/load_image.hpp.
|
||||
|
||||
----
|
||||
Copyright (c) 2007-2018, mlpack contributors (see COPYRIGHT.txt)
|
||||
All rights reserved.
|
||||
|
||||
@@ -23,7 +23,7 @@ src="https://cdn.rawgit.com/mlpack/mlpack.org/e7d36ed8/mlpack-black.svg" style="
|
||||
<p align="center">
|
||||
<em>
|
||||
Download:
|
||||
<a href="https://www.mlpack.org/files/mlpack-3.1.1.tar.gz">current stable version (3.1.1)</a>
|
||||
<a href="https://www.mlpack.org/files/mlpack-3.2.1.tar.gz">current stable version (3 2.1)</a>
|
||||
</em>
|
||||
</p>
|
||||
|
||||
@@ -83,10 +83,11 @@ Citations are beneficial for the growth and improvement of mlpack.
|
||||
|
||||
mlpack has the following dependencies:
|
||||
|
||||
Armadillo >= 6.500.0
|
||||
Armadillo >= 8.400.0
|
||||
Boost (program_options, math_c99, unit_test_framework, serialization,
|
||||
spirit)
|
||||
CMake >= 3.3.2
|
||||
ensmallen >= 2.10.0
|
||||
|
||||
All of those should be available in your distribution's package manager. If
|
||||
not, you will have to compile each of them by hand. See the documentation for
|
||||
@@ -100,6 +101,9 @@ following Python packages are installed:
|
||||
numpy
|
||||
pandas >= 0.15.0
|
||||
|
||||
If the STB library headers are available, image loading support will be
|
||||
compiled.
|
||||
|
||||
If you are compiling Armadillo by hand, ensure that LAPACK and BLAS are enabled.
|
||||
|
||||
### 4. Building mlpack from source
|
||||
@@ -160,10 +164,12 @@ Options are specified with the -D flag. The allowed options include:
|
||||
ARMADILLO_LIBRARY=(/path/to/armadillo/libarmadillo.so): Armadillo library
|
||||
BUILD_CLI_EXECUTABLES=(ON/OFF): whether or not to build command-line programs
|
||||
BUILD_PYTHON_BINDINGS=(ON/OFF): whether or not to build Python bindings
|
||||
PYTHON_EXECUTABLE=(/path/to/python_version): Path to specific Python executable
|
||||
BUILD_TESTS=(ON/OFF): whether or not to build tests
|
||||
BUILD_SHARED_LIBS=(ON/OFF): compile shared libraries as opposed to
|
||||
static libraries
|
||||
DOWNLOAD_ENSMALLEN=(ON/OFF): If ensmallen is not found, download it
|
||||
DOWNLOAD_STB_IMAGE=(ON/OFF): If STB is not found, download it
|
||||
ENSMALLEN_INCLUDE_DIR=(/path/to/ensmallen/include): path to include directory
|
||||
for ensmallen
|
||||
USE_OPENMP=(ON/OFF): whether or not to use OpenMP if available
|
||||
|
||||
@@ -104,16 +104,16 @@
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>false</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>C:\boost\boost_1_66_0;C:\mlpack\armadillo-8.500.1\include;C:\mlpack\mlpack-3.1.1\build\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>C:\boost\boost_1_66_0;C:\mlpack\armadillo-8.500.1\include;C:\mlpack\mlpack-3.2.1\build\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>C:\mlpack\mlpack-3.1.1\build\Debug\mlpack.lib;C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_serialization-vc141-mt-gd-x64-1_66.lib;C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_program_options-vc141-mt-gd-x64-1_66.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>C:\mlpack\mlpack-3.2.1\build\Debug\mlpack.lib;C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_serialization-vc141-mt-gd-x64-1_66.lib;C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_program_options-vc141-mt-gd-x64-1_66.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
<Command>xcopy /y "C:\mlpack\mlpack-3.1.1\build\Debug\mlpack.dll" $(OutDir)
|
||||
xcopy /y "C:\mlpack\mlpack-3.1.1\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir)
|
||||
<Command>xcopy /y "C:\mlpack\mlpack-3.2.1\build\Debug\mlpack.dll" $(OutDir)
|
||||
xcopy /y "C:\mlpack\mlpack-3.2.1\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir)
|
||||
xcopy /y "$(ProjectDir)..\..\..\..\src\mlpack\tests\data\german.csv" "$(ProjectDir)data\german.csv*"</Command>
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
|
||||
+20
-10
@@ -29,7 +29,7 @@ to build mlpack on Windows, see \ref build_windows (alternatively, you can read
|
||||
is based on older versions).
|
||||
|
||||
You can download the latest mlpack release from here:
|
||||
<a href="https://www.mlpack.org/files/mlpack-3.1.1.tar.gz">mlpack-3.1.1</a>
|
||||
<a href="https://www.mlpack.org/files/mlpack-3.2.1.tar.gz">mlpack-3.2.1</a>
|
||||
|
||||
@section build_simple Simple Linux build instructions
|
||||
|
||||
@@ -37,9 +37,9 @@ Assuming all dependencies are installed in the system, you can run the commands
|
||||
below directly to build and install mlpack.
|
||||
|
||||
@code
|
||||
$ wget https://www.mlpack.org/files/mlpack-3.1.1.tar.gz
|
||||
$ tar -xvzpf mlpack-3.1.1.tar.gz
|
||||
$ mkdir mlpack-3.1.1/build && cd mlpack-3.1.1/build
|
||||
$ wget https://www.mlpack.org/files/mlpack-3.2.1.tar.gz
|
||||
$ tar -xvzpf mlpack-3.2.1.tar.gz
|
||||
$ mkdir mlpack-3.2.1/build && cd mlpack-3.2.1/build
|
||||
$ cmake ../
|
||||
$ make -j4 # The -j is the number of cores you want to use for a build.
|
||||
$ sudo make install
|
||||
@@ -64,8 +64,8 @@ configure mlpack.
|
||||
First we should unpack the mlpack source and create a build directory.
|
||||
|
||||
@code
|
||||
$ tar -xvzpf mlpack-3.1.1.tar.gz
|
||||
$ cd mlpack-3.1.1
|
||||
$ tar -xvzpf mlpack-3.2.1.tar.gz
|
||||
$ cd mlpack-3.2.1
|
||||
$ mkdir build
|
||||
@endcode
|
||||
|
||||
@@ -76,9 +76,15 @@ The directory can have any name, not just 'build', but 'build' is sufficient.
|
||||
mlpack depends on the following libraries, which need to be installed on the
|
||||
system and have headers present:
|
||||
|
||||
- Armadillo >= 6.500.0 (with LAPACK support)
|
||||
- Armadillo >= 8.400.0 (with LAPACK support)
|
||||
- Boost (math_c99, program_options, serialization, unit_test_framework, heap,
|
||||
spirit) >= 1.49
|
||||
- ensmallen >= 2.10.0 (will be downloaded if not found)
|
||||
|
||||
In addition, mlpack has the following optional dependencies:
|
||||
|
||||
- STB: this will allow loading of images; the library is downloaded if not
|
||||
found and the CMake variable DOWNLOAD_STB_IMAGE is set to ON (the default)
|
||||
|
||||
For Python bindings, the following packages are required:
|
||||
|
||||
@@ -93,7 +99,8 @@ In Ubuntu and Debian, you can get all of these dependencies through apt:
|
||||
@code
|
||||
# apt-get install libboost-math-dev libboost-program-options-dev
|
||||
libboost-test-dev libboost-serialization-dev libarmadillo-dev binutils-dev
|
||||
python-pandas python-numpy cython python-setuptools
|
||||
python-pandas python-numpy cython python-setuptools libensmallen-dev
|
||||
libstb-dev
|
||||
@endcode
|
||||
|
||||
On Fedora, Red Hat, or CentOS, these same dependencies can be obtained via dnf:
|
||||
@@ -101,11 +108,12 @@ On Fedora, Red Hat, or CentOS, these same dependencies can be obtained via dnf:
|
||||
@code
|
||||
# dnf install boost-devel boost-test boost-program-options boost-math
|
||||
armadillo-devel binutils-devel python2-Cython python2-setuptools
|
||||
python2-numpy python2-pandas
|
||||
python2-numpy python2-pandas ensmallen-devel stbi-devel
|
||||
@endcode
|
||||
|
||||
(It's also possible to use python3 packages from the package manager---mlpack
|
||||
will work with either.)
|
||||
will work with either. Also, the ensmallen-devel package is only available in
|
||||
Fedora 29 or RHEL7 or newer.)
|
||||
|
||||
@section build_config Configuring CMake
|
||||
|
||||
@@ -146,8 +154,10 @@ The full list of options mlpack allows:
|
||||
(default OFF)
|
||||
- DOWNLOAD_ENSMALLEN=(ON/OFF): If ensmallen is not found, download it
|
||||
(default ON)
|
||||
- DOWNLOAD_STB_IMAGE=(ON/OFF): If STB is not found, download it (default ON)
|
||||
- BUILD_WITH_COVERAGE=(ON/OFF): Build with support for code coverage tools
|
||||
(gcc only) (default OFF)
|
||||
- PYTHON_EXECUTABLE=(/path/to/python_version): Path to specific Python executable
|
||||
- BUILD_MARKDOWN_BINDINGS=(ON/OFF): Build Markdown bindings for website
|
||||
documentation (default OFF)
|
||||
- MATHJAX=(ON/OFF): use MathJax for generated Doxygen documentation (default
|
||||
|
||||
@@ -17,11 +17,11 @@ Installing the mlpack bindings for Python is straightforward. It's easy to use
|
||||
conda or pip to do this:
|
||||
|
||||
@code{.sh}
|
||||
pip install mlpack/mlpack3
|
||||
pip install mlpack
|
||||
@endcode
|
||||
|
||||
@code{.sh}
|
||||
conda install -c mlpack mlpack
|
||||
conda install -c conda-forge mlpack
|
||||
@endcode
|
||||
|
||||
Otherwise, we can build the Python bindings from scratch, as follows. First we
|
||||
@@ -31,9 +31,9 @@ build and install mlpack. You can copy-paste the commands into your shell.
|
||||
@code{.sh}
|
||||
sudo apt-get install libboost-all-dev g++ cmake libarmadillo-dev python-pip wget
|
||||
sudo pip install cython setuptools distutils numpy pandas
|
||||
wget https://www.mlpack.org/files/mlpack-3.1.1.tar.gz
|
||||
tar -xvzpf mlpack-3.1.1.tar.gz
|
||||
mkdir -p mlpack-3.1.1/build/ && cd mlpack-3.1.1/build/
|
||||
wget https://www.mlpack.org/files/mlpack-3.2.1.tar.gz
|
||||
tar -xvzpf mlpack-3.2.1.tar.gz
|
||||
mkdir -p mlpack-3.2.1/build/ && cd mlpack-3.2.1/build/
|
||||
cmake ../ && make -j4 && sudo make install
|
||||
@endcode
|
||||
|
||||
|
||||
@@ -29,18 +29,18 @@ mlpack and dependencies in Release Mode).
|
||||
@code
|
||||
- C:\boost\boost_1_66_0
|
||||
- C:\mlpack\armadillo-8.500.1\include
|
||||
- C:\mlpack\mlpack-3.1.1\build\include
|
||||
- C:\mlpack\mlpack-3.2.1\build\include
|
||||
@endcode
|
||||
- Under Linker > Input > Additional Dependencies add:
|
||||
@code
|
||||
- C:\mlpack\mlpack-3.1.1\build\Debug\mlpack.lib
|
||||
- C:\mlpack\mlpack-3.2.1\build\Debug\mlpack.lib
|
||||
- C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_serialization-vc141-mt-gd-x64-1_66.lib
|
||||
- C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_program_options-vc141-mt-gd-x64-1_66.lib
|
||||
@endcode
|
||||
- Under Build Events > Post-Build Event > Command Line add:
|
||||
@code
|
||||
- xcopy /y "C:\mlpack\mlpack-3.1.1\build\Debug\mlpack.dll" $(OutDir)
|
||||
- xcopy /y "C:\mlpack\mlpack-3.1.1\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir)
|
||||
- xcopy /y "C:\mlpack\mlpack-3.2.1\build\Debug\mlpack.dll" $(OutDir)
|
||||
- xcopy /y "C:\mlpack\mlpack-3.2.1\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir)
|
||||
@endcode
|
||||
|
||||
@note Recent versions of Visual Studio set "Conformance Mode" enabled by default. This causes some issues with
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
/*!
|
||||
@file image.txt
|
||||
@author Mehul Kumar Nirala
|
||||
@brief Tutorial for how to load and save images in mlpack.
|
||||
|
||||
@page imagetutorial Image Utilities tutorial
|
||||
|
||||
@section intro_imagetut Introduction
|
||||
|
||||
Image datasets are becoming increasingly popular in deep learning.
|
||||
|
||||
mlpack's image saving/loading functionality is based on [stb/](https://github.com/nothings/stb).
|
||||
|
||||
@section toc_imagetut Table of Contents
|
||||
|
||||
This tutorial is split into the following sections:
|
||||
|
||||
- \ref intro_imagetut
|
||||
- \ref toc_imagetut
|
||||
- \ref model_api_imagetut
|
||||
- \ref imageinfo_api_imagetut
|
||||
- \ref load_api_imagetut
|
||||
- \ref save_api_imagetut
|
||||
|
||||
@section model_api_imagetut Model API
|
||||
|
||||
Image utilities supports loading and saving of images.
|
||||
|
||||
It supports filetypes "jpg", "png", "tga","bmp", "psd", "gif", "hdr", "pic", "pnm" for loading and "jpg", "png", "tga", "bmp", "hdr" for saving.
|
||||
|
||||
The datatype associated is unsigned char to support RGB values in the range 1-255. To feed data into the network typecast of `arma::Mat` may be required. Images are stored in matrix as (width * height * channels, NumberOfImages). Therefore imageMatrix.col(0) would be the first image if images are loaded in imageMatrix.
|
||||
|
||||
@section imageinfo_api_imagetut ImageInfo
|
||||
|
||||
ImageInfo class contains the metadata of the images.
|
||||
@code
|
||||
/**
|
||||
* Instantiate the ImageInfo object with the image width, height, channels.
|
||||
*
|
||||
* @param width Image width.
|
||||
* @param height Image height.
|
||||
* @param channels number of channels in the image.
|
||||
*/
|
||||
ImageInfo(const size_t width,
|
||||
const size_t height,
|
||||
const size_t channels);
|
||||
@endcode
|
||||
Other public memebers include:
|
||||
- flipVertical Flip the image vertical upon loading.
|
||||
- quality Compression of the image if saved as jpg (0-100).
|
||||
|
||||
@section load_api_imagetut Load
|
||||
|
||||
|
||||
Standalone loading of images.
|
||||
@code
|
||||
/**
|
||||
* Load the image file into the given matrix.
|
||||
*
|
||||
* @param filename Name of the image file.
|
||||
* @param matrix Matrix to load the image into.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Load(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose);
|
||||
@endcode
|
||||
|
||||
Loading a test image. It also fills up the ImageInfo class object.
|
||||
@code
|
||||
data::ImageInfo info;
|
||||
data::Load("test_image.png", matrix, info, false, true);
|
||||
@endcode
|
||||
|
||||
ImageInfo requires height, width, number of channels of the image.
|
||||
|
||||
@code
|
||||
size_t height = 64, width = 64, channels = 1;
|
||||
data::ImageInfo info(width, height, channels);
|
||||
@endcode
|
||||
|
||||
More than one image can be loaded into the same matrix.
|
||||
|
||||
Loading multiple images:
|
||||
|
||||
@code
|
||||
/**
|
||||
* Load the image file into the given matrix.
|
||||
*
|
||||
* @param files A vector consisting of filenames.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Load(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose);
|
||||
@endcode
|
||||
|
||||
@code
|
||||
data::ImageInfo info;
|
||||
std::vector<std::string>> files{"test_image1.bmp","test_image2.bmp"};
|
||||
data::load(files, matrix, info, false, true);
|
||||
@endcode
|
||||
|
||||
@section save_api_imagetut Save
|
||||
|
||||
Save images expects a matrix of type unsigned char in the form (width * height * channels, NumberOfImages).
|
||||
Just like load it can be used to save one image or multiple images. Besides image data it also expects the shape of the image as input (width, height, channels).
|
||||
|
||||
Saving one image:
|
||||
|
||||
@code
|
||||
/**
|
||||
* Save the image file from the given matrix.
|
||||
*
|
||||
* @param filename Name of the image file.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Save(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose);
|
||||
@endcode
|
||||
|
||||
@code
|
||||
data::ImageInfo info;
|
||||
info.width = info.height = 25;
|
||||
info.channels = 3;
|
||||
info.quality = 90;
|
||||
data::Save("test_image.bmp", matrix, info, false, true);
|
||||
@endcode
|
||||
|
||||
If the matrix contains more than one image, only the first one is saved.
|
||||
|
||||
Saving multiple images:
|
||||
|
||||
@code
|
||||
/**
|
||||
* Save the image file from the given matrix.
|
||||
*
|
||||
* @param files A vector consisting of filenames.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Save(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose);
|
||||
@endcode
|
||||
|
||||
@code
|
||||
data::ImageInfo info;
|
||||
info.width = info.height = 25;
|
||||
info.channels = 3;
|
||||
info.quality = 90;
|
||||
std::vector<std::string>> files{"test_image1.bmp", "test_image2.bmp"};
|
||||
data::Save(files, matrix, info, false, true);
|
||||
@endcode
|
||||
|
||||
Multiple images are saved according to the vector of filenames specified.
|
||||
|
||||
*/
|
||||
@@ -44,7 +44,7 @@ target_link_libraries(mlpack ${MLPACK_LIBRARIES})
|
||||
|
||||
set_target_properties(mlpack
|
||||
PROPERTIES
|
||||
VERSION 3.1
|
||||
VERSION 3.2
|
||||
SOVERSION 3
|
||||
)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
if (DEFINED ENV{DESTDIR})
|
||||
execute_process(COMMAND ${PYTHON_EXECUTABLE}
|
||||
"${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/setup.py" install
|
||||
--prefix=${CMAKE_INSTALL_PREFIX} --root="$ENV{DESTDIR}"
|
||||
--prefix=${CMAKE_INSTALL_PREFIX} --root=$ENV{DESTDIR}
|
||||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/src/mlpack/bindings/python/"
|
||||
RESULT_VARIABLE setup_res)
|
||||
else ()
|
||||
|
||||
@@ -108,5 +108,7 @@ setup(name='mlpack',
|
||||
cmdclass={ 'build_ext': build_ext },
|
||||
ext_modules = modules,
|
||||
setup_requires=['cython', 'pytest-runner'],
|
||||
tests_require=['pytest'],
|
||||
tests_require=['pytest>3;python_version>"3.4"', 'pytest>3,<=4.6;python_version<="3.4"',
|
||||
'more-itertools>=4.0.0,<6.0.0;python_version<="2.7"',
|
||||
'more-itertools>=4.0.0;python_version>"2.7"'],
|
||||
zip_safe = False)
|
||||
|
||||
@@ -10,6 +10,7 @@ set(SOURCES
|
||||
load_csv.hpp
|
||||
load_csv.cpp
|
||||
load.hpp
|
||||
load_image_impl.hpp
|
||||
load_model_impl.hpp
|
||||
load_vec_impl.hpp
|
||||
load_impl.hpp
|
||||
@@ -38,6 +39,7 @@ endforeach()
|
||||
# Add subdirectories.
|
||||
add_subdirectory(imputation_methods)
|
||||
add_subdirectory(map_policies)
|
||||
add_subdirectory(scaler_methods)
|
||||
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# parent scope).
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @file image_info.hpp
|
||||
* @author Mehul Kumar Nirala
|
||||
*
|
||||
* An image information holder.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_CORE_DATA_IMAGE_INFO_HPP
|
||||
#define MLPACK_CORE_DATA_IMAGE_INFO_HPP
|
||||
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
#include "extension.hpp"
|
||||
|
||||
#ifdef HAS_STB // Compile this only if stb is present.
|
||||
|
||||
#define STB_IMAGE_STATIC
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
#define STB_IMAGE_WRITE_STATIC
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include <stb_image_write.h>
|
||||
|
||||
#endif
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
#ifdef HAS_STB // Compile this only if stb is present.
|
||||
|
||||
/**
|
||||
* Checks if the given image filename is supported.
|
||||
*
|
||||
* @param filename Name of the image file.
|
||||
* @return Boolean value indicating success if it is an image.
|
||||
*/
|
||||
inline bool ImageFormatSupported(const std::string& fileName,
|
||||
const bool save = false);
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Implements meta-data of images required by data::Load and
|
||||
* data::Save for loading and saving images into arma::Mat.
|
||||
*/
|
||||
class ImageInfo
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Instantiate the ImageInfo object with the given image width, height,
|
||||
* number of channels and quality parameter.
|
||||
*
|
||||
* @param width Image width.
|
||||
* @param height Image height.
|
||||
* @param channels Number of channels in the image.
|
||||
* @param quality Compression of the image if saved as jpg (0 - 100).
|
||||
*/
|
||||
ImageInfo(const size_t width = 0,
|
||||
const size_t height = 0,
|
||||
const size_t channels = 3,
|
||||
const size_t quality = 90);
|
||||
|
||||
//! Get the image width.
|
||||
const size_t& Width() const { return width; }
|
||||
//! Modify the image width.
|
||||
size_t& Width() { return width; }
|
||||
//! Get the image height.
|
||||
|
||||
const size_t& Height() const { return height; }
|
||||
//! Modify the image height.
|
||||
size_t& Height() { return height; }
|
||||
|
||||
//! Get the image channels.
|
||||
const size_t& Channels() const { return channels; }
|
||||
//! Modify the image channels.
|
||||
size_t& Channels() { return channels; }
|
||||
|
||||
//! Get the image quality.
|
||||
const size_t& Quality() const { return quality; }
|
||||
//! Modify the image quality.
|
||||
size_t& Quality() { return quality; }
|
||||
|
||||
private:
|
||||
// To store the image width.
|
||||
size_t width;
|
||||
|
||||
// To store the image height.
|
||||
size_t height;
|
||||
|
||||
// To store the number of channels in the image.
|
||||
size_t channels;
|
||||
|
||||
// Compression of the image if saved as jpg (0 - 100).
|
||||
size_t quality;
|
||||
};
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation of Image.
|
||||
#include "image_info_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @file image_info_impl.hpp
|
||||
* @author Mehul Kumar Nirala
|
||||
*
|
||||
* An image information holder implementation.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_CORE_DATA_IMAGE_INFO_IMPL_HPP
|
||||
#define MLPACK_CORE_DATA_IMAGE_INFO_IMPL_HPP
|
||||
|
||||
#ifdef HAS_STB // Compile this only if stb is present.
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "image_info.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
static const std::vector<std::string> loadFileTypes({"jpg", "png", "tga",
|
||||
"bmp", "psd", "gif", "hdr", "pic", "pnm", "jpeg"});
|
||||
|
||||
static const std::vector<std::string> saveFileTypes({"jpg", "png", "tga",
|
||||
"bmp", "hdr"});
|
||||
|
||||
inline bool ImageFormatSupported(const std::string& fileName, const bool save)
|
||||
{
|
||||
if (save)
|
||||
{
|
||||
// Iterate over all supported file types that can be saved.
|
||||
for (auto extension : saveFileTypes)
|
||||
{
|
||||
if (extension == Extension(fileName))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Iterate over all supported file types that can be loaded.
|
||||
for (auto extension : loadFileTypes)
|
||||
{
|
||||
if (extension == Extension(fileName))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif // HAS_STB.
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
inline ImageInfo::ImageInfo(const size_t width,
|
||||
const size_t height,
|
||||
const size_t channels,
|
||||
const size_t quality) :
|
||||
width(width),
|
||||
height(height),
|
||||
channels(channels),
|
||||
quality(quality)
|
||||
{
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "format.hpp"
|
||||
#include "dataset_mapper.hpp"
|
||||
#include "image_info.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace data /** Functions to load and save matrices and models. */ {
|
||||
@@ -287,6 +288,47 @@ bool Load(const std::string& filename,
|
||||
const bool fatal = false,
|
||||
format f = format::autodetect);
|
||||
|
||||
/**
|
||||
* Image load/save interfaces.
|
||||
*/
|
||||
#ifdef HAS_STB
|
||||
|
||||
/**
|
||||
* Load the image file into the given matrix.
|
||||
*
|
||||
* @param filename Name of the image file.
|
||||
* @param matrix Matrix to load the image into.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Load(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true);
|
||||
|
||||
/**
|
||||
* Load the image file into the given matrix.
|
||||
*
|
||||
* @param files A vector consisting of filenames.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Load(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true);
|
||||
|
||||
#endif // HAS_STB.
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
@@ -294,5 +336,7 @@ bool Load(const std::string& filename,
|
||||
#include "load_model_impl.hpp"
|
||||
// Include implementation of Load() for vectors.
|
||||
#include "load_vec_impl.hpp"
|
||||
// Include implementation of Load() for images.
|
||||
#include "load_image_impl.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @file load_image_impl.hpp
|
||||
* @author Mehul Kumar Nirala
|
||||
*
|
||||
* An image loading utility implementation.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP
|
||||
#define MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "load.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
#ifdef HAS_STB // Compile this only if stb is present.
|
||||
|
||||
// Image loading API.
|
||||
template<typename eT>
|
||||
bool Load(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool /* fatal */,
|
||||
const bool transpose)
|
||||
{
|
||||
Timer::Start("loading_image");
|
||||
unsigned char* image;
|
||||
|
||||
if (!ImageFormatSupported(filename))
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "File type " << Extension(filename) << " not supported.\n";
|
||||
oss << "Currently it supports ";
|
||||
for (auto extension : loadFileTypes)
|
||||
oss << " " << extension;
|
||||
oss << std::endl;
|
||||
throw std::runtime_error(oss.str());
|
||||
return false;
|
||||
}
|
||||
|
||||
stbi_set_flip_vertically_on_load(transpose);
|
||||
|
||||
// Temporary variables needed as stb_image.h supports int parameters.
|
||||
int tempWidth, tempHeight, tempChannels;
|
||||
|
||||
// For grayscale images.
|
||||
if (info.Channels() == 1)
|
||||
{
|
||||
image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels,
|
||||
STBI_grey);
|
||||
}
|
||||
else
|
||||
{
|
||||
image = stbi_load(filename.c_str(), &tempWidth, &tempHeight, &tempChannels,
|
||||
STBI_rgb);
|
||||
}
|
||||
|
||||
if (tempWidth <= 0 || tempHeight <= 0)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Image '" << filename << "' not found." << std::endl;
|
||||
free(image);
|
||||
throw std::runtime_error(oss.str());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
info.Width() = tempWidth;
|
||||
info.Height() = tempHeight;
|
||||
info.Channels() = tempChannels;
|
||||
|
||||
// Copy image into armadillo Mat.
|
||||
matrix = arma::Mat<unsigned char>(image, info.Width() * info.Height() *
|
||||
info.Channels(), 1, true, true);
|
||||
|
||||
// Free the image pointer.
|
||||
free(image);
|
||||
Timer::Stop("loading_image");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Image loading API for multiple files.
|
||||
template<typename eT>
|
||||
bool Load(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose)
|
||||
{
|
||||
if (files.size() == 0)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Files vector is empty." << std::endl;
|
||||
|
||||
throw std::runtime_error(oss.str());
|
||||
return false;
|
||||
}
|
||||
|
||||
arma::Mat<unsigned char> img;
|
||||
bool status = Load(files[0], img, info, fatal, transpose);
|
||||
|
||||
// Decide matrix dimension using the image height and width.
|
||||
matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size());
|
||||
matrix.col(0) = img;
|
||||
|
||||
for (size_t i = 1; i < files.size() ; i++)
|
||||
{
|
||||
arma::Mat<unsigned char> colImg(matrix.colptr(i), matrix.n_rows, 1,
|
||||
false, true);
|
||||
status &= Load(files[i], colImg, info, fatal, transpose);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
#else // No STB.
|
||||
template<typename eT>
|
||||
bool Load(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true)
|
||||
{
|
||||
throw std::runtime_error("Load(): HAS_STB is not defined, "
|
||||
"so STB is not available and images cannot be loaded!");
|
||||
}
|
||||
|
||||
template<typename eT>
|
||||
bool Load(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true)
|
||||
{
|
||||
throw std::runtime_error("Load(): HAS_STB is not defined, "
|
||||
"so STB is not available and images cannot be loaded!");
|
||||
}
|
||||
#endif // HAS_STB.
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "format.hpp"
|
||||
#include "image_info.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace data /** Functions to load and save matrices. */ {
|
||||
@@ -90,6 +91,44 @@ bool Save(const std::string& filename,
|
||||
const bool fatal = false,
|
||||
format f = format::autodetect);
|
||||
|
||||
#ifdef HAS_STB
|
||||
|
||||
/**
|
||||
* Save the image file from the given matrix.
|
||||
*
|
||||
* @param filename Name of the image file.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Save(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true);
|
||||
|
||||
/**
|
||||
* Save the image file from the given matrix.
|
||||
*
|
||||
* @param files A vector consisting of filenames.
|
||||
* @param matrix Matrix to save the image from.
|
||||
* @param info An object of ImageInfo class.
|
||||
* @param fatal If an error should be reported as fatal (default false).
|
||||
* @param transpose If true, transpose the matrix after loading.
|
||||
* @return Boolean value indicating success or failure of load.
|
||||
*/
|
||||
template<typename eT>
|
||||
bool Save(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true);
|
||||
|
||||
#endif // HAS_STB.
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
|
||||
@@ -283,6 +283,147 @@ bool Save(const std::string& filename,
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAS_STB
|
||||
// Image saving API.
|
||||
template<typename eT>
|
||||
bool Save(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose)
|
||||
{
|
||||
Timer::Start("saving_image");
|
||||
// We transpose by default. So, un-transpose if necessary.
|
||||
if (!transpose)
|
||||
matrix = arma::trans(matrix);
|
||||
|
||||
int tempWidth, tempHeight, tempChannels, tempQuality;
|
||||
|
||||
tempWidth = info.Width();
|
||||
tempHeight = info.Height();
|
||||
tempChannels = info.Channels();
|
||||
tempQuality = info.Quality();
|
||||
|
||||
if (!ImageFormatSupported(filename, true))
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "File type " << Extension(filename) << " not supported.\n";
|
||||
oss << "Currently it supports ";
|
||||
for (auto extension : saveFileTypes)
|
||||
oss << ", " << extension;
|
||||
oss << std::endl;
|
||||
throw std::runtime_error(oss.str());
|
||||
return false;
|
||||
}
|
||||
if (matrix.n_cols > 1)
|
||||
{
|
||||
std::cout << "Input Matrix contains more than 1 image." << std::endl;
|
||||
std::cout << "Only the firstimage will be saved!" << std::endl;
|
||||
}
|
||||
stbi_flip_vertically_on_write(transpose);
|
||||
|
||||
bool status = false;
|
||||
try
|
||||
{
|
||||
unsigned char* image = matrix.memptr();
|
||||
|
||||
if ("png" == Extension(filename))
|
||||
{
|
||||
status = stbi_write_png(filename.c_str(), tempWidth, tempHeight,
|
||||
tempChannels, image, tempWidth * tempChannels);
|
||||
}
|
||||
else if ("bmp" == Extension(filename))
|
||||
{
|
||||
status = stbi_write_bmp(filename.c_str(), tempWidth, tempHeight,
|
||||
tempChannels, image);
|
||||
}
|
||||
else if ("tga" == Extension(filename))
|
||||
{
|
||||
status = stbi_write_tga(filename.c_str(), tempWidth, tempHeight,
|
||||
tempChannels, image);
|
||||
}
|
||||
else if ("hdr" == Extension(filename))
|
||||
{
|
||||
status = stbi_write_hdr(filename.c_str(), tempWidth, tempHeight,
|
||||
tempChannels, reinterpret_cast<float*>(image));
|
||||
}
|
||||
else if ("jpg" == Extension(filename))
|
||||
{
|
||||
status = stbi_write_jpg(filename.c_str(), tempWidth, tempHeight,
|
||||
tempChannels, image, tempQuality);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e)
|
||||
{
|
||||
Timer::Stop("saving_image");
|
||||
if (fatal)
|
||||
Log::Fatal << e.what() << std::endl;
|
||||
Log::Warn << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
Timer::Stop("saving_image");
|
||||
return status;
|
||||
}
|
||||
|
||||
// Image saving API for multiple files.
|
||||
template<typename eT>
|
||||
bool Save(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal,
|
||||
const bool transpose)
|
||||
{
|
||||
if (files.size() == 0)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Files vector is empty." << std::endl;
|
||||
|
||||
throw std::runtime_error(oss.str());
|
||||
return false;
|
||||
}
|
||||
// We transpose by default. So, un-transpose if necessary.
|
||||
if (!transpose)
|
||||
matrix = arma::trans(matrix);
|
||||
|
||||
arma::Mat<unsigned char> img;
|
||||
bool status = Save(files[0], img, info, fatal, transpose);
|
||||
|
||||
// Decide matrix dimension using the image height and width.
|
||||
matrix.set_size(info.Width() * info.Height() * info.Channels(), files.size());
|
||||
matrix.col(0) = img;
|
||||
|
||||
for (size_t i = 1; i < files.size() ; i++)
|
||||
{
|
||||
arma::Mat<unsigned char> colImg(matrix.colptr(i), matrix.n_rows, 1,
|
||||
false, true);
|
||||
status &= Save(files[i], colImg, info, fatal, transpose);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
#else
|
||||
template<typename eT>
|
||||
bool Save(const std::string& filename,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true)
|
||||
{
|
||||
throw std::runtime_error("Save(): HAS_STB is not defined, "
|
||||
"so STB is not available and images cannot be saved!");
|
||||
}
|
||||
|
||||
template<typename eT>
|
||||
bool Save(const std::vector<std::string>& files,
|
||||
arma::Mat<eT>& matrix,
|
||||
ImageInfo& info,
|
||||
const bool fatal = false,
|
||||
const bool transpose = true)
|
||||
{
|
||||
throw std::runtime_error("Save(): HAS_STB is not defined, "
|
||||
"so STB is not available and images cannot be saved!");
|
||||
}
|
||||
#endif // HAS_STB.
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# Define the files we need to compile
|
||||
# Anything not in this list will not be compiled into mlpack.
|
||||
set(SOURCES
|
||||
min_max_scaler.hpp
|
||||
max_abs_scaler.hpp
|
||||
standard_scaler.hpp
|
||||
mean_normalization.hpp
|
||||
pca_whitening.hpp
|
||||
zca_whitening.hpp
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
|
||||
endforeach()
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# the parent scope).
|
||||
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
|
||||
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* @file max_abs_scaler.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* MaxAbsScaler class to scale features.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_MAX_ABS_SCALE_HPP
|
||||
#define MLPACK_CORE_DATA_MAX_ABS_SCALE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple MaxAbs Scaler class.
|
||||
*
|
||||
* Given an input dataset this class helps you to scale each
|
||||
* feature by its maximum absolute value.
|
||||
*
|
||||
* \[z = x / max(abs(x))\]
|
||||
*
|
||||
* where max(abs(x)) is maximum absolute value of feature.
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* MaxAbsScaler scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class MaxAbsScaler
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
itemMin = arma::min(input, 1);
|
||||
itemMax = arma::max(input, 1);
|
||||
scale = arma::max(arma::abs(itemMin), arma::abs(itemMax));
|
||||
// Handling zeros in scale vector.
|
||||
scale.for_each([](arma::vec::elem_type& val) { val =
|
||||
(val == 0) ? 1 : val; });
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to scale features.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with scaled features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
if (scale.is_empty())
|
||||
{
|
||||
throw std::runtime_error("Call Fit() before Transform(), please"
|
||||
" refer to the documentation.");
|
||||
}
|
||||
output.copy_size(input);
|
||||
output = input.each_col() / scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output.copy_size(input);
|
||||
output = input.each_col() % scale;
|
||||
}
|
||||
|
||||
//! Get the Min row vector.
|
||||
const arma::vec& ItemMin() const { return itemMin; }
|
||||
//! Get the Max row vector.
|
||||
const arma::vec& ItemMax() const { return itemMax; }
|
||||
//! Get the Scale row vector.
|
||||
const arma::vec& Scale() const { return scale; }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMin);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMax);
|
||||
ar & BOOST_SERIALIZATION_NVP(scale);
|
||||
}
|
||||
private:
|
||||
// Vector which holds minimum of each feature.
|
||||
arma::vec itemMin;
|
||||
// Vector which holds maximum of each feature.
|
||||
arma::vec itemMax;
|
||||
// Vector which is used to scale up each feature.
|
||||
arma::vec scale;
|
||||
}; // class MaxAbsScaler
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @file mean_normalization.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* MeanNormalization class to scale features.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_MEAN_NORMALIZATION_HPP
|
||||
#define MLPACK_CORE_DATA_MEAN_NORMALIZATION_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple Mean Normalization class
|
||||
*
|
||||
* Given an input dataset this class helps you to normalize each
|
||||
* feature.
|
||||
*
|
||||
* \[z = x - average(x) / (max(x) - min(x))\]
|
||||
*
|
||||
* where x is an original value,z is the normalized value.
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* MeanNormalization scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class MeanNormalization
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
itemMean = arma::mean(input, 1);
|
||||
itemMin = arma::min(input, 1);
|
||||
itemMax = arma::max(input, 1);
|
||||
scale = itemMax - itemMin;
|
||||
// Handling zeros in scale vector.
|
||||
scale.for_each([](arma::vec::elem_type& val) { val =
|
||||
(val == 0) ? 1 : val; });
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to scale features.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with scaled features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
if (itemMean.is_empty() || scale.is_empty())
|
||||
{
|
||||
throw std::runtime_error("Call Fit() before Transform(), please"
|
||||
" refer to the documentation.");
|
||||
}
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() - itemMean).each_col() / scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() % scale).each_col() + itemMean;
|
||||
}
|
||||
|
||||
//! Get the Mean row vector.
|
||||
const arma::vec& ItemMean() const { return itemMean; }
|
||||
//! Get the Min row vector.
|
||||
const arma::vec& ItemMin() const { return itemMin; }
|
||||
//! Get the Max row vector.
|
||||
const arma::vec& ItemMax() const { return itemMax; }
|
||||
//! Get the Scale row vector.
|
||||
const arma::vec& Scale() const { return scale; }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMin);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMax);
|
||||
ar & BOOST_SERIALIZATION_NVP(scale);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMean);
|
||||
}
|
||||
|
||||
private:
|
||||
// Vector which holds mean of each feature.
|
||||
arma::vec itemMean;
|
||||
// Vector which holds minimum of each feature.
|
||||
arma::vec itemMin;
|
||||
// Vector which holds maximum of each feature.
|
||||
arma::vec itemMax;
|
||||
// Vector which is used to scale up each feature.
|
||||
arma::vec scale;
|
||||
}; // class MeanNormalization
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* @file min_max_scaler.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* MinMaxScaler class to scale features.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_SCALE_HPP
|
||||
#define MLPACK_CORE_DATA_SCALE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple MinMax Scaler class
|
||||
*
|
||||
* Given an input dataset this class helps you to scale each
|
||||
* feature to a given range.
|
||||
*
|
||||
* \[z = scale * x + scaleMin - min(x) * scale\]
|
||||
* \[scale = (scaleMax - scaleMin) / (max(x) - min(x))\]
|
||||
*
|
||||
* where scaleMin, scaleMax = feature_range and min(x), max(x)
|
||||
* are the minimum and maximum value of x respectively.
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* MinMaxScaler scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class MinMaxScaler
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param min Lower range of scaling.
|
||||
* @param max Upper range of scaling.
|
||||
*/
|
||||
MinMaxScaler(const double min = 0, const double max = 1)
|
||||
{
|
||||
scaleMin = min;
|
||||
scaleMax = max;
|
||||
// Ensure scaleMin is smaller than scaleMax.
|
||||
if (scaleMin > scaleMax)
|
||||
{
|
||||
throw std::runtime_error("Range is not appropriate");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
itemMin = arma::min(input, 1);
|
||||
itemMax = arma::max(input, 1);
|
||||
scale = itemMax - itemMin;
|
||||
// Handle zeros in scale vector.
|
||||
scale.for_each([](arma::vec::elem_type& val) { val =
|
||||
(val == 0) ? 1 : val; });
|
||||
scale = (scaleMax - scaleMin) / scale;
|
||||
scalerowmin.copy_size(itemMin);
|
||||
scalerowmin.fill(scaleMin);
|
||||
scalerowmin = scalerowmin - itemMin % scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to scale features.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with scaled features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
if (scalerowmin.is_empty() || scale.is_empty())
|
||||
{
|
||||
throw std::runtime_error("Call Fit() before Transform(), please"
|
||||
" refer to the documentation.");
|
||||
}
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() % scale).each_col() + scalerowmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() - scalerowmin).each_col() / scale;
|
||||
}
|
||||
|
||||
//! Get the Min row vector.
|
||||
const arma::vec& ItemMin() const { return itemMin; }
|
||||
//! Get the Max row vector.
|
||||
const arma::vec& ItemMax() const { return itemMax; }
|
||||
//! Get the Scale row vector.
|
||||
const arma::vec& Scale() const { return scale; }
|
||||
//! Get the upper range parameter.
|
||||
double ScaleMax() const { return scaleMax; }
|
||||
//! Get the lower range parameter.
|
||||
double ScaleMin() const { return scaleMin; }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMin);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMax);
|
||||
ar & BOOST_SERIALIZATION_NVP(scale);
|
||||
ar & BOOST_SERIALIZATION_NVP(scaleMin);
|
||||
ar & BOOST_SERIALIZATION_NVP(scaleMax);
|
||||
ar & BOOST_SERIALIZATION_NVP(scalerowmin);
|
||||
}
|
||||
|
||||
private:
|
||||
// Vector which holds minimum of each feature.
|
||||
arma::vec itemMin;
|
||||
// Vector which holds maximum of each feature.
|
||||
arma::vec itemMax;
|
||||
// Scale vector which is used to scale up each feature.
|
||||
arma::vec scale;
|
||||
// Lower value for range.
|
||||
double scaleMin;
|
||||
// Upper value for range.
|
||||
double scaleMax;
|
||||
// Column vector of scalemin
|
||||
arma::vec scalerowmin;
|
||||
}; // class MinMaxScaler
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* @file pca_whitening.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* Whitening scaling to scale features, Using PCA Whitening.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_PCA_WHITENING_SCALE_HPP
|
||||
#define MLPACK_CORE_DATA_PCA_WHITENING_SCALE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include <mlpack/core/math/lin_alg.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple PCAWhitening class.
|
||||
*
|
||||
* Whitens a matrix using the eigendecomposition of the covariance matrix.
|
||||
* Whitening means the covariance matrix of the result is the identity matrix.
|
||||
*
|
||||
* For whitening related formula and more info, check the link below.
|
||||
* http://ufldl.stanford.edu/tutorial/unsupervised/PCAWhitening/
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* PCAWhitening scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class PCAWhitening
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* A constructor to set the regularization parameter.
|
||||
*
|
||||
* @param eps Regularization parameter.
|
||||
*/
|
||||
PCAWhitening(double eps = 0.00005)
|
||||
{
|
||||
epsilon = eps;
|
||||
// Ensure scaleMin is smaller than scaleMax.
|
||||
if (epsilon < 0)
|
||||
{
|
||||
throw std::runtime_error("Regularization parameter is not correct");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
itemMean = arma::mean(input, 1);
|
||||
// Get eigenvectors and eigenvalues of covariance of input matrix.
|
||||
eig_sym(eigenValues, eigenVectors, mlpack::math::ColumnCovariance(
|
||||
input.each_col() - itemMean));
|
||||
eigenValues += epsilon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for PCA whitening.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with whitened features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
if (eigenValues.is_empty() || eigenVectors.is_empty())
|
||||
{
|
||||
throw std::runtime_error("Call Fit() before Transform(), please"
|
||||
" refer to the documentation.");
|
||||
}
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() - itemMean);
|
||||
output = arma::diagmat(1.0 / (arma::sqrt(eigenValues))) * eigenVectors.t()
|
||||
* output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output = arma::diagmat(arma::sqrt(eigenValues)) * inv(eigenVectors.t())
|
||||
* input;
|
||||
output = (output.each_col() + itemMean);
|
||||
}
|
||||
|
||||
//! Get the mean row vector.
|
||||
const arma::vec& ItemMean() const { return itemMean; }
|
||||
//! Get the eigenvalues vector.
|
||||
const arma::vec& EigenValues() const { return eigenValues; }
|
||||
//! Get the eigenvector.
|
||||
const arma::mat& EigenVectors() const { return eigenVectors; }
|
||||
//! Get the regularization parameter.
|
||||
const double& Epsilon() const { return epsilon; }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(eigenValues);
|
||||
ar & BOOST_SERIALIZATION_NVP(eigenVectors);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMean);
|
||||
ar & BOOST_SERIALIZATION_NVP(epsilon);
|
||||
}
|
||||
|
||||
private:
|
||||
// Vector which holds mean of each feature.
|
||||
arma::vec itemMean;
|
||||
// Mat which hold the eigenvectors.
|
||||
arma::mat eigenVectors;
|
||||
// Regularization Paramter.
|
||||
double epsilon;
|
||||
// Vector which hold the eigenvalues.
|
||||
arma::vec eigenValues;
|
||||
}; // class PCAWhitening
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* @file standard_scaler.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* StandardScaler class to scale features.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_STANDARD_SCALE_HPP
|
||||
#define MLPACK_CORE_DATA_STANDARD_SCALE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple Standard Scaler class
|
||||
*
|
||||
* Given an input dataset this class helps you to Standardize features
|
||||
* by removing the mean and scaling to unit variance.
|
||||
*
|
||||
* \[z = (x - u) / s\]
|
||||
*
|
||||
* where u is the mean of the training samples and s is the standard deviation
|
||||
* of the training samples.
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* StandardScaler scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class StandardScaler
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
itemMean = arma::mean(input, 1);
|
||||
itemStdDev = arma::stddev(input, 1, 1);
|
||||
// Handle zeros in scale vector.
|
||||
itemStdDev.for_each([](arma::vec::elem_type& val) { val =
|
||||
(val == 0) ? 1 : val; });
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to scale features.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with scaled features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
if (itemMean.is_empty() || itemStdDev.is_empty())
|
||||
{
|
||||
throw std::runtime_error("Call Fit() before Transform(), please"
|
||||
" refer to the documentation.");
|
||||
}
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() - itemMean).each_col() / itemStdDev;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output.copy_size(input);
|
||||
output = (input.each_col() % itemStdDev).each_col() + itemMean;
|
||||
}
|
||||
|
||||
//! Get the mean row vector.
|
||||
const arma::vec& ItemMean() const { return itemMean; }
|
||||
//! Get the standard deviation row vector.
|
||||
const arma::vec& ItemStdDev() const { return itemStdDev; }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(itemMean);
|
||||
ar & BOOST_SERIALIZATION_NVP(itemStdDev);
|
||||
}
|
||||
|
||||
private:
|
||||
// Vector which holds mean of each feature.
|
||||
arma::vec itemMean;
|
||||
// Vector which holds standard devation of each feature.
|
||||
arma::vec itemStdDev;
|
||||
}; // class StandardScaler
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* @file zca_whitening.hpp
|
||||
* @author Jeffin Sam
|
||||
*
|
||||
* Whitening scaling to scale features, Using ZCA Whitening.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_DATA_ZCA_WHITENING_SCALE_HPP
|
||||
#define MLPACK_CORE_DATA_ZCA_WHITENING_SCALE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include <mlpack/core/math/lin_alg.hpp>
|
||||
#include <mlpack/core/data/scaler_methods/pca_whitening.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace data {
|
||||
|
||||
/**
|
||||
* A simple ZCAWhitening class.
|
||||
*
|
||||
* Whitens a matrix using the eigendecomposition of the covariance matrix.
|
||||
* Whitening means the covariance matrix of the result is the identity matrix.
|
||||
*
|
||||
* For whitening related formula and more info, check the link below.
|
||||
* http://ufldl.stanford.edu/tutorial/unsupervised/PCAWhitening/
|
||||
*
|
||||
* @code
|
||||
* arma::mat input;
|
||||
* Load("train.csv", input);
|
||||
* arma::mat output;
|
||||
*
|
||||
* // Fit the features.
|
||||
* ZCAWhitening scale;
|
||||
* scale.Fit(input)
|
||||
*
|
||||
* // Scale the features.
|
||||
* scale.Transform(input, output);
|
||||
*
|
||||
* // Retransform the input.
|
||||
* scale.InverseTransform(output, input);
|
||||
* @endcode
|
||||
*/
|
||||
class ZCAWhitening
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* A constructor to set the regularization parameter.
|
||||
*
|
||||
* @param eps Regularization parameter.
|
||||
*/
|
||||
ZCAWhitening(double eps = 0.00005)
|
||||
{
|
||||
pca = new data::PCAWhitening(eps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to fit features, to find out the min max and scale.
|
||||
*
|
||||
* @param input Dataset to fit.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Fit(const MatType& input)
|
||||
{
|
||||
pca->Fit(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for ZCA whitening.
|
||||
*
|
||||
* @param input Dataset to scale features.
|
||||
* @param output Output matrix with whitened features.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Transform(const MatType& input, MatType& output)
|
||||
{
|
||||
pca->Transform(input, output);
|
||||
output = pca->EigenVectors() * output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to retrieve original dataset.
|
||||
*
|
||||
* @param input Scaled dataset.
|
||||
* @param output Output matrix with original Dataset.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void InverseTransform(const MatType& input, MatType& output)
|
||||
{
|
||||
output = inv(pca->EigenVectors()) * arma::diagmat(arma::sqrt(
|
||||
pca->EigenValues())) * inv(pca->EigenVectors().t()) * input;
|
||||
output = (output.each_col() + pca->ItemMean());
|
||||
}
|
||||
|
||||
//! Get the mean row vector.
|
||||
const arma::vec& ItemMean() const { return pca->ItemMean(); }
|
||||
//! Get the eigenvalues vector.
|
||||
const arma::vec& EigenValues() const { return pca->EigenValues(); }
|
||||
//! Get the eigenvector.
|
||||
const arma::mat& EigenVectors() const { return pca->EigenVectors(); }
|
||||
//! Get the regularization parameter.
|
||||
double Epsilon() const { return pca->Epsilon(); }
|
||||
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(pca);
|
||||
}
|
||||
|
||||
private:
|
||||
// A pointer to PcaWhitening Class.
|
||||
PCAWhitening* pca;
|
||||
}; // class ZCAWhitening
|
||||
|
||||
} // namespace data
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -168,6 +168,12 @@ class GaussianDistribution
|
||||
|
||||
void Covariance(arma::mat&& covariance);
|
||||
|
||||
//! Return the invCov.
|
||||
const arma::mat& InvCov() const { return invCov; }
|
||||
|
||||
//! Return the logDetCov.
|
||||
double LogDetCov() const { return logDetCov; }
|
||||
|
||||
/**
|
||||
* Serialize the distribution.
|
||||
*/
|
||||
|
||||
@@ -63,6 +63,13 @@ inline void FixedRandomSeed()
|
||||
srand((unsigned int) seed);
|
||||
arma::arma_rng::set_seed(seed);
|
||||
}
|
||||
|
||||
inline void CustomRandomSeed(const size_t seed)
|
||||
{
|
||||
randGen.seed((uint32_t) seed);
|
||||
srand((unsigned int) seed);
|
||||
arma::arma_rng::set_seed(seed);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,7 +61,7 @@ typename VecTypeA::elem_type LMetric<2, true>::Evaluate(
|
||||
const VecTypeA& a,
|
||||
const VecTypeB& b)
|
||||
{
|
||||
return sqrt(arma::accu(square(a - b)));
|
||||
return arma::norm(a - b, 2);
|
||||
}
|
||||
|
||||
template<>
|
||||
|
||||
@@ -56,6 +56,18 @@ DualTreeTraverser<RuleType>::Traverse(
|
||||
// Store the current traversal info.
|
||||
traversalInfo = rule.TraversalInfo();
|
||||
|
||||
// If both nodes are root nodes, just score them.
|
||||
if (queryNode.Parent() == NULL && referenceNode.Parent() == NULL)
|
||||
{
|
||||
const double rootScore = rule.Score(queryNode, referenceNode);
|
||||
// If root score is DBL_MAX, don't recurse.
|
||||
if (rootScore == DBL_MAX)
|
||||
{
|
||||
++numPrunes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If both are leaves, we must evaluate the base case.
|
||||
if (queryNode.IsLeaf() && referenceNode.IsLeaf())
|
||||
{
|
||||
|
||||
@@ -57,6 +57,18 @@ SingleTreeTraverser<RuleType>::Traverse(
|
||||
}
|
||||
else
|
||||
{
|
||||
// If it's the root node, just score it.
|
||||
if (referenceNode.Parent() == NULL)
|
||||
{
|
||||
const double rootScore = rule.Score(queryIndex, referenceNode);
|
||||
// If root score is DBL_MAX, don't recurse into that node.
|
||||
if (rootScore == DBL_MAX)
|
||||
{
|
||||
++numPrunes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If either score is DBL_MAX, we do not recurse into that node.
|
||||
double leftScore = rule.Score(queryIndex, *referenceNode.Left());
|
||||
double rightScore = rule.Score(queryIndex, *referenceNode.Right());
|
||||
|
||||
@@ -21,6 +21,18 @@
|
||||
namespace mlpack {
|
||||
namespace tree {
|
||||
|
||||
// Build the statistics, bottom-up.
|
||||
template<typename TreeType, typename StatisticType>
|
||||
void BuildStatistics(TreeType* node)
|
||||
{
|
||||
// Recurse first.
|
||||
for (size_t i = 0; i < node->NumChildren(); ++i)
|
||||
BuildStatistics<TreeType, StatisticType>(&node->Child(i));
|
||||
|
||||
// Now build the statistic.
|
||||
node->Stat() = StatisticType(*node);
|
||||
}
|
||||
|
||||
// Create the cover tree.
|
||||
template<
|
||||
typename MetricType,
|
||||
@@ -88,9 +100,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
{
|
||||
children.push_back(&(old->Child(i)));
|
||||
|
||||
// Set its parent correctly, and rebuild the statistic.
|
||||
// Set its parent correctly.
|
||||
old->Child(i).Parent() = this;
|
||||
old->Child(i).Stat() = StatisticType(old->Child(i));
|
||||
}
|
||||
|
||||
// Remove all the children so they don't get erased.
|
||||
@@ -104,14 +115,19 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
}
|
||||
|
||||
// Use the furthest descendant distance to determine the scale of the root
|
||||
// node.
|
||||
if (furthestDescendantDistance == 0.0)
|
||||
// node. Note that if the root is a leaf, we can have scale INT_MIN, but if
|
||||
// it *isn't* a leaf, we need to mark the scale as one higher than INT_MIN, so
|
||||
// that the recursions don't fail.
|
||||
if (furthestDescendantDistance == 0.0 && dataset.n_cols == 1)
|
||||
scale = INT_MIN;
|
||||
else if (furthestDescendantDistance == 0.0)
|
||||
scale = INT_MIN + 1;
|
||||
else
|
||||
scale = (int) ceil(log(furthestDescendantDistance) / log(base));
|
||||
|
||||
// Initialize statistic.
|
||||
stat = StatisticType(*this);
|
||||
// Initialize statistics recursively after the entire tree construction is
|
||||
// complete.
|
||||
BuildStatistics<CoverTree, StatisticType>(this);
|
||||
|
||||
Log::Info << distanceComps << " distance computations during tree "
|
||||
<< "construction." << std::endl;
|
||||
@@ -181,8 +197,6 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
|
||||
// Set its parent correctly.
|
||||
old->Child(i).Parent() = this;
|
||||
// Rebuild the statistic.
|
||||
old->Child(i).Stat() = StatisticType(old->Child(i));
|
||||
}
|
||||
|
||||
// Remove all the children so they don't get erased.
|
||||
@@ -196,14 +210,19 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
}
|
||||
|
||||
// Use the furthest descendant distance to determine the scale of the root
|
||||
// node.
|
||||
if (furthestDescendantDistance == 0.0)
|
||||
// node. Note that if the root is a leaf, we can have scale INT_MIN, but if
|
||||
// it *isn't* a leaf, we need to mark the scale as one higher than INT_MIN, so
|
||||
// that the recursions don't fail.
|
||||
if (furthestDescendantDistance == 0.0 && dataset.n_cols == 1)
|
||||
scale = INT_MIN;
|
||||
else if (furthestDescendantDistance == 0.0)
|
||||
scale = INT_MIN + 1;
|
||||
else
|
||||
scale = (int) ceil(log(furthestDescendantDistance) / log(base));
|
||||
|
||||
// Initialize statistic.
|
||||
stat = StatisticType(*this);
|
||||
// Initialize statistics recursively after the entire tree construction is
|
||||
// complete.
|
||||
BuildStatistics<CoverTree, StatisticType>(this);
|
||||
|
||||
Log::Info << distanceComps << " distance computations during tree "
|
||||
<< "construction." << std::endl;
|
||||
@@ -272,9 +291,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
{
|
||||
children.push_back(&(old->Child(i)));
|
||||
|
||||
// Set its parent correctly, and rebuild the statistic.
|
||||
// Set its parent correctly.
|
||||
old->Child(i).Parent() = this;
|
||||
old->Child(i).Stat() = StatisticType(old->Child(i));
|
||||
}
|
||||
|
||||
// Remove all the children so they don't get erased.
|
||||
@@ -288,14 +306,19 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
}
|
||||
|
||||
// Use the furthest descendant distance to determine the scale of the root
|
||||
// node.
|
||||
if (furthestDescendantDistance == 0.0)
|
||||
// node. Note that if the root is a leaf, we can have scale INT_MIN, but if
|
||||
// it *isn't* a leaf, we need to mark the scale as one higher than INT_MIN, so
|
||||
// that the recursions don't fail.
|
||||
if (furthestDescendantDistance == 0.0 && dataset->n_cols == 1)
|
||||
scale = INT_MIN;
|
||||
else if (furthestDescendantDistance == 0.0)
|
||||
scale = INT_MIN + 1;
|
||||
else
|
||||
scale = (int) ceil(log(furthestDescendantDistance) / log(base));
|
||||
|
||||
// Initialize statistic.
|
||||
stat = StatisticType(*this);
|
||||
// Initialize statistics recursively after the entire tree construction is
|
||||
// complete.
|
||||
BuildStatistics<CoverTree, StatisticType>(this);
|
||||
|
||||
Log::Info << distanceComps << " distance computations during tree "
|
||||
<< "construction." << std::endl;
|
||||
@@ -363,9 +386,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
{
|
||||
children.push_back(&(old->Child(i)));
|
||||
|
||||
// Set its parent correctly, and rebuild the statistic.
|
||||
// Set its parent correctly.
|
||||
old->Child(i).Parent() = this;
|
||||
old->Child(i).Stat() = StatisticType(old->Child(i));
|
||||
}
|
||||
|
||||
// Remove all the children so they don't get erased.
|
||||
@@ -379,14 +401,19 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
}
|
||||
|
||||
// Use the furthest descendant distance to determine the scale of the root
|
||||
// node.
|
||||
if (furthestDescendantDistance == 0.0)
|
||||
// node. Note that if the root is a leaf, we can have scale INT_MIN, but if
|
||||
// it *isn't* a leaf, we need to mark the scale as one higher than INT_MIN, so
|
||||
// that the recursions don't fail.
|
||||
if (furthestDescendantDistance == 0.0 && dataset->n_cols == 1)
|
||||
scale = INT_MIN;
|
||||
else if (furthestDescendantDistance == 0.0)
|
||||
scale = INT_MIN + 1;
|
||||
else
|
||||
scale = (int) ceil(log(furthestDescendantDistance) / log(base));
|
||||
|
||||
// Initialize statistic.
|
||||
stat = StatisticType(*this);
|
||||
// Initialize statistics recursively after the entire tree construction is
|
||||
// complete.
|
||||
BuildStatistics<CoverTree, StatisticType>(this);
|
||||
|
||||
Log::Info << distanceComps << " distance computations during tree "
|
||||
<< "construction." << std::endl;
|
||||
@@ -429,15 +456,11 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
{
|
||||
this->scale = INT_MIN;
|
||||
numDescendants = 1;
|
||||
stat = StatisticType(*this);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, create the children.
|
||||
CreateChildren(indices, distances, nearSetSize, farSetSize, usedSetSize);
|
||||
|
||||
// Initialize statistic.
|
||||
stat = StatisticType(*this);
|
||||
}
|
||||
|
||||
// Manually create a cover tree node.
|
||||
@@ -472,9 +495,6 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
|
||||
// If necessary, create a local metric.
|
||||
if (localMetric)
|
||||
this->metric = new MetricType();
|
||||
|
||||
// Initialize the statistic.
|
||||
stat = StatisticType(*this);
|
||||
}
|
||||
|
||||
template<
|
||||
@@ -907,8 +927,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
|
||||
other.Dataset().col(other.Point()));
|
||||
|
||||
math::RangeType<ElemType> result;
|
||||
result.Lo() = distance - furthestDescendantDistance -
|
||||
other.FurthestDescendantDistance();
|
||||
result.Lo() = std::max(distance - furthestDescendantDistance -
|
||||
other.FurthestDescendantDistance(), 0.0);
|
||||
result.Hi() = distance + furthestDescendantDistance +
|
||||
other.FurthestDescendantDistance();
|
||||
|
||||
@@ -930,8 +950,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
|
||||
const ElemType distance) const
|
||||
{
|
||||
math::RangeType<ElemType> result;
|
||||
result.Lo() = distance - furthestDescendantDistance -
|
||||
other.FurthestDescendantDistance();
|
||||
result.Lo() = std::max(distance - furthestDescendantDistance -
|
||||
other.FurthestDescendantDistance(), 0.0);
|
||||
result.Hi() = distance + furthestDescendantDistance +
|
||||
other.FurthestDescendantDistance();
|
||||
|
||||
@@ -952,8 +972,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
|
||||
{
|
||||
const ElemType distance = metric->Evaluate(dataset->col(point), other);
|
||||
|
||||
return math::RangeType<ElemType>(distance - furthestDescendantDistance,
|
||||
distance + furthestDescendantDistance);
|
||||
return math::RangeType<ElemType>(
|
||||
std::max(distance - furthestDescendantDistance, 0.0),
|
||||
distance + furthestDescendantDistance);
|
||||
}
|
||||
|
||||
//! Return the minimum and maximum distance to another point given that the
|
||||
@@ -970,8 +991,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
|
||||
RangeDistance(const arma::vec& /* other */,
|
||||
const ElemType distance) const
|
||||
{
|
||||
return math::RangeType<ElemType>(distance - furthestDescendantDistance,
|
||||
distance + furthestDescendantDistance);
|
||||
return math::RangeType<ElemType>(
|
||||
std::max(distance - furthestDescendantDistance, 0.0),
|
||||
distance + furthestDescendantDistance);
|
||||
}
|
||||
|
||||
//! For a newly initialized node, create children using the near and far set.
|
||||
@@ -1526,11 +1548,10 @@ inline void CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
|
||||
// Now take its child.
|
||||
children.push_back(&(old->Child(0)));
|
||||
|
||||
// Set its parent and parameters correctly, and rebuild the statistic.
|
||||
// Set its parent and parameters correctly.
|
||||
old->Child(0).Parent() = this;
|
||||
old->Child(0).ParentDistance() = old->ParentDistance();
|
||||
old->Child(0).DistanceComps() = old->DistanceComps();
|
||||
old->Child(0).Stat() = StatisticType(old->Child(0));
|
||||
|
||||
// Remove its child (so it doesn't delete it).
|
||||
old->Children().erase(old->Children().begin() + old->Children().size() - 1);
|
||||
|
||||
@@ -42,6 +42,18 @@ void Octree<MetricType, StatisticType, MatType>::DualTreeTraverser<RuleType>::
|
||||
// Store the current traversal info.
|
||||
traversalInfo = rule.TraversalInfo();
|
||||
|
||||
// If both nodes are root nodes, just score them.
|
||||
if (queryNode.Parent() == NULL && referenceNode.Parent() == NULL)
|
||||
{
|
||||
const double rootScore = rule.Score(queryNode, referenceNode);
|
||||
// If root score is DBL_MAX, don't recurse.
|
||||
if (rootScore == DBL_MAX)
|
||||
{
|
||||
++numPrunes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (queryNode.IsLeaf() && referenceNode.IsLeaf())
|
||||
{
|
||||
const size_t begin = queryNode.Point(0);
|
||||
|
||||
@@ -22,7 +22,8 @@ template<typename MetricType, typename StatisticType, typename MatType>
|
||||
template<typename RuleType>
|
||||
Octree<MetricType, StatisticType, MatType>::SingleTreeTraverser<RuleType>::
|
||||
SingleTreeTraverser(RuleType& rule) :
|
||||
rule(rule)
|
||||
rule(rule),
|
||||
numPrunes(0)
|
||||
{
|
||||
// Nothing to do.
|
||||
}
|
||||
@@ -42,6 +43,18 @@ void Octree<MetricType, StatisticType, MatType>::SingleTreeTraverser<RuleType>::
|
||||
}
|
||||
else
|
||||
{
|
||||
// If it's the root node, just score it.
|
||||
if (referenceNode.Parent() == NULL)
|
||||
{
|
||||
const double rootScore = rule.Score(queryIndex, referenceNode);
|
||||
// If root score is DBL_MAX, don't recurse into that node.
|
||||
if (rootScore == DBL_MAX)
|
||||
{
|
||||
++numPrunes;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Do a prioritized recursion, by scoring all candidates and then sorting
|
||||
// them.
|
||||
arma::vec scores(referenceNode.NumChildren());
|
||||
|
||||
@@ -181,10 +181,24 @@ class RectangleTree
|
||||
/**
|
||||
* Create a rectangle tree by moving the other tree.
|
||||
*
|
||||
* @param other The tree to be copied.
|
||||
* @param other The tree to be moved.
|
||||
*/
|
||||
RectangleTree(RectangleTree&& other);
|
||||
|
||||
/**
|
||||
* Copy the given rectangle tree.
|
||||
*
|
||||
* @param other The tree to be copied.
|
||||
*/
|
||||
RectangleTree& operator=(const RectangleTree& other);
|
||||
|
||||
/**
|
||||
* Take ownership of the given rectangle tree.
|
||||
*
|
||||
* @param other The tree to take ownership of.
|
||||
*/
|
||||
RectangleTree& operator=(RectangleTree&& other);
|
||||
|
||||
/**
|
||||
* Construct the tree from a boost::serialization archive.
|
||||
*/
|
||||
@@ -542,6 +556,13 @@ class RectangleTree
|
||||
*/
|
||||
void SplitNode(std::vector<bool>& relevels);
|
||||
|
||||
/**
|
||||
* Builds statistics for a node and all its descendants in a bottom-up way.
|
||||
*
|
||||
* @param node Node for which statistics will be built.
|
||||
*/
|
||||
void BuildStatistics(RectangleTree* node);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* A default constructor. This is meant to only be used with
|
||||
|
||||
@@ -21,6 +21,25 @@
|
||||
namespace mlpack {
|
||||
namespace tree {
|
||||
|
||||
// Build the statistics, bottom-up.
|
||||
template<typename MetricType,
|
||||
typename StatisticType,
|
||||
typename MatType,
|
||||
typename SplitType,
|
||||
typename DescentType,
|
||||
template<typename> class AuxiliaryInformationType>
|
||||
void RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
|
||||
AuxiliaryInformationType>::
|
||||
BuildStatistics(RectangleTree* node)
|
||||
{
|
||||
// Recurse first.
|
||||
for (size_t i = 0; i < node->NumChildren(); ++i)
|
||||
BuildStatistics(&node->Child(i));
|
||||
|
||||
// Now build the statistic.
|
||||
node->Stat() = StatisticType(*node);
|
||||
}
|
||||
|
||||
template<typename MetricType,
|
||||
typename StatisticType,
|
||||
typename MatType,
|
||||
@@ -52,13 +71,14 @@ RectangleTree(const MatType& data,
|
||||
points(maxLeafSize + 1), // Add one to make splitting the node simpler.
|
||||
auxiliaryInfo(this)
|
||||
{
|
||||
stat = StatisticType(*this);
|
||||
|
||||
// For now, just insert the points in order.
|
||||
RectangleTree* root = this;
|
||||
|
||||
for (size_t i = firstDataIndex; i < data.n_cols; i++)
|
||||
root->InsertPoint(i);
|
||||
|
||||
// Initialize statistic recursively after tree construction is complete.
|
||||
BuildStatistics(this);
|
||||
}
|
||||
|
||||
template<typename MetricType,
|
||||
@@ -92,13 +112,14 @@ RectangleTree(MatType&& data,
|
||||
points(maxLeafSize + 1), // Add one to make splitting the node simpler.
|
||||
auxiliaryInfo(this)
|
||||
{
|
||||
stat = StatisticType(*this);
|
||||
|
||||
// For now, just insert the points in order.
|
||||
RectangleTree* root = this;
|
||||
|
||||
for (size_t i = firstDataIndex; i < dataset->n_cols; i++)
|
||||
root->InsertPoint(i);
|
||||
|
||||
// Initialize statistic recursively after tree construction is complete.
|
||||
BuildStatistics(this);
|
||||
}
|
||||
|
||||
template<typename MetricType,
|
||||
@@ -131,7 +152,8 @@ RectangleTree(
|
||||
points(maxLeafSize + 1), // Add one to make splitting the node simpler.
|
||||
auxiliaryInfo(this)
|
||||
{
|
||||
stat = StatisticType(*this);
|
||||
// Initialize statistic.
|
||||
BuildStatistics(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -234,6 +256,96 @@ RectangleTree(RectangleTree&& other) :
|
||||
other.ownsDataset = false;
|
||||
}
|
||||
|
||||
template<typename MetricType,
|
||||
typename StatisticType,
|
||||
typename MatType,
|
||||
typename SplitType,
|
||||
typename DescentType,
|
||||
template<typename> class AuxiliaryInformationType>
|
||||
RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
|
||||
AuxiliaryInformationType>&
|
||||
RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
|
||||
AuxiliaryInformationType>::
|
||||
operator=(const RectangleTree& other)
|
||||
{
|
||||
// Return if it's the same tree.
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
for (size_t i = 0; i < numChildren; i++)
|
||||
delete children[i];
|
||||
|
||||
if (ownsDataset)
|
||||
delete dataset;
|
||||
|
||||
maxNumChildren = other.MaxNumChildren();
|
||||
minNumChildren = other.MinNumChildren();
|
||||
numChildren = other.NumChildren();
|
||||
children.resize(maxNumChildren + 1, NULL);
|
||||
parent = NULL;
|
||||
begin = other.Begin();
|
||||
count = other.Count();
|
||||
numDescendants = other.numDescendants;
|
||||
maxLeafSize = other.MaxLeafSize();
|
||||
minLeafSize = other.MinLeafSize();
|
||||
bound = other.bound;
|
||||
parentDistance = other.ParentDistance();
|
||||
dataset = new MatType(*other.dataset);
|
||||
ownsDataset = true;
|
||||
points = other.points;
|
||||
auxiliaryInfo = AuxiliaryInfoType(other.auxiliaryInfo, this, true);
|
||||
|
||||
if (numChildren > 0)
|
||||
{
|
||||
for (size_t i = 0; i < numChildren; i++)
|
||||
children[i] = new RectangleTree(other.Child(i), true, this);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename MetricType,
|
||||
typename StatisticType,
|
||||
typename MatType,
|
||||
typename SplitType,
|
||||
typename DescentType,
|
||||
template<typename> class AuxiliaryInformationType>
|
||||
RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
|
||||
AuxiliaryInformationType>&
|
||||
RectangleTree<MetricType, StatisticType, MatType, SplitType, DescentType,
|
||||
AuxiliaryInformationType>::
|
||||
operator=(RectangleTree&& other)
|
||||
{
|
||||
// Return if it's the same tree.
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
for (size_t i = 0; i < numChildren; i++)
|
||||
delete children[i];
|
||||
|
||||
if (ownsDataset)
|
||||
delete dataset;
|
||||
|
||||
maxNumChildren = other.MaxNumChildren();
|
||||
minNumChildren = other.MinNumChildren();
|
||||
numChildren = other.NumChildren();
|
||||
children = std::move(other.children);
|
||||
parent = other.Parent();
|
||||
begin = other.Begin();
|
||||
count = other.Count();
|
||||
numDescendants = other.numDescendants;
|
||||
maxLeafSize = other.MaxLeafSize();
|
||||
minLeafSize = other.MinLeafSize();
|
||||
bound = std::move(other.bound);
|
||||
parentDistance = other.ParentDistance();
|
||||
dataset = other.dataset;
|
||||
ownsDataset = other.ownsDataset;
|
||||
points = std::move(other.points);
|
||||
auxiliaryInfo = std::move(other.auxiliaryInfo);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct the tree from a boost::serialization archive.
|
||||
*/
|
||||
|
||||
@@ -36,28 +36,22 @@ ARMA_64BIT_WORD disabled."
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Check if OpenMP was enabled when mlpack was built. This only matters for
|
||||
// Armadillo 8.300.1 and newer.
|
||||
#if (ARMA_VERSION_MAJOR > 8) || \
|
||||
((ARMA_VERSION_MAJOR == 8) && (ARMA_VERSION_MINOR > 300)) || \
|
||||
((ARMA_VERSION_MAJOR == 8) && (ARMA_VERSION_MINOR == 300) && \
|
||||
(ARMA_VERSION_PATCH >= 1))
|
||||
#ifdef ARMA_USE_OPENMP
|
||||
#ifdef MLPACK_ARMA_DONT_USE_OPENMP
|
||||
#pragma message "mlpack was compiled without OpenMP support, but you are \
|
||||
// Check if OpenMP was enabled when mlpack was built.
|
||||
#ifdef ARMA_USE_OPENMP
|
||||
#ifdef MLPACK_ARMA_DONT_USE_OPENMP
|
||||
#pragma message "mlpack was compiled without OpenMP support, but you are \
|
||||
compiling with OpenMP support (either -fopenmp or another option). This will \
|
||||
almost certainly cause irreparable disaster. Either compile your application \
|
||||
*without* OpenMP support (i.e. remove -fopenmp or another flag), or, recompile \
|
||||
mlpack with OpenMP support."
|
||||
#endif
|
||||
#else
|
||||
#ifdef MLPACK_ARMA_USE_OPENMP
|
||||
#pragma message "mlpack was compiled with OpenMP support, but you are \
|
||||
#endif
|
||||
#else
|
||||
#ifdef MLPACK_ARMA_USE_OPENMP
|
||||
#pragma message "mlpack was compiled with OpenMP support, but you are \
|
||||
compiling without OpenMP support. This will almost certainly cause \
|
||||
irreparable disaster. Either enable OpenMP support in your application (e.g., \
|
||||
add -fopenmp to your compiler command line), or, recompile mlpack *without* \
|
||||
OpenMP support."
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
@@ -36,11 +36,43 @@
|
||||
#include <mlpack/bindings/cli/cli_option.hpp>
|
||||
#include <mlpack/bindings/cli/print_doc_functions.hpp>
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_STRING() returns a string that contains the correct
|
||||
* language-specific representation of a parameter's name.
|
||||
*/
|
||||
#define PRINT_PARAM_STRING mlpack::bindings::cli::ParamString
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_VALUE() returns a string that contains a correct
|
||||
* language-specific representation of a parameter's value.
|
||||
*/
|
||||
#define PRINT_PARAM_VALUE mlpack::bindings::cli::PrintValue
|
||||
|
||||
/**
|
||||
* PRINT_CALL() returns a string that contains the full language-specific
|
||||
* representation of a call to an mlpack binding. The first argument should be
|
||||
* the name of the binding, and all other arguments should be names of
|
||||
* parameters followed by values (in the case where the preceding parameter is
|
||||
* not a flag).
|
||||
*/
|
||||
#define PRINT_CALL mlpack::bindings::cli::ProgramCall
|
||||
|
||||
/**
|
||||
* PRINT_DATASET() returns a string that contains a correct language-specific
|
||||
* representation of a dataset name.
|
||||
*/
|
||||
#define PRINT_DATASET mlpack::bindings::cli::PrintDataset
|
||||
|
||||
/**
|
||||
* PRINT_MODEL() returns a string that contains a correct language-specific
|
||||
* representation of an mlpack model name.
|
||||
*/
|
||||
#define PRINT_MODEL mlpack::bindings::cli::PrintModel
|
||||
|
||||
/**
|
||||
* BINDING_IGNORE_CHECK() is an internally-used macro to determine whether or
|
||||
* not a specific parameter check should be ignored.
|
||||
*/
|
||||
#define BINDING_IGNORE_CHECK mlpack::bindings::cli::IgnoreCheck
|
||||
|
||||
namespace mlpack {
|
||||
@@ -90,7 +122,20 @@ int main(int argc, char** argv)
|
||||
#define PRINT_PARAM_VALUE(A, B) std::string(" ")
|
||||
#define PRINT_DATASET(A) std::string(" ")
|
||||
#define PRINT_MODEL(A) std::string(" ")
|
||||
|
||||
/**
|
||||
* PRINT_CALL() returns a string that contains the full language-specific
|
||||
* representation of a call to an mlpack binding. The first argument should be
|
||||
* the name of the binding, and all other arguments should be names of
|
||||
* parameters followed by values (in the case where the preceding parameter is
|
||||
* not a flag).
|
||||
*/
|
||||
#define PRINT_CALL(...) std::string(" ")
|
||||
|
||||
/**
|
||||
* BINDING_IGNORE_CHECK() is an internally-used macro to determine whether or
|
||||
* not a specific parameter check should be ignored.
|
||||
*/
|
||||
#define BINDING_IGNORE_CHECK mlpack::bindings::tests::IgnoreCheck
|
||||
|
||||
namespace mlpack {
|
||||
@@ -119,11 +164,43 @@ using Option = mlpack::bindings::tests::TestOption<T>;
|
||||
#include <mlpack/bindings/python/py_option.hpp>
|
||||
#include <mlpack/bindings/python/print_doc_functions.hpp>
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_STRING() returns a string that contains the correct
|
||||
* language-specific representation of a parameter's name.
|
||||
*/
|
||||
#define PRINT_PARAM_STRING mlpack::bindings::python::ParamString
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_VALUE() returns a string that contains a correct
|
||||
* language-specific representation of a parameter's value.
|
||||
*/
|
||||
#define PRINT_PARAM_VALUE mlpack::bindings::python::PrintValue
|
||||
|
||||
/**
|
||||
* PRINT_DATASET() returns a string that contains a correct language-specific
|
||||
* representation of a dataset name.
|
||||
*/
|
||||
#define PRINT_DATASET mlpack::bindings::python::PrintDataset
|
||||
|
||||
/**
|
||||
* PRINT_MODEL() returns a string that contains a correct language-specific
|
||||
* representation of an mlpack model name.
|
||||
*/
|
||||
#define PRINT_MODEL mlpack::bindings::python::PrintModel
|
||||
|
||||
/**
|
||||
* PRINT_CALL() returns a string that contains the full language-specific
|
||||
* representation of a call to an mlpack binding. The first argument should be
|
||||
* the name of the binding, and all other arguments should be names of
|
||||
* parameters followed by values (in the case where the preceding parameter is
|
||||
* not a flag).
|
||||
*/
|
||||
#define PRINT_CALL mlpack::bindings::python::ProgramCall
|
||||
|
||||
/**
|
||||
* BINDING_IGNORE_CHECK() is an internally-used macro to determine whether or
|
||||
* not a specific parameter check should be ignored.
|
||||
*/
|
||||
#define BINDING_IGNORE_CHECK mlpack::bindings::python::IgnoreCheck
|
||||
|
||||
namespace mlpack {
|
||||
@@ -170,11 +247,43 @@ PARAM_FLAG("copy_all_inputs", "If specified, all input parameters will be deep"
|
||||
#include <mlpack/bindings/markdown/md_option.hpp>
|
||||
#include <mlpack/bindings/markdown/print_doc_functions.hpp>
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_STRING() returns a string that contains the correct
|
||||
* language-specific representation of a parameter's name.
|
||||
*/
|
||||
#define PRINT_PARAM_STRING mlpack::bindings::markdown::ParamString
|
||||
|
||||
/**
|
||||
* PRINT_PARAM_VALUE() returns a string that contains a correct
|
||||
* language-specific representation of a parameter's value.
|
||||
*/
|
||||
#define PRINT_PARAM_VALUE mlpack::bindings::markdown::PrintValue
|
||||
|
||||
/**
|
||||
* PRINT_DATASET() returns a string that contains a correct language-specific
|
||||
* representation of a dataset name.
|
||||
*/
|
||||
#define PRINT_DATASET mlpack::bindings::markdown::PrintDataset
|
||||
|
||||
/**
|
||||
* PRINT_MODEL() returns a string that contains a correct language-specific
|
||||
* representation of an mlpack model name.
|
||||
*/
|
||||
#define PRINT_MODEL mlpack::bindings::markdown::PrintModel
|
||||
|
||||
/**
|
||||
* PRINT_CALL() returns a string that contains the full language-specific
|
||||
* representation of a call to an mlpack binding. The first argument should be
|
||||
* the name of the binding, and all other arguments should be names of
|
||||
* parameters followed by values (in the case where the preceding parameter is
|
||||
* not a flag).
|
||||
*/
|
||||
#define PRINT_CALL mlpack::bindings::markdown::ProgramCall
|
||||
|
||||
/**
|
||||
* BINDING_IGNORE_CHECK() is an internally-used macro to determine whether or
|
||||
* not a specific parameter check should be ignored.
|
||||
*/
|
||||
#define BINDING_IGNORE_CHECK mlpack::bindings::markdown::IgnoreCheck
|
||||
|
||||
// This doesn't actually matter for this binding type.
|
||||
|
||||
+108
-36
@@ -77,7 +77,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* Define a flag parameter.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -100,7 +102,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
* @param DEF Default value of the parameter.
|
||||
*
|
||||
@@ -131,7 +135,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* will be issued.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
*
|
||||
@@ -153,7 +159,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
* @param DEF Default value of the parameter.
|
||||
*
|
||||
@@ -183,7 +191,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* will be issued.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
*
|
||||
@@ -206,7 +216,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* PROGRAM_INFO() macro), the parameter can be specified with just --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
* @param DEF Default value of the parameter.
|
||||
*
|
||||
@@ -236,7 +248,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -264,7 +278,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -290,7 +306,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -321,7 +339,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -348,7 +368,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -376,7 +398,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -409,7 +433,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -435,7 +461,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -462,7 +490,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -494,7 +524,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -521,7 +553,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -547,7 +581,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -573,7 +609,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -599,7 +637,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -626,7 +666,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -657,7 +699,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -688,7 +732,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -719,7 +765,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -750,7 +798,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* types.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @bug
|
||||
@@ -771,7 +821,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* --ID=value1,value2,value3.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
* @param DEF Default value of the parameter.
|
||||
*
|
||||
@@ -802,7 +854,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* will be issued.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
*
|
||||
@@ -838,7 +892,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* @endcode
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS One-character string representing the alias of the parameter.
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -881,7 +937,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
*
|
||||
* @param TYPE Type of the model to be loaded.
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter.
|
||||
* @param DESC Description of the parameter. Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*/
|
||||
#define PARAM_MODEL_IN(TYPE, ID, DESC, ALIAS) \
|
||||
@@ -913,7 +971,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
*
|
||||
* @param TYPE Type of the model to be loaded.
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter.
|
||||
* @param DESC Description of the parameter. Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*/
|
||||
#define PARAM_MODEL_IN_REQ(TYPE, ID, DESC, ALIAS) \
|
||||
@@ -936,7 +996,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
*
|
||||
* @param TYPE Type of the model to be saved.
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter.
|
||||
* @param DESC Description of the parameter. Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*/
|
||||
#define PARAM_MODEL_OUT(TYPE, ID, DESC, ALIAS) \
|
||||
@@ -948,7 +1010,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* The parameter must then be specified on the command line with --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -970,7 +1034,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* The parameter must then be specified on the command line with --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -992,7 +1058,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* The parameter must then be specified on the command line with --ID=value.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -1015,7 +1083,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
* --ID=value1,value2,value3.
|
||||
*
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Quick description of the parameter (1-2 sentences).
|
||||
* @param DESC Quick description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS An alias for the parameter (one letter).
|
||||
*
|
||||
* @see mlpack::CLI, PROGRAM_INFO()
|
||||
@@ -1052,7 +1122,9 @@ using DatasetInfo = DatasetMapper<IncrementPolicy, std::string>;
|
||||
*
|
||||
* @param T Type of the parameter.
|
||||
* @param ID Name of the parameter.
|
||||
* @param DESC Description of the parameter (1-2 sentences).
|
||||
* @param DESC Description of the parameter (1-2 sentences). Don't use
|
||||
* printing macros like PRINT_PARAM_STRING() or PRINT_DATASET() or others
|
||||
* here---it will cause problems.
|
||||
* @param ALIAS Alias for this parameter (one letter).
|
||||
* @param DEF Default value of the parameter.
|
||||
* @param REQ Whether or not parameter is required (boolean value).
|
||||
|
||||
@@ -220,7 +220,7 @@ struct NAME \
|
||||
#define SINGLE_ARG(...) __VA_ARGS__
|
||||
|
||||
/**
|
||||
* HAS_METHOD_FORM generates a template that allows to check at compile time
|
||||
* HAS_METHOD_FORM generates a template that allows a compile time check for
|
||||
* whether a given class has a method of the requested form. For example, for
|
||||
* the following class
|
||||
*
|
||||
@@ -254,8 +254,8 @@ struct NAME \
|
||||
HAS_METHOD_FORM_BASE(SINGLE_ARG(METHOD), SINGLE_ARG(NAME), 7)
|
||||
|
||||
/**
|
||||
* HAS_EXACT_METHOD_FORM generates a template that allows to check at compile
|
||||
* time whether a given class has a method of the requested form. For example,
|
||||
* HAS_EXACT_METHOD_FORM generates a template that allows a compile time check
|
||||
* whether a given class has a method of the requested form. For example,
|
||||
* for the following class
|
||||
*
|
||||
* class A
|
||||
@@ -291,7 +291,7 @@ struct NAME \
|
||||
* A version of HAS_METHOD_FORM() where the maximum number of extra arguments is
|
||||
* set to the default of 7.
|
||||
*
|
||||
* HAS_METHOD_FORM generates a template that allows to check at compile time
|
||||
* HAS_METHOD_FORM generates a template that allows a compile time check as to
|
||||
* whether a given class has a method of the requested form. For example, for
|
||||
* the following class
|
||||
*
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
// The version of mlpack. If this is a git repository, this will be a version
|
||||
// with higher number than the most recent release.
|
||||
#define MLPACK_VERSION_MAJOR 3
|
||||
#define MLPACK_VERSION_MINOR 1
|
||||
#define MLPACK_VERSION_MINOR 2
|
||||
#define MLPACK_VERSION_PATCH 2
|
||||
|
||||
// The name of the version (for use by --version).
|
||||
|
||||
@@ -18,6 +18,7 @@ add_subdirectory(convolution_rules)
|
||||
add_subdirectory(gan)
|
||||
add_subdirectory(rbm)
|
||||
add_subdirectory(augmented)
|
||||
add_subdirectory(regularizer)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
|
||||
@@ -257,6 +257,14 @@ class FFN
|
||||
*/
|
||||
void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
|
||||
|
||||
//! Get the network model.
|
||||
const std::vector<LayerTypes<CustomLayers...> >& Model() const
|
||||
{
|
||||
return network;
|
||||
}
|
||||
//! Modify the network model.
|
||||
std::vector<LayerTypes<CustomLayers...> >& Model() { return network; }
|
||||
|
||||
//! Return the number of separable functions (the number of predictor points).
|
||||
size_t NumFunctions() const { return numFunctions; }
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ set(SOURCES
|
||||
wgangp_impl.hpp
|
||||
)
|
||||
|
||||
add_subdirectory(metrics)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <mlpack/methods/ann/visitor/reset_visitor.hpp>
|
||||
#include <mlpack/methods/ann/visitor/weight_size_visitor.hpp>
|
||||
#include <mlpack/methods/ann/visitor/weight_set_visitor.hpp>
|
||||
#include "metrics/inception_score.hpp"
|
||||
|
||||
|
||||
namespace mlpack {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# Define the files we need to compile.
|
||||
# Anything not in this list will not be compiled into mlpack.
|
||||
set(SOURCES
|
||||
inception_score
|
||||
inception_score_impl
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
|
||||
endforeach()
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# the parent scope).
|
||||
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @file inception_score.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of Inception Score for Generative Adversarial Networks.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_METRICS_INCEPTION_SCORE_HPP
|
||||
#define MLPACK_METHODS_METRICS_INCEPTION_SCORE_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /* Artificial Neural Network */ {
|
||||
|
||||
/**
|
||||
* Function that computes Inception Score for a set of images produced by a GAN.
|
||||
* For more information, see the following.
|
||||
*
|
||||
* @code
|
||||
* @article{Goodfellow2016,
|
||||
* author = {Tim Salimans, Ian Goodfellow, Wojciech Zaremba, Vicki Cheung,
|
||||
* Alec Radford, Xi Chen},
|
||||
* title = {Improved Techniques for Training GANs},
|
||||
* year = {2016},
|
||||
* url = {https://arxiv.org/abs/1606.03498},
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @param Model Model for evaluating the quality of images.
|
||||
* @param images Images generated by GAN.
|
||||
*/
|
||||
template<typename ModelType>
|
||||
double InceptionScore(ModelType Model,
|
||||
arma::mat images,
|
||||
size_t splits = 1);
|
||||
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#include "inception_score_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* @file inception_score_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of Inception Score for Generative Adversarial Networks.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_METRICS_INCEPTION_SCORE_IMPL_HPP
|
||||
#define MLPACK_METHODS_METRICS_INCEPTION_SCORE_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "inception_score.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /* Artificial Neural Network */ {
|
||||
|
||||
template <typename ModelType>
|
||||
double InceptionScore(ModelType model,
|
||||
arma::mat images,
|
||||
size_t splits)
|
||||
{
|
||||
size_t samples = images.n_cols;
|
||||
size_t splitSize = samples / splits;
|
||||
size_t remainder = samples % splits;
|
||||
arma::mat preds;
|
||||
model.Predict(images, preds);
|
||||
|
||||
size_t index = 0;
|
||||
arma::vec scores = arma::vec(splits);
|
||||
|
||||
for (int i = 0; i < splits; i++)
|
||||
{
|
||||
size_t curSize = splitSize;
|
||||
if (remainder)
|
||||
{
|
||||
curSize++;
|
||||
remainder--;
|
||||
}
|
||||
arma::mat curPreds =
|
||||
arma::mat(preds.colptr(index), preds.n_rows, curSize, false, true);
|
||||
arma::colvec c = arma::log(arma::mean(curPreds, 1));
|
||||
arma::mat temp = arma::log(curPreds);
|
||||
temp.each_col() -= c;
|
||||
curPreds %= temp;
|
||||
scores(i) = exp(arma::as_scalar(arma::mean(arma::sum(curPreds, 0))));
|
||||
index += curSize;
|
||||
}
|
||||
|
||||
return arma::mean(scores);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -40,6 +40,8 @@ set(SOURCES
|
||||
gru_impl.hpp
|
||||
hard_tanh.hpp
|
||||
hard_tanh_impl.hpp
|
||||
highway.hpp
|
||||
highway_impl.hpp
|
||||
join.hpp
|
||||
join_impl.hpp
|
||||
layer.hpp
|
||||
@@ -63,6 +65,8 @@ set(SOURCES
|
||||
max_pooling_impl.hpp
|
||||
mean_pooling.hpp
|
||||
mean_pooling_impl.hpp
|
||||
minibatch_discrimination.hpp
|
||||
minibatch_discrimination_impl.hpp
|
||||
multiply_constant.hpp
|
||||
multiply_constant_impl.hpp
|
||||
multiply_merge.hpp
|
||||
@@ -88,6 +92,8 @@ set(SOURCES
|
||||
vr_class_reward_impl.hpp
|
||||
c_relu.hpp
|
||||
c_relu_impl.hpp
|
||||
weight_norm.hpp
|
||||
weight_norm_impl.hpp
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
|
||||
@@ -37,7 +37,6 @@ namespace ann /** Artificial Neural Network. */ {
|
||||
* journal = {Advances in Neural Information Processing Systems},
|
||||
* year = {2017}
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include <mlpack/methods/ann/convolution_rules/svd_convolution.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
#include "padding.hpp"
|
||||
|
||||
namespace mlpack{
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
@@ -165,6 +166,9 @@ class AtrousConvolution
|
||||
//! Modify the output height.
|
||||
size_t& OutputHeight() { return outputHeight; }
|
||||
|
||||
//! Modify the bias weights of the layer.
|
||||
arma::mat& Bias() { return bias; }
|
||||
|
||||
/**
|
||||
* Serialize the layer
|
||||
*/
|
||||
@@ -220,53 +224,6 @@ class AtrousConvolution
|
||||
output = arma::fliplr(arma::flipud(input));
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Mat<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Mat<eT>& output)
|
||||
{
|
||||
if (output.n_rows != input.n_rows + wPad * 2 ||
|
||||
output.n_cols != input.n_cols + hPad * 2)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
|
||||
}
|
||||
|
||||
output.submat(wPad, hPad, wPad + input.n_rows - 1,
|
||||
hPad + input.n_cols - 1) = input;
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Cube<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Cube<eT>& output)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2,
|
||||
input.n_cols + hPad * 2, input.n_slices);
|
||||
|
||||
for (size_t i = 0; i < input.n_slices; ++i)
|
||||
{
|
||||
Pad<double>(input.slice(i), wPad, hPad, output.slice(i));
|
||||
}
|
||||
}
|
||||
|
||||
//! Locally-stored number of input channels.
|
||||
size_t inSize;
|
||||
|
||||
@@ -336,6 +293,9 @@ class AtrousConvolution
|
||||
//! Locally-stored transformed gradient parameter.
|
||||
arma::cube gradientTemp;
|
||||
|
||||
//! Locally-stored padding layer.
|
||||
Padding<>* padding;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
|
||||
@@ -79,6 +79,7 @@ AtrousConvolution<
|
||||
dilationH(dilationH)
|
||||
{
|
||||
weights.set_size((outSize * inSize * kW * kH) + outSize, 1);
|
||||
padding = new Padding<>(padW, padW, padH, padH);
|
||||
}
|
||||
|
||||
template<
|
||||
@@ -124,7 +125,14 @@ void AtrousConvolution<
|
||||
|
||||
if (padW != 0 || padH != 0)
|
||||
{
|
||||
Pad(inputTemp, padW, padH, inputPaddedTemp);
|
||||
inputPaddedTemp.set_size(inputTemp.n_rows + padW * 2,
|
||||
inputTemp.n_cols + padH * 2, inputTemp.n_slices);
|
||||
|
||||
for (size_t i = 0; i < inputTemp.n_slices; ++i)
|
||||
{
|
||||
padding->Forward(std::move(inputTemp.slice(i)),
|
||||
std::move(inputPaddedTemp.slice(i)));
|
||||
}
|
||||
}
|
||||
|
||||
size_t wConv = ConvOutSize(inputWidth, kW, dW, padW, dilationW);
|
||||
|
||||
@@ -151,7 +151,7 @@ class BatchNorm
|
||||
//! Locally-stored epsilon value.
|
||||
double eps;
|
||||
|
||||
//! Whether we are in loading or saving mode.
|
||||
//! Variable to keep track of whether we are in loading or saving mode.
|
||||
bool loading;
|
||||
|
||||
//! Locally-stored scale parameter.
|
||||
@@ -195,6 +195,9 @@ class BatchNorm
|
||||
|
||||
//! Locally-stored normalized input.
|
||||
OutputDataType normalized;
|
||||
|
||||
//! Locally-stored zero mean input.
|
||||
OutputDataType inputMean;
|
||||
}; // class BatchNorm
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace ann { /** Artificial Neural Network. */
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
BatchNorm<InputDataType, OutputDataType>::BatchNorm() :
|
||||
size(0),
|
||||
eps(1e-8),
|
||||
loading(false),
|
||||
deterministic(false),
|
||||
@@ -80,6 +81,7 @@ void BatchNorm<InputDataType, OutputDataType>::Forward(
|
||||
|
||||
// Normalize the input.
|
||||
output = input.each_col() - mean;
|
||||
inputMean = output;
|
||||
output.each_col() /= arma::sqrt(variance + eps);
|
||||
|
||||
// Use Welford method to compute the sample variance and mean.
|
||||
@@ -87,9 +89,9 @@ void BatchNorm<InputDataType, OutputDataType>::Forward(
|
||||
{
|
||||
count += 1;
|
||||
|
||||
OutputDataType delta = input.col(i) - runningMean;
|
||||
runningMean = runningMean + delta / count;
|
||||
runningVariance += delta % (input.col(i) - runningMean);
|
||||
OutputDataType diff = input.col(i) - runningMean;
|
||||
runningMean = runningMean + diff / count;
|
||||
runningVariance += diff % (input.col(i) - runningMean);
|
||||
}
|
||||
|
||||
// Reused in the backward and gradient step.
|
||||
@@ -106,10 +108,9 @@ template<typename eT>
|
||||
void BatchNorm<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
const arma::mat inputMean = input.each_col() - mean;
|
||||
const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps);
|
||||
|
||||
// Step 1: dl / dxhat
|
||||
// Step 1: dl / dxhat.
|
||||
const arma::mat norm = gy.each_col() % gamma;
|
||||
|
||||
// Step 2: sum dl / dxhat * (x - mu) * -0.5 * stdInv^3.
|
||||
@@ -123,8 +124,7 @@ void BatchNorm<InputDataType, OutputDataType>::Backward(
|
||||
|
||||
// Step 3: sum (dl / dxhat * -1 / stdInv) + variance *
|
||||
// (sum -2 * (x - mu)) / m.
|
||||
g.each_col() += (arma::sum(norm.each_col() % -stdInv, 1) + (var %
|
||||
arma::mean(-2 * inputMean, 1))) / input.n_cols;
|
||||
g.each_col() += arma::sum(norm.each_col() % -stdInv, 1) / input.n_cols;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <mlpack/methods/ann/convolution_rules/svd_convolution.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
#include "padding.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
@@ -161,6 +162,9 @@ class Convolution
|
||||
//! Modify the output height.
|
||||
size_t& OutputHeight() { return outputHeight; }
|
||||
|
||||
//! Modify the bias weights of the layer.
|
||||
arma::mat& Bias() { return bias; }
|
||||
|
||||
/**
|
||||
* Serialize the layer
|
||||
*/
|
||||
@@ -214,53 +218,6 @@ class Convolution
|
||||
output = arma::fliplr(arma::flipud(input));
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Mat<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Mat<eT>& output)
|
||||
{
|
||||
if (output.n_rows != input.n_rows + wPad * 2 ||
|
||||
output.n_cols != input.n_cols + hPad * 2)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
|
||||
}
|
||||
|
||||
output.submat(wPad, hPad, wPad + input.n_rows - 1,
|
||||
hPad + input.n_cols - 1) = input;
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Cube<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Cube<eT>& output)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2,
|
||||
input.n_cols + hPad * 2, input.n_slices);
|
||||
|
||||
for (size_t i = 0; i < input.n_slices; ++i)
|
||||
{
|
||||
Pad<eT>(input.slice(i), wPad, hPad, output.slice(i));
|
||||
}
|
||||
}
|
||||
|
||||
//! Locally-stored number of input channels.
|
||||
size_t inSize;
|
||||
|
||||
@@ -324,6 +281,9 @@ class Convolution
|
||||
//! Locally-stored transformed gradient parameter.
|
||||
arma::cube gradientTemp;
|
||||
|
||||
//! Locally-stored padding layer.
|
||||
Padding<>* padding;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
|
||||
@@ -74,6 +74,7 @@ Convolution<
|
||||
outputHeight(0)
|
||||
{
|
||||
weights.set_size((outSize * inSize * kW * kH) + outSize, 1);
|
||||
padding = new Padding<>(padW, padW, padH, padH);
|
||||
}
|
||||
|
||||
template<
|
||||
@@ -119,7 +120,14 @@ void Convolution<
|
||||
|
||||
if (padW != 0 || padH != 0)
|
||||
{
|
||||
Pad(inputTemp, padW, padH, inputPaddedTemp);
|
||||
inputPaddedTemp.set_size(inputTemp.n_rows + padW * 2,
|
||||
inputTemp.n_cols + padH * 2, inputTemp.n_slices);
|
||||
|
||||
for (size_t i = 0; i < inputTemp.n_slices; ++i)
|
||||
{
|
||||
padding->Forward(std::move(inputTemp.slice(i)),
|
||||
std::move(inputPaddedTemp.slice(i)));
|
||||
}
|
||||
}
|
||||
|
||||
size_t wConv = ConvOutSize(inputWidth, kW, dW, padW);
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
/**
|
||||
* @file highway.hpp
|
||||
* @author Konstantin Sidorov
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the Highway layer.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_HIGHWAY_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_HIGHWAY_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
#include <boost/ptr_container/ptr_vector.hpp>
|
||||
|
||||
#include "../visitor/delete_visitor.hpp"
|
||||
#include "../visitor/delta_visitor.hpp"
|
||||
#include "../visitor/output_height_visitor.hpp"
|
||||
#include "../visitor/output_parameter_visitor.hpp"
|
||||
#include "../visitor/output_width_visitor.hpp"
|
||||
|
||||
#include "layer_types.hpp"
|
||||
#include "add_merge.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Implementation of the Highway layer. The Highway class can vary its behavior
|
||||
* between that of feed-forward fully connected network container and that
|
||||
* of a layer which simply passes its inputs through depending on the transform
|
||||
* gate. Note that the size of the input and output matrices of this class
|
||||
* should be equal.
|
||||
*
|
||||
* For more information, refer the following paper.
|
||||
*
|
||||
* @code
|
||||
* @article{Srivastava2015,
|
||||
* author = {Rupesh Kumar Srivastava, Klaus Greff, Jurgen Schmidhuber},
|
||||
* title = {Training Very Deep Networks},
|
||||
* journal = {Advances in Neural Information Processing Systems},
|
||||
* year = {2015},
|
||||
* url = {https://arxiv.org/abs/1507.06228},
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat,
|
||||
typename... CustomLayers>
|
||||
class Highway
|
||||
{
|
||||
public:
|
||||
//! Create the Highway object.
|
||||
Highway();
|
||||
|
||||
/**
|
||||
* Create the Highway object.
|
||||
*
|
||||
* @param inSize The number of input units.
|
||||
* @param model Expose all the network modules.
|
||||
*/
|
||||
Highway(const size_t inSize, const bool model = true);
|
||||
|
||||
//! Destroy the Highway object.
|
||||
~Highway();
|
||||
|
||||
/**
|
||||
* Destroy all the modules added to the Highway object.
|
||||
*/
|
||||
void DeleteModules();
|
||||
|
||||
/**
|
||||
* Reset the layer parameter.
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Ordinary feed-forward pass of a neural network, evaluating the function
|
||||
* f(x) by propagating the activity forward through f.
|
||||
*
|
||||
* @param input Input data used for evaluating the specified function.
|
||||
* @param output Resulting output activation.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
|
||||
|
||||
/**
|
||||
* Ordinary feed-backward pass of a neural network, calculating the function
|
||||
* f(x) by propagating x backwards through f. Using the results from the
|
||||
* feed-forward pass.
|
||||
*
|
||||
* @param input The propagated input activation.
|
||||
* @param gy The backpropagated error.
|
||||
* @param g The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Backward(const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g);
|
||||
|
||||
/**
|
||||
* Calculate the gradient using the output delta and the input activation.
|
||||
*
|
||||
* @param input The input parameter used for calculating the gradient.
|
||||
* @param error The calculated error.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Gradient(arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient);
|
||||
|
||||
/**
|
||||
* Add a new module to the model.
|
||||
*
|
||||
* @param args The layer parameter.
|
||||
*/
|
||||
template <class LayerType, class... Args>
|
||||
void Add(Args... args) { network.push_back(new LayerType(args...)); }
|
||||
|
||||
/**
|
||||
* Add a new module to the model.
|
||||
*
|
||||
* @param layer The Layer to be added to the model.
|
||||
*/
|
||||
void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
|
||||
|
||||
//! Return the modules of the model.
|
||||
std::vector<LayerTypes<CustomLayers...> >& Model()
|
||||
{
|
||||
if (model)
|
||||
{
|
||||
return network;
|
||||
}
|
||||
|
||||
return empty;
|
||||
}
|
||||
|
||||
//! Get the parameters.
|
||||
OutputDataType const& Parameters() const { return weights; }
|
||||
//! Modify the parameters.
|
||||
OutputDataType& Parameters() { return weights; }
|
||||
|
||||
//! Get the input parameter.
|
||||
InputDataType const& InputParameter() const { return inputParameter; }
|
||||
//! Modify the input parameter.
|
||||
InputDataType& InputParameter() { return inputParameter; }
|
||||
|
||||
//! Get the output parameter.
|
||||
OutputDataType const& OutputParameter() const { return outputParameter; }
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
|
||||
//! Get the delta.
|
||||
OutputDataType const& Delta() const { return delta; }
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
//! Get the gradient.
|
||||
OutputDataType const& Gradient() const { return gradient; }
|
||||
//! Modify the gradient.
|
||||
OutputDataType& Gradient() { return gradient; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
private:
|
||||
//! Locally-stored number of input units.
|
||||
size_t inSize;
|
||||
|
||||
//! Parameter which indicates if the modules should be exposed.
|
||||
bool model;
|
||||
|
||||
//! Indicator if we already initialized the model.
|
||||
bool reset;
|
||||
|
||||
//! Locally-stored network modules.
|
||||
std::vector<LayerTypes<CustomLayers...> > network;
|
||||
|
||||
//! Locally-stored empty list of modules.
|
||||
std::vector<LayerTypes<CustomLayers...> > empty;
|
||||
|
||||
//! Locally-stored weight object.
|
||||
OutputDataType weights;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
//! Locally-stored gradient object.
|
||||
OutputDataType gradient;
|
||||
|
||||
//! Weights for transformation of output.
|
||||
OutputDataType transformWeight;
|
||||
|
||||
//! Bias for transformation of output.
|
||||
OutputDataType transformBias;
|
||||
|
||||
//! Locally-stored transform gate parameters.
|
||||
OutputDataType transformGate;
|
||||
|
||||
//! Locally-stored transform gate activation.
|
||||
OutputDataType transformGateActivation;
|
||||
|
||||
//! Locally-stored transform gate error.
|
||||
OutputDataType transformGateError;
|
||||
|
||||
//! Locally-stored input parameter object.
|
||||
InputDataType inputParameter;
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
|
||||
//! The input width.
|
||||
size_t width;
|
||||
|
||||
//! The input height.
|
||||
size_t height;
|
||||
|
||||
//! The normal output without highway network.
|
||||
OutputDataType networkOutput;
|
||||
|
||||
//! Locally-stored delta visitor.
|
||||
DeltaVisitor deltaVisitor;
|
||||
|
||||
//! Locally-stored output parameter visitor.
|
||||
OutputParameterVisitor outputParameterVisitor;
|
||||
|
||||
//! Locally-stored delete visitor.
|
||||
DeleteVisitor deleteVisitor;
|
||||
|
||||
//! Locally-stored output width visitor.
|
||||
OutputWidthVisitor outputWidthVisitor;
|
||||
|
||||
//! Locally-stored output height visitor.
|
||||
OutputHeightVisitor outputHeightVisitor;
|
||||
}; // class Highway
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "highway_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,249 @@
|
||||
/**
|
||||
* @file highway_impl.hpp
|
||||
* @author Konstantin Sidorov
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of Highway layer.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_HIGHWAY_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_HIGHWAY_IMPL_HPP
|
||||
|
||||
// In case it hasn't yet been included.
|
||||
#include "highway.hpp"
|
||||
|
||||
#include "../visitor/forward_visitor.hpp"
|
||||
#include "../visitor/backward_visitor.hpp"
|
||||
#include "../visitor/gradient_visitor.hpp"
|
||||
#include "../visitor/set_input_height_visitor.hpp"
|
||||
#include "../visitor/set_input_width_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
Highway<InputDataType, OutputDataType, CustomLayers...>::Highway() :
|
||||
inSize(0),
|
||||
model(true),
|
||||
reset(false),
|
||||
width(0),
|
||||
height(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template<
|
||||
typename InputDataType, typename OutputDataType, typename... CustomLayers>
|
||||
Highway<InputDataType, OutputDataType, CustomLayers...>::Highway(
|
||||
const size_t inSize,
|
||||
const bool model) :
|
||||
inSize(inSize),
|
||||
model(model),
|
||||
reset(false),
|
||||
width(0),
|
||||
height(0)
|
||||
{
|
||||
weights.set_size(inSize * inSize + inSize, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
Highway<InputDataType, OutputDataType, CustomLayers...>::~Highway()
|
||||
{
|
||||
if (!model)
|
||||
{
|
||||
for (LayerTypes<CustomLayers...>& layer : network)
|
||||
{
|
||||
boost::apply_visitor(deleteVisitor, layer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
void Highway<
|
||||
InputDataType, OutputDataType, CustomLayers...>::DeleteModules()
|
||||
{
|
||||
if (model)
|
||||
{
|
||||
for (LayerTypes<CustomLayers...>& layer : network)
|
||||
{
|
||||
boost::apply_visitor(deleteVisitor, layer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
void Highway<InputDataType, OutputDataType, CustomLayers...>::Reset()
|
||||
{
|
||||
transformWeight = arma::mat(weights.memptr(), inSize, inSize, false, false);
|
||||
transformBias = arma::mat(weights.memptr() + transformWeight.n_elem,
|
||||
inSize, 1, false, false);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void Highway<InputDataType, OutputDataType, CustomLayers...>::Forward(
|
||||
arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
boost::apply_visitor(ForwardVisitor(std::move(input), std::move(
|
||||
boost::apply_visitor(outputParameterVisitor, network.front()))),
|
||||
network.front());
|
||||
|
||||
if (!reset)
|
||||
{
|
||||
if (boost::apply_visitor(outputWidthVisitor, network.front()) != 0)
|
||||
{
|
||||
width = boost::apply_visitor(outputWidthVisitor, network.front());
|
||||
}
|
||||
|
||||
if (boost::apply_visitor(outputHeightVisitor, network.front()) != 0)
|
||||
{
|
||||
height = boost::apply_visitor(outputHeightVisitor, network.front());
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 1; i < network.size(); ++i)
|
||||
{
|
||||
if (!reset)
|
||||
{
|
||||
// Set the input width.
|
||||
boost::apply_visitor(SetInputWidthVisitor(width), network[i]);
|
||||
|
||||
// Set the input height.
|
||||
boost::apply_visitor(SetInputHeightVisitor(height), network[i]);
|
||||
}
|
||||
|
||||
boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, network[i - 1])), std::move(
|
||||
boost::apply_visitor(outputParameterVisitor, network[i]))),
|
||||
network[i]);
|
||||
|
||||
if (!reset)
|
||||
{
|
||||
// Get the output width.
|
||||
if (boost::apply_visitor(outputWidthVisitor, network[i]) != 0)
|
||||
{
|
||||
width = boost::apply_visitor(outputWidthVisitor, network[i]);
|
||||
}
|
||||
|
||||
// Get the output height.
|
||||
if (boost::apply_visitor(outputHeightVisitor, network[i]) != 0)
|
||||
{
|
||||
height = boost::apply_visitor(outputHeightVisitor, network[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!reset)
|
||||
{
|
||||
reset = true;
|
||||
}
|
||||
|
||||
output = boost::apply_visitor(outputParameterVisitor, network.back());
|
||||
|
||||
if (arma::size(output) != arma::size(input))
|
||||
{
|
||||
Log::Fatal << "The sizes of the output and input matrices of the Highway"
|
||||
<< " network should be equal. Please examine the network layers.";
|
||||
}
|
||||
|
||||
transformGate = transformWeight * input;
|
||||
transformGate.each_col() += transformBias;
|
||||
transformGateActivation = 1.0 /(1 + arma::exp(-transformGate));
|
||||
inputParameter = input;
|
||||
networkOutput = output;
|
||||
output = (output % transformGateActivation) +
|
||||
(input % (1 - transformGateActivation));
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void Highway<InputDataType, OutputDataType, CustomLayers...>::Backward(
|
||||
const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g)
|
||||
{
|
||||
boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, network.back())),
|
||||
std::move(gy % transformGateActivation),
|
||||
std::move(boost::apply_visitor(deltaVisitor, network.back()))),
|
||||
network.back());
|
||||
|
||||
for (size_t i = 2; i < network.size() + 1; ++i)
|
||||
{
|
||||
boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, network[network.size() - i])), std::move(
|
||||
boost::apply_visitor(deltaVisitor, network[network.size() - i + 1])),
|
||||
std::move(boost::apply_visitor(deltaVisitor,
|
||||
network[network.size() - i]))), network[network.size() - i]);
|
||||
}
|
||||
|
||||
g = boost::apply_visitor(deltaVisitor, network.front());
|
||||
|
||||
transformGateError = gy % (networkOutput - inputParameter) %
|
||||
transformGateActivation % (1.0 - transformGateActivation);
|
||||
g += transformWeight.t() * transformGateError;
|
||||
g += gy % (1 - transformGateActivation);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void Highway<InputDataType, OutputDataType, CustomLayers...>::Gradient(
|
||||
arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient)
|
||||
{
|
||||
boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, network[network.size() - 2])),
|
||||
std::move(error % transformGateActivation)), network.back());
|
||||
|
||||
for (size_t i = 2; i < network.size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(GradientVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, network[network.size() - i - 1])), std::move(
|
||||
boost::apply_visitor(deltaVisitor, network[network.size() - i + 1]))),
|
||||
network[network.size() - i]);
|
||||
}
|
||||
|
||||
boost::apply_visitor(GradientVisitor(std::move(input), std::move(
|
||||
boost::apply_visitor(deltaVisitor, network[1]))), network.front());
|
||||
|
||||
gradient.submat(0, 0, transformWeight.n_elem - 1, 0) = arma::vectorise(
|
||||
transformGateError * input.t());
|
||||
gradient.submat(transformWeight.n_elem, 0, gradient.n_elem - 1, 0) =
|
||||
arma::sum(transformGateError, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename Archive>
|
||||
void Highway<InputDataType, OutputDataType, CustomLayers...>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
// If loading, delete the old layers and set size for weights.
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
for (LayerTypes<CustomLayers...>& layer : network)
|
||||
{
|
||||
boost::apply_visitor(deleteVisitor, layer);
|
||||
}
|
||||
weights.set_size(inSize * inSize + inSize, 1);
|
||||
}
|
||||
|
||||
ar & BOOST_SERIALIZATION_NVP(model);
|
||||
ar & BOOST_SERIALIZATION_NVP(network);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -19,12 +19,15 @@
|
||||
#include "convolution.hpp"
|
||||
#include "dropconnect.hpp"
|
||||
#include "glimpse.hpp"
|
||||
#include "highway.hpp"
|
||||
#include "layer_norm.hpp"
|
||||
#include "layer_types.hpp"
|
||||
#include "linear.hpp"
|
||||
#include "linear_no_bias.hpp"
|
||||
#include "lstm.hpp"
|
||||
#include "minibatch_discrimination.hpp"
|
||||
#include "multiply_merge.hpp"
|
||||
#include "padding.hpp"
|
||||
#include "gru.hpp"
|
||||
#include "fast_lstm.hpp"
|
||||
#include "recurrent.hpp"
|
||||
@@ -35,5 +38,6 @@
|
||||
#include "concat.hpp"
|
||||
#include "vr_class_reward.hpp"
|
||||
#include "transposed_convolution.hpp"
|
||||
#include "weight_norm.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
@@ -155,7 +155,7 @@ class LayerNorm
|
||||
//! Locally-stored epsilon value.
|
||||
double eps;
|
||||
|
||||
//! Whether we are in loading or saving mode.
|
||||
//! Variable to keep track of whether we are in loading or saving mode.
|
||||
bool loading;
|
||||
|
||||
//! Locally-stored scale parameter.
|
||||
@@ -184,6 +184,9 @@ class LayerNorm
|
||||
|
||||
//! Locally-stored normalized input.
|
||||
OutputDataType normalized;
|
||||
|
||||
//! Locally-stored zero mean input.
|
||||
OutputDataType inputMean;
|
||||
}; // class LayerNorm
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace ann { /** Artificial Neural Network. */
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
LayerNorm<InputDataType, OutputDataType>::LayerNorm() :
|
||||
size(0),
|
||||
eps(1e-8),
|
||||
loading(false)
|
||||
{
|
||||
@@ -63,7 +64,7 @@ void LayerNorm<InputDataType, OutputDataType>::Forward(
|
||||
|
||||
// Normalize the input.
|
||||
output = input.each_row() - mean;
|
||||
|
||||
inputMean = output;
|
||||
output.each_row() /= arma::sqrt(variance + eps);
|
||||
|
||||
// Reused in the backward and gradient step.
|
||||
@@ -79,10 +80,9 @@ template<typename eT>
|
||||
void LayerNorm<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
const arma::mat inputMean = input.each_row() - mean;
|
||||
const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps);
|
||||
|
||||
// dl / dxhat
|
||||
// dl / dxhat.
|
||||
const arma::mat norm = gy.each_col() % gamma;
|
||||
|
||||
// sum dl / dxhat * (x - mu) * -0.5 * stdInv^3.
|
||||
@@ -96,8 +96,7 @@ void LayerNorm<InputDataType, OutputDataType>::Backward(
|
||||
|
||||
// sum (dl / dxhat * -1 / stdInv) + variance *
|
||||
// (sum -2 * (x - mu)) / m.
|
||||
g.each_row() += (arma::sum(norm.each_row() % -stdInv, 0) + (var %
|
||||
arma::mean(-2 * inputMean, 0))) / input.n_rows;
|
||||
g.each_row() += arma::sum(norm.each_row() % -stdInv, 0) / input.n_rows;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
|
||||
@@ -112,6 +112,10 @@ HAS_MEM_FUNC(Loss, HasLoss);
|
||||
// can use with SFINAE to catch when a type has a Run() function.
|
||||
HAS_MEM_FUNC(Run, HasRunCheck);
|
||||
|
||||
// This gives us a HasBiasCheck<T, U> type (where U is a function pointer) we
|
||||
// can use with SFINAE to catch when a type has a Bias() function.
|
||||
HAS_MEM_FUNC(Bias, HasBiasCheck);
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
|
||||
@@ -40,12 +40,16 @@
|
||||
#include <mlpack/methods/ann/layer/reparametrization.hpp>
|
||||
#include <mlpack/methods/ann/layer/select.hpp>
|
||||
#include <mlpack/methods/ann/layer/subview.hpp>
|
||||
#include <mlpack/methods/ann/layer/virtual_batch_norm.hpp>
|
||||
|
||||
// Convolution modules.
|
||||
#include <mlpack/methods/ann/convolution_rules/border_modes.hpp>
|
||||
#include <mlpack/methods/ann/convolution_rules/naive_convolution.hpp>
|
||||
#include <mlpack/methods/ann/convolution_rules/fft_convolution.hpp>
|
||||
|
||||
// Regularizers.
|
||||
#include <mlpack/methods/ann/regularizer/no_regularizer.hpp>
|
||||
|
||||
// Loss function modules.
|
||||
#include <mlpack/methods/ann/loss_functions/negative_log_likelihood.hpp>
|
||||
|
||||
@@ -56,13 +60,32 @@ template<typename InputDataType, typename OutputDataType> class BatchNorm;
|
||||
template<typename InputDataType, typename OutputDataType> class DropConnect;
|
||||
template<typename InputDataType, typename OutputDataType> class Glimpse;
|
||||
template<typename InputDataType, typename OutputDataType> class LayerNorm;
|
||||
template<typename InputDataType, typename OutputDataType> class Linear;
|
||||
template<typename InputDataType, typename OutputDataType> class LinearNoBias;
|
||||
template<typename InputDataType, typename OutputDataType> class LSTM;
|
||||
template<typename InputDataType, typename OutputDataType> class GRU;
|
||||
template<typename InputDataType, typename OutputDataType> class FastLSTM;
|
||||
template<typename InputDataType, typename OutputDataType> class VRClassReward;
|
||||
template<typename InputDataType, typename OutputDataType> class Concatenate;
|
||||
template<typename InputDataType, typename OutputDataType> class Padding;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
class Linear;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
class LinearNoBias;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType
|
||||
>
|
||||
class VirtualBatchNorm;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType
|
||||
>
|
||||
class MiniBatchDiscrimination;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType
|
||||
@@ -82,6 +105,12 @@ template<typename InputDataType,
|
||||
>
|
||||
class Sequential;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename... CustomLayers
|
||||
>
|
||||
class Highway;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename... CustomLayers
|
||||
@@ -140,6 +169,26 @@ template<typename InputDataType,
|
||||
>
|
||||
class MultiplyMerge;
|
||||
|
||||
template <typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename... CustomLayers
|
||||
>
|
||||
class WeightNorm;
|
||||
|
||||
using MoreTypes = boost::variant<
|
||||
Recurrent<arma::mat, arma::mat>*,
|
||||
RecurrentAttention<arma::mat, arma::mat>*,
|
||||
ReinforceNormal<arma::mat, arma::mat>*,
|
||||
Reparametrization<arma::mat, arma::mat>*,
|
||||
Select<arma::mat, arma::mat>*,
|
||||
Sequential<arma::mat, arma::mat, false>*,
|
||||
Sequential<arma::mat, arma::mat, true>*,
|
||||
Subview<arma::mat, arma::mat>*,
|
||||
VRClassReward<arma::mat, arma::mat>*,
|
||||
VirtualBatchNorm<arma::mat, arma::mat>*,
|
||||
WeightNorm<arma::mat, arma::mat>*
|
||||
>;
|
||||
|
||||
template <typename... CustomLayers>
|
||||
using LayerTypes = boost::variant<
|
||||
Add<arma::mat, arma::mat>*,
|
||||
@@ -173,12 +222,13 @@ using LayerTypes = boost::variant<
|
||||
FlexibleReLU<arma::mat, arma::mat>*,
|
||||
Glimpse<arma::mat, arma::mat>*,
|
||||
HardTanH<arma::mat, arma::mat>*,
|
||||
Highway<arma::mat, arma::mat>*,
|
||||
Join<arma::mat, arma::mat>*,
|
||||
LayerNorm<arma::mat, arma::mat>*,
|
||||
LeakyReLU<arma::mat, arma::mat>*,
|
||||
CReLU<arma::mat, arma::mat>*,
|
||||
Linear<arma::mat, arma::mat>*,
|
||||
LinearNoBias<arma::mat, arma::mat>*,
|
||||
Linear<arma::mat, arma::mat, NoRegularizer>*,
|
||||
LinearNoBias<arma::mat, arma::mat, NoRegularizer>*,
|
||||
LogSoftMax<arma::mat, arma::mat>*,
|
||||
Lookup<arma::mat, arma::mat>*,
|
||||
LSTM<arma::mat, arma::mat>*,
|
||||
@@ -186,19 +236,13 @@ using LayerTypes = boost::variant<
|
||||
FastLSTM<arma::mat, arma::mat>*,
|
||||
MaxPooling<arma::mat, arma::mat>*,
|
||||
MeanPooling<arma::mat, arma::mat>*,
|
||||
MiniBatchDiscrimination<arma::mat, arma::mat>*,
|
||||
MultiplyConstant<arma::mat, arma::mat>*,
|
||||
MultiplyMerge<arma::mat, arma::mat>*,
|
||||
NegativeLogLikelihood<arma::mat, arma::mat>*,
|
||||
Padding<arma::mat, arma::mat>*,
|
||||
PReLU<arma::mat, arma::mat>*,
|
||||
Recurrent<arma::mat, arma::mat>*,
|
||||
RecurrentAttention<arma::mat, arma::mat>*,
|
||||
ReinforceNormal<arma::mat, arma::mat>*,
|
||||
Reparametrization<arma::mat, arma::mat>*,
|
||||
Select<arma::mat, arma::mat>*,
|
||||
Sequential<arma::mat, arma::mat, false>*,
|
||||
Sequential<arma::mat, arma::mat, true>*,
|
||||
Subview<arma::mat, arma::mat>*,
|
||||
VRClassReward<arma::mat, arma::mat>*,
|
||||
MoreTypes,
|
||||
CustomLayers*...
|
||||
>;
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#define MLPACK_METHODS_ANN_LAYER_LINEAR_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include <mlpack/methods/ann/regularizer/no_regularizer.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
|
||||
@@ -31,7 +32,8 @@ namespace ann /** Artificial Neural Network. */ {
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat
|
||||
typename OutputDataType = arma::mat,
|
||||
typename RegularizerType = NoRegularizer
|
||||
>
|
||||
class Linear
|
||||
{
|
||||
@@ -45,7 +47,9 @@ class Linear
|
||||
* @param inSize The number of input units.
|
||||
* @param outSize The number of output units.
|
||||
*/
|
||||
Linear(const size_t inSize, const size_t outSize);
|
||||
Linear(const size_t inSize,
|
||||
const size_t outSize,
|
||||
RegularizerType regularizer = RegularizerType());
|
||||
|
||||
/*
|
||||
* Reset the layer parameter.
|
||||
@@ -113,6 +117,9 @@ class Linear
|
||||
//! Modify the gradient.
|
||||
OutputDataType& Gradient() { return gradient; }
|
||||
|
||||
//! Modify the bias weights of the layer.
|
||||
arma::mat& Bias() { return bias; }
|
||||
|
||||
/**
|
||||
* Serialize the layer
|
||||
*/
|
||||
@@ -146,6 +153,9 @@ class Linear
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
|
||||
//! Locally-stored regularizer object.
|
||||
RegularizerType regularizer;
|
||||
}; // class Linear
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -19,50 +19,60 @@
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
Linear<InputDataType, OutputDataType>::Linear()
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
Linear<InputDataType, OutputDataType, RegularizerType>::Linear() :
|
||||
inSize(0),
|
||||
outSize(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template <typename InputDataType, typename OutputDataType>
|
||||
Linear<InputDataType, OutputDataType>::Linear(
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
Linear<InputDataType, OutputDataType, RegularizerType>::Linear(
|
||||
const size_t inSize,
|
||||
const size_t outSize) :
|
||||
const size_t outSize,
|
||||
RegularizerType regularizer) :
|
||||
inSize(inSize),
|
||||
outSize(outSize)
|
||||
outSize(outSize),
|
||||
regularizer(regularizer)
|
||||
{
|
||||
weights.set_size(outSize * inSize + outSize, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
void Linear<InputDataType, OutputDataType>::Reset()
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
void Linear<InputDataType, OutputDataType, RegularizerType>::Reset()
|
||||
{
|
||||
weight = arma::mat(weights.memptr(), outSize, inSize, false, false);
|
||||
bias = arma::mat(weights.memptr() + weight.n_elem,
|
||||
outSize, 1, false, false);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void Linear<InputDataType, OutputDataType>::Forward(
|
||||
void Linear<InputDataType, OutputDataType, RegularizerType>::Forward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
output = weight * input;
|
||||
output.each_col() += bias;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void Linear<InputDataType, OutputDataType>::Backward(
|
||||
void Linear<InputDataType, OutputDataType, RegularizerType>::Backward(
|
||||
const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
g = weight.t() * gy;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void Linear<InputDataType, OutputDataType>::Gradient(
|
||||
void Linear<InputDataType, OutputDataType, RegularizerType>::Gradient(
|
||||
const arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient)
|
||||
@@ -71,11 +81,13 @@ void Linear<InputDataType, OutputDataType>::Gradient(
|
||||
error * input.t());
|
||||
gradient.submat(weight.n_elem, 0, gradient.n_elem - 1, 0) =
|
||||
arma::sum(error, 1);
|
||||
regularizer.Evaluate(weights, gradient);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename Archive>
|
||||
void Linear<InputDataType, OutputDataType>::serialize(
|
||||
void Linear<InputDataType, OutputDataType, RegularizerType>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(inSize);
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#define MLPACK_METHODS_ANN_LAYER_LINEAR_NO_BIAS_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include <mlpack/methods/ann/regularizer/no_regularizer.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
|
||||
@@ -31,7 +32,8 @@ namespace ann /** Artificial Neural Network. */ {
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat
|
||||
typename OutputDataType = arma::mat,
|
||||
typename RegularizerType = NoRegularizer
|
||||
>
|
||||
class LinearNoBias
|
||||
{
|
||||
@@ -44,7 +46,9 @@ class LinearNoBias
|
||||
* @param inSize The number of input units.
|
||||
* @param outSize The number of output units.
|
||||
*/
|
||||
LinearNoBias(const size_t inSize, const size_t outSize);
|
||||
LinearNoBias(const size_t inSize,
|
||||
const size_t outSize,
|
||||
RegularizerType regularizer = RegularizerType());
|
||||
|
||||
/*
|
||||
* Reset the layer parameter.
|
||||
@@ -142,6 +146,9 @@ class LinearNoBias
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
|
||||
//! Locally-stored regularizer object.
|
||||
RegularizerType regularizer;
|
||||
}; // class LinearNoBias
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -19,57 +19,70 @@
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
LinearNoBias<InputDataType, OutputDataType>::LinearNoBias()
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias() :
|
||||
inSize(0),
|
||||
outSize(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
LinearNoBias<InputDataType, OutputDataType>::LinearNoBias(
|
||||
const size_t inSize, const size_t outSize) :
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
LinearNoBias<InputDataType, OutputDataType, RegularizerType>::LinearNoBias(
|
||||
const size_t inSize,
|
||||
const size_t outSize,
|
||||
RegularizerType regularizer) :
|
||||
inSize(inSize),
|
||||
outSize(outSize)
|
||||
outSize(outSize),
|
||||
regularizer(regularizer)
|
||||
{
|
||||
weights.set_size(outSize * inSize, 1);
|
||||
}
|
||||
|
||||
template <typename InputDataType, typename OutputDataType>
|
||||
void LinearNoBias<InputDataType, OutputDataType>::Reset()
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::Reset()
|
||||
{
|
||||
weight = arma::mat(weights.memptr(), outSize, inSize, false, false);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void LinearNoBias<InputDataType, OutputDataType>::Forward(
|
||||
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::Forward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
output = weight * input;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void LinearNoBias<InputDataType, OutputDataType>::Backward(
|
||||
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::Backward(
|
||||
const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
g = weight.t() * gy;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename eT>
|
||||
void LinearNoBias<InputDataType, OutputDataType>::Gradient(
|
||||
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::Gradient(
|
||||
const arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient)
|
||||
{
|
||||
gradient.submat(0, 0, weight.n_elem - 1, 0) = arma::vectorise(
|
||||
error * input.t());
|
||||
regularizer.Evaluate(weights, gradient);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename Archive>
|
||||
void LinearNoBias<InputDataType, OutputDataType>::serialize(
|
||||
void LinearNoBias<InputDataType, OutputDataType, RegularizerType>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(inSize);
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* @file minibatch_discrimination.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the MiniBatchDiscrimination layer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_MINIBATCH_DISCRIMINATION_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_MINIBATCH_DISCRIMINATION_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Implementation of the MiniBatchDiscrimination layer. MiniBatchDiscrimination
|
||||
* is a layer of the discriminator that allows the discriminator to look at
|
||||
* multiple data examples in combination and perform what is called as
|
||||
* mini-batch discrimination.
|
||||
* This helps prevent the collapse of the generator parameters to a setting
|
||||
* where it emits the same point. This happens because normally a
|
||||
* discriminator will process each example independently and there will be
|
||||
* no mechanism to diversify the outputs of the generator.
|
||||
*
|
||||
* For more information, see the following.
|
||||
*
|
||||
* @code
|
||||
* @article{Goodfellow2016,
|
||||
* author = {Tim Salimans, Ian Goodfellow, Wojciech Zaremba, Vicki Cheung,
|
||||
* Alec Radford, Xi Chen},
|
||||
* title = {Improved Techniques for Training GANs},
|
||||
* year = {2016},
|
||||
* url = {https://arxiv.org/abs/1606.03498},
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat
|
||||
>
|
||||
class MiniBatchDiscrimination
|
||||
{
|
||||
public:
|
||||
//! Create the MiniBatchDiscrimination object.
|
||||
MiniBatchDiscrimination();
|
||||
|
||||
/**
|
||||
* Create the MiniBatchDiscrimination layer object using the specified
|
||||
* number of units.
|
||||
*
|
||||
* @param inSize The number of input units.
|
||||
* @param outSize The number of output units.
|
||||
* @param features The number of features to compute for each dimension.
|
||||
*/
|
||||
MiniBatchDiscrimination(const size_t inSize,
|
||||
const size_t outSize,
|
||||
const size_t features);
|
||||
|
||||
/**
|
||||
* Reset the layer parameter.
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Ordinary feed-forward pass of a neural network, evaluating the function
|
||||
* f(x) by propagating the activity forward through f.
|
||||
*
|
||||
* @param input Input data used for evaluating the specified function.
|
||||
* @param output Resulting output activation.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
|
||||
|
||||
/**
|
||||
* Ordinary feed-backward pass of a neural network, calculating the function
|
||||
* f(x) by propagating x backwards through f. Using the results from the
|
||||
* feed-forward pass.
|
||||
*
|
||||
* @param input The propagated input activation.
|
||||
* @param gy The backpropagated error.
|
||||
* @param g The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Backward(const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g);
|
||||
|
||||
/**
|
||||
* Calculate the gradient using the output delta and the input activation.
|
||||
*
|
||||
* @param input The input parameter used for calculating the gradient.
|
||||
* @param error The calculated error.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Gradient(const arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& /* error */,
|
||||
arma::Mat<eT>&& gradient);
|
||||
|
||||
//! Get the parameters.
|
||||
OutputDataType const& Parameters() const { return weights; }
|
||||
//! Modify the parameters.
|
||||
OutputDataType& Parameters() { return weights; }
|
||||
|
||||
//! Get the input parameter.
|
||||
InputDataType const& InputParameter() const { return inputParameter; }
|
||||
//! Modify the input parameter.
|
||||
InputDataType& InputParameter() { return inputParameter; }
|
||||
|
||||
//! Get the output parameter.
|
||||
OutputDataType const& OutputParameter() const { return outputParameter; }
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
|
||||
//! Get the delta.
|
||||
OutputDataType const& Delta() const { return delta; }
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
//! Get the gradient.
|
||||
OutputDataType const& Gradient() const { return gradient; }
|
||||
//! Modify the gradient.
|
||||
OutputDataType& Gradient() { return gradient; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
private:
|
||||
//! Locally-stored dimensions of weight.
|
||||
size_t A, B, C;
|
||||
|
||||
//! Locally-stored input batch size.
|
||||
size_t batchSize;
|
||||
|
||||
//! Locally-stored temporary features object.
|
||||
arma::mat tempM;
|
||||
|
||||
//! Locally-stored weight object.
|
||||
OutputDataType weights;
|
||||
|
||||
//! Locally-stored weight parameters.
|
||||
OutputDataType weight;
|
||||
|
||||
//! Locally-stored features of input.
|
||||
arma::cube M;
|
||||
|
||||
//! Locally-stored delta for features object.
|
||||
arma::cube deltaM;
|
||||
|
||||
//! Locally-stored L1 distances between features.
|
||||
arma::cube distances;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
//! Locally-stored temporary delta object.
|
||||
OutputDataType deltaTemp;
|
||||
|
||||
//! Locally-stored gradient object.
|
||||
OutputDataType gradient;
|
||||
|
||||
//! Locally-stored input parameter object.
|
||||
InputDataType inputParameter;
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
}; // class MiniBatchDiscrimination
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "minibatch_discrimination_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* @file minibatch_discrimination_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of the MiniBatchDiscrimination layer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_MINIBATCH_DISCRIMINATION_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_MINIBATCH_DISCRIMINATION_IMPL_HPP
|
||||
|
||||
// In case it hasn't yet been included.
|
||||
#include "minibatch_discrimination.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
MiniBatchDiscrimination<InputDataType, OutputDataType
|
||||
>::MiniBatchDiscrimination() :
|
||||
A(0),
|
||||
B(0),
|
||||
C(0),
|
||||
batchSize(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template <typename InputDataType, typename OutputDataType>
|
||||
MiniBatchDiscrimination<InputDataType, OutputDataType
|
||||
>::MiniBatchDiscrimination(
|
||||
const size_t inSize,
|
||||
const size_t outSize,
|
||||
const size_t features) :
|
||||
A(inSize),
|
||||
B(outSize - inSize),
|
||||
C(features),
|
||||
batchSize(0)
|
||||
{
|
||||
weights.set_size(A * B * C, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
void MiniBatchDiscrimination<InputDataType, OutputDataType>::Reset()
|
||||
{
|
||||
weight = arma::mat(weights.memptr(), B * C, A, false, false);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void MiniBatchDiscrimination<InputDataType, OutputDataType>::Forward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
batchSize = input.n_cols;
|
||||
tempM = weight * input;
|
||||
M = arma::cube(tempM.memptr(), B, C, batchSize, false, false);
|
||||
distances.set_size(B, batchSize, batchSize);
|
||||
output.set_size(B, batchSize);
|
||||
|
||||
for (size_t i = 0; i < M.n_slices; i++)
|
||||
{
|
||||
output.col(i).ones();
|
||||
for (size_t j = 0; j < M.n_slices; j++)
|
||||
{
|
||||
if (j < i)
|
||||
{
|
||||
output.col(i) += distances.slice(j).col(i);
|
||||
}
|
||||
else if (i == j)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
distances.slice(i).col(j) =
|
||||
arma::exp(-arma::sum(abs(M.slice(i) - M.slice(j)), 1));
|
||||
output.col(i) += distances.slice(i).col(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output = join_cols(input, output); // (A + B) x batchSize
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void MiniBatchDiscrimination<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
g = gy.head_rows(A);
|
||||
arma::Mat<eT> gM = gy.tail_rows(B);
|
||||
deltaM.zeros(B, C, batchSize);
|
||||
|
||||
for (size_t i = 0; i < M.n_slices; i++)
|
||||
{
|
||||
for (size_t j = 0; j < M.n_slices; j++)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
arma::mat t = arma::sign(M.slice(i) - M.slice(j));
|
||||
t.each_col() %=
|
||||
distances.slice(std::min(i, j)).col(std::max(i, j)) % gM.col(i);
|
||||
deltaM.slice(i) -= t;
|
||||
deltaM.slice(j) += t;
|
||||
}
|
||||
}
|
||||
|
||||
deltaTemp = arma::mat(deltaM.memptr(), B * C, batchSize, false, false);
|
||||
g += weight.t() * deltaTemp;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void MiniBatchDiscrimination<InputDataType, OutputDataType>::Gradient(
|
||||
const arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& /* error */,
|
||||
arma::Mat<eT>&& gradient)
|
||||
{
|
||||
gradient = arma::vectorise(deltaTemp * input.t());
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename Archive>
|
||||
void MiniBatchDiscrimination<InputDataType, OutputDataType>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(A);
|
||||
ar & BOOST_SERIALIZATION_NVP(B);
|
||||
ar & BOOST_SERIALIZATION_NVP(C);
|
||||
|
||||
// This is inefficient, but we have to allocate this memory so that
|
||||
// WeightSetVisitor gets the right size.
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
weights.set_size(A * B * C, 1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @file padding.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the Padding class that pads the incoming data.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_PADDING_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_PADDING_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Implementation of the Padding module class. The Padding module applies a bias term
|
||||
* to the incoming data.
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat
|
||||
>
|
||||
class Padding
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the Padding object using the specified number of output units.
|
||||
*
|
||||
* @param padWLeft Left padding width of the input.
|
||||
* @param padWLeft Right padding width of the input.
|
||||
* @param padHTop Top padding height of the input.
|
||||
* @param padHBottom Bottom padding height of the input.
|
||||
*/
|
||||
Padding(const size_t padWLeft = 0,
|
||||
const size_t padWRight = 0,
|
||||
const size_t padHTop = 0,
|
||||
const size_t padHBottom = 0);
|
||||
|
||||
/**
|
||||
* Ordinary feed forward pass of a neural network, evaluating the function
|
||||
* f(x) by propagating the activity forward through f.
|
||||
*
|
||||
* @param input Input data used for evaluating the specified function.
|
||||
* @param output Resulting output activation.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
|
||||
|
||||
/**
|
||||
* Ordinary feed backward pass of a neural network, calculating the function
|
||||
* f(x) by propagating x backwards trough f. Using the results from the feed
|
||||
* forward pass.
|
||||
*
|
||||
* @param input The propagated input activation.
|
||||
* @param gy The backpropagated error.
|
||||
* @param g The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Backward(const arma::Mat<eT>&& /* input */,
|
||||
const arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g);
|
||||
|
||||
//! Get the output parameter.
|
||||
OutputDataType const& OutputParameter() const { return outputParameter; }
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
|
||||
//! Get the delta.
|
||||
OutputDataType const& Delta() const { return delta; }
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
private:
|
||||
//! Locally-stored left padding width.
|
||||
size_t padWLeft;
|
||||
|
||||
//! Locally-stored right padding width.
|
||||
size_t padWRight;
|
||||
|
||||
//! Locally-stored top padding height.
|
||||
size_t padHTop;
|
||||
|
||||
//! Locally-stored bottom padding height.
|
||||
size_t padHBottom;
|
||||
|
||||
//! Locally-stored number of rows and columns of input.
|
||||
size_t nRows, nCols;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
}; // class Padding
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "padding_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @file padding_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of the Padding class that adds padding to the incoming
|
||||
* data.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_PADDING_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_PADDING_IMPL_HPP
|
||||
|
||||
// In case it hasn't yet been included.
|
||||
#include "padding.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
Padding<InputDataType, OutputDataType>::Padding(
|
||||
const size_t padWLeft,
|
||||
const size_t padWRight,
|
||||
const size_t padHTop,
|
||||
const size_t padHBottom) :
|
||||
padWLeft(padWLeft),
|
||||
padWRight(padWRight),
|
||||
padHTop(padHTop),
|
||||
padHBottom(padHBottom),
|
||||
nRows(0),
|
||||
nCols(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void Padding<InputDataType, OutputDataType>::Forward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
nRows = input.n_rows;
|
||||
nCols = input.n_cols;
|
||||
output = arma::zeros(nRows + padWLeft + padWRight,
|
||||
nCols + padHTop + padHBottom);
|
||||
output.submat(padWLeft, padHTop, padWLeft + nRows - 1,
|
||||
padHTop + nCols - 1) = input;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void Padding<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>&& /* input */,
|
||||
const arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g)
|
||||
{
|
||||
g = gy.submat(padWLeft, padHTop, padWLeft + nRows - 1,
|
||||
padHTop + nCols - 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename Archive>
|
||||
void Padding<InputDataType, OutputDataType>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(padWLeft);
|
||||
ar & BOOST_SERIALIZATION_NVP(padWRight);
|
||||
ar & BOOST_SERIALIZATION_NVP(padHTop);
|
||||
ar & BOOST_SERIALIZATION_NVP(padHBottom);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -21,12 +21,13 @@
|
||||
#include <mlpack/methods/ann/convolution_rules/svd_convolution.hpp>
|
||||
|
||||
#include "layer_types.hpp"
|
||||
#include "padding.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Implementation of the Transposed Convolution class. The Transposed
|
||||
* Implementation of the Transposed Convolution class. The Transposed
|
||||
* Convolution class represents a single layer of a neural network.
|
||||
*
|
||||
* @tparam ForwardConvolutionRule Convolution to perform forward process.
|
||||
@@ -162,6 +163,9 @@ class TransposedConvolution
|
||||
//! Modify the output height.
|
||||
size_t& OutputHeight() { return outputHeight; }
|
||||
|
||||
//! Modify the bias weights of the layer.
|
||||
arma::mat& Bias() { return bias; }
|
||||
|
||||
/**
|
||||
* Serialize the layer
|
||||
*/
|
||||
@@ -216,53 +220,6 @@ class TransposedConvolution
|
||||
output = arma::fliplr(arma::flipud(input));
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Mat<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Mat<eT>& output)
|
||||
{
|
||||
if (output.n_rows != input.n_rows + wPad * 2 ||
|
||||
output.n_cols != input.n_cols + hPad * 2)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
|
||||
}
|
||||
|
||||
output.submat(wPad, hPad, wPad + input.n_rows - 1,
|
||||
hPad + input.n_cols - 1) = input;
|
||||
}
|
||||
|
||||
/*
|
||||
* Pad the given input data.
|
||||
*
|
||||
* @param input The input to be padded.
|
||||
* @param wPad Padding width of the input.
|
||||
* @param hPad Padding height of the input.
|
||||
* @param output The padded output data.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Pad(const arma::Cube<eT>& input,
|
||||
size_t wPad,
|
||||
size_t hPad,
|
||||
arma::Cube<eT>& output)
|
||||
{
|
||||
output = arma::zeros(input.n_rows + wPad * 2,
|
||||
input.n_cols + hPad * 2, input.n_slices);
|
||||
|
||||
for (size_t i = 0; i < input.n_slices; ++i)
|
||||
{
|
||||
Pad<eT>(input.slice(i), wPad, hPad, output.slice(i));
|
||||
}
|
||||
}
|
||||
|
||||
//! Locally-stored number of input channels.
|
||||
size_t inSize;
|
||||
|
||||
@@ -326,6 +283,9 @@ class TransposedConvolution
|
||||
//! Locally-stored transformed gradient parameter.
|
||||
arma::cube gradientTemp;
|
||||
|
||||
//! Locally-stored padding layer.
|
||||
Padding<>* padding;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ TransposedConvolution<
|
||||
outputHeight(0)
|
||||
{
|
||||
weights.set_size((outSize * inSize * kW * kH) + outSize, 1);
|
||||
padding = new Padding<>(padW, padW, padH, padH);
|
||||
}
|
||||
|
||||
template<
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
/**
|
||||
* @file virtual_batch_norm.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the VirtualBatchNorm layer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_VIRTUALBATCHNORM_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_VIRTUALBATCHNORM_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Declaration of the VirtualBatchNorm layer class. Instead of using the
|
||||
* batch statistics for normalizing on a mini-batch, it uses a reference subset of
|
||||
* the data for calculating the normalization statistics.
|
||||
*
|
||||
* For more information, refer to the following paper,
|
||||
*
|
||||
* @code
|
||||
* @article{Goodfellow2016,
|
||||
* author = {Tim Salimans, Ian Goodfellow, Wojciech Zaremba, Vicki Cheung,
|
||||
* Alec Radford, Xi Chen},
|
||||
* title = {Improved Techniques for Training GANs},
|
||||
* year = {2016},
|
||||
* url = {https://arxiv.org/abs/1606.03498},
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat
|
||||
>
|
||||
class VirtualBatchNorm
|
||||
{
|
||||
public:
|
||||
//! Create the VirtualBatchNorm object.
|
||||
VirtualBatchNorm();
|
||||
|
||||
/**
|
||||
* Create the VirtualBatchNorm layer object for a specified number of input units.
|
||||
*
|
||||
* @param referenceBatch The data from which the normalization
|
||||
* statistics are computed.
|
||||
* @param size The number of input units.
|
||||
* @param eps The epsilon added to variance to ensure numerical stability.
|
||||
*/
|
||||
template<typename eT>
|
||||
VirtualBatchNorm(const arma::Mat<eT>& referenceBatch,
|
||||
const size_t size,
|
||||
const double eps = 1e-8);
|
||||
|
||||
/**
|
||||
* Reset the layer parameters.
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Forward pass of the Virtual Batch Normalization layer. Transforms the input data
|
||||
* into zero mean and unit variance, scales the data by a factor gamma and
|
||||
* shifts it by beta.
|
||||
*
|
||||
* @param input Input data for the layer.
|
||||
* @param output Resulting output activations.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
|
||||
|
||||
/**
|
||||
* Backward pass through the layer.
|
||||
*
|
||||
* @param input The input activations.
|
||||
* @param gy The backpropagated error.
|
||||
* @param g The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Backward(const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g);
|
||||
|
||||
/**
|
||||
* Calculate the gradient using the output delta and the input activations.
|
||||
*
|
||||
* @param input The input activations.
|
||||
* @param error The calculated error.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Gradient(const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient);
|
||||
|
||||
//! Get the parameters.
|
||||
OutputDataType const& Parameters() const { return weights; }
|
||||
//! Modify the parameters.
|
||||
OutputDataType& Parameters() { return weights; }
|
||||
|
||||
//! Get the output parameter.
|
||||
OutputDataType const& OutputParameter() const { return outputParameter; }
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
|
||||
//! Get the delta.
|
||||
OutputDataType const& Delta() const { return delta; }
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
//! Get the gradient.
|
||||
OutputDataType const& Gradient() const { return gradient; }
|
||||
//! Modify the gradient.
|
||||
OutputDataType& Gradient() { return gradient; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
private:
|
||||
//! Locally-stored number of input units.
|
||||
size_t size;
|
||||
|
||||
//! Locally-stored epsilon value.
|
||||
double eps;
|
||||
|
||||
//! Variable to keep track of whether we are in loading or saving mode.
|
||||
bool loading;
|
||||
|
||||
//! Locally-stored scale parameter.
|
||||
OutputDataType gamma;
|
||||
|
||||
//! Locally-stored shift parameter.
|
||||
OutputDataType beta;
|
||||
|
||||
//! Locally-stored parameters.
|
||||
OutputDataType weights;
|
||||
|
||||
//! Mean of features in the reference batch.
|
||||
OutputDataType referenceBatchMean;
|
||||
|
||||
//! Variance of features in the reference batch.
|
||||
OutputDataType referenceBatchMeanSquared;
|
||||
|
||||
//! The coefficient for reference batch statistics.
|
||||
double oldCoefficient;
|
||||
|
||||
//! The coefficient for input batch statistics.
|
||||
double newCoefficient;
|
||||
|
||||
//! Locally-stored mean object.
|
||||
OutputDataType mean;
|
||||
|
||||
//! Locally-stored variance object.
|
||||
OutputDataType variance;
|
||||
|
||||
//! Locally-stored gradient object.
|
||||
OutputDataType gradient;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
|
||||
//! Locally-stored input parameter object.
|
||||
OutputDataType inputParameter;
|
||||
|
||||
//! Locally-stored normalized input.
|
||||
OutputDataType normalized;
|
||||
|
||||
//! Locally-stored zero mean input.
|
||||
OutputDataType inputSubMean;
|
||||
}; // class VirtualBatchNorm
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include the implementation.
|
||||
#include "virtual_batch_norm_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* @file virtual_batch_norm_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of the VirtualBatchNorm layer.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_VIRTUALBATCHNORM_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_VIRTUALBATCHNORM_IMPL_HPP
|
||||
|
||||
// In case it is not included.
|
||||
#include "virtual_batch_norm.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann { /** Artificial Neural Network. */
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
VirtualBatchNorm<InputDataType, OutputDataType>::VirtualBatchNorm() :
|
||||
size(0),
|
||||
eps(1e-8),
|
||||
loading(false),
|
||||
oldCoefficient(0),
|
||||
newCoefficient(0)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
template <typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
VirtualBatchNorm<InputDataType, OutputDataType>::VirtualBatchNorm(
|
||||
const arma::Mat<eT>& referenceBatch,
|
||||
const size_t size,
|
||||
const double eps) :
|
||||
size(size),
|
||||
eps(eps),
|
||||
loading(false)
|
||||
{
|
||||
weights.set_size(size + size, 1);
|
||||
|
||||
referenceBatchMean = arma::mean(referenceBatch, 1);
|
||||
referenceBatchMeanSquared = arma::mean(arma::square(referenceBatch), 1);
|
||||
newCoefficient = 1.0 / (referenceBatch.n_cols + 1);
|
||||
oldCoefficient = 1 - newCoefficient;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
void VirtualBatchNorm<InputDataType, OutputDataType>::Reset()
|
||||
{
|
||||
gamma = arma::mat(weights.memptr(), size, 1, false, false);
|
||||
beta = arma::mat(weights.memptr() + gamma.n_elem, size, 1, false, false);
|
||||
|
||||
if (!loading)
|
||||
{
|
||||
gamma.fill(1.0);
|
||||
beta.fill(0.0);
|
||||
}
|
||||
|
||||
loading = false;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void VirtualBatchNorm<InputDataType, OutputDataType>::Forward(
|
||||
const arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
inputParameter = input;
|
||||
arma::mat inputMean = arma::mean(input, 1);
|
||||
arma::mat inputMeanSquared = arma::mean(arma::square(input), 1);
|
||||
|
||||
mean = oldCoefficient * referenceBatchMean + newCoefficient * inputMean;
|
||||
arma::mat meanSquared = oldCoefficient * referenceBatchMeanSquared +
|
||||
newCoefficient * inputMeanSquared;
|
||||
variance = meanSquared - arma::square(mean);
|
||||
// Normalize the input.
|
||||
output = input.each_col() - mean;
|
||||
inputSubMean = output;
|
||||
output.each_col() /= arma::sqrt(variance + eps);
|
||||
|
||||
// Reused in the backward and gradient step.
|
||||
normalized = output;
|
||||
// Scale and shift the output.
|
||||
output.each_col() %= gamma;
|
||||
output.each_col() += beta;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void VirtualBatchNorm<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
const arma::mat stdInv = 1.0 / arma::sqrt(variance + eps);
|
||||
|
||||
// dl / dxhat.
|
||||
const arma::mat norm = gy.each_col() % gamma;
|
||||
|
||||
// sum dl / dxhat * (x - mu) * -0.5 * stdInv^3.
|
||||
const arma::mat var = arma::sum(norm % inputSubMean, 1) %
|
||||
arma::pow(stdInv, 3.0) * -0.5;
|
||||
|
||||
// dl / dxhat * 1 / stdInv + variance * 2 * (x - mu) / m +
|
||||
// dl / dmu * newCoefficient / m.
|
||||
g = (norm.each_col() % stdInv) + ((inputParameter.each_col() %
|
||||
var) * 2 * newCoefficient / inputParameter.n_cols);
|
||||
|
||||
// (sum (dl / dxhat * -1 / stdInv) + (variance * mean * -2)) *
|
||||
// newCoefficient / m.
|
||||
g.each_col() += (arma::sum(norm.each_col() % -stdInv, 1) + (var %
|
||||
mean * -2)) * newCoefficient / inputParameter.n_cols;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void VirtualBatchNorm<InputDataType, OutputDataType>::Gradient(
|
||||
const arma::Mat<eT>&& /* input */,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient)
|
||||
{
|
||||
gradient.set_size(size + size, 1);
|
||||
|
||||
// Step 5: dl / dy * xhat.
|
||||
gradient.submat(0, 0, gamma.n_elem - 1, 0) = arma::sum(normalized % error, 1);
|
||||
|
||||
// Step 6: dl / dy.
|
||||
gradient.submat(gamma.n_elem, 0, gradient.n_elem - 1, 0) =
|
||||
arma::sum(error, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename Archive>
|
||||
void VirtualBatchNorm<InputDataType, OutputDataType>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(size);
|
||||
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
weights.set_size(size + size, 1);
|
||||
loading = false;
|
||||
}
|
||||
|
||||
ar & BOOST_SERIALIZATION_NVP(eps);
|
||||
ar & BOOST_SERIALIZATION_NVP(gamma);
|
||||
ar & BOOST_SERIALIZATION_NVP(beta);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* @file weight_norm.hpp
|
||||
* @author Toshal Agrawal
|
||||
*
|
||||
* Definition of the WeightNorm layer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
#include "layer_types.hpp"
|
||||
|
||||
#include "../visitor/delete_visitor.hpp"
|
||||
#include "../visitor/delta_visitor.hpp"
|
||||
#include "../visitor/output_parameter_visitor.hpp"
|
||||
#include "../visitor/reset_visitor.hpp"
|
||||
#include "../visitor/weight_size_visitor.hpp"
|
||||
#include "../visitor/weight_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
/**
|
||||
* Declaration of the WeightNorm layer class. The layer reparameterizes
|
||||
* the weight vectors in a neural network, decoupling the length of those weight
|
||||
* vectors from their direction. This reparameterization does not introduce any
|
||||
* dependencies between the examples in a mini-batch.
|
||||
*
|
||||
* This class will be a wrapper around existing layers. It will just modify the
|
||||
* calculation and updation of weights of the layer.
|
||||
*
|
||||
* For more information, refer to the following paper,
|
||||
*
|
||||
* @code
|
||||
* @inproceedings{Salimans2016WeightNorm,
|
||||
* title = {Weight Normalization: A Simple Reparameterization to Accelerate
|
||||
* Training of Deep Neural Networks},
|
||||
* author = {Tim Salimans, Diederik P. Kingma},
|
||||
* booktitle = {Neural Information Processing Systems 2016},
|
||||
* year = {2016}
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
|
||||
* arma::sp_mat or arma::cube).
|
||||
* @tparam CustomLayers Additional custom layers that can be added.
|
||||
*/
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat,
|
||||
typename... CustomLayers
|
||||
>
|
||||
class WeightNorm
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the WeightNorm layer object.
|
||||
*
|
||||
* @param layer The layer whose weights are needed to be normalized.
|
||||
*/
|
||||
WeightNorm(LayerTypes<CustomLayers...> layer = LayerTypes<CustomLayers...>());
|
||||
|
||||
//! Destructor to release allocated memory.
|
||||
~WeightNorm();
|
||||
|
||||
/**
|
||||
* Reset the layer parameters.
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Forward pass of the WeightNorm layer. Calculates the weights of the
|
||||
* wrapped layer from the parameter vector v and the scalar parameter g.
|
||||
* It then calulates the output of the wrapped layer from the calculated
|
||||
* weights.
|
||||
*
|
||||
* @param input Input data for the layer.
|
||||
* @param output Resulting output activations.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
|
||||
|
||||
/**
|
||||
* Backward pass through the layer. This function calls the Backward()
|
||||
* function of the wrapped layer.
|
||||
*
|
||||
* @param input The input activations.
|
||||
* @param gy The backpropagated error.
|
||||
* @param g The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Backward(const arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& gy,
|
||||
arma::Mat<eT>&& g);
|
||||
|
||||
/**
|
||||
* Calculate the gradient using the output delta, input activations and the
|
||||
* weights of the wrapped layer.
|
||||
*
|
||||
* @param input The input activations.
|
||||
* @param error The calculated error.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename eT>
|
||||
void Gradient(arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient);
|
||||
|
||||
//! Get the delta.
|
||||
OutputDataType const& Delta() const { return delta; }
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
//! Get the gradient.
|
||||
OutputDataType const& Gradient() const { return gradient; }
|
||||
//! Modify the gradient.
|
||||
OutputDataType& Gradient() { return gradient; }
|
||||
|
||||
//! Get the output parameter.
|
||||
OutputDataType const& OutputParameter() const { return outputParameter; }
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
|
||||
//! Get the parameters.
|
||||
OutputDataType const& Parameters() const { return weights; }
|
||||
//! Modify the parameters.
|
||||
OutputDataType& Parameters() { return weights; }
|
||||
|
||||
//! Get the wrapped layer.
|
||||
LayerTypes<CustomLayers...> const& Layer() { return wrappedLayer; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
private:
|
||||
//! Locally-stored number of bias elements in the weights of wrapped layer.
|
||||
size_t biasWeightSize;
|
||||
|
||||
//! Locally-stored delete visitor module object.
|
||||
DeleteVisitor deleteVisitor;
|
||||
|
||||
//! Locally-stored delta object.
|
||||
OutputDataType delta;
|
||||
|
||||
//! Locally-stored delta visitor module object.
|
||||
DeltaVisitor deltaVisitor;
|
||||
|
||||
//! Locally-stored gradient object.
|
||||
OutputDataType gradient;
|
||||
|
||||
//! Locally-stored wrapped layer.
|
||||
LayerTypes<CustomLayers...> wrappedLayer;
|
||||
|
||||
//! Locally stored number of elements in the weights of wrapped layer.
|
||||
size_t layerWeightSize;
|
||||
|
||||
//! Locally-stored output parameter object.
|
||||
OutputDataType outputParameter;
|
||||
|
||||
//! Locally-stored output parameter visitor module object.
|
||||
OutputParameterVisitor outputParameterVisitor;
|
||||
|
||||
//! Reset the gradient for all modules that implement the Gradient function.
|
||||
void ResetGradients(arma::mat& gradient);
|
||||
|
||||
//! Locally-stored reset visitor.
|
||||
ResetVisitor resetVisitor;
|
||||
|
||||
//! Locally-stored scalar parameter.
|
||||
OutputDataType scalarParameter;
|
||||
|
||||
//! Locally-stored parameter vector.
|
||||
OutputDataType vectorParameter;
|
||||
|
||||
//! Locally-stored parameters.
|
||||
OutputDataType weights;
|
||||
|
||||
//! Locally-stored weight size visitor.
|
||||
WeightSizeVisitor weightSizeVisitor;
|
||||
|
||||
//! Locally-stored gradients of wrappedLayer.
|
||||
OutputDataType layerGradients;
|
||||
|
||||
//! Locally-stored weights of wrappedLayer.
|
||||
OutputDataType layerWeights;
|
||||
}; // class WeightNorm
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include the implementation.
|
||||
#include "weight_norm_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* @file weight_norm_impl.hpp
|
||||
* @author Toshal Agrawal
|
||||
*
|
||||
* Implementation of the WeightNorm Layer.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LAYER_WEIGHTNORM_IMPL_HPP
|
||||
|
||||
// In case it is not included.
|
||||
#include "weight_norm.hpp"
|
||||
|
||||
#include "../visitor/forward_visitor.hpp"
|
||||
#include "../visitor/backward_visitor.hpp"
|
||||
#include "../visitor/gradient_visitor.hpp"
|
||||
#include "../visitor/bias_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann { /** Artificial Neural Network. */
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
WeightNorm<InputDataType, OutputDataType, CustomLayers...>::WeightNorm(
|
||||
LayerTypes<CustomLayers...> layer) :
|
||||
wrappedLayer(layer)
|
||||
{
|
||||
layerWeightSize = boost::apply_visitor(weightSizeVisitor, wrappedLayer);
|
||||
weights.set_size(layerWeightSize + 1, 1);
|
||||
|
||||
layerWeights.set_size(layerWeightSize, 1);
|
||||
layerGradients.set_size(layerWeightSize, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
WeightNorm<InputDataType, OutputDataType, CustomLayers...>::~WeightNorm()
|
||||
{
|
||||
boost::apply_visitor(deleteVisitor, wrappedLayer);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::Reset()
|
||||
{
|
||||
// Set the weights of the inside layer to layerWeights.
|
||||
// This is done to set the non-bias terms correctly.
|
||||
boost::apply_visitor(WeightSetVisitor(std::move(layerWeights), 0),
|
||||
wrappedLayer);
|
||||
|
||||
boost::apply_visitor(resetVisitor, wrappedLayer);
|
||||
|
||||
biasWeightSize = boost::apply_visitor(BiasSetVisitor(std::move(weights),
|
||||
0), wrappedLayer);
|
||||
|
||||
vectorParameter = arma::mat(weights.memptr() + biasWeightSize,
|
||||
layerWeightSize - biasWeightSize, 1, false, false);
|
||||
|
||||
scalarParameter = arma::mat(weights.memptr() + layerWeightSize, 1, 1, false,
|
||||
false);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::Forward(
|
||||
arma::Mat<eT>&& input, arma::Mat<eT>&& output)
|
||||
{
|
||||
// Initialize the non-bias weights of wrapped layer.
|
||||
const double normVectorParameter = arma::norm(vectorParameter, 2);
|
||||
layerWeights.rows(0, layerWeightSize - biasWeightSize - 1) =
|
||||
scalarParameter(0) * vectorParameter / normVectorParameter;
|
||||
|
||||
boost::apply_visitor(ForwardVisitor(std::move(input), std::move(
|
||||
boost::apply_visitor(outputParameterVisitor, wrappedLayer))),
|
||||
wrappedLayer);
|
||||
|
||||
output = boost::apply_visitor(outputParameterVisitor, wrappedLayer);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::Backward(
|
||||
const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
|
||||
{
|
||||
boost::apply_visitor(BackwardVisitor(std::move(boost::apply_visitor(
|
||||
outputParameterVisitor, wrappedLayer)), std::move(gy), std::move(
|
||||
boost::apply_visitor(deltaVisitor, wrappedLayer))), wrappedLayer);
|
||||
|
||||
g = boost::apply_visitor(deltaVisitor, wrappedLayer);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename eT>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::Gradient(
|
||||
arma::Mat<eT>&& input,
|
||||
arma::Mat<eT>&& error,
|
||||
arma::Mat<eT>&& gradient)
|
||||
{
|
||||
ResetGradients(layerGradients);
|
||||
|
||||
// Calculate the gradients of the wrapped layer.
|
||||
boost::apply_visitor(GradientVisitor(std::move(input),
|
||||
std::move(error)), wrappedLayer);
|
||||
|
||||
// Store the norm of vector parameter temporarily.
|
||||
const double normVectorParameter = arma::norm(vectorParameter, 2);
|
||||
|
||||
// Set the gradients of the bias terms.
|
||||
if (biasWeightSize != 0)
|
||||
{
|
||||
gradient.rows(0, biasWeightSize - 1) = arma::mat(layerGradients.memptr() +
|
||||
layerWeightSize - biasWeightSize, biasWeightSize, 1, false, false);
|
||||
}
|
||||
|
||||
// Calculate the gradients of the scalar parameter.
|
||||
gradient[gradient.n_rows - 1] = arma::accu(layerGradients.rows(0,
|
||||
layerWeightSize - biasWeightSize - 1) % vectorParameter) /
|
||||
normVectorParameter;
|
||||
|
||||
// Calculate the gradients of the vector parameter.
|
||||
gradient.rows(biasWeightSize, layerWeightSize - 1) =
|
||||
scalarParameter(0) / normVectorParameter * (layerGradients.rows(0,
|
||||
layerWeightSize - biasWeightSize - 1) - gradient[gradient.n_rows - 1] /
|
||||
normVectorParameter * vectorParameter);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::ResetGradients(
|
||||
arma::mat& gradient)
|
||||
{
|
||||
boost::apply_visitor(GradientSetVisitor(std::move(gradient), 0),
|
||||
wrappedLayer);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename... CustomLayers>
|
||||
template<typename Archive>
|
||||
void WeightNorm<InputDataType, OutputDataType, CustomLayers...>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
boost::apply_visitor(deleteVisitor, wrappedLayer);
|
||||
}
|
||||
|
||||
ar & BOOST_SERIALIZATION_NVP(wrappedLayer);
|
||||
ar & BOOST_SERIALIZATION_NVP(layerWeightSize);
|
||||
|
||||
// If we are loading, we need to initialize the weights.
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
weights.set_size(layerWeightSize + 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,18 @@
|
||||
# Define the files we need to compile.
|
||||
# Anything not in this list will not be compiled into mlpack.
|
||||
set(SOURCES
|
||||
lregularizer.hpp
|
||||
lregularizer_impl.hpp
|
||||
orthogonal_regularizer.hpp
|
||||
orthogonal_regularizer_impl.hpp
|
||||
no_regularizer.hpp
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
|
||||
endforeach()
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# the parent scope).
|
||||
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* @file lregularizer.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Generalized L-regularizer, allowing both l1 and l2 regularization methods.
|
||||
* This also gives several convenience typedefs for commonly used L-regularizers.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LREGULARIZER_HPP
|
||||
#define MLPACK_METHODS_ANN_LREGULARIZER_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* The L_p regularizer for arbitrary integer p.
|
||||
*
|
||||
* @tparam Power Power of regularizer; i.e. Power = 1 gives the L1-regularization.
|
||||
*/
|
||||
template<int TPower>
|
||||
class LRegularizer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the regularizer object.
|
||||
*
|
||||
* @param factor The factor for regularization.
|
||||
*/
|
||||
LRegularizer(double factor = 1.0);
|
||||
|
||||
/**
|
||||
* Calculate the gradient for regularization.
|
||||
*
|
||||
* @tparam MatType Type of weight matrix.
|
||||
* @param weight The weight matrix to be regularized.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Evaluate(const MatType& weight, MatType& gradient);
|
||||
|
||||
//! Serialize the regularizer (nothing to do).
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
//! The power of the regularizer.
|
||||
static const int Power = TPower;
|
||||
|
||||
//! The constant for the regularization
|
||||
double factor;
|
||||
};
|
||||
|
||||
// Convenience typedefs.
|
||||
/**
|
||||
* The L1 Regularizer.
|
||||
*/
|
||||
typedef LRegularizer<1> L1Regularizer;
|
||||
|
||||
/**
|
||||
* The L2 Regularizer.
|
||||
*/
|
||||
typedef LRegularizer<2> L2Regularizer;
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "lregularizer_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @file lregularization_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of template specializations of LRegularizer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_LREGULARIZER_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_LREGULARIZER_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included.
|
||||
#include "lregularizer.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
template<int Power>
|
||||
LRegularizer<Power>::LRegularizer(double factor) :
|
||||
factor(factor)
|
||||
{
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
// Unspecialized implementation. This should almost never be used...
|
||||
template<int Power>
|
||||
template<typename MatType>
|
||||
void LRegularizer<Power>::Evaluate(const MatType& weight, MatType& gradient)
|
||||
{
|
||||
gradient += arma::vectorise(arma::pow(weight, Power - 1) * Power * factor);
|
||||
}
|
||||
|
||||
// L1-Regularizer specializations.
|
||||
template<>
|
||||
template<typename MatType>
|
||||
void LRegularizer<1>::Evaluate(const MatType& weight, MatType& gradient)
|
||||
{
|
||||
gradient += arma::vectorise(factor * weight / arma::abs(weight));
|
||||
}
|
||||
|
||||
// L2-Regularizer specializations.
|
||||
template<>
|
||||
template<typename MatType>
|
||||
void LRegularizer<2>::Evaluate(const MatType& weight, MatType& gradient)
|
||||
{
|
||||
gradient += arma::vectorise(2 * factor * weight);
|
||||
}
|
||||
|
||||
template<int Power>
|
||||
template<typename Archive>
|
||||
void LRegularizer<Power>::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(factor);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* @file no_regularizer.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the NoRegularizer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_NO_REGULARIZER_HPP
|
||||
#define MLPACK_METHODS_ANN_NO_REGULARIZER_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* Implementation of the NoRegularizer. This does not add any
|
||||
* regularization to the weights.
|
||||
*/
|
||||
class NoRegularizer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the regularizer object.
|
||||
*/
|
||||
NoRegularizer()
|
||||
{
|
||||
// Nothing to do here.
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate the gradient for regularization.
|
||||
*
|
||||
* @tparam MatType Type of weight matrix.
|
||||
* @param weight The weight matrix to be regularized.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Evaluate(const MatType& /* weight */, MatType& /* gradient */)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @file orthogonal_regularizer.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Definition of the OrthogonalRegularizer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_ORTHOGONAL_REGULARIZER_HPP
|
||||
#define MLPACK_METHODS_ANN_ORTHOGONAL_REGULARIZER_HPP
|
||||
|
||||
#include <mlpack/prereqs.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* Implementation of the OrthogonalRegularizer. Orthogonality of weights is a
|
||||
* desirable property because multiplication by an orthogonal matrix leaves
|
||||
* the norm of the matrix unchanged. The orthogonal regularization technique
|
||||
* encourages weights to be orthogonal.
|
||||
*
|
||||
* For more information, see the following.
|
||||
*
|
||||
* @code
|
||||
* @inproceedings{WanICML2013,
|
||||
* title={Neural Photo Editing with Introspective Adversarial Networks},
|
||||
* booktitle = {5th International Conference on Learning Representations
|
||||
* (ICLR - 17)},
|
||||
* author = {Andrew Brock and Theodore Lim and J.M. Ritchie and Nick Weston},
|
||||
* year = {2017}
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
class OrthogonalRegularizer
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the regularizer object.
|
||||
*
|
||||
* @param factor The factor for regularization.
|
||||
*/
|
||||
OrthogonalRegularizer(double factor = 1.0);
|
||||
|
||||
/**
|
||||
* Calculate the gradient for regularization.
|
||||
*
|
||||
* @tparam MatType Type of weight matrix.
|
||||
* @param weight The weight matrix to be regularized.
|
||||
* @param gradient The calculated gradient.
|
||||
*/
|
||||
template<typename MatType>
|
||||
void Evaluate(const MatType& weight, MatType& gradient);
|
||||
|
||||
//! Serialize the regularizer (nothing to do).
|
||||
template<typename Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */);
|
||||
|
||||
//! The constant for the regularization
|
||||
double factor;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "orthogonal_regularizer_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @file orthogonal_regularizer_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of OrthogonalRegularizer class.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_ORTHOGONAL_REGULARIZER_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_ORTHOGONAL_REGULARIZER_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included.
|
||||
#include "orthogonal_regularizer.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
OrthogonalRegularizer::OrthogonalRegularizer(double factor) :
|
||||
factor(factor)
|
||||
{
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template<typename MatType>
|
||||
void OrthogonalRegularizer::Evaluate(const MatType& weight, MatType& gradient)
|
||||
{
|
||||
arma::mat grad = arma::zeros(arma::size(weight));
|
||||
|
||||
for (size_t i = 0; i < weight.n_rows; i++)
|
||||
{
|
||||
for (size_t j = 0; j < weight.n_rows; j++)
|
||||
{
|
||||
if (i == j)
|
||||
{
|
||||
double s =
|
||||
arma::as_scalar(
|
||||
arma::sign((weight.row(i) * weight.row(i).t()) - 1));
|
||||
grad.row(i) += 2 * s * weight.row(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
double s = arma::as_scalar(
|
||||
arma::sign(weight.row(i) * weight.row(j).t()));
|
||||
grad.row(i) += s * weight.row(j);
|
||||
grad.row(j) += s * weight.row(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gradient += arma::vectorise(grad) * factor;
|
||||
}
|
||||
|
||||
template<typename Archive>
|
||||
void OrthogonalRegularizer::serialize(
|
||||
Archive& ar, const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(factor);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @file regularizer.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* This includes various regularizers to construct a model.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_REGULARIZER_HPP
|
||||
#define MLPACK_METHODS_ANN_REGULARIZER_HPP
|
||||
|
||||
#include "lregularizer.hpp"
|
||||
#include "orthogonal_regularizer.hpp"
|
||||
#include "no_regularizer.hpp"
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,8 @@ set(SOURCES
|
||||
add_visitor_impl.hpp
|
||||
backward_visitor.hpp
|
||||
backward_visitor_impl.hpp
|
||||
bias_set_visitor.hpp
|
||||
bias_set_visitor_impl.hpp
|
||||
copy_visitor.hpp
|
||||
copy_visitor_impl.hpp
|
||||
delete_visitor.hpp
|
||||
|
||||
@@ -36,6 +36,8 @@ class AddVisitor : public boost::static_visitor<void>
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The layer that should be added.
|
||||
LayerTypes<CustomLayers...> newLayer;
|
||||
|
||||
@@ -34,6 +34,12 @@ inline void AddVisitor<CustomLayers...>::operator()(LayerType* layer) const
|
||||
LayerAdd<LayerType>(layer);
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
inline void AddVisitor<CustomLayers...>::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
|
||||
@@ -40,6 +40,8 @@ class BackwardVisitor : public boost::static_visitor<void>
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input parameter set.
|
||||
arma::mat&& input;
|
||||
|
||||
@@ -50,6 +50,11 @@ inline void BackwardVisitor::operator()(LayerType* layer) const
|
||||
LayerBackward(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void BackwardVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* @file bias_set_visitor.hpp
|
||||
* @author Toshal Agrawal
|
||||
*
|
||||
* This file provides an abstraction for the Bias() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* mlpack is free software; you may redistribute it and/or modify it under the
|
||||
* terms of the 3-clause BSD license. You should have received a copy of the
|
||||
* 3-clause BSD license along with mlpack. If not, see
|
||||
* http://www.opensource.org/licenses/BSD-3-Clause for more information.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* BiasSetVisitor updates the module bias parameters given the parameters set.
|
||||
*/
|
||||
class BiasSetVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Update the bias parameters given the parameters' set and offset.
|
||||
BiasSetVisitor(arma::mat&& weight, const size_t offset = 0);
|
||||
|
||||
//! Update the parameters' set.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameters' set.
|
||||
arma::mat&& weight;
|
||||
|
||||
//! The parameters' offset.
|
||||
const size_t offset;
|
||||
|
||||
//! Do not update the bias parameters if the module doesn't implement the
|
||||
//! Bias() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Bias() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Model() and
|
||||
//! Bias() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "bias_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user