【Python】torch.Tensor与numpy.ndarray格式转换

文章目录

  • torch.Tensor与numpy.ndarray格式转换
    • torch.Tensor转换成numpy.ndarray
    • numpy.ndarray转换成torch.Tensor

torch.Tensor与numpy.ndarray格式转换

torch.Tensor转换成numpy.ndarray

import torch
a = torch.ones(5)
print(a)
print(type(a)) #
b = a.numpy()
print(b)
print(type(b)) #

numpy.ndarray转换成torch.Tensor

import numpy as np
import torch
a = np.ones(5)
b = torch.from_numpy(a)
print(a)  
print(type(a)) #
print(b)
print(type(b)) #

你可能感兴趣的:(文件类型转换,python,r语言,pytorch)