神经网络求解 逆矩阵 算法二

 神经网络求解 逆矩阵 算法二_第1张图片

 

上图是使用MATLAB函数inv求逆结果和本文介绍的神经网络算法的MATLAB版的结果

下图是c/c++实现的

从结果的比较来看,该算法是很好的

 

function C=inverse_2(A) learning_rate=0.015; epoch=2500; C=zeros(size(A)); I=eye(size(A)); X=ones(size(A)); X=-1*X+2*I; for i=1:epoch U=A*X; Y=C*U; C=C+learning_rate*(X-Y)*transpose(U); end

c版源码

改源码依赖于其他几个关于矩阵操作的文件,之后我会把完整的程序贴出来

#include <assert.h> #include "matrix.h" //initialize stimulus,bipolar vector set static void _initialize_X(Matrix&X) { assert(X.size()!=0); int size=X.size(); Vect v(size); for (int i=0;i<size;i++){ v[i]=-1; } for (int i=0;i<size;i++){ X[i]=v; X[i][i]=1; } } //C is the inverse of A,the original matrix static void _initialize_C(Matrix& C ) { assert(C.size()!=0); Vect v(C.size()); for (unsigned int i=0;i<v.size();i++){ C[i]=v; } } //identity matrix static void _initialize_I(Matrix& I) { for (int i=0;i<I.size();i++){ I[i][i]=1; } } Matrix matrix_inverse_2(const Matrix&mat) { int mat_size=mat.size(); Matrix X(mat_size); _initialize_X(X); Matrix C(mat_size); _initialize_C(C); Matrix I=C; _initialize_I(I); double learning_rate=0.015;//learning rate int epoch=2500;//training generation Matrix U; Matrix Y; while (epoch-->0){ U=mat*X; Y=C*U; C=C+learning_rate*(X-Y)*matrix_transpose(U); } return C; }

你可能感兴趣的:(c,算法,网络,vector,matlab,Matrix)