pytorch學習的一點小竅門(1) 資料轉換

2021-08-19 19:46:55 字數 1616 閱讀 7621

最近幾天,由於要復現一篇**,但是**中的公式太過複雜,層層巢狀,本人表示無法求出導數來,遂決定借助框架的力量來進行操作,嘗試過tensorflow,但是其靜態圖的模式除錯起來過於麻煩,不利於隨時隨地取出資料來驗證,於是開始轉攻pytorch,用來一段時間,感覺真的很好用,但是在編寫過程中也遇到很多的坑,於是決定將這些坑分享出來,以待後來人。

pytorch和tensorflow一樣的一點在於他們接受的能夠處理的資料型別是張量,將我們在python中常用的list,array等資料型別轉換為張量是使用框架至關重要的一部,如果你連合格的資料都提供不了,就不用說接下來的學習了,下面貼出python常用型別與張量tensor之間的互換函式:

tensor---->numpy  可以使用 data.numpy(),data為tensor變數

numpy ----> tensor 可以使用torch.from_numpy(data),data為numpy變數

如果遇到型別為list的資料可以先轉換為array型別的再用torch.from_numpy轉換為tensor型別

# torch.

long

() 將tensor投射為

long

型別

long_tensor = tensor.

long

()print(long_tensor)

# torch.half()將tensor投射為半精度浮點型別

half_tensor = tensor.half()print(half_tensor)

# torch.int()將該tensor投射為int型別

int_tensor = tensor.

int()print(int_tensor)

# torch.double()將該tensor投射為double型別

double_tensor = tensor.

double

()print(double_tensor)

# torch.float()將該tensor投射為float型別

float_tensor = tensor.

float

()print(float_tensor)

# torch.char()將該tensor投射為char型別

char_tensor = tensor.

char

()print(char_tensor)

# torch.byte()將該tensor投射為byte型別

byte_tensor = tensor.

byte

()print(byte_tensor)

# torch.short()將該tensor投射為short型別

short_tensor = tensor.

short

()print(short_tensor)

前端學習的小竅門

1.data callback else json 第乙個是url,第二個是post時傳入的值,function中的data並不是傳入值,而是傳出值,function是對傳出值進行處理的函式,callback同樣是對傳出值處理的函式。因為js是非同步處理的,不能像同步時一樣可以return結果,只能...

乙個有趣的小竅門

無聊,cmd 後看見好多東西,其中有個好玩的 如果 d 未在命令列上被指定,當 cmd.exe 開始時,它會尋找 以下 reg sz reg expand sz 登錄檔變數。如果其中乙個或 兩個都存在,這兩個變數會先被執行。hkey local machine software microsoft ...

Python的小竅門(一) 提示程式的執行

1.環境 win10 x64 python2.7.13 x64 2.目的 在控制台顯示乙個程式執行的狀態,向開發者反饋程式執行正常與否。雖然python的擴充套件模組tqdm可以動態跟蹤程式的運 況,網上也推薦了很多庫,這裡自定義乙個小函式,比較簡單。3.解決方式 這裡以7為乙個列印週期,每次列印乙...