How to multiply symbolic with numeric?

Hi all'
assume that
w=randn(2,2)
a=sym('a',[2 2])
I need to get the answer of multiblication of (a .* w) with smae size [ 2 2] since, I multiplied them like (a .* w) i got this answer
ans =
[ -(4545034027795199*a1_1)/18014398509481984, -(8311781718565711*a1_2)/18014398509481984]
[ -(1390961227654289*a2_1)/1125899906842624, (5388115603050723*a2_2)/18014398509481984]
However, the random values of w is
w =
-0.2523 -0.4614
-1.2354 0.2991
a =
[ a1_1, a1_2]
[ a2_1, a2_2]
I don't belive this correct results however, I need the results like this [ -0.2523 * a1_1, -0.4614* a1_2
-1.2354 * a2_1, 0.2991 * a2_2]
thanks indeed

 Accepted Answer

Steven Lord
Steven Lord on 21 Dec 2020
Call vpa on the result.

3 Comments

Thanks alot sir
it worked but the results are not round i got vpa(w.*a) = 0.22524425* a1_1 how can i make it only three digit like 0.225 * a1_1
Thnks indeed sir

Sign in to comment.

More Answers (1)

w = round(sym(randn(2,2),'d'),4)
You are, by the way, not correct about what the value of w is. The actual values for w extend to about 15 significant decimal places, and you are getting confused because you have the default format in effect. I recommend that you use
format long g
and change your preferences to use that.
However, you defined your required output in terms of four decimal places, so I included a call to force four decimal places.
Note: symbolic floating point numbers give the strong impression that they are base 10 internally, but closer to the truth is that they use a base 2^30 representation, which is close enough to base 10^10 representation that it takes real effort to prove the difference.

13 Comments

thank you so much mr Walter. I appreciate that in fact, i have huge number of symbolic matrix and also am using diff in it
for instance
w12=sym('w12%d%d',[80,784]);
w23=sym('w23%d%d',[60,80]);
w34=sym('w34%d%d',[10,60]);
b12=sym('b12%d%d',[80,1]);
b23=sym('b23%d%d',[60,1]);
b34=sym('b34%d%d',[10,1]);
cox1=sym('cox1%d%d',[80,784]);
cox2=sym('cox2%d%d',[60,80]);
cox3=sym('cox3%d%d',[10,60]);
cox4=sym('cox4%d%d',[80,1]);
cox5=sym('cox5%d%d',[60,1]);
cox6=sym('cox6%d%d',[10,1]);
will multiply both of them and get answers thus, there is further process then using diff
so, this kind of sym taking long time to generat code may i know if there is diffrent approch to take less time
your support is highly appreciated.
w23=sym('w23%d%d',[60,80]);
w23(11,1) => w23111
w23(1,11) => w23111
Are you sure you want to be generating duplicate symbol names?
w23=sym('w23%d_%d',[60,80]);
would not have any duplicates.
well, I didn't noticed that basically, I don't want to duplicate so am going to use w23%d_%d due to all values of w23 souldn't be same
In other word, can I generate symbolic code with huge number of matrices
many thanks sir
MATLAB doesn't care much how many matrices you have, or how large they are, as long as you fit within available memory. There are theoretical limits, but you would need hundreds of petabytes of memory before you encountered the MATLAB limits.
You can, though, get performance issues depending on what you do with the matrices. For example det() of a dense 10 x 10 matrix takes more four minutes, and symbolic eigenvalues beyond about 4 x 4 takes quite long.
If you are taking a gradient with respect to a matrix of symbols, then the resulting array needs numel(expression) by numel(symbols), and calculating that can add up in time.
Totally agree sir I thought there is like functions or even toolbox that would help me to generate code like this beacuse am running my code right now since 15 minutes and still didn't get any results,
my regards.
well, do you think if run it using GPU enviroment that would help ? Is it possibal with sym?
GPU cannot help with symbolic calculations.
You have not really indicated what kind of calculations you are doing.
Alright, sir I need to generate code of neural network feedforwad and also backword peocesses in symbolic form. see the example
w12=sym('w12',[80,784]);
w23=sym('w23',[60,80]);
w34=sym('w34',[10,60]);
b12=sym('b12',[80,1]);
b23=sym('b23',[60,1]);
b34=sym('b34',[10,1]);
cox1=sym('cox1',[80,784]);
cox2=sym('cox2',[60,80]);
cox3=sym('cox3',[10,60]);
cox4=sym('cox4',[80,1]);
cox5=sym('cox5',[60,1]);
cox6=sym('cox6',[10,1]);
a1=sym('a1',[784,1]);
y=sym('y',[10,1]);
z2=w12*a1+b12;
a2 = z2./(1+exp(-z2));
z3=w23*a2+b23;
a3= z3./(1+exp(-z3));
z4=w34*a3+b34;
a4 = z4./(1+exp(-z4));
MSE= 1/2.*(a4-y).^2;
dMSEda4=(a4-y);
dL4= a4+ 1./(1+exp(-z4)).*(1- a4);
dL3= a3+ 1./(1+exp(-z3)).*(1- a3);
dL2= a2+ 1./(1+exp(-a2)).*(1- a2);
error4 = dMSEda4.* dL4;
error3 = (w34.'*error4).*dL3;
error2 = (w23.'*error3).*dL2;
errortot4 = error4;
errortot3 = error3;
errortot2 = error2;
grad4 = error4*a3.';
grad3 = error3*a2.';
grad2 = error2*a1.';
%%
mse=sum(MSE);
cox1_grad2= sum(sum(cox1.*grad2));
cox2_grad3= sum(sum(cox2.*grad3));
cox3_grad4= sum(sum(cox3.*grad4));
cox4_errortot2= sum(sum(cox4.*errortot2));
cox5_errortot3= sum(sum(cox5.*errortot3));
cox6_errortot4= sum(sum(cox6.*errortot4));
H= mse+ cox1_grad2+ cox2_grad3+ cox3_grad4+ cox4_errortot2+ cox5_errortot3+ cox6_errortot4;
%% dervative of W12
for i=1:80
for j=1:784
dH_dw12(i,j)=diff(H,w12(i,j));
end
end
%% dervative of W23
for i=1:60
for j=1:80
dH_dw23(i,j)=diff(H,w23(i,j));
end
end
%% dervative of W34
for i=1:10
for j=1:60
dH_dw34(i,j)=diff(H,w34(i,j));
end
end
%% dervative of b12
for i=1:80
for j=1:1
dH_db12(i,j)=diff(H,b12(i,j));
end
end
%% dervative of b23
for i=1:60
for j=1:1
dH_db23(i,j)=diff(H,b23(i,j));
end
end
%% dervative of b34
for i=1:10
for j=1:1
dH_db34(i,j)=diff(H,b34(i,j));
end
end
This is it what am try to do obvesily, when i run this code I didn't get any results at all even after 5 hours of running.
Really appreciate your help.
I need to generate code of neural network feedforwad and also backword peocesses in symbolic form
... Why??
The formulas are going to be very complicated, and they are going to be pretty much indecipherable. You generate some big ratios of polynomials and square them, and similar operations, and the symbolic Engine might well decide that it needs to expand everything out -- an expression by itself raised to a power might be left alone, but as soon as you start doing arithmetic then it has to worry because the arithemetic might give a 0. Divisions it might decide that it needs to express in lowest terms. Symbolic operations are not just building simple matrix formulas: every operation might be expanded.
By the time you get to dL4, you are dealing with vectors of expressions involving over 69000 variables (w2 by itself is over 62000 variables so any matrix multiply by it is going to be at least that many variables.)
Your expressions are going to be very long and complicated. The symbolic engine is not that fast, and there isn't much that can be done to speed it up when you use matrices that big.
I tried taking the derivative of the first entry of dL4 with respect to one of the variables (a11 it turned out.) It took over an hour just to compute it. Formatting it for output took another 7 hours before I gave up and killed the computation.
Taking one derivative of one entry of dL4 took my system about 18 minutes. You have over 65000 derivatives to take at the bottom of your code, each on a value that is more complicated than dL4 is. You would expect a minimum of 20 minutes per derivative, times 65000 derivivates, gives about 130000 minutes -> over 900 days.
Each derivative looks like it is going to be over 2 gigabytes to write out -> 130 petabytes .
Suppose that I whipped up something to run on distributed AWS and (somehow!!) managed to create that 130+ petabyte file tomorrow: what would you do with it?
It's just an idea for a project, anyway your explanation is clear and I have understood all things thank you so much sir really appreciate your time to help.
Maybe I will try another idea,
my regards.

Sign in to comment.

Asked:

on 21 Dec 2020

Commented:

on 23 Dec 2020

Community Treasure Hunt

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

Start Hunting!