当前位置:网站首页>[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.
边栏推荐
- Vb how to connect mysql_vb how to connect to the database collection "advice"
- Foreign media right, apple on May be true in inventory
- 2022年必读的12本机器学习书籍推荐
- 牛客 HJ3 明明的随机数
- 动态规划(一)
- Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
- Deployment application life cycle and Pod health check
- flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
- Flutter 获取状态栏statusbar的高度
- 牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
猜你喜欢
随机推荐
复制延迟案例(3)-单调读
what exactly is json (c# json)
Implement anti-shake and throttling functions
LevelSequence源码分析
MySQL database operations
Visualize GraphQL schemas with GraphiQL
Baidu cloud web speed playback (is there any website available)
在资源管理类中提供对原始资源的访问——条款15
Grafana安装后web打开报错
mysql黑窗口~建库建表
Implementing click on the 3D model in RenderTexture in Unity
Why is the field of hacking almost filled with boys?
jeecg主从数据库读写分离配置「建议收藏」
update data table update
基于ABP实现DDD
对话庄表伟:开源第一课
ASP.NET Core generates continuous Guid
牛客 HJ20 密码验证合格程序
C language "the third is" upgrade (mode selection + AI chess)
7. Summary of common interview questions