HSIC Bottleneck的C++程序实现

HSIC Bottleneck的C++程序实现,第1张

论文【The HSIC Bottleneck: Deep Learning without Back-Propagation】中HSIC方法的C++实现程序:

#include
#include
//稠密矩阵的代数运算(逆、特征值等)
#include

using namespace Eigen;
using namespace std;
#define sigma 2
#define lambda 100

vector kernelmat(vector x)
{
int m = x.size(), dim = x[0].size();
vector H(m, vector(m, -1.0 / 128));
for (int i = 0; i < m; i++)
{
H[i][i] = 1 + H[i][i];
}
vector r(m);
for (int i = 0; i < m; i++)
{
double sum_value = 0;
for (int j = 0; j < dim; j++)
{
sum_value += x[i][j]* x[i][j];
}
r[i] = sum_value;
}
vector a(m, vector(m));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
double sum_value = 0;
for (int p = 0; p < dim; p++)
{
sum_value += x[i][p] * x[j][p];
}
a[i][j] = sum_value;
}
}
vector D(m, vector(m));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
D[i][j] = abs(-2 * a[i][j] + r[j] + r[i]);
}
}
double variance = dim * sigma*sigma * 2;
vector Kx(m, vector(m));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
Kx[i][j] = exp(-D[i][j] / variance);
}
}
vector Kxc(m, vector(m));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
double sum_value = 0;
for (int p = 0; p < m; p++)
{
sum_value += Kx[i][p] * H[j][p];
}
Kxc[i][j] = sum_value;
}
}

return Kxc;

}

template
vector make_matrix(const _Tp* data, const int rows, const int cols)
{
vector c(rows, vector(cols));
for (int y = 0; y < rows; ++y)
{
for (int x = 0; x < cols; ++x)
{
c[y][x] = data[y * cols + x];
}
}
return c;
}

double hsic_normalized_cca(vector x, vector y)
{
vector Kxc = kernelmat(x);
vector Kyc = kernelmat(y);
int m = Kxc.size();
vector Kxc_j(m, vector(m)), Kyc_j(m, vector(m));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
i == j ? Kxc_j[i][j] = epsilon * m * 1 + Kxc[i][j] : Kxc_j[i][j] = Kxc[i][j];
i == j ? Kyc_j[i][j] = epsilon * m * 1 + Kyc[i][j] : Kyc_j[i][j] = Kyc[i][j];
}
}

vector fn1, fn2;
for (int i = 0; i < Kxc_j.size(); i++)
{
    for (int j = 0; j < Kxc_j[0].size(); j++)
    {
        fn1.push_back(Kxc_j[i][j]);
    }
}
Map map_x(fn1.data(), Kxc_j.size(), Kxc_j[0].size());
Eigen::MatrixXf map_i = map_x.inverse();
vector> Kxc_i = make_matrix(map_i.data(), Kxc_j.size(), Kxc_j[0].size());

for (int i = 0; i < Kyc_j.size(); i++)
{
    for (int j = 0; j < Kyc_j[0].size(); j++)
    {
        fn2.push_back(Kyc_j[i][j]);
    }
}
Map map_y(fn2.data(), Kyc_j.size(), Kyc_j[0].size());
Eigen::MatrixXf map_j = map_y.inverse();
vector> Kyc_i = make_matrix(map_j.data(), Kyc_j.size(), Kyc_j[0].size());

vector> Rx(m, vector(m)), Ry(m, vector(m));
for (int i = 0; i < m; i++)
{
    for (int j = 0; j < m; j++)
    {
        double sum_value_x = 0, sum_value_y = 0;
        for (int p = 0; p < m; p++)
        {
            sum_value_x += Kxc[i][p] * Kxc_i[p][j];
            sum_value_y += Kyc[i][p] * Kyc_i[p][j];
        }
        Rx[i][j] = sum_value_x;
        Ry[i][j] = sum_value_y;
    }
}
double Pxy = 0;
for (int i = 0; i < m; i++)
{
    for (int j = 0; j < m; j++)
    {
        Pxy += Rx[i][j] * Ry[j][i];
    }
}
return Pxy;

}

void main()
{
double hsic_hy_val = hsic_normalized_cca(hidden, h_target);
double hsic_hx_val = hsic_normalized_cca(hidden, h_data);
double loss = hsic_hx_val - lambda * hsic_hy_val;
}
其中,hidden为隐藏层的输出矩阵,h_target为标签矩阵,h_data为输入矩阵。
文章转自:HSIC Bottleneck的C++程序实现_Java-答学网

作者:答学网,转载请注明原文链接:http://www.dxzl8.com/

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/2889677.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存