PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)
生活随笔
收集整理的這篇文章主要介紹了
PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考文章1:PyTorch版《動(dòng)手學(xué)深度學(xué)習(xí)》PDF 版開源了
參考文章2:https://download.csdn.net/download/Dontla/12371960
文章目錄
- 1.1 安裝pytorch
- 1.2 數(shù)據(jù)操作
- 2.2.2 操作
- 2.2.5 TENSOR 和 NUMPY的相互轉(zhuǎn)換
- 2.2.6 TENSOR ON GPU
- 2.3 自動(dòng)求梯度
1.1 安裝pytorch
pytorch官網(wǎng) 點(diǎn)擊get started–>
安裝CUDA和cuDNN
安裝pytorch:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -t D:\20200228_play_with_pytorch\python\Lib\site-packages torch===1.4.0 torchvision===0.5.0鏡像源安裝不了
還是用原來的裝吧
pip install -t D:\20200228_play_with_pytorch\python\Lib\site-packages torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html太慢了,直接放棄:
不搞了,貌似沒法指定安裝目錄
直接pip吧,不指定位置了
pip也慢。。。
我的筆記本上cuda剛好裝的9.0的,找到支持9.0cuda的最新版本的pytorch,
在這里安裝老版本的pytorch
我的python是3.6的,安裝這個(gè)版本:
直接下太慢,右鍵用迅雷下
下載好,就可以安裝了,安裝方法:python pip如何安裝wheel文件?.whl(pip install [wheel])
1.2 數(shù)據(jù)操作
# -*- coding: utf-8 -*- """ @File : 2.2.1 創(chuàng)建 TENSOR.py @Time : 2020/3/4 15:05 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import torch# 創(chuàng)建?一個(gè)5x3的未初始化的 Tensor x = torch.empty(5, 3) # print(x) # tensor([[1.0286e-38, 1.0653e-38, 1.0194e-38], # [8.4490e-39, 9.6429e-39, 8.4490e-39], # [9.6429e-39, 9.2755e-39, 1.0286e-38], # [9.0919e-39, 8.9082e-39, 9.2755e-39], # [8.4490e-39, 1.0194e-38, 1.0194e-38]])# 創(chuàng)建?一個(gè)5x3的隨機(jī)初始化的 Tensor x = torch.rand(5, 3) # print(x) # tensor([[0.0680, 0.7391, 0.4845], # [0.2930, 0.1596, 0.7253], # [0.9158, 0.4388, 0.4839], # [0.3747, 0.3134, 0.2967], # [0.5608, 0.2200, 0.3671]])# 創(chuàng)建?一個(gè)5x3的long型全0的 Tensor x = torch.zeros(5, 3, dtype=torch.long) # print(x) # tensor([[0, 0, 0], # [0, 0, 0], # [0, 0, 0], # [0, 0, 0], # [0, 0, 0]])# 直接根據(jù)數(shù)據(jù)創(chuàng)建tensor x = torch.tensor([5.5, 3]) # print(x) # tensor([5.5000, 3.0000])# 通過現(xiàn)有的 Tensor 來創(chuàng)建,此方法會(huì)默認(rèn)重用輸入 Tensor 的一些屬性,例如數(shù)據(jù)類型,除?非?自定義數(shù)據(jù)類型。 x = x.new_ones(5, 3, dtype=torch.float64) # 返回的tensor默認(rèn)具有相同的torch.dtype和torch.device print(x) x = torch.randn_like(x, dtype=torch.float) # 指定新的數(shù)據(jù)類型 print(x)2.2.2 操作
其他略了,自己看文檔去
感覺這個(gè)比較叼,不叼的我都不附上來,???,
2.2.5 TENSOR 和 NUMPY的相互轉(zhuǎn)換
torch.Tensor.numpy() # 轉(zhuǎn)換成numpy數(shù)組
torch.from_numpy() # 將numpy數(shù)組轉(zhuǎn)換成Tensor
2.2.6 TENSOR ON GPU
# -*- coding: utf-8 -*- """ @File : test.py @Time : 2020/4/1 19:24 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import torch# 以下代碼只有在PyTorch GPU版本上才能執(zhí)行 x = torch.empty(5, 3) if torch.cuda.is_available():device = torch.device("cuda") # GPUy = torch.ones_like(x, device=device) # 直接創(chuàng)建一個(gè)在GPU上的Tensorx = x.to(device) # 等價(jià)于.to("cuda")z = x + yprint(z)print(z.to("cpu", torch.double)) # to()還可以同時(shí)更改數(shù)據(jù)類型結(jié)果:
D:\20191031_tensorflow_yolov3\python\python.exe C:/Users/SIQI/Desktop/PyQt5/test.py tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], device='cuda:0') tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64)Process finished with exit code 02.3 自動(dòng)求梯度
總結(jié)
以上是生活随笔為你收集整理的PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 实现队列功能 queue
- 下一篇: 解决pip安装时速度慢的问题 镜像源(p