Pytorch基本数据类型

本文详细介绍了PyTorch中的基本数据类型,包括张量的创建、标量表示、维度管理以及不同维度张量的应用场景。通过实例展示了如何使用torch.tensor和torch.FloatTensor创建不同形状的张量,并解释了张量在神经网络中如线性层、偏置和RNN输入中的作用。此外,还涵盖了张量的属性如shape、size和numel,以及张量在CNN和RNN中的应用,如4维张量在CNN中的表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pytorch基本数据类型

基本数据类型:
a = torch.randn(2, 3)
print(a.type())
print(type(a))
torch.FloatTensor
torch.Tensor
标量的表示:Dimension 0 / rank 0 通常使用在loss
print(torch.tensor(1.))
print(torch.tensor(1.3))
tensor(1.)
tensor(1.3000)
b = torch.tensor(2.2)
print(b.shape)
print(len(b.shape))
print(b.size())
torch.Size([])
0
torch.Size([])
Dimension 1的张量 通常用于偏置(Bias) 线性层(liner input)
.Tensor 接收数据的内容
.FloatTensor 接收的是数据的shape
import numpy as np
import torch
print(torch.tensor([1.1]))
print(torch.tensor([1.1, 2.2]))
print(torch.FloatTensor(1))
print(torch.FloatTensor(2))
data = np.ones(2)
print(data)
print(torch.from_numpy(data))
tensor([1.1000])
tensor([1.1000, 2.2000])
tensor([0.])
tensor([0., 0.])
[1. 1.]
tensor([1., 1.], dtype=torch.float64)
Dimension2的张量 (Liner Input batch)
c = torch.randn(2,3)
print(c)
print(c.shape)     # torch.Size(2,3)
print(c.size(0))   # 索引0 值为2
print(c.size(1))   # 索引1 值为3
print(c.shape[1])  # 索引1 值为3
Dimension3的张量 通常使用在RNN Input Batch
d = torch.rand(1,2,3)
print(d)

print("d.shape \t:", d.shape)
print("d[0] \t:", d[0])
print("list(d.shape) \t:", list(d.shape))    # 转换成python中的list
tensor([[[0.8113, 0.7288, 0.7128],
         [0.8423, 0.1709, 0.7813]]])
d.shape 	: torch.Size([1, 2, 3])
d[0] 	: tensor([[0.8113, 0.7288, 0.7128],
        [0.8423, 0.1709, 0.7813]])
list(d.shape) 	: [1, 2, 3]
Dimension4的张量 CNN [b, c, h, w]
e = torch.rand(2,3,28,28) # 2:数量  3:通道  28:height 28:weight
print("e: \t",e)

print("e.shape: \t",e.shape)

print("e.numel: \t",e.numel) # numel是指tensor占用内存的数量

print("e.shape: \t",e.dim()) # 查看Dimension
e: 	 tensor([[[[0.0064, 0.9952, 0.7189,  ..., 0.5260, 0.9961, 0.3329],
          [0.0319, 0.2366, 0.1659,  ..., 0.5725, 0.9700, 0.3408],
          [0.0533, 0.4911, 0.5068,  ..., 0.5154, 0.3181, 0.8749],
          ...,
          [0.2711, 0.4107, 0.9548,  ..., 0.0914, 0.1216, 0.4669],
          [0.3121, 0.1884, 0.6874,  ..., 0.4863, 0.1310, 0.8281],
          [0.8134, 0.2958, 0.7994,  ..., 0.7888, 0.0644, 0.6937]],

         [[0.6706, 0.9958, 0.8819,  ..., 0.8354, 0.7882, 0.2423],
          [0.9388, 0.0550, 0.6665,  ..., 0.4306, 0.0095, 0.3302],
          [0.3554, 0.9706, 0.7250,  ..., 0.6531, 0.8118, 0.0519],
          ...,
          [0.9702, 0.3393, 0.9195,  ..., 0.5785, 0.0750, 0.5565],
          [0.1810, 0.4161, 0.6777,  ..., 0.0729, 0.1671, 0.6183],
          [0.2405, 0.1588, 0.9701,  ..., 0.6085, 0.5801, 0.8102]],

         [[0.5726, 0.4358, 0.5602,  ..., 0.0822, 0.4775, 0.2254],
          [0.3146, 0.2055, 0.0449,  ..., 0.7919, 0.7323, 0.2093],
          [0.7855, 0.4358, 0.6886,  ..., 0.5912, 0.2422, 0.1586],
          ...,
          [0.7620, 0.0566, 0.2233,  ..., 0.3073, 0.0329, 0.2335],
          [0.4894, 0.1519, 0.0398,  ..., 0.6721, 0.9245, 0.5043],
          [0.2206, 0.3090, 0.2272,  ..., 0.5258, 0.7253, 0.2134]]],


        [[[0.7348, 0.3334, 0.3664,  ..., 0.7411, 0.9987, 0.0850],
          [0.6815, 0.8772, 0.6845,  ..., 0.2191, 0.6869, 0.5559],
          [0.7102, 0.8957, 0.7105,  ..., 0.6611, 0.3372, 0.1313],
          ...,
          [0.7818, 0.4233, 0.6212,  ..., 0.7316, 0.5175, 0.7100],
          [0.3687, 0.7642, 0.5971,  ..., 0.7126, 0.0531, 0.1984],
          [0.7542, 0.5635, 0.0263,  ..., 0.9695, 0.3727, 0.8665]],

         [[0.7535, 0.9495, 0.9007,  ..., 0.0361, 0.9683, 0.2283],
          [0.5435, 0.4133, 0.2253,  ..., 0.1438, 0.8811, 0.7469],
          [0.9631, 0.1310, 0.7819,  ..., 0.5443, 0.2486, 0.4881],
          ...,
          [0.4766, 0.0668, 0.0626,  ..., 0.3374, 0.1362, 0.9786],
          [0.3321, 0.4976, 0.5352,  ..., 0.4788, 0.2453, 0.7434],
          [0.4099, 0.0636, 0.1159,  ..., 0.1899, 0.8210, 0.5086]],

         [[0.4633, 0.9661, 0.8107,  ..., 0.2209, 0.3187, 0.7845],
          [0.0394, 0.6189, 0.1503,  ..., 0.5803, 0.4563, 0.8865],
          [0.2606, 0.6595, 0.5380,  ..., 0.4935, 0.3973, 0.1511],
          ...,
          [0.4009, 0.7124, 0.9389,  ..., 0.9483, 0.4283, 0.6653],
          [0.3785, 0.1570, 0.0520,  ..., 0.6226, 0.7004, 0.8791],
          [0.8059, 0.1177, 0.8035,  ..., 0.0045, 0.3060, 0.5216]]]])
e.shape: 	 torch.Size([2, 3, 28, 28])
e.numel: 	 <built-in method numel of Tensor object at 0x0000014538BE4228>
e.dim: 	 4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值