python自带rbf函数吗_Scipy径向基函数(scipy.interpolate.rbf)中的Python MemoryError

I'm trying to interpolate a not-so-large (~10.000 samples) pointcloud representing a 2D surface, using Scipy Radial Basis Function (Rbf). I got some good results, but with my last datasets I'm consistently getting MemoryError, even though the error appears almost instantly during execution (the RAM is obviously not being eaten up).

I decided to hack a copy of the rbf.py file from Scipy, starting by filling it up with some print statements, which have been very useful. By decomposing the _euclidean_norm method line by line, like this:

def _euclidean_norm(self, x1, x2):

d = x1 - x2

s = d**2

su = s.sum(axis=0)

sq = sqrt(su)

return sq

I get the error in the first line:

File "C:\MyRBF.py", line 68, in _euclidean_norm

d = x1 - x2

MemoryError

That norm is called upon an array X1 in the form [[x1, y1], [x2, y2], [x3, y3], ..., [xn, yn]], and X2, which is X1 transposed by the following method inside Rbf class, already hacked by me with debugging purposes:

def _call_norm(self, x1, x2):

print x1.shape

print x2.shape

print

if len(x1.shape) == 1:

x1 = x1[newaxis, :]

if len(x2.shape) == 1:

x2 = x2[newaxis, :]

x1 = x1[..., :, newaxis]

x2 = x2[..., newaxis, :]

print x1.shape

print x2.shape

print

return self._euclidean_norm(x1, x2)

Please notice that I print the shapes of inputs. With my current dataset, that's what I get (I added the comments manually):

(2, 10744) ## Input array of 10744 x,y pairs

(2, 10744) ## The same array, which is to be "reshaped/transposed"

(2, 10744, 1) ## The first "reshaped/transposed" form of the array

(2, 1, 10744) ## The second "reshaped/transposed" form of the array

The rationale is, according to documentation, to get "a matrix of the distances from each point in x1 to each point in x2", which mean, since the arrays are the same, a matrix of distances between every pair of the entry array (which contains the X and Y dimensions).

I tested the operation manually with much smaller arrays (shapes (2,5,1) and (2,1,5), for example) and the subtraction works.

How can I find out why it is not working with my dataset? Is there any other obvious error? Should I check some form of ill-conditioning of my dataset, or perform some pre-processing on it? I think it is well-conditioned, since I can plot it in 3D and the cloudpoint is visually very well formed.

Any help would be very much appreciated.

Thanks for reading.

解决方案

Your dataset should be fine: the error appears because you don't have enough RAM to store the result of the subtraction.

According to the broadcasting rules, the result will have shape

(2, 10744, 1)

-(2, 1, 10744)

------------------

(2, 10744, 10744)

Assuming these are arrays of dtype float64, you need 2*10744**2*8 = 1.72 GiB of free memory. If there isn't enough free memory, numpy won't be able to allocate the output array and will immediately fail with the error you see.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值