How can I speed up spliting a matrix into submatricies?

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

Are bC(1:rows,i) and bC(:,i); identical columns? That is, is rows==size(bC,1) ?
Yes, both composite matricies have the same number of rows, such that size(wC,1) = size(bC,1). The column dimension is not equal, tho. bC is a composition of column vectors (2 columns here), whereas wC is a composition of dissimilar matricies (2 matricies here).
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.
Is there a fast function that can split a matrix according to an array of indices? Split a 4,6 and a 2,2 matrix from a 4,8 matrix, for example.
If not, then I would need to extract the matricies in a loop with repeated calls to the composite matrix, or essentially the same thing as I am doing now.
Which brings me to a followup question: Would a cell array of matricies be faster from an indexing perspective than splitting arrays?
No there is no routine to do fast splitting. mat2cell and num2cell just loop.

Sign in to comment.

Answers (0)

Products

Release

R2020b

Asked:

on 28 Oct 2020

Commented:

on 28 Oct 2020

Community Treasure Hunt

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

Start Hunting!