1. numpy转tensor(共享内存)
使用torch.from_numpy() 将numpy转为tensor
import numpy as np
import torch
n = np.random.randn(224, 224, 3)
print(n.shape)
t = torch.from_numpy(n)
print(t.shape)
(224, 224, 3)
torch.Size([224, 224, 3])
2.tensor转numpy(共享内存)
使用.numpy()转为numpy
a = torch.randn(3,4,5)
print(a.shape)
b = a.numpy()
print(b.shape)
torch.Size([3, 4, 5])
(3, 4, 5)