#include <Eigen/Core>
#include <Eigen/Dense>
typedef Eigen::Matrix<float, 4, 4, Eigen::ColMajor> EigenMatrix44f;
typedef Eigen::Array<float, 4, 4, Eigen::ColMajor> EigenArray44f;
EigenArray44f a1, a2;
EigenMatrix44f m1, m2;
m1 = a1 * a2; // coeffwise product, implicit conversion from array to matrix.
a1 = m1 * m2; // matrix product, implicit conversion from matrix to array.
a2 = a1 + m1.array(); // mixing array and matrix is forbidden
m2 = a1.matrix() + m1; // and explicit conversion is required.
Eigen::ArrayWrapper<EigenMatrix44f> m1a(m1); // m1a is an alias for m1.array(), they share the same coefficients
Eigen::MatrixWrapper<EigenArray44f> a1m(a1);
// more info can be found at https://eigen.tuxfamily.org/dox/group__QuickRefPage.html
3. Eigen Softmax 实现
softmax标准公式:
\(\sigma (\mathbf {z} )_{j}={\frac {e^{z_{j}}}{\sum _{k=1}^{K}e^{z_{k}}}}\)
一般为了数值的稳定性,上式可以改写为:
\(log [\sigma (\mathbf {z} )_{j}] = log[{\frac {e^{z_{j}-max(z)}}{\sum _{k=1}^{K}e^{z_{k}-max(z)}}}]
= z_{j}-max(z)-log [{\sum _{k=1}^{K}e^{z_{k}-max(z)}}]\)
void EMatrixSoftmax(const EigenMatrixRow& input, EigenMatrixRow& output) {
// using arrays allows to call native vectorizable exp and log
EigenArray wMinusMax = input.colwise() - input.rowwise().maxCoeff();
output = (wMinusMax.colwise() - wMinusMax.exp().rowwise().sum().log()).exp();
4. Eigen 最小二乘法实现
最小二乘可参阅这篇blog
5. Eigen 使用 MKL 加速
MKL加速可参阅这篇blog
6. 题外话
cmake入门实践, cmake
WIN + CLion + WSL 模拟linux开发CPP, WSL安装, CLion配置(以ssh形式连接wsl中的cmake)
cmake/make/gcc 区别与联系, 简述