Pytorch 在Tensor和numpy之间相互转换

在Pytorch 中Tensor和numpy之间可以相互转换,Tensor转换为numpy很简单,只需要numpy()函数即可,Numpy转化为Tensor也很简单,只需torch.from_numpy(b)即可:
2.jpg
# -*- coding: utf-8 -*-
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

a = torch.Tensor(3,3)
print a
b = a.numpy()
print torch.from_numpy(b)

0 个评论

要回复文章请先登录注册