当前位置: 首页 > java >正文

小白的进阶之路系列之八----人工智能从初步到精通pytorch综合运用的讲解第一部分

PyTorch Tensors

通过大量实例学习编程应用是最有效的方法。

本篇是PyTorch综合运用,旨在让读者通过一行行代码亲自掌握Pytorch工具包的各种功能,有利于大家部署自己的神经网络人工智能计算工程。

首先,载入torch库。

import torch

我们来看看一些基本的张量操作。首先,只是一些创建张量的方法:

z = torch.zeros(5, 3)
print(z)
print(z.dtype)

输出为:

tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])
torch.float32

上面,我们创建了一个5x3的矩阵,里面充满了零,并查询它的数据类型来发现零是32位浮点数,这是PyTorch的默认值。

如果你想要整数呢?你总是可以覆盖默认值:

z = torch.zeros(5, 3)
print(z)
print(z.dtype)

输出为:

tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])
torch.float32

您可以看到,当我们更改默认值时,张量在打印时有用地报告了这一点。

随机初始化学习权重是很常见的,通常会为PRNG设置一个特定的种子,以获得结果的可重复性:

torch.manual_seed(1729)
r1 = torch.rand(2, 2)
print('A random tensor:')
print(r1)r2 = torch.rand(2, 2)
print('\nA different random tensor:')
print(r2) # new valuestorch.manual_seed(1729)
r3 = torch.rand(2, 2)
print('\nShould match r1:')
print(r3) # repeats values of r1 because of re-seed

输出为:

A random tensor:
tensor([[0.3126, 0.3791],[0.3087, 0.0736]])A different random tensor:
tensor([[0.4216, 0.0691],[0.2332, 0.4047]])Should match r1:
tensor([[0.3126, 0.3791],[0.3087, 0.0736]])

PyTorch张量直观地执行算术运算。相似形状的张量可以相加、相乘等。标量操作分布在张量上:

ones = torch.ones(2, 3)
print(ones)twos = torch.ones(2, 3) * 2 # every element is multiplied by 2
print(twos)threes = ones + twos       # addition allowed because shapes are similar
print(threes)              # tensors are added element-wise
print(threes.shape)        # this has the same dimensions as input tensorsr1 = torch.rand(2, 3)
r2 = torch.rand(3, 2)
# uncomment this line to get a runtime error
# r3 = r1 + r2

输出为:

tensor([[1., 1., 1.],[1., 1., 1.]])
tensor([[2., 2., 2.],[2., 2., 2.]])
tensor([[3., 3., 3.],[3., 3., 3.]])
torch.Size([2, 3])

下面是可用的数学运算的一个小示例:

r = (torch.rand(2, 2) - 0.5) * 2 # values between -1 and 1
print('A random matrix, r:')
print(r)# Common mathematical operations are supported:
print('\nAbsolute value of r:')
print(torch.abs(r))# ...as are trigonometric functions:
print('\nInverse sine of r:')
print(torch.asin(r))# ...and linear algebra operations like determinant and singular value decomposition
print('\nDeterminant of r:')
print(torch.det(r))
print('\nSingular value decomposition of r:')
print(torch.svd(r))# ...and statistical and aggregate operations:
print('\nAverage and standard deviation of r:')
print
http://www.xdnf.cn/news/10225.html

相关文章:

  • 【C++】 类和对象(上)
  • Matlab数据类型
  • 界面形成能的理解
  • Python简易音乐播放器开发教程
  • day61—DFS—省份数量(LeetCode-547)
  • 【计算机网络】 ARP协议和DNS协议
  • 38.springboot使用rabbitmq
  • qwen 2.5 并行计算机制:依靠 PyTorch 和 Transformers 库的分布式能力
  • GB 36246-2018 中小学合成材料面层运动场地检测
  • 【七. Java字符串操作与StringBuilder高效拼接技巧】
  • 新闻数据加载(鸿蒙App开发实战)
  • 飞致云开源社区月度动态报告(2025年5月)
  • uniapp 键盘顶起页面问题
  • G25-05-31Rust开源项目日报 Top10
  • C++ - STL #什么是STL #STL的版本 #闭源开源 #STL的六大组件
  • Ubuntu搭建DNS服务器
  • python:PyMOL 能处理 *.pdb 文件吗?
  • 俄军操作系统 Astra Linux 安装教程
  • 机器学习:欠拟合、过拟合、正则化
  • Android15 userdebug版本不能remount
  • reverse_ssh 建立反向 SSH 连接指南 混淆AV [好东西哟]
  • 腾讯云推出云开发AI Toolkit,国内首个面向智能编程的后端服务
  • Vue 3 中ref 结合ts 获取 DOM 元素的实践指南。
  • BLE 广播与扫描机制详解:如何让设备“被看见”?
  • Deseq2:MAG相对丰度差异检验
  • 算法分析与设计概述
  • 达梦的TEMP_SPACE_LIMIT参数
  • 如何实现一个请求库?【面试场景题】
  • NLP学习路线图(十三):正则表达式
  • CloudCompare-源码分析-绘制与 3D 场景分离的“前景”元素