当前位置:网站首页>[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.
边栏推荐
- 动态规划(一)
- WPF project - basic usage of controls entry, you must know XAML
- Emmet syntax
- Precautions and solutions when SIGABRT error is reported
- 牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
- Codeforces Round #796 (Div. 2) (A-D)
- How C programs run 01 - the composition of ordinary executable files
- 2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
- Insert into data table to insert data
- 全新宝马3系上市,安全、舒适一个不落
猜你喜欢

Graham‘s Scan法求解凸包问题

MySQL基础篇【单行函数】

i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)
[TypeScript] In-depth study of TypeScript type operations

6-22漏洞利用-postgresql数据库密码破解

Implementing DDD based on ABP

The new BMW 3 Series is on the market, with safety and comfort

The 2nd China PWA Developer Day

动态规划(一)

t-sne 数据可视化网络中的部分参数+
随机推荐
C语言”三子棋“升级版(模式选择+AI下棋)
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
Getting Started with TextBlock Control Basic Tools Usage, Get Started
单细胞测序流程(单细胞rna测序)
Dialogue with Zhuang Biaowei: The first lesson of open source
Kubernetes common commands
Kubernetes principle analysis and practical application manual, too complete
对话庄表伟:开源第一课
Delete table data or clear table
Grafana安装后web打开报错
After Grafana is installed, the web opens and reports an error
MySQL的相关问题
t-sne 数据可视化网络中的部分参数+
牛客 HJ3 明明的随机数
What is the difference between BI software in the domestic market?
LevelSequence源码分析
Unity 之 图集属性详解和代码示例 -- 拓展一键自动打包图集工具
复制延迟案例(1)-最终一致性
The use of border controls
T - sne + data visualization parts of the network parameters