当前位置:网站首页>【pytorch】1.7 pytorch与numpy,tensor与array的转换
【pytorch】1.7 pytorch与numpy,tensor与array的转换
2022-07-31 16:15:00 【Enzo 想砸电脑】
1、pytorch与numpy 函数对比
2、tensor与ndarray的转换
torch.Tensor(array)
torch.tensor(array)
torch.as_tensor(array)
torch.from_array(array)
import torch
import numpy as np
array = np.array([1,2,3,4])
print(array.dtype) #int64
# 用4种方式 将numpy array转换为torch tensor
tensor = torch.tensor(array)
Tensor = torch.Tensor(array)
as_tensor = torch.as_tensor(array)
from_array = torch.from_numpy(array)
# ====== 对比几种不同方法之间的异同 =========
print(tensor.dtype) #torch.int64
print(Tensor.dtype) #torch.float32
print(as_tensor.dtype) #torch.int64
print(from_array.dtype) #torch.int64
# ========== 判断是否共享内存 ===========
#修改数组的值
array[0] = 10
print(tensor) #tensor([1, 2, 3, 4])
print(Tensor) #tensor([1., 2., 3., 4.])
print(as_tensor) #tensor([10, 2, 3, 4])
print(from_array) #tensor([10, 2, 3, 4])
结论:
1)torch.Tensor() 和 torch.tensor() 在转换的时候,是开辟了新的内存空间来存储tensor的。
- torch.Tensor 在生成tensor的时候,默认生成 float数据类型
- torch.tensor 是根据的情况来推断长生什么样的数据类型。
2)torch.as_tensor() 和 torch.from_array() 转换的tensor,和 numpy array 是共享内存的;所以数据肯定也是一致的。
边栏推荐
- Stuck in sill idealTree buildDeps during npm installation, npm installation is slow, npm installation is stuck in one place
- Snake Project (Simple)
- type of timer
- 复制延迟案例(3)-单调读
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- .NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的
- LevelSequence源码分析
- The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
- Single-cell sequencing workflow (single-cell RNA sequencing)
- 牛客网刷题(三)
猜你喜欢
对话庄表伟:开源第一课
基于ABP实现DDD
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
C语言”三子棋“升级版(模式选择+AI下棋)
Premiere Pro 2022 for (pr 2022)v22.5.0
T - sne + data visualization parts of the network parameters
The use of border controls
The 2nd China PWA Developer Day
基于C语言的编译器设计与实现
The new BMW 3 Series is on the market, with safety and comfort
随机推荐
SringMVC中个常见的几个问题
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
jeecg master-slave database read-write separation configuration "recommended collection"
01 Encounter typescript, build environment
第二届中国PWA开发者日
T - sne + data visualization parts of the network parameters
深度学习机器学习理论及应用实战-必备知识点整理分享
Implement anti-shake and throttling functions
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
牛客网刷题(二)
Emmet syntax
Qt实战案例(54)——利用QPixmap设计图片透明度
删除表格数据或清空表格
go图书管理系统
MySQL多表联合查询
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
tooltips使用教程(鼠标悬停时显示提示)
ML.NET related resources
【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
6. 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务