1.torch转np
# 创建一个形状为[2, 2, 40, 256]的随机张量
torch_tensor = torch.rand(2, 2, 40, 256)
# 将张量转换为NumPy数组
numpy_array = torch_tensor.numpy()
2.np转torch
# 创建一个形状为[2, 2, 40, 256]的随机NumPy数组
numpy_array = np.random.rand(2, 2, 40, 256)
# 将NumPy数组转换为PyTorch张量
torch_tensor = torch.from_numpy(numpy_array)
标签:转换,tensor,256,torch,40,np,numpy
From: https://www.cnblogs.com/fly-smart/p/17662702.html