为什么 numpy 零初始化数组给出除零以外的数字?

问题描述

我正在尝试为我的硕士论文在 C++ xtensor 库中重新实现 numpy 代码。但我无法理解为什么 numpy 零初始化数组给出 number(10,20,43,28,....)。请参阅下面的代码。

代码如下:

import numpy as np
    
scene_size = np.array([4,40,48],dtype=np.float32)
voxel_size = np.array([0.4,0.2,0.2],dtype=np.float32)
grid_size = np.array([10,200,240],dtype=np.int64)
lidar_coord = np.array([0,3],dtype=np.float32)
max_point_number = 45
#lidar point clouds,each point has 4 fields (x,y,z,intensity)
np.random.shuffle(point_cloud)  #(N,4) N number of points
    
    
shifted_coord = point_cloud[:,:3] + lidar_coord
# reverse the point cloud coordinate (X,Y,Z) -> (Z,X)
voxel_index = np.floor(
    shifted_coord[:,::-1] / voxel_size).astype(np.int)  #(N,3)
    
bound_x = np.logical_and(
    voxel_index[:,2] >= 0,voxel_index[:,2] < grid_size[2]) #(N,)
    
bound_y = np.logical_and(
    voxel_index[:,1] >= 0,1] < grid_size[1]) #(N,)
    
bound_z = np.logical_and(
    voxel_index[:,0] >= 0,0] < grid_size[0]) #(N,)
    
    
bound_box = np.logical_and(np.logical_and(bound_x,bound_y),bound_z) #(N,)
    
    
point_cloud = point_cloud[bound_box] ##(dynmaic_value,4)
voxel_index = voxel_index[bound_box] ##(dynmaic_value,3)
    
# [K,3] coordinate buffer as described in the paper
coordinate_buffer = np.unique(voxel_index,axis=0)  #removing the duplicates in axis 0
#which means for an example voxel_index[10] = (1,2,3) and voxel_index[20] = (1,3),remove one of them
K = len(coordinate_buffer)
T = max_point_number
#Not storing any points,but zeros initialized array of shape K
# [K,1] store number of points in each voxel grid
number_buffer = np.zeros(shape=(K),dtype=np.int64)
    
# [K,T,7] feature buffer as described in the paper
feature_buffer = np.zeros(shape=(K,7),dtype=np.float32)
    
# build a reverse index for coordinate buffer
index_buffer = {}
for i in range(K):
    index_buffer[tuple(coordinate_buffer[i])] = i
    
for voxel,point in zip(voxel_index,point_cloud):
    index = index_buffer[tuple(voxel)]
    number = number_buffer[index]
    print(number)  #according to my understand it should be zero,but giving number like 4,19,34,23,26
    #Because number buffer is zeros initialized 
    if number < T:
        feature_buffer[index,number,:4] = point
        number_buffer[index] += 1

你可以在上面代码的注释部分看到,

  1. 我不明白,number_buffer 怎么会给出除 0 和大于零以外的实际值?

我正在使用第三方库在 C++ 中实现 numpy 功能。

谁能解释一下代码的行为方式?

为什么零初始化数组会给出大于 0 的实数值(这意味着每个体素中存储的点数)?

在 C++ 中使用 xtensor 实现的代码可以正常工作,直到条件:

if(number <T):

在 C++ 中,number 总是为零,因为 number_buffer 在 numpy 和 C++ 中都是零初始化的。因此,C++ 代码给出了 0,但 numpy 代码给出了某些索引的非零(例如大于零的值,这意味着每个体素中存储的点数)。

我很高兴提供我需要的更多信息。请帮我解决这个问题。

提前致谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...