torch中的tensor可以跟numpy中的array進行轉化

轉化學習連接

import numpy as np
import torch

numpy_data = np.arange(6).reshape([2, 3])

torch_data = torch.from_numpy(numpy_data)

tensor2array = torch_data.numpy()

print(numpy_data)
print(torch_data)
print(tensor2array)

輸出結果:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe D:/project/pytorch_learn/test.py
[[0 1 2]
 [3 4 5]]
tensor([[0, 1, 2],
        [3, 4, 5]], dtype=torch.int32)
[[0 1 2]
 [3 4 5]]

Process finished with exit code 0

 

發佈了358 篇原創文章 · 獲贊 113 · 訪問量 82萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章