我写了一个多层感知器,它应该能够学习异或。但是,无论我做什么,它都会收敛到输入(1,1),(0,1)和(1,0)的0.5。而对于输入(0,0),它收敛到零。有谁知道我的错误在哪里?
前传:
void MLP::feedforward() {
for(int hidden = 0; hidden < nHidden; hidden++) {
hiddenNeurons.at(hidden) = 0;
for(int input = 0 ; input < nInput; input ++) {
hiddenNeurons.at(hidden) += inputNeurons.at(input)*weightItoH(input,hidden);
}
}
//Propagate towards the output layer
for(int i =0; i< nOutput; i ++) {
outputNeurons.at(i) = 0;
for(int j = 0; j <nHidden; j++) {
outputNeurons.at(i) += hiddenNeurons.at(j) * weightHtoO(j,i);
}
outputNeurons.at(i) = sigmoid(outputNeurons.at(i));
}
}
反向传播:
void MLP::backPropagation(int i) {
float learningRate = 0.75;
float error = desiredOutput[i] - outputNeurons[0];
// Calculate delta for output layer
for(int i=0; i<nOutput; i++) {
outputDelta.at(i) = error * dersigmoid(outputNeurons[i]);
}
//Calculate delta for hidden layer
for(int i = 0; i < nHidden; i++) {
hiddenDelta.at(i) = 0;//zero the values from the previous iteration
//add to the delta for each connection with an output neuron
for(int j = 0; j < nOutput; j ++) {
hiddenDelta.at(i) += outputDelta.at(j) * weightHtoO(i,j) ;
}
}
//Adjust weights Input to Hidden
for(int i = 0; i < nInput; i ++) {
for(int j = 0; j < nHidden; j ++) {
weightItoH(i,j) += learningRate * hiddenDelta.at(j);
}
}
//Adjust weights hidden to Output
for(int i = 0; i < nOutput; i++) {
for(int j = 0; j < nHidden; j ++) {
weightHtoO(j,i) += learningRate * outputDelta.at(i) * hiddenNeurons.at(j);
}
}
}
输入
nInputPatterns = 4;
inputPatterns.assign(nInputPatterns, vector<int>(2));
inputPatterns[0][0] = 1;
inputPatterns[0][1] = 1;
inputPatterns[1][0] = 0;
inputPatterns[1][1] = 1;
inputPatterns[2][0] = 1;
inputPatterns[2][1] = 0;
inputPatterns[3][0] = 0;
inputPatterns[3][1] = 0;
desiredOutput = {0,1,1,0};
Sigmoid功能和宏
#define sigmoid(value) (1/(1+exp(-value)));
#define dersigmoid(value) (value*(1-value));
//Macro's
#define weightItoH(input,hidden) weightsIH.at(nInput*hidden+input)
#define weightHtoO(hidden,output) weightsHO.at(nHidden*output+hidden)
C ++文件:http://pastebin.com/8URZAHSy 标题文件:http://pastebin.com/YiMXpmZX
答案 0 :(得分:1)
没有随机初始化。这是打破对称性所必需的;否则你所有的神经元都会学到完全相同的值。这与有一个神经元有效,一个神经元不足以进行XOR。