1.tensor转np

# 创建一个形状为[2, 2, 40, 256]的随机张量
torch_tensor = torch.rand(2, 2, 40, 256)

# 将tensor转换为NumPy数组
numpy_array = torch_tensor.numpy()

2.np转tensor

# 创建一个形状为[2, 2, 40, 256]的随机NumPy数组
numpy_array = np.random.rand(2, 2, 40, 256)

# 将NumPy数组转换为tensor
torch_tensor = torch.from_numpy(numpy_array)
posted on 2023-08-28 16:25  孜孜不倦fly  阅读(29)  评论(0)    收藏  举报