Sep 30, 2015 at 11:32am UTC
Dear i am new in c++ programming, i have written the bellow code but after running it i get some unexpected return value for two of my variables and i can't understand why. if one could help me out here it would be great thanks.
#include <iostream>
#include <math.h>
#include "utils.h"
#include "rbm.h"
#include <stdio.h>
#include <stdlib.h>
#include <algorithm> // std::count
#include <vector> // std::vector
using namespace std;
using namespace utils;
int visible_E;
int hidden_E;
double *vbias;
double *hbias;
double **weight;
// finding the number of hidden and visible elements in our vector
void visible_hidden ()
{
// counting elements in array:
int myints[]= {0,1,0,1,0,0,0,1}; // 8 elements
int mycount = std::count (myints, myints+8, 1);
std::cout << "visible elements = " << mycount <<endl;
visible_E = mycount;
// counting elements in container:
std::vector<int> myvector (myints, myints+8);
mycount = std::count (myvector.begin(), myvector.end(), 0);
std::cout << "Hidden elements = " << mycount <<endl;
hidden_E = mycount;
}//end of function visible_hidden
void bias_weight()
{
double **w;
double *hb;
double *vb;
if(w == NULL)
{
weight = new double*[hidden_E];
for(int i=0; i<hidden_E; i++) weight[i] = new double[visible_E];
double ai = 1.0 / visible_E;
for(int i=0; i<hidden_E; i++)
{
for(int j=0; j<visible_E; j++)
{
weight[i][j] = uniform(-ai, ai);
}
}
}
else
{
weight = w;
}
cout<<"the weight = ";
cout<< w<<endl;
if (hb == NULL)
{
hbias = new double [hidden_E];
for(int i = 0; i<hidden_E; i++)
{
hbias [i] = 0;
}
}
else
{
hbias = hb;
}
// print the hidden bias
cout<<"hidden bias =";
cout<< hb <<endl;
if (vb == NULL)
{
vbias = new double [visible_E];
for (int j = 0; j<visible_E; j++)
{
vbias [j] = 0;
}
}
else
{
vbias = vb;
}
//printing out visible bias
cout<<"visible bias = ";
cout<< vb<<endl;
}//end of void bias weight
int main()
{
visible_hidden();
bias_weight();
return 0;
}
//here is my ouput:
visible elements = 3
Hidden elements = 5
the weight = 0
hidden bias =0x5
visible bias = 0xb7478e55
Last edited on Oct 2, 2015 at 11:39am UTC
Sep 30, 2015 at 12:33pm UTC
the two values are address , because hb and vb are pointers . The overloaded operator << prints the address for a pointer type . Simply add a star before like this cout << *hb ;
Oct 2, 2015 at 9:39am UTC
Dear Ericool;
thanks for the hints, it works just find now with the *hb notation.
now i get this run time " Segmentation fault (core dumped)" when running my program. what could possibly generate that? thanks
Oct 2, 2015 at 10:23am UTC
double **w;
value of w is undefined. It might be anything. One thing is certain: it does not point to valid data.
if (w == NULL)
a huge chance that w is not in fact null, but does not contain anything useful either. Why do you even have this check? You can see clearly, there is no way that w will containg anything useful.
Same for other pointers.
Oct 2, 2015 at 11:39am UTC
I made few changes but still get the segmentation fault error.
Oct 2, 2015 at 12:26pm UTC
YOu have still did not change comparison with undefined vale I was tlking about.