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