PyTorch基础(part2)
生活随笔
收集整理的這篇文章主要介紹了
PyTorch基础(part2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習筆記,僅供參考,有錯必究
文章目錄
- PyTorch基礎
- 基本操作
- 加法
- 減法/乘法/除法
- 求和、最大、最小
- 數據的索引
- 求導
PyTorch基礎
基本操作
# 導入常用的包 import torch import numpy as np
加法
a = torch.randint(1, 5, (2, 3)) b = torch.randint(1, 5, (2, 3)) a b tensor([[4, 1, 4],[3, 3, 2]])tensor([[3, 4, 2],[1, 3, 2]]) # 加法的3種方式 a + b torch.add(a, b) # a與b相加,并將返回值賦值給a a.add_(b) # 注意:任何使張量會發生變化的操作都有一個后綴"_",例如: a.add_(), a.sub_() a tensor([[7, 5, 6],[4, 6, 4]])tensor([[7, 5, 6],[4, 6, 4]])tensor([[7, 5, 6],[4, 6, 4]])tensor([[7, 5, 6],[4, 6, 4]]) # 構造一個tensor,并將a + b的結果輸出到tensor中 r = torch.zeros(2, 3) torch.add(a, b, out=r) tensor([[10., 9., 8.],[ 5., 9., 6.]]) r tensor([[10., 9., 8.],[ 5., 9., 6.]])減法/乘法/除法
# 減法 a - b tensor([[4, 1, 4],[3, 3, 2]]) # 乘法(對應元素相乘) a * b tensor([[21, 20, 12],[ 4, 18, 8]]) # 除法(對應元素相除) a/b tensor([[2.3333, 1.2500, 3.0000],[4.0000, 2.0000, 2.0000]]) # 取余 a%b tensor([[1, 1, 0],[0, 0, 0]]) # 取整 a // b D:\software\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').?
tensor([[2, 1, 3],[4, 2, 2]]) # 矩陣乘法(必須是張量中數據類型相同,才可以相乘) a = torch.tensor([[1, 0, 2], [0, 1, 1]], dtype = float) b = torch.ones(3, 5, dtype = float) a b a.dtype, b.dtype torch.matmul(a, b) # 2*3 * 3*5 => 2*5 tensor([[1., 0., 2.],[0., 1., 1.]], dtype=torch.float64)tensor([[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.]], dtype=torch.float64)(torch.float64, torch.float64)tensor([[3., 3., 3., 3., 3.],[2., 2., 2., 2., 2.]], dtype=torch.float64)求和、最大、最小
# 張量求和、求平均、求中位數、求最大、求最小 a = torch.rand(2, 3) a torch.sum(a) torch.mean(a) torch.median(a) torch.max(a) torch.min(a) tensor([[0.6162, 0.4631, 0.9147],[0.1778, 0.2920, 0.7270]])tensor(3.1909)tensor(0.5318)tensor(0.4631)tensor(0.9147)tensor(0.1778) # 最大值所處位置、最小值所處位置 torch.argmin(a) torch.argmax(a) tensor(3)tensor(2) # 對張量中的每個值進行開方/平方 torch.sqrt(a) a ** 2 tensor([[0.7850, 0.6805, 0.9564],[0.4217, 0.5404, 0.8526]])tensor([[0.3797, 0.2145, 0.8367],[0.0316, 0.0853, 0.5285]])數據的索引
a = torch.arange(2, 14) # 生成數據 print(a) tensor([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) tensor(5) print(a[3]) print(a[1:3]) print(a[5:-2]) print(a[6:]) tensor(5) tensor([3, 4]) tensor([ 7, 8, 9, 10, 11]) tensor([ 8, 9, 10, 11, 12, 13]) idx = [1, 5, 3, 7, 7] a[idx] tensor([3, 7, 5, 9, 9]) for t in a:print(t) tensor(2) tensor(3) tensor(4) tensor(5) tensor(6) tensor(7) tensor(8) tensor(9) tensor(10) tensor(11) tensor(12) tensor(13)求導
z=1n∑i=1n3×yi2,yi=xi+1z=\frac{1}{n} \sum_{i=1}^n 3 \times y_i^2, \; y_i = x_i + 1z=n1?∑i=1n?3×yi2?,yi?=xi?+1
x = torch.ones((2, 2), requires_grad=True) # 定義2*2的張量,設置為允許計算梯度 x tensor([[1., 1.],[1., 1.]], requires_grad=True) y = x + 1 y tensor([[2., 2.],[2., 2.]], grad_fn=<AddBackward0>) z = (y**2) * 3 z tensor([[12., 12.],[12., 12.]], grad_fn=<MulBackward0>) out_mean = z.mean() print(out_mean) tensor(12., grad_fn=<MeanBackward0>) # 計算 out_mean的梯度(求導) out_mean.backward() # out_mean對x進行求導,則x的導數為3 x.grad tensor([[3., 3.],[3., 3.]]) 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的PyTorch基础(part2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 李瑞峰:长城Hi4等价四驱平替两驱 全民
- 下一篇: PC 端也能收发苹果 iMessage