トーチtorch.max()およびtorch.min()関数の理解



Understanding Torch Torch



  • 簡単な紹介

テンソルデータ型では、max関数とmin関数を使用して、2つのテンソルデータサイズ、または取得したテンソルデータの最大値を比較します。関数maxおよびmin関数を使用する場合、次のシナリオがあります。

tensorAとtensorBの場合:



  1. torch.max(tensorA):テンソルの最大値を返します。
  2. torch.mac(tensorA、dim):dimは、指定された次元を示し、添え字の最大数に対応する指定された次元を返し、
  3. torch.max(tensorA、tensorB):比較テンソルAとテンソルBの比較的大きな要素。
Input: x = th.arange(0,16,1).view(4,4) print('x: ',x) print('t.max(x): ',t.max(x)) print('t.max(x,1): ',t.max(x,1)) print('t.max(x,0): ',t.max(x,0)) print('t.max(x,1)[0]: ',t.max(x,1)[0]) print('t.max(x,1)[1]: ',t.max(x,1)[1]) print('t.max(x,1)[1].data: ',t.max(x,1)[1].data) print('t.max(x,1)[1].data.numpy(): ',t.max(x,1)[1].data.numpy()) print('t.max(x,1)[1].data.numpy().squeeze(): ',t.max(x,1)[1].data.numpy().squeeze()) print('t.max(x,1)[0].data: ',t.max(x,1)[0].data) print('t.max(x,1)[0].data.numpy(): ',t.max(x,1)[0].data.numpy()) print('t.max(x,1)[0].data.numpy().squeeze(): ',t.max(x,1)[0].data.numpy().squeeze()) Output: x: tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) t.max(x): tensor(15) t.max(x,1): torch.return_types.max( values=tensor([ 3, 7, 11, 15]), indices=tensor([3, 3, 3, 3])) t.max(x,0): torch.return_types.max( values=tensor([12, 13, 14, 15]), indices=tensor([3, 3, 3, 3])) t.max(x,1)[0]: tensor([ 3, 7, 11, 15]) t.max(x,1)[1]: tensor([3, 3, 3, 3]) t.max(x,1)[1].data: tensor([3, 3, 3, 3]) t.max(x,1)[1].data.numpy(): [3 3 3 3] t.max(x,1)[1].data.numpy().squeeze(): [3 3 3 3] t.max(x,1)[0].data: tensor([ 3, 7, 11, 15]) t.max(x,1)[0].data.numpy(): [ 3 7 11 15] t.max(x,1)[0].data.numpy().squeeze(): [ 3 7 11 15]

上記のコードのXは、2次テンソル4 * 4:です。

Input: x = th.arange(0,16,1).view(2,2,4) print('x: ',x) print('t.max(x): ',t.max(x)) print('t.max(x,1): ',t.max(x,1)) print('t.max(x,0): ',t.max(x,0)) print('t.max(x,1)[0]: ',t.max(x,1)[0]) print('t.max(x,1)[1]: ',t.max(x,1)[1]) print('t.max(x,1)[1].data: ',t.max(x,1)[1].data) print('t.max(x,1)[1].data.numpy(): ',t.max(x,1)[1].data.numpy()) print('t.max(x,1)[1].data.numpy().squeeze(): ',t.max(x,1)[1].data.numpy().squeeze()) print('t.max(x,1)[0].data: ',t.max(x,1)[0].data) print('t.max(x,1)[0].data.numpy(): ',t.max(x,1)[0].data.numpy()) print('t.max(x,1)[0].data.numpy().squeeze(): ',t.max(x,1)[0].data.numpy().squeeze()) Output: x: tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]]) t.max(x): tensor(15) t.max(x,1): torch.return_types.max( values=tensor([[ 4, 5, 6, 7], [12, 13, 14, 15]]), indices=tensor([[1, 1, 1, 1], [1, 1, 1, 1]])) t.max(x,0): torch.return_types.max( values=tensor([[ 8, 9, 10, 11], [12, 13, 14, 15]]), indices=tensor([[1, 1, 1, 1], [1, 1, 1, 1]])) t.max(x,1)[0]: tensor([[ 4, 5, 6, 7], [12, 13, 14, 15]]) t.max(x,1)[1]: tensor([[1, 1, 1, 1], [1, 1, 1, 1]]) t.max(x,1)[1].data: tensor([[1, 1, 1, 1], [1, 1, 1, 1]]) t.max(x,1)[1].data.numpy(): [[1 1 1 1] [1 1 1 1]] t.max(x,1)[1].data.numpy().squeeze(): [[1 1 1 1] [1 1 1 1]] t.max(x,1)[0].data: tensor([[ 4, 5, 6, 7], [12, 13, 14, 15]]) t.max(x,1)[0].data.numpy(): [[ 4 5 6 7] [12 13 14 15]] t.max(x,1)[0].data.numpy().squeeze(): [[ 4 5 6 7] [12 13 14 15]]

t.max(x)は、xから抽出された最大値が15であることを示します



各行のt.max(x、1)は、削除された最大の要素を表し、そのインデックス、最初の行3、2番目の3、3番目の行11、4番目の行14の動作を返します。注:t.max(x、1)1は行を表し、列は0を表します。これは、2次テンソルの例であり、行と列のみであるためです。

など各列のt.max(x、1)は、削除された最大の要素、つまり[12、13、14、15]を表します。

.dataは、データセクションの変数のみを返します(:を含む削除された変数)。



.data.numpy()データをnumpyndarryに入れます。

] .Data.numpy()。 ()を絞って、1outの次元のデータエントリを削除します。

torch.max(tensorA、tensorB)要素ごとの比較、およびtensorB tensorAは要素であり、大きい方の値を返します。

それが2枚の3番目の注文である場合はどうなりますか?

以下では、テンソルx 2 * 2 * 4、2次元の4行2行になります。

Input: x = th.arange(0,16,1).view(2,2,4) print('x: ',x) print('t.max(x): ',t.max(x)) print('t.max(x,2): ',t.max(x,2)) print('t.max(x,2)[0]: ',t.max(x,2)) print('t.max(x,1)[0][0]: ',t.max(x,2)[0][0]) Output: x: tensor([[[ 0, 1, 2, 3], [ 4, 5, 6, 7]], [[ 8, 9, 10, 11], [12, 13, 14, 15]]]) t.max(x): tensor(15) t.max(x,2): torch.return_types.max( values=tensor([[ 3, 7], [11, 15]]), indices=tensor([[3, 3], [3, 3]])) t.max(x,2)[0]: torch.return_types.max( values=tensor([[ 3, 7], [11, 15]]), indices=tensor([[3, 3], [3, 3]])) t.max(x,1)[0][0]: tensor([3, 7])

出力の観点から、

  1. |_+_|
  2. |_+_|
    tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]])
  3. |_+_|
     t.max (x): the maximum is still taken from xOnevalue.

それを行う方法の最大の要素の各列の各次元を削除したい場合はどうなりますか? t.max(x、dim)を指定するだけで、列ベクトルからdim = 2を取り出すことができます。

 t.max (x, 1): Output