Pytorch中的variable, tensor与numpy相互转化的方法

1.将numpy矩阵转换为Tensor张量

sub_ts = torch.from_numpy(sub_img) #sub_img为numpy类型

 

2.将Tensor张量转化为numpy矩阵

sub_np1 = sub_ts.numpy() #sub_ts为tensor张量

 

3.将numpy转换为Variable

sub_va = Variable(torch.from_numpy(sub_img))

 

4.将Variable张量转化为numpy

sub_np2 = sub_va.data.numpy()

 

posted @ 2019-04-03 14:47  Archer-Fang  阅读(737)  评论(0编辑  收藏  举报