我必须执行大量此类计算:
X.dot(Y).dot(Xt)
X = 1 x n矩阵
Y =对称n x n矩阵,每个元素有5个值之一(例如0,0.25,0.5,0.75,1)
Xt = n×1矩阵,X的转置,即X[np.newaxis].T
X和Y密集。我遇到的问题是大n,由于内存问题,我无法存储和使用矩阵Y.我只能使用一台机器,因此不能选择分布式计算。
我想到Y有2个功能,理论上可以减少存储Y所需的内存量:
我如何在实践中实现这一点?我查找了对称矩阵的存储,但据我所知,所有numpy矩阵乘法都需要"解包"对称性产生规则的n×n矩阵。
我知道numpy是专为内存计算而设计的,因此我可以选择不依赖于numpy的基于python的替代解决方案。我也愿意牺牲速度来提高记忆效率。
答案 0 :(得分:4)
更新:我发现使用一种将3个矩阵元素压缩成一个字节的格式实际上非常快。在下面的示例中,与使用2x
的直接乘法相比,速度惩罚小于@
,同时节省的空间大于20x
。
>>> Y = np.random.randint(0, 5, (3000, 3000), dtype = np.int8)
>>> i, j = np.triu_indices(3000, 1)
>>> Y[i, j] = Y[j, i]
>>> values = np.array([0.3, 0.5, 0.6, 0.9, 2.0])
>>> Ycmp = (np.reshape(Y, (1000, 3, 3000)) * np.array([25, 5, 1], dtype=np.int8)[None, :, None]).sum(axis=1, dtype=np.int8)
>>>
>>> full = values[Y]
>>> x @ full @ x
1972379.8153972814
>>>
>>> vtable = values[np.transpose(np.unravel_index(np.arange(125), (5,5,5)))]
>>> np.dot(np.concatenate([(vtable * np.bincount(row, x, minlength=125)[:, None]).sum(axis=0) for row in Ycmp]), x)
1972379.8153972814
>>>
>>> timeit('x @ full @ x', globals=globals(), number=100)
0.7130507210385986
>>> timeit('np.dot(np.concatenate([(vtable * np.bincount(row, x, minlength=125)[:, None]).sum(axis=0) for row in Ycmp]), x)', globals=globals(), number=100)
1.3755558349657804
以下解决方案速度较慢且内存效率较低。我留下它们只是为了参考。
如果你能为每个矩阵元素提供半个字节,那么你可以像np.bincount
这样使用:
>>> Y = np.random.randint(0, 5, (1000, 1000), dtype = np.int8)
>>> i, j = np.triu_indices(1000, 1)
>>> Y[i, j] = Y[j, i]
>>> values = np.array([0.3, 0.5, 0.6, 0.9, 2.0])
>>> full = values[Y]
>>> # full would correspond to your original matrix,
>>> # Y is the 'compressed' version
>>>
>>> x = np.random.random((1000,))
>>>
>>> # direct method for reference
>>> x @ full @ x
217515.13954751115
>>>
>>> # memory saving version
>>> np.dot([(values * np.bincount(row, x)).sum() for row in Y], x)
217515.13954751118
>>>
>>> # to save another almost 50% exploit symmetry
>>> upper = Y[i, j]
>>> diag = np.diagonal(Y)
>>>
>>> boundaries = np.r_[0, np.cumsum(np.arange(999, 0, -1))]
>>> (values*np.bincount(diag, x*x)).sum() + 2 * np.dot([(values*np.bincount(upper[boundaries[i]:boundaries[i+1]], x[i+1:],minlength=5)).sum() for i in range(999)], x[:-1])
217515.13954751115
答案 1 :(得分:1)
{@ 1}}的每一行(如果表示为Y
数据类型numpy.array
,如@PaperPanzer的答案中所建议的那样,可以压缩以占用更少的内存:实际上,您可以存储27 64位元素,因为64 / log2(5)= 27.56 ...
首先,压缩:
int
然后,解压缩:
import numpy as np
row = np.random.randint(5, size=100)
# pad with zeros to length that is multiple of 27
if len(row)%27:
row_pad = np.append(row, np.zeros(27 - len(row)%27, dtype=int))
else:
row_pad = row
row_compr = []
y_compr = 0
for i, y in enumerate(row_pad):
if i > 0 and i % 27 == 0:
row_compr.append(y_compr)
y_compr = 0
y_compr *= 5
y_compr += y
# append last
row_compr.append(y_compr)
row_compr = np.array(row_compr, dtype=np.int64)
解压缩的数组与row_decompr = []
for y_compr in row_compr:
y_block = np.zeros(shape=27, dtype=np.uint8)
for i in range(27):
y_block[26-i] = y_compr % 5
y_compr = int(y_compr // 5)
row_decompr.append(y_block)
row_decompr = np.array(row_decompr).flatten()[:len(row)]
的原始行重合:
Y