稀疏张量基础
稀疏張量基礎(chǔ)
稀疏張量是稀疏矩陣的高維擴(kuò)展,其中非零元素表示為一組索引和關(guān)聯(lián)值。
Data Generation
可以通過(guò)提取非零元素直接生成數(shù)據(jù)。本文展示了一個(gè)簡(jiǎn)單的2D數(shù)組,其中心有5個(gè)非零元素。
data = [
[0, 0, 2.1, 0, 0],
[0, 1, 1.4, 3, 0],
[0, 0, 4.0, 0, 0]
]
def to_sparse_coo(data):
# An intuitive way to extract coordinates and features
coords, feats = [], []
for i, row in enumerate(data):
for j, val in enumerate(row):
if val != 0:
coords.append([i, j])
feats.append([val])
return torch.IntTensor(coords), torch.FloatTensor(feats)
to_sparse_coo(data)
注意,將坐標(biāo)與特征一起提取。這是一個(gè)簡(jiǎn)單的示例,效率很低而且是人為的。在許多實(shí)際應(yīng)用中,不太可能獲得離散坐標(biāo)。
稀疏張量初始化
流水線(xiàn)的下一步是初始化稀疏張量。AMinkowskiEngine.SparseTensor需要具有批次索引的坐標(biāo);導(dǎo)致張量稀疏D+1 維空間尺寸(如果原始坐標(biāo)具有 D維 尺寸)。
coords0, feats0 = to_sparse_coo(data_batch_0)
coords1, feats1 = to_sparse_coo(data_batch_1)
coords, feats = ME.utils.sparse_collate(
coords=[coords0, coords1], feats=[feats0, feats1])
這里使用了MinkowskiEngine.utils.sparse_collate函數(shù),但是可以使用MinkowskiEngine.utils.batched_coordinates將坐標(biāo)列表轉(zhuǎn)換為MinkowskiEngine.SparseTensor兼容坐標(biāo)。
連續(xù)坐標(biāo)的稀疏張量
在許多情況下,神經(jīng)網(wǎng)絡(luò)中使用的坐標(biāo)是連續(xù)的。稀疏張量網(wǎng)絡(luò)中使用的稀疏張量是在離散坐標(biāo)系中定義的。要將連續(xù)坐標(biāo)中的特征轉(zhuǎn)換為離散坐標(biāo),提供了特征平均功能,可將連續(xù)坐標(biāo)中的特征轉(zhuǎn)換為離散坐標(biāo)。可以為此簡(jiǎn)單地使用稀疏張量初始化。例如,
sinput = ME.SparseTensor(
feats=torch.from_numpy(colors), # Convert to a tensor
coords=ME.utils.batched_coordinates([coordinates / voxel_size]), # coordinates must be defined in a integer grid. If the scale
quantization_mode=ME.SparseTensorQuantizationMode.UNWEIGHTED_AVERAGE # when used with continuous coordinates, average features in the same coordinate
)
logits = model(sinput).slice(sinput)
稀疏張量算法
可以將初始化的稀疏張量與簡(jiǎn)單的前饋神經(jīng)網(wǎng)絡(luò)一起使用,但是在許多情況下,需要執(zhí)行一些非常規(guī)的算子,這就是為什么來(lái)使用此庫(kù)的原因。這里,提供了一些簡(jiǎn)單的算子,這些算子允許沿特征維的稀疏張量和級(jí)聯(lián)之間的二進(jìn)制運(yùn)算。
sparse tensors
A = ME.SparseTensor(coords=coords, feats=feats)
B = ME.SparseTensor(
coords=new_coords,
feats=new_feats,
coords_manager=A.coords_man, # must share the same coordinate manager
force_creation=True # must force creation since tensor stride [1] exists
)
C = A + B
C = A - B
C = A * B
C = A / B
在這里,創(chuàng)建兩個(gè)具有不同稀疏模式的稀疏張量。強(qiáng)制第二個(gè)稀疏張量B共享坐標(biāo)管理器coords_man。允許在兩個(gè)稀疏張量之間共享計(jì)算圖。目前的語(yǔ)義相當(dāng)丑陋,但將來(lái)會(huì)隱藏起來(lái)。
如果添加兩個(gè)稀疏張量,將添加兩個(gè)功能。如果存在一個(gè)非零元素,但在特定坐標(biāo)上的另一個(gè)稀疏張量上沒(méi)有,將不存在的值設(shè)為0,因?yàn)橄∈鑿埩績(jī)H保存非零元素。根據(jù)定義,未指定的任何內(nèi)容均為0。其他所有二進(jìn)制算子也是如此。
對(duì)于局部算子,強(qiáng)制坐標(biāo)具有相同的稀疏模式。
in place operations
Note that it requires the same coords_key (no need to feed coords)
D = ME.SparseTensor(
# coords=coords, not required
feats=feats,
coords_manager=A.coords_man, # must share the same coordinate manager
coords_key=A.coords_key # For inplace, must share the same coords key
)
A += D
A -= D
A *= D
A /= D
注意,對(duì)稀疏張量D使用相同的coords_key。如果嘗試使用帶有不同coords_key的稀疏張量,將給一個(gè)斷言錯(cuò)誤。
功能串聯(lián)
如果兩個(gè)稀疏張量共享相同的coords_key,可以沿特征維連接們。
If you have two or more sparse tensors with the same coords_key, you can concatenate features
E = ME.cat(A, D)
分批分解
稀疏張量的內(nèi)部結(jié)構(gòu)將批處理中的所有非零元素折疊為坐標(biāo)矩陣和特征矩陣。要分解輸出,可以使用幾個(gè)函數(shù)和屬性。
coords0, feats0 = to_sparse_coo(data_batch_0)
coords1, feats1 = to_sparse_coo(data_batch_1)
coords, feats = ME.utils.sparse_collate(
coords=[coords0, coords1], feats=[feats0, feats1])
sparse tensors
A = ME.SparseTensor(coords=coords, feats=feats)
conv = ME.MinkowskiConvolution(
in_channels=1, out_channels=2, kernel_size=3, stride=2, dimension=2)
B = conv(A)
Extract features and coordinates per batch index
coords = B.decomposed_coordinates
feats = B.decomposed_features
coords, feats = B.decomposed_coordinates_and_features
To specify a batch index
batch_index = 1
coords = B.coordinates_at(batch_index)
feats = B.features_at(batch_index)
總結(jié)
- 上一篇: 稀疏张量网络
- 下一篇: MinkowskiPooling池化(上