Numpy
新建维度
arr = np.array((1,2,3,4,5,6))
print(arr.shape)
arr1 = arr[np.newaxis, :]
print(arr1.shape)
arr2 = arr[:, np.newaxis]
print(arr2.shape)
arr3 = arr2.reshape(2,-1)
print(arr3.shape)
arr4 = arr3[:, np.newaxis,:]
print(arr4.shape)
arr5 = np.expand_dims(arr4, axis=0)
print(arr5.shape)
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[11,21,31],[7,8,9]])
a = a[:,:,np.newaxis]
b = b[:,:,np.newaxis]
np.concatenate((a,b),axis=2)
array([[[ 1, 11],
[ 2, 21],
[ 3, 31]],
[[ 4, 7],
[ 5, 8],
[ 6, 9]]])
a = np.array(([1,2,3],[3, 4, 5]))
a.ndim
torch
src = torch.from_numpy(src)
print(src.ndimension())
print(src.size())
x.unsqueeze(0)
x = torch.randn(2, 3, 5)
x.size()
x.permute(2, 0, 1).size()
x.contiguous()
clone
bboxes.new_tensor([1,2,3]) 生成一个tensor,内容是1,2,3 类型和属性与bboxes一致
使用einops库
from einops import rearrange, reduce, repeat
output_tensor = rearrange(input_tensor, 't b c -> b c t')
output_tensor = reduce(input_tensor, 'b c (h h2) (w w2) -> b h w c', 'mean', h2=2, w2=2)
output_tensor = repeat(input_tensor, 'h w -> h w c', c=3)