python如何安装torch_PyTorch安装与基本使用详解
什么要學習PyTorch?
有的人總是選擇,選擇的人最多的框架,來作為自己的初學框架,比如Tensorflow,但是大多論文的實現都是基于PyTorch的,如果我們要深入論文的細節,就必須選擇學習入門PyTorch
安裝PyTorch
一行命令即可 官網
pip install torch===1.6.0 torchvision===0.7.0 - https://download.pytorch.org/whl/torch_stable.html
時間較久,耐心等待
測試自己是否安裝成功
運行命令測試
import torch
x = torch.rand(5,3)
print(x)
輸出
tensor([[0.5096, 0.1209, 0.7721],
[0.9486, 0.8676, 0.2157],
[0.0586, 0.3467, 0.5015],
[0.9470, 0.5654, 0.9317],
[0.2127, 0.2386, 0.0629]])
開始學習PyTorch
不初始化的創建張量
import torch
x = torch.empty([5,5])
print(x)
輸出
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
隨機創建一個0-1的張量
import torch
x = torch.rand(5,5)
print(x)
輸出
tensor([[0.3369, 0.5339, 0.8419, 0.6857, 0.6241],
[0.4991, 0.1691, 0.8356, 0.4574, 0.0395],
[0.9714, 0.2975, 0.9322, 0.5213, 0.8509],
[0.3037, 0.8690, 0.3481, 0.2538, 0.9513],
[0.0156, 0.9516, 0.3674, 0.1831, 0.6466]])
創建全為0的張量
import torch
x = torch.zeros(5,5, dtype=torch.float32)
print(x)
創建的時候可以通過dtype指定數據類型
輸出
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
使用數據來直接創建張量
import torch
x = torch.zeros([5,5], dtype=torch.float32)
print(x)
輸出
tensor([5., 5.])
使用原有tensor創建新的tensor
import torch
x = torch.tensor([5,5], dtype=torch.float32)
x = x.new_zeros(5, 3)
y = torch.rand_like(x)
print(x)
print(y)
輸出
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[0.5552, 0.3333, 0.0426],
[0.3861, 0.3945, 0.6658],
[0.6978, 0.3508, 0.4813],
[0.8193, 0.2274, 0.8384],
[0.9360, 0.9226, 0.1453]])
觀察tensor的維度信息
x = torch.rand(3,3)
x.size()
輸出
torch.Size([3, 3])
一些簡單的運算
x = torch.tensor([1])
y = torch.tensor([3])
'''
方式1
'''
z = x + y
'''
方式2
'''
z = torch.add(x, y)
'''
方式3
'''
result = torch.empty(1)
# 不初始化數據
torch.add(x, y, out=result)
# 將結果返回到result中
'''
方式4
'''
x.add_(y)
輸出
tensor([4])
索引操作
x = torch.rand(5,5)
x[:,:]
x[1,:]
x[:,1]
x[1,1]
分別輸出
tensor([[0.4012, 0.2604, 0.1720, 0.0996, 0.7806],
[0.8734, 0.9087, 0.4828, 0.3543, 0.2375],
[0.0924, 0.9040, 0.4408, 0.9758, 0.2250],
[0.7179, 0.7244, 0.6165, 0.1142, 0.7363],
[0.8504, 0.0391, 0.0753, 0.4530, 0.7372]])
tensor([0.8734, 0.9087, 0.4828, 0.3543, 0.2375])
tensor([0.2604, 0.9087, 0.9040, 0.7244, 0.0391])
tensor(0.9087)
維度變換
x = torch.rand(4,4)
x.view(16)
x.view(8,2)
x.view(-1,8)
分別輸出
tensor([0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954, 0.4679,
0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797])
tensor([[0.9277, 0.9547],
[0.9487, 0.9841],
[0.4114, 0.1693],
[0.8691, 0.3954],
[0.4679, 0.7914],
[0.7456, 0.0522],
[0.0043, 0.2097],
[0.5932, 0.9797]])
tensor([[0.9277, 0.9547, 0.9487, 0.9841, 0.4114, 0.1693, 0.8691, 0.3954],
[0.4679, 0.7914, 0.7456, 0.0522, 0.0043, 0.2097, 0.5932, 0.9797]])
注意:必須維度變換數據的數量必須保持一致
到此這篇關于PyTorch安裝與基本使用詳解的文章就介紹到這了,更多相關PyTorch安裝與使用內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持我們!
本文標題: PyTorch安裝與基本使用詳解
本文地址: http://www.cppcns.com/jiaoben/python/340145.html
總結
以上是生活随笔為你收集整理的python如何安装torch_PyTorch安装与基本使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 幻影无卡支付是什么
- 下一篇: java webservice报文过长_