How can I speed up spliting a matrix into submatricies?
Show older comments
I am working on a back propagation assignment and am running into high epoch times for large training samples. I think that how I am storing and accessing the weight and bias matricies is one of the leading causes.
For storing the overall weights during training, I have two matricies of doubles whose size is dependent on the network specs. For the current configuration, the weight matrix is 400x1184 entries and the bias matrix is 400x2 entries. These matricies consist, each, of two smaller matricies, [w1,w2], [b1,b2], with the unused space filled with zeros (since size(w1) ~= size(w2)).
During training, I extract the individual weight matricies from the newly calculated wC and wB matricies via
w = wC(1:rows,offset+1:cols+offset);
b = bC(1:rows,i);
where wC,wB are the composite matricies and rows, cols, offset are variables that define the current size and position of the submatricies within wC and bC. This operation occurs twice per training sample for this network, once for w1 and once for w2. Each time an output matrix is calculated from w and b, but w and b is not directly calculated upon. The operation above takes just about 0.0009s to complete.
My problem is what happens to the execution time when I have 60,000 training samples, when the time component of that operation swells to over 54 seconds. I drastically need to reduce this.
Is there a more time efficent way to split or index these matricies so as to reduce cycle time?
Note that I cannot use existing machine learning libraries on this assignment.
Thanks
5 Comments
per isakson
on 28 Oct 2020
Edited: per isakson
on 28 Oct 2020
Are bC(1:rows,i) and bC(:,i); identical columns? That is, is rows==size(bC,1) ?
J Krause
on 28 Oct 2020
Walter Roberson
on 28 Oct 2020
depending on the flow it might possibly be worth extracting the submatrix (1:rows, 1:max_offset+cols) outside the for i loop, which you could then use : for the first index.
The reason this could save time is that extracting all rows of a range of columns becomes equivalent to extracting a consecutive chunk of memory, whereas extracting a subrange of rows requires breaking up the chunks. Doing the extraction of the submatrix has a cost but the overhead is lower doing it once instead of looping.
Because of the extra cost of the initial extracting, this approach has the potential to be slower, and you might not save many seconds, perhaps only three out of 54.
J Krause
on 28 Oct 2020
Walter Roberson
on 28 Oct 2020
No there is no routine to do fast splitting. mat2cell and num2cell just loop.
Answers (0)
Categories
Find more on AI for Signals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!