【python】list,tensor,numpy相互转化
# 1.1 list 转 numpy
ndarray = np.array(list) # 1.2 numpy 转 list list = ndarray.tolist() # 2.1 list 转 torch.Tensor tensor=torch.Tensor(list) # 2.2 torch.Tensor 转 list 先转numpy,后转list list = tensor.numpy().tolist() # 3.1 torch.Tensor 转 numpy ndarray = tensor.numpy() # *gpu上的tensor不能直接转为numpy ndarray = tensor.cpu().numpy() # 3.2 numpy 转 torch.Tensor tensor = torch.from_numpy(ndarray)
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步