How to multiply 3 matrices?

I want to multiply 3 matrix. each has dimension sigmas=1*784, poshidstates=100*500, vishid=784*500. Then i want to add this with matrix of dimension visbiases=1*784. numdims=784,numcases=100.
i have done this as,
negdatapart=repmat(sigmas,numdims,1)' *(poshidstates*vishid')' ;
negdata= negdatapart'+repmat(visbiases,numcases,1)
something goes wrong here i guess. Can someone give me an idea

9 comentarios

Geoff Hayes
Geoff Hayes el 19 de Ag. de 2014
Subha - what do you mean by something goes wrong here i guess. What are you expecting to happen? Is there a problem because there is a variable named visbias and one named visbiases - are they one and the same?
dpb
dpb el 19 de Ag. de 2014
Looks ok if just fix the type with
negdata=negdatapart.'+repmat(visbiasenumcases,1);
NB: Probably (almost certainly) want the .' element-wise transpose instead of just ' here.
subha
subha el 19 de Ag. de 2014
Editada: subha el 19 de Ag. de 2014
@Geoff - this is part of my Restricted Boltzmann Machine program. I want to multiply sigmas,poshidstates, vishid and want to add result with visbiases. But when i remove sigmas term in the above statement,My program produces proper output. With the above lines i get improper result. So i guess, its due to i have done something wrong in these two statements.
subha
subha el 19 de Ag. de 2014
@dpb .Even after changing problem didnt solved.
Michael Haderlein
Michael Haderlein el 19 de Ag. de 2014
Dear Subha,
I guess most people here haven't programmed a Restriced Boltzmann Machine. So for us, the variables are just like a, b, c and so on. As you have a logical error and not a syntax error, we cannot help unless you tell us what is going wrong. Best would be a little example with, say, poshidstates of size (2,3), vishid of size (4,3) and so on. Set it to simple numbers and tell us what the result should be.
Patrik Ek
Patrik Ek el 19 de Ag. de 2014
MxN * NxP * PxK = MxK
Fundamental algebra. You need to multiply the matrices in the correct order, with correct dimensions
Michael Haderlein
Michael Haderlein el 19 de Ag. de 2014
The dimensions are fine, (poshidstates*vishid') is 100x784, transposed is 784x100, multiplied with 784x784 is 784x100. No runtime error to expect. But if the result is not fine, we need to know what should be the result for a simple case. I see the repmat(sigmas,numdims,1)' part critical, but that's just a very wild guess.
subha
subha el 19 de Ag. de 2014
Editada: subha el 19 de Ag. de 2014
@Michael- I want to represent this equation. visbias+sigmas(poshidstates*vishid).
Dimensions are mentioned in the question. I want to get negdata variable to have dimension 100*784. I too guess the problem is with sigmas only. Because if i remove sigmas and write as below, my program works good( that is, i was able to see the output. Otherwise values are getting NAN after few iterations. ). I believe this is because of this two statements. Inparticular because of sigma. i removed sigma and run. It was working fine
negdatapart= vishid*poshidstates'
dpb
dpb el 20 de Ag. de 2014
As others have suggested, you need to back off to a small-enough problem that you can compute the correct answer and then use debug to work thru the logic error in where it goes wrong. That it's "after a few iterations" probably means a logic error elsewhere as if it were a dimensions problem as has been pointed out, it wouldn't work at all.
IOW, as another has also suggested, what you apparently have is a logic error, not Matlab-specific error and you need to see why the result begins to deviate to follow where it goes wrong. That again is probably practical only w/ a very small problem sample size instead of order of 100's.

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
Matt J el 19 de Ag. de 2014
Editada: Matt J el 19 de Ag. de 2014

1 voto

negdata=bsxfun(@plus, sigmas+visbiases, poshidstates*vishid')

3 comentarios

subha
subha el 19 de Ag. de 2014
I want to multiply sigma with poshidstates*vishid an then add this to visbiases
Matt J
Matt J el 20 de Ag. de 2014
Editada: Matt J el 20 de Ag. de 2014
mult_result=bsxfun(@times, sigmas, poshidstates*vishid');
add_result=bsxfun(@plus, visbiases, mult_result);
If this istill sn't precisely what you want, you should still be able to figure it out from the documentation from bsxfun. It's generally what you would use for any kind of element-wise operation between a matrix and a vector.
subha
subha el 21 de Ag. de 2014
Editada: subha el 21 de Ag. de 2014
Hi matt,
Thanks. I just found this answer and came to post the answer. You have given already.. Its working proper. This is the perfect answer

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 19 de Ag. de 2014

Editada:

el 21 de Ag. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by