gpt4 book ai didi

java - 如何正确将反向传播神经网络的权重和偏差值导出到另一种编程语言(Java)

转载 作者:行者123 更新时间:2023-12-02 07:21:03 25 4
gpt4 key购买 nike

我使用 Matlab 创建了反向传播神经网络。我尝试使用 Matlab 实现异或门,然后获取其权重和偏差以在 java 中创建神经网络。网络由 2 个输入神经元、2 个隐藏层组成,每个隐藏层使用 2 个神经元和 1 个输出神经元。训练网络后,我得到以下权重和偏差:

clear;
clc;
i = [0 0 1 1; 0 1 0 1];
o = [0 1 1 0];
net = newff(i,o,{2,2},{'tansig','logsig','purelin'});
net.IW{1,1} = [
-5.5187 -5.4490;
3.7332 2.7697
];
net.LW{2,1} = [
-2.8093 -3.0692;
-1.6685 6.7527
];
net.LW{3,2} = [
-4.9318 -0.9651
];
net.b{1,1} = [
2.1369;
2.6529
];
net.b{2,1} = [
-0.2274;
-4.9512
];
net.b{3,1} = [
1.4848
];

input = net.IW{1,1};
layer = net.LW{2,1};
output = net.LW{3,2};

biasinput = net.b{1,1};
biaslayer = net.b{2,1};
biasoutput= net.b{3,1};


a = sim(net,i);
a;

我使用 1 和 1 作为输入进行模拟,得到以下结果:

>> f = [1;1]

f =

1
1

>> sim(net,f)

ans =

-0.1639

然后我尝试编写简单的 java 代码来计算这个神经网络。我的代码:

public class Xor {

//Value of neuron
static double[] neuroninput = new double[2];
static double[] neuronhidden1 = new double[2];
static double[] neuronhidden2 = new double[2];
static double[] neuronoutput = new double[2];

//Weight variable init
//For first hidden layer
static double[] weighthidden11 = new double[2];
static double[] weighthidden12 = new double[2];

//for second hidden layer
static double[] weighthidden21 = new double[2];
static double[] weighthidden22 = new double[2];

//for output layer
static double[] weightoutput = new double[2];
//End of weight variable init

//Bias value input
static double[] biashidden1 = new double[2];
static double[] biashidden2 = new double[2];
static double[] biasoutput = new double[1];

public static void main(String[] args) {
neuroninput[0] = 1;
neuroninput[1] = 1;

weighthidden11[0] = -5.5187;
weighthidden11[1] = -5.4490;
weighthidden12[0] = 3.7332;
weighthidden12[1] = 2.7697;

weighthidden21[0] = -2.8093;
weighthidden21[1] = -3.0692;
weighthidden22[0] = -1.6685;
weighthidden22[1] = 6.7527;

weightoutput[0] = -4.9318;
weightoutput[1] = -0.9651;

biashidden1[0] = 2.1369;
biashidden1[1] = 2.6529;

biashidden2[0] = -0.2274;
biashidden2[1] = -4.9512;

biasoutput[0] = 1.4848;

//Counting each neuron (Feed forward)
neuronhidden1[0] = sigma(neuroninput,weighthidden11,biashidden1[0]);
neuronhidden1[0] = tansig(neuronhidden1[0]);

neuronhidden1[1] = sigma(neuroninput,weighthidden12,biashidden1[1]);
neuronhidden1[1] = tansig(neuronhidden1[1]);


neuronhidden2[0] = sigma(neuronhidden1,weighthidden21,biashidden2[0]);
neuronhidden2[0] = logsig(neuronhidden2[0]);

neuronhidden2[1] = sigma(neuronhidden1,weighthidden22,biashidden2[1]);
neuronhidden2[1] = logsig(neuronhidden2[1]);

neuronoutput[0] = sigma(neuronhidden2,weightoutput,biasoutput[0]);
neuronoutput[0] = purelin(neuronoutput[0]);
System.out.println(neuronoutput[0]);
}

static double tansig(double x) {
double value = 0;
value = (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
return value;
}

static double logsig(double x) {
double value = 0;
value = 1 / (1+Math.exp(-x));
return value;
}

static double purelin(double x) {
double value = x;
return value;
}

static double sigma(double[] val, double[] weight, double hidden) {
double value = 0;
for (int i = 0; i < val.length; i++) {
value += (val[i] * weight[i]);
//System.out.println(val[i]);
}
value += hidden;
return value;
}
}

但得到的结果如下:

-1.3278721528152158

我的问题,将权重和偏差值从 matlab 导出到 java 时是否有任何错误或我的错误?也许我在我的java程序中犯了错误?非常感谢..

最佳答案

我认为问题在于标准化: http://www.mathworks.com/matlabcentral/answers/14590

如果您使用 0,1 输入,则必须使用 f(x)=2*x-1 标准化函数,该函数将值转换为 [-1; 1] 间隔,然后 g(x)=(x+1)/2 将输出变换回 [0; 1]。伪代码:

g( java_net( f(x), f(y) ) ) = matlab_net(x, y)

我在其他网络上尝试过此操作并为我工作。

关于java - 如何正确将反向传播神经网络的权重和偏差值导出到另一种编程语言(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com