Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Memory optimised way of defining matrices in numpy

import numpy as np

Preface:

Skip preface if you get bored reading because you know this already.

I've recently come across a problem while debugging. I wrote `A = B = C = np.zeros([3,3]) and I thought I've just defined three new matrices. What I did was in fact different. I defined one new matrix (filled with zeros) and three labels, each referring to the same matrix. Let me illustrate with the following example:

>>> a = b = [0,0]
>>> a
[0,0]
>>> b
[0,0]
>>> # All good so far.
>>> a[0] = 1
>>> a
[1,0]
>>> # Nothing short of what one would expect...
>>> b
[1,0]
>>> # ... but since 'b' is assigned tot he same tuple, it changes as well.

The question:

Well. Now that I know that's no problem right? Surely I can just write:

A = np.zeros([3,3])
B = np.zeros([3,3])
C = np.zeros([3,3])

and everything works? That's right but I could equally well write:

A, B, C = np.zeros([3,3,3])

I would think that the second option uses memory in more efficient way since it defines a 3x3x3 tensor and then 3 labels A, B and C for each of it's layers instead of three separate matrices with possible bits of memory between them.

Which one would you think is better?

like image 720
MarcinKonowalczyk Avatar asked Sep 02 '26 15:09

MarcinKonowalczyk


1 Answers

Most of all, it smells like premature optimization. If we're talking about a small number of matrices, it doesn't matter either way. If we're talkiing about a large number of matrices, you're not likely to make use of unpacking.

Having said that, the second option involves creating a larger underlying storage, while the first one creates three separate storages. The former is somewhat more efficient if all three matrices share the same lifetime. The latter is more readable, and allows releasing memory of individual matrices. If this kind of optimization matters to you at all, measure.

like image 118
user4815162342 Avatar answered Sep 04 '26 04:09

user4815162342