pytorch中tensor的属性 类型转换 形状变换 转置 最大值
2024-08-26 16:34:47
import torch
import numpy as np
a = torch.tensor([[[1]]])
#只有一个数据的时候,获取其数值
print(a.item()) #tensor转化为nparray
b = a.numpy()
print(b,type(b),type(a)) #获取张量的形状
a = torch.tensor(np.arange(30).reshape(3,2,5))
print(a)
print(a.shape)
print(a.size())
print(a.size(0)) #形状变换
print(a.view([2,3,5])) #转置
b = torch.tensor(np.arange(15).reshape(3,5))
print(b)
print(b.transpose(0,1))
print(b.T) #最大值
print(b.max(dim=-1)) D:\anaconda\python.exe C:/Users/liuxinyu/Desktop/pytorch_test/day1/张量的属性和方法.py
1
[[[1]]] <class 'numpy.ndarray'> <class 'torch.Tensor'>
tensor([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]], [[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]], dtype=torch.int32)
torch.Size([3, 2, 5])
torch.Size([3, 2, 5])
3
tensor([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]], [[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]], dtype=torch.int32)
tensor([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]], dtype=torch.int32)
tensor([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]], dtype=torch.int32)
tensor([[ 0, 5, 10],
[ 1, 6, 11],
[ 2, 7, 12],
[ 3, 8, 13],
[ 4, 9, 14]], dtype=torch.int32)
torch.return_types.max(
values=tensor([ 4, 9, 14], dtype=torch.int32),
indices=tensor([4, 4, 4])) Process finished with exit code 0
最新文章
- Android大神博客
- top命令使用详解
- POJ1002 487-3279
- C++中“强制转换”的四大天王
- tarjan总结
- win7下jdk安装环境变量配置
- Xcode7真机测试
- [oracle]查询一个表中数据的插入时间
- JDK8 BigDecimal API-创建BigDecimal源码浅析三
- CSS中清除浮动的作用以及如何清除浮动
- cdnbest区域里快速配置全部节点的缓存
- selenium捕捉视频
- Python3基础系列——枚举类型大揭秘
- SQL语句内做除法得出百分比
- python进程之守护进程
- java系列之 原生数据类型
- this指针的调整
- ASP.NET MVC ScriptBundle 不能加载.min.js文件
- Linux下的堆off-by-one的利用
- Groovy 与 DSL
热门文章
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之七(四十三)
- C - 饭卡
- mysql 中UNIQUE KEY 到底是约束还是索引?
- DevOps技能自查表
- 1098 Insertion or Heap Sort (25分)
- 打造一款 刷Java 知识的小程序(一)
- 将wxpy的登录二维码放到网页上登录
- 汇编刷题:在M单元和N单元分别存有一个8位无符号数36H和95H,要求比较并输出 M大于N 或者 M小于N
- redis修改密码以及验证登录,启动服务常用命令
- Mac配置hosts文件