-
【数据类型】Python 与 PyTorch 常见数据类型对应:

用
a.type()获取数据类型,用isinstance(a, 目标类型)进行类型合法化检测python3>>> import torch >>> a = torch.randn(2,3) >>> a tensor([[-1.7818, -0.2472, -2.0684], [ 0.0117, 1.4698, -0.9359]]) >>> a.type() ## 获取数据类型 'torch.FloatTensor' >>> isinstance(a, torch.FloatTensor) ## 类型合法化检测 True >>> -
【什么是张量】标量与张量:用
a.dim(),a.shape或者a.size()查看 dim 为 0 是标量,否则是张量python3>>> import torch >>> >>> a = torch.tensor(1) >>> a tensor(1) >>> a.dim() >>> 0 ## 标量 >>> a = torch.Tensor([1]) >>> a tensor([1.]) >>> a.dim() >>> 1 ## 张量 -
【生成张量】常见方法如下:
-
常见随机方法:
torch.randn(shape),torch.rand(shape),torch.randint(min, max, shape),torch.rand_like(a),torch.normal(mean, std)... 具体示例如下 -
Dim 1 / rank 1: 以 size 2 为例
python3>>> a = torch.randn(2) ## 随机, >>> a: tensor([1.4785, 0.6089]) >>> a = torch.Tensor(2) ## 接收维度, unintialized 不推荐 >>> a: tensor([5.4086e+26, 4.5907e-41]) >>> a = torch.Tensor([1,2]) ## 同 torch.tensor([1,2]) 接收具体数据 >>> a: tensor([1, 2]) >>> a = torch.from_numpy(np_data) ## 数据维持不变,类型一一对应 >>> a = torch.full([2],7) ## 全部填充为一样的值 >>> a: tensor([7, 7]) >>> a = torch.arange(0,10) ## arange >>> a: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> a = torch.linspace(0,10, steps=4) ## >>> a: tensor([ 0.0000, 3.3333, 6.6667, 10.0000]) >>> a = torch.logspace(0,10, steps=4) >>> a: tensor([1.0000e+00, 2.1544e+03, 4.6416e+06, 1.0000e+10]) -
Dim 2 / rank 2: 以 size [2,3] 为例
python3>>> a = torch.randn(2, 3) ## 随机 >>> a: tensor([[ 2.0631, -1.7011, 0.6375], [-1.2104, -1.3341, -0.8187]]) >>> a = torch.Tensor(2, 3) ## 接收维度, unintialized 不推荐 >>> a: tensor([[-0.2438, -0.9554, -0.4694], [ 0.8636, 1.6497, -0.8862]]) >>> a = torch.Tensor([[1,2,3],[4,5,6]]) ## 同 torch.tensor([[1,2,3],[4,5,6]]) 接收具体数据 >>> a: tensor([[1., 2., 3.], [4., 5., 6.]]) >>> a = torch.from_numpy(np_data) ## 数据维持不变,类型一一对应 >>> a = torch.full([2,3],7) ## 全部填充为一样的值 >>> a: tensor([[7, 7, 7], [7, 7, 7]])...
-
PyTorch 张量数据类型
CODE_RabbitV2024-06-15 10:10
相关推荐
泉城老铁3 分钟前
Dify知识库如何实现多关键词AND检索?阿星AI工作室6 分钟前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!databook6 分钟前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升Halo咯咯7 分钟前
别再学写代码了,顶级工程师现在在学管理AI agent | 值得一读明明如月学长14 分钟前
被 Claude Code 劝退?这款免费开源好用的 AI 神器更适合普通人恋猫de小郭2 小时前
AI 正在造就你的「认知卸载」,但是时代如此飞哥数智坊9 小时前
我的“龙虾”罢工了!正好对比下GLM、MiniMax、Kimi 3家谁更香风象南10 小时前
很多人说,AI 让技术平权了,小白也能乱杀老师傅 ?董董灿是个攻城狮11 小时前
大模型连载1:了解 Token花酒锄作田13 小时前
使用 pkgutil 实现动态插件系统