Neural network does not learn XOR (converges to 0.5 output) - c++

I wrote a multilayer perceptron which should be able to learn XOR. However, whatever I do it converges to an output of 0.5 for inputs (1,1), (0,1) and (1,0). While for the input (0,0) it converges to zero. Does anyone have an idea where my mistake is?
Forward pass:
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));
}
}
Backpropagation:
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);
}
}
}
Input
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 function and macro's
#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++ file: http://pastebin.com/8URZAHSy
Header file: http://pastebin.com/YiMXpmZX

There's no random initialization. This is needed to break the symmetry; else all your neurons learn the exact same values. That's effectively the same as having one neuron, and one neuron is insufficient for XOR.

Related

MAX_value in 2d array for each column

What is wrong with that code?
Sorry, if here bad code.
I'm need to find max value for all column in 2d dimensional of float values, then find them sum.
First of all I write values for Array, then display them at screen and tried to find max values for each column.
Photo: didn't working and correctly work
___________________
[5.7 ; 4.2 ; 5.8;]
[654.87; 5.86; 3.76;] - Work correctly
[8.54; 7.54; 8.4;]
------------------
Max value of 1 column = A[2,1] = 654.87;
Max value of 2 column = A[3,2] = 7.54;
Max value of 3 column = A[3,3] = 8.4;
___________________
[4.6 ; 2.65 ; 76.3;]
[65.64; 7.32; 76.2;] - Work not correctly
[654.8; 1.6; 5.7;]
------------------
Max value of 1 column = A[3,1] = 654.8;
Max value of 2 column = A[3,2] = 7.32;
Max value of 3 column = A[3,3] = 5.7;
#include<iostream>
#include<conio.h>
int main(void)
{
system("cls");
int N;
int suma = 0;
A:
std::cout<<"Write array size N x N : ";
std::cin>>N;
if(N>10 || N<=1)
{
system("cls");
std::cout<<"N must be <= 10 and > 1;"<<std::endl;
goto A;
}
float **A = new float *[N];
for(int i = 0; i < N; i++)
{
A[i] = new float [N];
for(int j = 0; j < N; j++)
{
std::cout<<"A["<<i+1<<"][";
std::cout<<j+1<<"] = ";
std::cin>>(*(*(A+i)+j));
}
}
system("cls");
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(j<N-1)
{
std::cout<<"A["<<i+1<<"]["<<j+1<<"] = "<<*(*(A+i)+j);
std::cout<<"; ";
}
else
{
std::cout<<"A["<<i+1<<"]["<<j+1<<"] = "<<*(*(A+i)+j);
std::cout<<";"<<std::endl;
}
}
}
float *max = new float [N];
std::cout<<"------------------------------------";
std::cout<<std::endl;
for(int i = 0; i < N - (N - 1); i++)
{
for(int j = 0; j < N; j++)
{
*(max+j) = *(*(A+i)+j);
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( (*(max+i)) < (*(*(A+j))+i) &&
(*(max+i)) != ((*(*(A+j)))+i) )
{
*(max+i) = *(*(A+j)+i);
}
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if( (*(*(A+j)+i)) == (*(max+i)) )
{
std::cout<<"Max value of "<<i+1;
std::cout<<" column = ";
std::cout<<"A["<<j+1<<"]["<<i+1;
std::cout<<"] = "<<(*(max+i));
std::cout<<std::endl;
}
}
suma+=(*(max+i));
}
std::cout<<"Sum of largest value = "<<suma;
_getch();
}
Oh... sorry just a mistake in if() by which i losen more than 3.5 hour...
if( (*(max+i)) < (*(*(A+j))+i)
&&
(*(max+i)) != ((*(*(A+j)))+i) )
{...}
I changed it to:
if( (*(max + i)) < (*(*(A + j) + i))
&&
(*(max + i)) != (*(*(A + j) + i))
)
{...}
After that change all work properly

Why is my largest sub-square-matrix sum program not woring with small matrices with negtive numbers?

This program is meant to take in a square matrix of integers and outputs the largest sub-square-matrix sum.
The first line of input is an integer which indicates the dimension of the square matrix, followed by the actual matrix row-by-row.
My program works almost perfectly except it does not work when using small matrices with negative values. Can anyone help me optimise the code, I cant see where its is going wrong
Example Input1:
3
1 2 3
4 5 6
7 8 9
Output: 45
Example Input2:
3
1 2 3
4 5 6
-7 -8 -9
Output: 16
NB: Since the largest square matrix is [2 3; 5 6] which sums to 16
My code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int Numberofelements,n,counter = 0,sum=0,result = 0,Maximumvalue = -1, *pointervalue = NULL;
int count = 0;
cin>>n;
int mat[n][n];
int TempMatrix[n][n];
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
cin>>mat[i][j];
if(mat[i][j]<0){
count++;
}
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j< n; ++j)
{
sum = sum + mat[i][j];
}
if (sum > 0 )
{
counter++;
}
sum = 0;
}
Numberofelements = counter;
for (int j = 0; j < n; j++)
{
sum = 0;
for (int i = 0; i < Numberofelements; i++)
{
sum = sum + mat[i][j];
}
TempMatrix[0][j] = sum;
for (int i=1; i<n-Numberofelements+1; i++)
{
sum = sum+(mat[i+Numberofelements-1][j] - mat[i-1][j]);
TempMatrix[i][j] = sum;
}
}
for (int i=0; i<n-Numberofelements+1; i++)
{
sum = 0;
for (int j = 0; j < Numberofelements; j++)
{
sum = sum + TempMatrix[i][j];
}
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][0]);
}
for (int j = 1; j < n-Numberofelements+1; j++)
{
sum = sum + (TempMatrix[i][j+Numberofelements-1] - TempMatrix[i][j-1]);
if (sum > Maximumvalue)
{
Maximumvalue = sum;
pointervalue = &(mat[i][j]);
}
}
}
for (int i = 0; i < Numberofelements; i++)
{
for (int j = 0; j < Numberofelements; j++)
{
result+=*(pointervalue + i*n + j);
}
}
cout << result;
return 0;
}

how to improve performance of 2d array in C++

I have a low-level function that will be called millions of times, so it should be very efficient. When I use "gprof" in Linux, I found that a part of the code takes 60% of the total computation of the function (the rest part is to solve the roots of a cubic equation). Here Point is a data structure has x and v, which will be converted to a matrix for later use. The idea is to subtract each row by the first row. The code shows like below
double x[4][3] = {0}, v[4][3] = {0};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = Point[i]->v[j];
x[i][j] = Point[i]->x[j];
}
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = v[0][j] - v[i][j];
x[i][j] = x[0][j] - x[i][j];
}
}
Can anyone show me the problem of this code? Why it performs so badly?
You can do it all in one pass:
double x[4][3] = {
{ Point[0]->x[0], Point[0]->x[1], Point[0]->x[2] }
};
double v[4][3] = {
{ Point[0]->v[0], Point[0]->v[1], Point[0]->v[2] }
};
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] = x[0][j] - Point[i]->x[j];
v[i][j] = v[0][j] - Point[i]->v[j];
}
}
You could even take that to the next level and put the entire thing into the initializers for x and v.
Or, if x and v in Point are each contiguous arrays:
double x[4][3], v[4][3]; // no init
// fill entire arrays
for (int i = 0; i < 4; ++i){
memcpy(x[0], Point[0]->x, sizeof(x[0]));
memcpy(v[0], Point[0]->v, sizeof(v[0]));
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] -= Point[i]->x[j];
v[i][j] -= Point[i]->v[j];
}
}

Putting String into a 2D Matrix in Objective C++

So I'm using Objective C++ and I want to put a string into a 4 by X (X = length of string/4) int array by using the ASCII code. The first quarter of the string (which is formatted to fit completely into a 4 by X array) is supposed to go in [0][col], the second quarter into [1][col], the third quarter into [2][col] and the fourth quarter into [3][col]. So I tried the following with 4 for loops, but it doesnt work at all, and I just can't seem to get it to work somehow. Any suggestions would be greatly appreciated.
textMatrix is the matrix in which I want to put the NSString/ASCII number, and inputFinal is the NSString itself. Length * (1/4) or whatever is also always going to be an integer.
for(int i = 0; i < length*(1/4); i++)
{
textMatrix[0][i] = (int)[inputFinal characterAtIndex: i];
}
for(int j = length*(1/4); j < length*(2/4); j++)
{
textMatrix[1][j] = (int)[inputFinal characterAtIndex: j];
}
for(int k = length*(2/4); k < length*(3/4); k++)
{
textMatrix[2][k] = (int)[inputFinal characterAtIndex: k];
}
for(int l = length*(3/4); l < length; l++)
{
textMatrix[3][l] = (int)[inputFinal characterAtIndex: l];
}
You can rewrite your 4 loops in 1 loop:
for(int i = 0; i < length; i++)
{
textMatrix[i/4][i%4] = (int)[inputFinal characterAtIndex:i];
}
I don't think I understand what you're trying to do..
Given a string: "Here";
do you want:
Matrix[0][0] = 'H';
Matrix[1][1] = 'e';
Matrix[2][2] = 'r';
Matrix[3][3] = 'e';
If so then this works:
#import <objc/objc.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
#implementation TestObj
int main()
{
NSString* str = #"Here";
int matrix[4][4] = {0};
for (int i = 0, j = 0; j < 4; ++j)
{
matrix[i][i++] = (int) [str characterAtIndex: j];
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
printf("%c", (char)matrix[i][j]);
}
}
return 0;
}
#end
The above prints Here.
actually a double loop like so ended up working best for me:
int index = 0;
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < length/4; col++)
{
textMatrix[row][col] = (int)[inputFinal characterAtIndex:index];
index++;
}
}

Infinite Impluse Response (IIR) Function

I am trying to design a signal class which includes an IIR filter function. The following is my code:
void signal::IIRFilter(vector<double> coefA, vector<double> coefB){
double ** temp;
temp = new double*[_nchannels];
for(int i = 0; i < _nchannels; i++){
temp[i] = new double[_ninstances];
}
for(int i = 0; i < _nchannels; i++){
for(int j = 0; j < _ninstances; j++){
temp[i][j] = 0;
}
}
for(int i = 0; i < _nchannels; i++){
for (int j = 0; j < _ninstances; j++){
int sum1 = 0;
int sum2 = 0;
for(int k = 0; k < coefA.size(); k++){
if ((j-k) > 0 ){
sum1 += coefA.at(k)*temp[i][j-k-1];
}
}
for (int m = 0; m < coefB.size(); m++){
if(j >= m){
sum2 += coefB.at(m)*_data[i][j-m];
}
}
temp[i][j] = sum2-sum1;
}
}
for(int i = 0; i < _nchannels; i++){
for(int j = 0; j < _ninstances; j++){
_data[i][j] = temp[i][j];
}
}
}
_data contains my original signal, _ninstances is my number of samples, and _nchannels is the number of channels. The function compiles and works but the result I am getting is different from the result given by MATLAB. I even use the same coefficients given by MATLAB. Is there anything that I'm doing wrong in my function?
One issue that I can see is that you are declaring sum1 and sum2 as integers when they should be double. To avoid this kind of error in the future, you should try configuring your compiler to warn of implicit conversions. In g++, this is accomplished using the -Wconversion flag.