torch 矩阵操作

1、按索引赋值:index_put

#index_put不改变变量本身
#index_put_改变变量本身,相当于inplace操作
#index_input((维度1坐标列表, 维度2坐标列表), 赋值列表)
x = torch.ones((3, 3))
print(x)
index = [torch.LongTensor([0, 1, 0, 2]),torch.LongTensor([0, 2, 2, 1])]#生成索引
value = torch.Tensor([0, 1, 1, 2])
x1 = x.index_put(index, value)
print(x)

2、按条件筛选:masked_select

x2 = torch.masked_select(x1, x1>=0)

3、矩阵扩展:expand,类似numpy tile。

x3 = x2.expand(5, 5)

你可能感兴趣的:(Pytorch,python,pytorch)