我使用Matlab创建了反向传播神经网络。我尝试使用Matlab实现XOR门,然后获得它的重量和偏见来在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程序中犯了错误? 非常感谢你..
答案 0 :(得分:2)
我认为问题在于规范化: 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)
我尝试使用其他网络并为我工作。
答案 1 :(得分:0)
您的问题肯定与您的JAPA版本的Matlab sim()
命令有关。
这是一个复杂的Matlab命令,其中有许多设置会影响要模拟的网络架构。为了使调试更容易,请尝试在Matlab中自己实现sim()命令。可能会减少层数,直到您在sim()
- builtin和您自己的sim
版本之间匹配Matlab。当它工作时转换为JAVA。
修改强>
在Matlab中重新实现sim()
函数的原因是,如果你不能在这里实现它,你将无法在JAVA中正确实现它。使用Matlab矢量符号很容易实现前馈网络。