>> a = torch.randn(4, 4) >> a 0.0692 0.3142 1.2513 -0.5428 0.9288 0.8552 -0.2073 0.6409 1.0695 -0.0101 -2.4507 -1.2230 0.7426 -0.7666 0.4862 -0.6628 torch.FloatTensor of size 4x4] >> torch.min(a, 1) -0.5428 -0.2073 -2.4507 -0.7666 torch.FloatTensor of size 4x1] 3 2 2 1 torch.LongTensor of size 4x1]
torch.min(input, other, out=None) -->Tensor input中逐元素与other相应位置的元素对比,返回最小值到输出张量。即,outi = min(tensori,otheri) 两张量形状不需匹配,但元素数必须相同。
当形状不匹配时,input的形状作为返回张量的形状。
参数:
--input (Tensor):输入张量
--other (Tensor):第二个输入张量
--out (Tensor, optional):结果张量
例子:
>>> a = torch.randn(4) >>> a 1.3869 0.3912 -0.8634 -0.5468 [torch.FloatTensor of size 4] >>> b = torch.randn(4) >>> b 1.0067 -0.8010 0.6258 0.3627 [torch.FloatTensor of size 4] >>> torch.min(a, b) 1.0067 -0.8010 -0.8634 -0.5468 [torch.FloatTensor of size 4]
torch.ne(input, other, out=None) -->Tensor 逐元素比较input和other,即是否input ̸= other。第二个参数可以为一个数或与第一个参数相同形状和类型的张量。
参数:
--input (Tensor):待对比的张量
--other (Tensor or float):对比的张量或 float 值
--out (Tensor, optional):输出张量。必须为 ByteTensor 或者与 input 相同类型。
--返回值:一个 torch.ByteTensor 张量,包含了每个位置的比较结果 (如果 tensor!= other 为 True ,返回 1)。返回类型:Tensor。
例子:
>>> torch.ne(torch.Tensor([[1, 2], [3, 4]]), torch.Tensor([[1, 1], [4, 4]])) , → 0 1 1 0 [torch.ByteTensor of size 2x2]torch . sort( input , dim = None , descending = False , out = None ) -> (Tensor, LongTensor) 对输入张量input沿着指定维按升序排序。如果不给定dim,则默认为输入的最后一维。如果指定参数descending为True,则按降序排序。返回元祖(sorted_tensor,sorted_indices),sorted_indices为原始输入中的下标。 参数: -- input (Tensor) :要对比的张量 --dim(int,optional) :沿着此维排序 --descending(bool,optional) :布尔值,控制升降排序 --out(tuple,optional) :输出张量。必须为 ByteTensor 或者与第一个参数 tensor相同类型。 例子:
>>> x = torch.randn(3, 4) >>> sorted, indices = torch.sort(x) >>> sorted -1.6747 0.0610 0.1190 1.4137 -1.4782 0.7159 1.0341 1.3678 -0.3324 -0.0782 0.3518 0.4763 [torch.FloatTensor of size 3x4] >>> indices 0 1 3 2 2 1 0 3 3 1 0 2 [torch.LongTensor of size 3x4] >>> sorted, indices = torch.sort(x, 0) >>> sorted -1.6747 -0.0782 -1.4782 -0.3324 0.3518 0.0610 0.4763 0.1190 1.0341 0.7159 1.4137 1.3678 [torch.FloatTensor of size 3x4] >>> indices 0 2 1 2 2 0 2 0 1 1 0 1 [torch.LongTensor of size 3x4]torch . topk( input , k, dim = None , largest = True , sorted = True , out = None ) -> (Tensor, LongTensor)沿给定dim维度返回输入张量Input中K个最大值。如果不指定dim,则默认为input的最后一维。如果为largest为False,则返回最小值的k个值。返回一个元祖(values,indices),其中indices是原始输入张量input中测元素下标。如果设定布尔值sorted为_True_,将会确保返回的K个值被排序。 参数: --input (Tensor) :输入张量 --k (int) :“ top-k ”中的 k -- dim (int, optional) :排序的维 --largest (bool, optional) :布尔值,控制返回最大或最小值 --sorted (bool, optional) :布尔值,控制返回值是否排序 --out (tuple, optional) :可选输出张量 (Tensor, LongTensor) output buffers 例子:
torch.topk(x,3) (tensor([5, 4, 3]), tensor([4, 3, 2])) torch.topk(x,3,0,largest=False) (tensor([1, 2, 3]), tensor([0, 1, 2]))



