1. 環境は、Window 10 Home (64bit) 上で行った。
2. Anaconda3 (64bit) – Spyder上で、動作確認を行った。
3. python の バージョンは、python 3.7.0 である。
4. pytorch の バージョンは、pytorch 0.4.1 である。
5. GPU は, NVIDIA社 の GeForce GTX 1050 である。
6. CPU は, Intel社 の Core(TM) i7-7700HQ である。
今回確認した内容は、現場で使える! PyTorch開発入門 深層学習モデルの作成とアプリケーションへの実装 (AI & TECHNOLOGY) の 6.1 行列因子分解 (P.152 – P.156) である。
※1. プログラムの詳細は, 書籍を参考(P.152 – P.156)にして下さい.
※2. どちらかというと, torch.sum() の 使い方を確認した内容となっている.
■torch.sum()の動作確認.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# -*- coding: utf-8 -*- # 1. library import. from __future__ import print_function import torch # 2. sample. # torch.sum() # https://pytorch.org/docs/stable/torch.html#torch.sum a = torch.randn(1, 3) print('--- a.size(): ------------------------------------') print(a.size()) a_out = torch.sum(a) print('--- torch.sum(a): --------------------------------') print("type(a_out): " + str(type(a_out))) print("a_out: " + str(a_out)) print() b = torch.tensor([[ 0.05, -0.25, 0.07], [-0.30, 0.90, 0.90]]) print('--- b.size(): ------------------------------------') print(b.size()) b_out = torch.sum(b) # 0.05 - 0.25 + 0.07 - 0.30 + 0.90 + 0.90 = 1.37 print('--- torch.sum(b): --------------------------------') print("b_out: " + str(b_out)) # 0.05 - 0.25 + 0.07 = -0.13 # -0.30 + 0.90 + 0.90 = 1.5 b_out = torch.sum(b, 1) print('--- torch.sum(b, 1): -----------------------------') print("b_out: " + str(b_out)) print() c = torch.tensor([[[ 0.05, -0.25, 0.07], [-0.30, 0.90, 0.90]], [[ 0.15, -0.35, 0.70], [-1.30, 0.50, 1.20]], [[ 0.05, -0.25, 0.85], [-1.10, 0.65, 1.15]]]) # 0.05 - 0.25 + 0.07 - 0.30 + 0.90 + 0.90 + 0.15 - 0.35 + 0.70 - 1.30 + 0.50 + 1.20 # 0.05 - 0.25 + 0.85 - 1.10 + 0.65 + 1.15 = 3.62 print('--- c.size(): ------------------------------------') print(c.size()) c_out = torch.sum(c) print('--- torch.sum(c): --------------------------------') print("c_out: " + str(c_out)) # 0.05 - 0.25 + 0.07 - 0.30 + 0.90 + 0.90 = 1.37 # 0.15 - 0.35 + 0.70 - 1.30 + 0.50 + 1.20 = 0.90 # 0.05 - 0.25 + 0.85 - 1.10 + 0.65 + 1.15 = 1.35 c_out = torch.sum(c, (1, 2)) print('--- torch.sum(c, (1, 2)): ------------------------') print("c_out: " + str(c_out)) c_out = torch.sum(c, (2, 1)) print('--- torch.sum(c, (2, 1)): ------------------------') print("c_out: " + str(c_out)) print() d = torch.tensor([[[[ 0.5, -0.4, 0.7], [-0.3, 0.9, 1.0]]], [[[ 0.1, -0.3, 0.8], [-1.3, 0.5, 1.2]]], [[[ 0.3, -0.2, 0.9], [-0.5, 1.1, 0.3]]]]) print('--- d.size(): ------------------------------------') print(d.size()) d_out = torch.sum(d) print('--- torch.sum(d): --------------------------------') print("d_out: " + str(d_out)) # torch.Size([3, 1, 2, 3]) の 2番目で集計. d_out = torch.sum(d, 1) print('--- torch.sum(d, 1): -----------------------------') print("d_out: " + str(d_out)) # torch.Size([3, 1, 2, 3]) の 2, 3番目で集計. # 0.5 - 0.3 = 0.2, -0.4 + 0.9 = 0.5, 0.7 + 1.0 = 1.7 # 0.1 - 1.3 = -1.2, -0.3 + 0.5 = 0.2, 0.8 + 1.2 = 2.0 # 0.3 - 0.5 = -0.2, -0.2 + 1.1 = 0.9, 0.9 + 0.3 = 1.2 d_out = torch.sum(d, (1, 2)) print('--- torch.sum(d, (1, 2)): ------------------------') print("d_out: " + str(d_out)) d_out = torch.sum(d, (2, 1)) print('--- torch.sum(d, (2, 1)): ------------------------') print("d_out: " + str(d_out)) # torch.Size([3, 1, 2, 3]) の 2, 4番目で集計. # 0.5 - 0.4 + 0.7 = 0.8, -0.3 + 0.9 + 1.0 = 1.6 # 0.1 - 0.3 + 0.8 = 0.6, -1.3 + 0.5 + 1.2 = 0.4 # 0.3 - 0.2 + 0.9 = 1.0, -0.5 + 1.1 + 0.3 = 0.9 d_out = torch.sum(d, (1, 3)) print('--- torch.sum(d, (1, 3)): ------------------------') print("d_out: " + str(d_out)) d_out = torch.sum(d, (3, 1)) print('--- torch.sum(d, (3, 1)): ------------------------') print("d_out: " + str(d_out)) # torch.Size([3, 1, 2, 3]) の 3, 4番目で集計. # 0.5 - 0.4 + 0.7 - 0.3 + 0.9 + 1.0 = 2.4 # 0.1 - 0.3 + 0.8 - 1.3 + 0.5 + 1.2 = 1.0 # 0.3 - 0.2 + 0.9 - 0.5 + 1.1 + 0.3 = 1.9 d_out = torch.sum(d, (2, 3)) print('--- torch.sum(d, (2, 3)): ------------------------') print("d_out: " + str(d_out)) d_out = torch.sum(d, (3, 2)) print('--- torch.sum(d, (3, 2)): ------------------------') print("d_out: " + str(d_out)) |
■実行結果.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
--- a.size(): ------------------------------------ torch.Size([1, 3]) --- torch.sum(a): -------------------------------- type(a_out): <class 'torch.Tensor'> a_out: tensor(0.0049) --- b.size(): ------------------------------------ torch.Size([2, 3]) --- torch.sum(b): -------------------------------- b_out: tensor(1.3700) --- torch.sum(b, 1): ----------------------------- b_out: tensor([-0.1300, 1.5000]) --- c.size(): ------------------------------------ torch.Size([3, 2, 3]) --- torch.sum(c): -------------------------------- c_out: tensor(3.6200) --- torch.sum(c, (1, 2)): ------------------------ c_out: tensor([1.3700, 0.9000, 1.3500]) --- torch.sum(c, (2, 1)): ------------------------ c_out: tensor([1.3700, 0.9000, 1.3500]) --- d.size(): ------------------------------------ torch.Size([3, 1, 2, 3]) --- torch.sum(d): -------------------------------- d_out: tensor(5.3000) --- torch.sum(d, 1): ----------------------------- d_out: tensor([[[ 0.5000, -0.4000, 0.7000], [-0.3000, 0.9000, 1.0000]], [[ 0.1000, -0.3000, 0.8000], [-1.3000, 0.5000, 1.2000]], [[ 0.3000, -0.2000, 0.9000], [-0.5000, 1.1000, 0.3000]]]) --- torch.sum(d, (1, 2)): ------------------------ d_out: tensor([[ 0.2000, 0.5000, 1.7000], [-1.2000, 0.2000, 2.0000], [-0.2000, 0.9000, 1.2000]]) --- torch.sum(d, (2, 1)): ------------------------ d_out: tensor([[ 0.2000, 0.5000, 1.7000], [-1.2000, 0.2000, 2.0000], [-0.2000, 0.9000, 1.2000]]) --- torch.sum(d, (1, 3)): ------------------------ d_out: tensor([[0.8000, 1.6000], [0.6000, 0.4000], [1.0000, 0.9000]]) --- torch.sum(d, (3, 1)): ------------------------ d_out: tensor([[0.8000, 1.6000], [0.6000, 0.4000], [1.0000, 0.9000]]) --- torch.sum(d, (2, 3)): ------------------------ d_out: tensor([[2.4000], [1.0000], [1.9000]]) --- torch.sum(d, (3, 2)): ------------------------ d_out: tensor([[2.4000], [1.0000], [1.9000]]) |
■参照サイト
【参照URL①】torch.sum()
■参考書籍
現場で使える! PyTorch開発入門 深層学習モデルの作成とアプリケーションへの実装 (AI & TECHNOLOGY)