PyTorch学习笔记(一)

1.Tensor

1.1 构造一个5x3矩阵,不初始化

python 复制代码
x = torch.empty(5, 3)
print(x)

打印结果:

复制代码
tensor([[1.0102e-38, 1.0194e-38, 3.6736e-39],
        [8.3572e-39, 4.5918e-39, 4.5918e-39],
        [4.0408e-39, 4.5918e-39, 4.7755e-39],
        [8.5408e-39, 8.3571e-39, 4.5918e-39],
        [4.6837e-39, 4.0408e-39, 4.5918e-39]])

1.2 构造一个随机初始化的矩阵

python 复制代码
x = torch.rand(5, 3)
print(x)

打印结果:

复制代码
tensor([[0.1306, 0.2627, 0.1585],
        [0.8739, 0.0200, 0.4470],
        [0.6009, 0.5557, 0.1189],
        [0.5708, 0.4116, 0.6806],
        [0.0506, 0.6534, 0.2358]])

1.3 构造一个矩阵全为 0,而且数据类型是 long.

python 复制代码
x = torch.zeros(5, 3, dtype=torch.long)
print(x)

输出结果

复制代码
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

1.4 直接使用数据构造一个张量

python 复制代码
x = torch.tensor([5.5, 3])
print(x)

输出结果

复制代码
tensor([5.5000, 3.0000])

1.5 创建一个 tensor 基于已经存在的 tensor

python 复制代码
x = tensor([5.5000, 3.0000])
x = x.new_ones(5, 3, dtype=torch.double)
print(x)

输出结果

复制代码
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
python 复制代码
x = torch.rand_like(x, dtype=torch.float)
print(x)

输出结果

复制代码
tensor([[0.0996, 0.3518, 0.2875],
        [0.2665, 0.5578, 0.1388],
        [0.3313, 0.8641, 0.5232],
        [0.8819, 0.6924, 0.9274],
        [0.8298, 0.4196, 0.1312]])

size

python 复制代码
print(x.size())
相关推荐
其实防守也摸鱼1 小时前
运维--怎么看接口的请求和返回
运维·学习·网络安全·网络攻击模型·burpsuite·攻防对抗·蓝队
酷讯网络_2408701601 小时前
区块粮仓宠物NFT源码区块狗/抢购转让预约区块投资理财系统
学习·开源·宠物
疯狂打码的少年1 小时前
【软件工程】软件开发模型:增量模型与迭代模型
笔记·软件工程
Generalzy2 小时前
从本地 Demo 到生产级检索:Milvus 学习笔记(3)
笔记·学习·milvus
心中有国也有家3 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
tyqtyq223 小时前
求职信生成:AI 智能求职信撰写系统的鸿蒙实现
人工智能·学习·华为·生活·harmonyos
MartinYeung53 小时前
[论文学习]PrivacyLens:评估语言模型在行动中的隐私规范意识
人工智能·学习·语言模型
凉、介4 小时前
Virtio 系列(一):框架概览
笔记·学习·嵌入式·虚拟化·virtio
pokemen邪15 小时前
PyTorch KernelAgent 源码解读 ---(6)--- Composer
人工智能·pytorch·composer
Yang_jie_035 小时前
笔记:数据结构(栈是否使用底指针以及头指针的初始化值)
数据结构·笔记·算法