pytorch 實現(xiàn)tensor與numpy數(shù)組轉換
更新時間:2019年12月27日 15:52:30 作者:頭發(fā)光了你就強了
今天小編就為大家分享一篇使用pytorch 實現(xiàn)tensor與numpy數(shù)組轉換,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
看代碼,tensor轉numpy:
a = torch.ones(2,2) b = a.numpy() c=np.array(a) #也可以轉numpy數(shù)組 print(type(a)) print(type(b)) print(a) print(b)
輸出為:
<class ‘torch.Tensor'> <class ‘numpy.ndarray'> tensor([[1., 1.], [1., 1.]]) [[1. 1.] [1. 1.]]
numpy轉tensor:
import torch import numpy as np a = np.ones(5) b = torch.from_numpy(a) c=torch.Tensor(a) #也可以轉pytorch Tensor print(type(a)) print(type(b)) print(a) print(b)
輸出為:
<class ‘numpy.ndarray'> <class ‘torch.Tensor'> [1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
可見pytorch的tensor對象與numpy數(shù)組是可以相互轉換的,且numpy數(shù)組的默認類型是double
以上這篇pytorch 實現(xiàn)tensor與numpy數(shù)組轉換就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python獲取基金網(wǎng)站網(wǎng)頁內容、使用BeautifulSoup庫分析html操作示例
這篇文章主要介紹了Python獲取基金網(wǎng)站網(wǎng)頁內容、使用BeautifulSoup庫分析html操作,結合實例形式分析了Python基于urllib包的網(wǎng)頁內容獲取,以及使用BeautifulSoup分析html相關操作技巧,需要的朋友可以參考下2019-06-06基于本地知識的問答機器人langchain-ChatGLM 大語言模型實現(xiàn)方法詳解
這篇文章主要介紹了基于本地知識的問答機器人langchain-ChatGLM 大語言模型實現(xiàn)方法,結合具體實例形式詳細分析了langchain-ChatGLM的功能、原理、部署方法與操作注意事項,需要的朋友可以參考下2023-07-07