1

I am implementing Gaussian input based RBM in MATLAB.

vi has dimension of 100*784, w has dimension of 784*500, sigma has dimension of 1*784.

p(h|v)= sigmoid(cj+wij*vi/sigma^2). I am getting dimensional error when I multiply w*v/sigma^2. I have implemented it as below,

poshidprobspart = bsxfun(@rdivide,data,sigmas.^2);
poshidprobs = 1./(1 + exp(-((vishid * poshidprobspart) + repmat(hidbiases,numcases,1))));

What is causing the error in the code

chappjc
  • 30,359
  • 6
  • 75
  • 132
prakash
  • 125
  • 6

2 Answers2

1

In the part of the code bsxfun(@rdivide,data,sigmas.^2), you need to align the matching non-singleton dimensions. In other words, if the size of sigmas is 1x784, and the size of data is 784x500, you need to match the 784 dimension. You probably want to transpose sigmas:

% bsxfun: 784x500 @rdivide 784x1 => 784x500
poshidprobspart = bsxfun(@rdivide,data,(sigmas.^2).');

Then poshidprobspart will be 784x500, which can then be multiplied: vishid * poshidprobspart if vishid is 100x784, resulting in a 100x500 matrix.

If numcases is 100 and hidviases is a row vector of length 500, then your code will run.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • when i do that way, i got following error.Error using bsxfun Non-singleton dimensions of the two input arrays must match each other. Error in grbmcd (line 74) neghidprobspart1=bsxfun(@rdivide,negdata,(sigmas.^2)'); – prakash Nov 23 '13 at 23:15
  • What is `size(negdata)` and `size(sigmas)`? – chappjc Nov 23 '13 at 23:16
  • negdata size is same as data size, sigmas=1*784 – prakash Nov 23 '13 at 23:24
  • 1
    Well, what is `size(data)`? 784x500? `bsxfun(@rdivide,negdata,(sigmas.^2)');` works fine if `negdata` is 784x500 and `sigmas` is 1x784 – chappjc Nov 23 '13 at 23:29
  • OK... so, do you understand my advice? You don't need to transpose everything in `neghidprobspart2=vishid'*neghidprobspart1';` if you instead transpose `sigmas` as long as `data`/`negdata`/whatever is 784x500. – chappjc Nov 23 '13 at 23:33
  • but if i do transpose of sigma, i get error as i mentioned above.i told it works fine with the answer i mentioned – prakash Nov 24 '13 at 00:47
  • That's great, my point is `bsxfun(@rdivide,rand(784,500),rand(784,1))` works (try it!) so you can do it either way, you just need to make sure the arguments match. Clearly `negdata` is not 784x500 (it must be 500x784). That is what you would need to check. Your question does not at all make it clear the size of the variables, the names of which keep changing! – chappjc Nov 24 '13 at 00:51
  • sorry for not making clear datasize and changing the name.Next time when i post i keep it in mind. it happens beacuse i was keep trying and changing in matlab and paste same here. I understand your point.thanks – prakash Nov 24 '13 at 01:11
0

It works fine,if i implement in following way.

neghidprobspart1=bsxfun(@rdivide,negdata,sigmas.^2);
neghidprobspart2=vishid'*neghidprobspart1';
neghidprobs  =1./(exp(-(neghidprobspart2'+repmat(hidbiases,numcases,1))));                                                            
prakash
  • 125
  • 6
  • I'm guessing `negdata` is just `data'`. In that case, it's the same thing I'm suggesting. Just align the non-singleton dimensions. – chappjc Nov 23 '13 at 23:31