【发布时间】:2015-09-22 06:10:28
【问题描述】:
我正在尝试通过反向传播在 Java 中实现 FFNN,但不知道我做错了什么。当我在网络中只有一个神经元时它起作用了,但是我写了另一个类来处理更大的网络并且没有收敛。这似乎是数学中的一个问题——或者更确切地说是我的数学实现——但我已经检查了好几次,我找不到任何问题。这应该可以工作。
节点类:
package arr;
import util.ActivationFunction;
import util.Functions;
public class Node {
public ActivationFunction f;
public double output;
public double error;
private double sumInputs;
private double sumErrors;
public Node(){
sumInputs = 0;
sumErrors = 0;
f = Functions.SIG;
output = 0;
error = 0;
}
public Node(ActivationFunction func){
this();
this.f = func;
}
public void addIW(double iw){
sumInputs += iw;
}
public void addIW(double input, double weight){
sumInputs += (input*weight);
}
public double calculateOut(){
output = f.eval(sumInputs);
return output;
}
public void addEW(double ew){
sumErrors+=ew;
}
public void addEW(double error, double weight){
sumErrors+=(error*weight);
}
public double calculateError(){
error = sumErrors * f.deriv(sumInputs);
return error;
}
public void resetValues(){
sumErrors = 0;
sumInputs = 0;
}
}
LineNetwork 类:
package arr;
import util.Functions;
public class LineNetwork {
public double[][][] weights; //layer of node to, # of node to, # of node from
public Node[][] nodes; //layer, #
public double lc;
public LineNetwork(){
weights = new double[2][][];
weights[0] = new double[2][1];
weights[1] = new double[1][3];
initializeWeights();
nodes = new Node[2][];
nodes[0] = new Node[2];
nodes[1] = new Node[1];
initializeNodes();
lc = 1;
}
private void initializeWeights(){
for(double[][] layer: weights)
for(double[] curNode: layer)
for(int i=0; i<curNode.length; i++)
curNode[i] = Math.random()/10;
}
private void initializeNodes(){
for(Node[] layer: nodes)
for(int i=0; i<layer.length; i++)
layer[i] = new Node();
nodes[nodes.length-1][0].f = Functions.HSF;
}
public double feedForward(double[] inputs) {
for(int j=0; j<nodes[0].length; j++)
nodes[0][j].addIW(inputs[j], weights[0][j][0]);
double[] outputs = new double[nodes[0].length];
for(int i=0; i<nodes[0].length; i++)
outputs[i] = nodes[0][i].calculateOut();
for(int l=1; l<nodes.length; l++){
for(int i=0; i<nodes[l].length; i++){
for(int j=0; j<nodes[l-1].length; j++)
nodes[l][i].addIW(
outputs[j],
weights[l][i][j]);
nodes[l][i].addIW(weights[l][i][weights[l][i].length-1]);
}
outputs = new double[nodes[l].length];
for(int i=0; i<nodes[l].length; i++)
outputs[i] = nodes[l][i].calculateOut();
}
return outputs[0];
}
public void backpropagate(double[] inputs, double expected) {
nodes[nodes.length-1][0].addEW(expected-nodes[nodes.length-1][0].output);
for(int l=nodes.length-2; l>=0; l--){
for(Node n: nodes[l+1])
n.calculateError();
for(int i=0; i<nodes[l].length; i++)
for(int j=0; j<nodes[l+1].length; j++)
nodes[l][i].addEW(nodes[l+1][j].error, weights[l+1][j][i]);
for(int j=0; j<nodes[l+1].length; j++){
for(int i=0; i<nodes[l].length; i++)
weights[l+1][j][i] += nodes[l][i].output*lc*nodes[l+1][j].error;
weights[l+1][j][nodes[l].length] += lc*nodes[l+1][j].error;
}
}
for(int i=0; i<nodes[0].length; i++){
weights[0][i][0] += inputs[i]*lc*nodes[0][i].calculateError();
}
}
public double train(double[] inputs, double expected) {
double r = feedForward(inputs);
backpropagate(inputs, expected);
return r;
}
public void resetValues() {
for(Node[] layer: nodes)
for(Node n: layer)
n.resetValues();
}
public static void main(String[] args) {
LineNetwork ln = new LineNetwork();
System.out.println(str2d(ln.weights[0]));
for(int i=0; i<10000; i++){
double[] in = {Math.round(Math.random()),Math.round(Math.random())};
int out = 0;
if(in[1]==1 ^ in[0] ==1) out = 1;
ln.resetValues();
System.out.print(i+": {"+in[0]+", "+in[1]+"}: "+out+" ");
System.out.println((int)ln.train(in, out));
}
System.out.println(str2d(ln.weights[0]));
}
private static String str2d(double[][] a){
String str = "[";
for(double[] arr: a)
str = str + str1d(arr) + ",\n";
str = str.substring(0, str.length()-2)+"]";
return str;
}
private static String str1d(double[] a){
String str = "[";
for(double d: a)
str = str+d+", ";
str = str.substring(0, str.length()-2)+"]";
return str;
}
}
结构快速解释:每个节点都有一个激活函数f; f.eval 评估函数,f.deriv 评估其导数。 Functions.SIG 是标准的 sigmoidal 函数,Functions.HSF 是 Heaviside 阶跃函数。为了设置函数的输入,您调用 addIW 并使用已经包含前一个输出权重的值。在addEW 的反向传播中也做了类似的事情。节点以 2d 数组的形式组织,权重分别以 3d 数组的形式组织,如上所述。
我知道这个问题可能有点多 - 我当然知道这段代码打破了多少 Java 约定 - 但我感谢任何人提供的任何帮助。
编辑:由于这个问题和我的代码是如此巨大的文本墙,如果有一行涉及括号中的许多复杂表达式而您不想弄清楚,请添加评论或询问我的内容,我会尽量尽快回答。
编辑 2:这里的具体问题是该网络不会在 XOR 上收敛。下面是一些输出来说明这一点:
9995: {1.0, 0.0}: 1 1
9996: {0.0, 1.0}: 1 1
9997: {0.0, 0.0}: 0 1
9998: {0.0, 1.0}: 1 0
9999: {0.0, 1.0}: 1 1
每行的格式为TEST NUMBER: {INPUTS}: EXPECTED ACTUAL网络每次测试都会调用train,所以这个网络反向传播了10000次。
如果有人想运行它,这里有两个额外的类:
package util;
public class Functions {
public static final ActivationFunction LIN = new ActivationFunction(){
public double eval(double x) {
return x;
}
public double deriv(double x) {
return 1;
}
};
public static final ActivationFunction SIG = new ActivationFunction(){
public double eval(double x) {
return 1/(1+Math.exp(-x));
}
public double deriv(double x) {
double ev = eval(x);
return ev * (1-ev);
}
};
public static final ActivationFunction HSF = new ActivationFunction(){
public double eval(double x) {
if(x>0) return 1;
return 0;
}
public double deriv(double x) {
return (1);
}
};
}
package util;
public interface ActivationFunction {
public double eval(double x);
public double deriv(double x);
}
现在更长了。该死。
【问题讨论】:
-
具体是什么问题?预期的结果是什么?你能做一个更短的程序来重现它吗?就目前而言,我投票关闭它,因为“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题中重现它所需的最短代码本身。没有明确问题陈述的问题对其他读者没有用处。"
-
如果您可以训练单个神经元,则问题可能出在您的反向传播方法中。您是否尝试过用小网“手动”计算来比较它?如果您可以发布缺少的类以便您的代码可以运行,这也会有所帮助。
-
@KErlandsson :我添加了具体问题,我会研究一个更短的程序,但这肯定需要时间,因为我不完全确定什么不起作用,因此我会做什么可以拿出来。
-
@jbkm :我改变了一些东西,如果你把所有的代码都放进去,现在应该可以运行了。之前我不敢尝试手动计算,因为它是 5 层,但现在是 2 层,我会尝试。
-
如果你够疯狂,你可以查看这个实现并尝试找出不同之处github.com/AdamSkywalker/btc-indexer/blob/master/src/com/ssau/…
标签: java neural-network backpropagation feed-forward