import torch
import numpy as np
a = torch.tensor([[1,2],[3,4]],dtype=torch.float32)
print(a,type(a),a.dtype)
"""
tensor([[1., 2.],
[3., 4.]]) <class 'torch.Tensor'> torch.float32
"""
b = np.array([[1,2],[3,4]],dtype=np.float32)
print(b,type(b),b.dtype)
""""
[[1. 2.]
[3. 4.]] <class 'numpy.ndarray'> float32
"""
c = a.numpy()
print(c,type(c),c.dtype)
"""
[[1. 2.]
[3. 4.]] <class 'numpy.ndarray'> float32
"""
d = torch.from_numpy(b)#<class 'numpy.ndarray'>----》<class 'torch.Tensor'>
print(d,type(d),d.dtype)
"""
tensor([[1., 2.],
[3., 4.]]) <class 'torch.Tensor'> torch.float32
"""
菜鸟进阶:(5)<class ‘numpy.ndarray‘>和<class ‘torch.Tensor‘>相互转换
最新推荐文章于 2024-09-03 17:04:48 发布