Pyhon初心者がPytorchを触る(1)
はじめまして、Yusaku Sekineと申します。初の記事なので緊張しますが頑張って書くのでご指摘ありましたらご連絡をください。
また、この記事ではPytorchのインストール方法、基本文法についての説明は省略させていただきます。Pytorchの基本文法に焦点を当てています。
1.記事を書く経緯
人工知能に興味があり、以前からTensorFlowを使用していたのですが、Windows端末でTensorFlowが使えなくなってしまいました。
そこで、この機会にPytorchに乗り換えようと思い、その練習も兼ねて記事を書くことにしました。
学習方法は以下の通りです。
1.公式ドキュメント Pytorch tutrial リンク
2.ネット記事(Qitta or 技術ブログ)
3.実際に動かす
今回の記事は公式ドキュメントを参考にさせていただいたので原文で読みたい方は是非公式ドキュメントをご覧ください。
2.Pytorch 入門
2-1.Tensor(テンソル)
まずはこれから使うPytorchの基本的なデータの形Tensorです。以下のコードを作成し、実行してください。
本記事ではTensorが何なのかという深い話しは致しません。とりあえず行列に似たものだと思っていただければ良いかと思います。
import torch
tensor = torch.rand(3,3)
print(tensor)
tensor([[0.7545, 0.3774, 0.7312],
[0.9000, 0.6083, 0.5135],
[0.6012, 0.9147, 0.0625]]
実行結果に 「tensor」という文字が記述されています。pytorchではこのtensorを計算し、機械学習を行います。
また、torch.rand は0から1までの数字をランダムで(行,列)で生成します。行と列の数字を変えて実行するとさらに理解が深まるかもしれません。
次に一般的なtensorの宣言をしてみましょう。
import torch
tensor = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(tensor)
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
こちらでもtensorの宣言ができます。使い方に応じてtensorの生成方法は選んでください。
2-2.Tensorの基本計算
tensorの宣言ができるようになったので、次はこのテンソルを使った計算をしましょう。せっかくなので先ほど宣言したtensorを使って 和、差、積、商 を求めてみましょう。
import torch
tensor = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print("和")
print(tensor+tensor)
print("差")
print(tensor-tensor)
print("積")
print(tensor*tensor)
print("商")
print(tensor//tensor)
和
tensor([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
差
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
積
tensor([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
商
tensor([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
問題なく計算ができていることが確認できます。
余談ですが、tensor同士の和と差は他の書き方で求めることもできます。
import torch
print(torch.add(tensor,tensor))
print(torch.sub(tensor,tensor))
tensor([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
2-3.便利な計算機能
私がPytorchを使うなかで便利だと感じた機能を紹介します。あくまで私の感覚ですので自分で調べて便利機能を探すのも楽しいかもしれません。
*すべて「import torch」を行ってから利用できます。
2-3-1.任意の数 0 or 1 を生成
print(torch.zeros(3,3))
print(torch.ones(3,3))
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
2-3-2.最大値、最小値
tensor = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(torch.max(tensor))
print(torch.min(tensor))
tensor(9)
tensor(1)
2-3-3.Tensorのサイズ確認
tensor = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
print(tensor.size())
torch.Size([3, 3])
2-3-4. Tensor内全要素の和
tensor = torch.tensor([1,2,3,4,5])
print(torch.sum(tensor))
tensor(15)
2-3-5.Tensorの四捨五入、絶対値の計算
#絶対値用tensor
tensor1 = torch.tensor([[-1,-2,-3],
[-4,-5,-6],
[-7,-8,-9]])
#四捨五入用tensor
tensor2 = torch.tensor([[1.1,2.4,3.5],
[-4.5,-5.7,-6.8],
[-7.1,-8.1,-9.0]])
print(torch.abs(tensor1))
print(torch.round(tensor2))
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
tensor([[ 1., 2., 4.],
[-4., -6., -7.],
[-7., -8., -9.]])
*負の数の場合は5で切り下げが起こる場合があります。私は切り下げが起こりました。
2-3-6.Tensorの階乗、平方根計算
#階乗計算用tensor
tensor3 = torch.tensor([[1,2,3],
[4,5,6],
[7,8,9]])
'''
平方根計算用tensor
平方根を行う場合は型をfloatへ変更する必要がある。型変換は dtype = "型"
'''
tensor4 = torch.tensor([[1,4,9],
[16,25,36],
[49,64,81]],dtype=torch.float32)
print(torch.pow(tensor3,2)) #2の部分を任意に変えることで何乗にもできる
print(torch.sqrt(tensor4))
tensor([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
2-3-7.numpyからへのtensor変換
Pythonの行列計算で使われる有名拡張モジュール 'numpy' Pytorchはnumpyをtensorに変換することも可能である。
import numpy as np
import torch
numpy = np.array([1,2,3,4,5])
numpy_to_tensor = torch.from_numpy(numpy)
print(numpy)
print(numpy_to_tensor)
[1 2 3 4 5]
tensor([1, 2, 3, 4, 5])
#「numpy」と「tensor」で表記が異なっていることが確認できる。
最後に
今回は簡単なtensorとtensor計算について紹介致しました。次は少し数学的要素を含んだ微分の記事を書く予定です。
Qittaの記事執筆力もあげていきたいのでご指摘や改善点があればぜひご指摘ください。
最後まで読んでいただきありがとうございました。
Author And Source
この問題について(Pyhon初心者がPytorchを触る(1)), 我々は、より多くの情報をここで見つけました https://qiita.com/You-saku/items/be59a33b532c90f6b9ea著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .