日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)

發布時間:2025/3/20 pytorch 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考文章1:PyTorch版《動手學深度學習》PDF 版開源了

參考文章2:https://download.csdn.net/download/Dontla/12371960

文章目錄

    • 1.1 安裝pytorch
    • 1.2 數據操作
      • 2.2.2 操作
      • 2.2.5 TENSOR 和 NUMPY的相互轉換
      • 2.2.6 TENSOR ON GPU
    • 2.3 自動求梯度

1.1 安裝pytorch

pytorch官網 點擊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 torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html

還是用原來的裝吧

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

# CUDA 9.0 Download and install wheel from https://download.pytorch.org/whl/cu90/torch_stable.html

我的python是3.6的,安裝這個版本:

直接下太慢,右鍵用迅雷下

下載好,就可以安裝了,安裝方法:python pip如何安裝wheel文件?.whl(pip install [wheel])

1.2 數據操作

# -*- coding: utf-8 -*- """ @File : 2.2.1 創建 TENSOR.py @Time : 2020/3/4 15:05 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import torch# 創建?一個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]])# 創建?一個5x3的隨機初始化的 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]])# 創建?一個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]])# 直接根據數據創建tensor x = torch.tensor([5.5, 3]) # print(x) # tensor([5.5000, 3.0000])# 通過現有的 Tensor 來創建,此方法會默認重用輸入 Tensor 的一些屬性,例如數據類型,除?非?自定義數據類型。 x = x.new_ones(5, 3, dtype=torch.float64) # 返回的tensor默認具有相同的torch.dtype和torch.device print(x) x = torch.randn_like(x, dtype=torch.float) # 指定新的數據類型 print(x)

2.2.2 操作

其他略了,自己看文檔去

感覺這個比較叼,不叼的我都不附上來,???,

2.2.5 TENSOR 和 NUMPY的相互轉換

torch.Tensor.numpy() # 轉換成numpy數組
torch.from_numpy() # 將numpy數組轉換成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版本上才能執行 x = torch.empty(5, 3) if torch.cuda.is_available():device = torch.device("cuda") # GPUy = torch.ones_like(x, device=device) # 直接創建一個在GPU上的Tensorx = x.to(device) # 等價于.to("cuda")z = x + yprint(z)print(z.to("cpu", torch.double)) # to()還可以同時更改數據類型

結果:

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 0

2.3 自動求梯度

總結

以上是生活随笔為你收集整理的PyTorch 《动手学深度学习》学习笔记(Dive-into-DL-Pytorch)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。