当前位置:网站首页>[pytorch] 1.7 pytorch and numpy, tensor and array conversion
[pytorch] 1.7 pytorch and numpy, tensor and array conversion
2022-07-31 16:25:00 【Enzo wants to smash the computer】
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
# ========== Determine whether to share memory ===========
#修改数组的值
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() 在转换的时候,Is to open up a new memory space to storetensor的.
- torch.Tensor 在生成tensor的时候,默认生成 float数据类型
- torch.tensor It is based on the situation to infer what kind of data type for longevity.
2)torch.as_tensor() 和 torch.from_array() 转换的tensor,和 numpy array 是共享内存的;So the data must be the same.
边栏推荐
- Baidu cloud web speed playback (is there any website available)
- WPF project - basic usage of controls entry, you must know XAML
- .NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
- what exactly is json (c# json)
- 2022年必读的12本机器学习书籍推荐
- [Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
- Single-cell sequencing workflow (single-cell RNA sequencing)
- 仿生毛毛虫机器人源码
- 牛客 HJ17 坐标移动
- LeetCode_733_Image rendering
猜你喜欢
t-sne 数据可视化网络中的部分参数+
智能垃圾桶(九)——震动传感器(树莓派pico实现)
Getting Started with TextBlock Control Basic Tools Usage, Get Started
上传图片-微信小程序(那些年的坑记录2022.4)
BGP综合实验(建立对等体、路由反射器、联邦、路由宣告及聚合)
MySQL的相关问题
What is the difference between BI software in the domestic market?
仿生毛毛虫机器人源码
Qt practical cases (54) - using transparency QPixmap design pictures
二分查找的细节坑
随机推荐
form 表单提交后,使页面不跳转[通俗易懂]
长得很怪的箱图
Oracle dynamically registers non-1521 ports
网站漏洞修复服务商关于越权漏洞分析
单细胞测序流程(单细胞rna测序)
type of timer
外媒所言非虚,苹果降价或许是真的在清库存
Tencent Cloud Deployment----DevOps
Single-cell sequencing workflow (single-cell RNA sequencing)
gerrit中如何切换远程服务器
牛客网刷题(三)
MySQL database operations
贪吃蛇项目(简单)
在资源管理类中提供对原始资源的访问——条款15
Handling write conflicts under multi-master replication (4) - multi-master replication topology
Kubernetes common commands
Design and Implementation of Compiler Based on C Language
mysql black window ~ build database and build table
多主复制下处理写冲突(4)-多主复制拓扑
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版