PyTorch一小时掌握之基本操作篇

 更新时间:2021年09月08日 09:37:51   作者:我是小白呀  
这篇文章主要介绍了PyTorch一小时掌握之基本操作篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

创建数据

在这里插入图片描述

torch.empty()

创建一个空张量矩阵.

格式:

torch.empty(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False, memory_format=torch.contiguous_format) → Tensor

参数:

  • size: 生成矩阵的形状, 必选
  • dtype: 数据类型, 默认为 None

例子:

# 创建一个形状为[2, 2]的矩阵
a = torch.empty(2, 2)
print(a)

# 创建一个形状为[3, 3]的矩阵
b = torch.empty(3, 3)
print(b)

输出结果:

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

torch.zeros()

创建一个全零矩阵.

格式:

torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

参数:

  • size: 生成矩阵的形状, 必选
  • dtype: 数据类型, 默认为 None

例子:

# 创建一个形状为[2, 2]的全零数组
a = torch.zeros([2, 2], dtype=torch.float32)
print(a)

# 创建一个形状为[3, 3]的全零数组
b = torch.zeros([3, 3], dtype=torch.float32)
print(b)

输出结果:

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

torch.ones()

创建一个全一矩阵.

格式:

torch.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

参数:

  • size: 生成矩阵的形状, 必选
  • dtype: 数据类型, 默认为 None

例子:

# 创建一个形状为[2, 2]的全一数组
a = torch.ones([2, 2], dtype=torch.float32)
print(a)

# 创建一个形状为[3, 3]的全一数组
b = torch.ones([3, 3], dtype=torch.float32)
print(b)

输出结果:

tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])

torch.tensor()

通过数据创建张量.

格式:

torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

参数:

  • data: 数据 (数组, 元组, ndarray, scalar)
  • dtype: 数据类型, 默认为 None

例子:

# 通过数据创建张量
array = np.arange(1, 10).reshape(3, 3)
print(array)
print(type(array))

tensor = torch.tensor(array)
print(tensor)
print(type(tensor))

输出结果:

[[1 2 3]
[4 5 6]
[7 8 9]]
<class 'numpy.ndarray'>
tensor([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=torch.int32)
<class 'torch.Tensor'>

torch.rand()

创建一个 0~1 随机数的张量矩阵.

格式:

torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

参数:

  • size: 生成矩阵的形状, 必选
  • dtype: 数据类型, 默认为 None

例子:

# 创建形状为[2, 2]的随机数矩阵
rand = torch.rand(2, 2)
print(rand)

输出结果:

tensor([[0.6209, 0.3424],
[0.3506, 0.7986]])

数学运算

在这里插入图片描述

torch.add()

返回相加的张量.

格式:

torch.add(input, other, *, out=None) → Tensor

例子:

# 张量相加
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)

input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)

output = torch.add(input1, input2)
print(output)

输出结果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[5, 5],
[5, 5]])

注: 相加的张量形状必须一致, 否则会报错.

torch.sub()

返回相减的张量.

例子:

# 张量相减
input1 = torch.tensor([[1, 2], [3, 4]])
print(input1)

input2 = torch.tensor([[4, 3], [2, 1]])
print(input2)

output = torch.sub(input1, input2)
print(output)

输出结果:

tensor([[1, 2],
[3, 4]])
tensor([[4, 3],
[2, 1]])
tensor([[-3, -1],
[ 1, 3]])

torch.matmul()

例子:

# 张量矩阵相乘
input1 = torch.tensor([[1, 1, 1]])
print(input1)

input2 = torch.tensor([[3], [3], [3]])
print(input2)

output = torch.matmul(input1, input2)
print(output)

输出结果:

tensor([[1, 1, 1]])
tensor([[3],
[3],
[3]])
tensor([[9]])

索引操作

索引 (index) 可以帮助我们快速的找到张量中的特定信息.

在这里插入图片描述

例子:

# 简单的索引操作
ones = torch.ones([3, 3])
print(ones[: 2])
print(ones[:, : 2])

调试输出:

tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 1.],
[1., 1.],
[1., 1.]])

到此这篇关于PyTorch一小时掌握之基本操作篇的文章就介绍到这了,更多相关PyTorch基本操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现批量转换图片为黑白

    python实现批量转换图片为黑白

    这篇文章主要为大家详细介绍了python实现批量转换图片为黑白,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • python 递归遍历文件夹,并打印满足条件的文件路径实例

    python 递归遍历文件夹,并打印满足条件的文件路径实例

    下面小编就为大家带来一篇python 递归遍历文件夹,并打印满足条件的文件路径实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • python实现的文件同步服务器实例

    python实现的文件同步服务器实例

    这篇文章主要介绍了python实现的文件同步服务器,实例分析了文件同步服务器的原理及客户端、服务端的实现技巧,需要的朋友可以参考下
    2015-06-06
  • pycharm新建Vue项目的方法步骤(图文)

    pycharm新建Vue项目的方法步骤(图文)

    这篇文章主要介绍了pycharm新建Vue项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  •  分享Python 中的 7 种交叉验证方法

     分享Python 中的 7 种交叉验证方法

    这篇文章主要给大家分享的是Python 中的 7 种交叉验证方法,交叉验证是一种用于估计机器学习模型性能的统计方法,它是一种评估统计分析结果如何推广到独立数据集的方法,下文相关介绍,需要的朋友可以参考一下
    2022-03-03
  • Python Pandas中布尔索引的用法详解

    Python Pandas中布尔索引的用法详解

    布尔索引是一种使用 DataFrame 中数据的实际值的索引。本文将通过一些示例为大家详细讲讲Python中布尔索引的用法,需要的可以参考一下
    2022-08-08
  • python构建基础的爬虫教学

    python构建基础的爬虫教学

    在本篇内容里小编给大家分享的是关于python构建基础的爬虫教学内容,需要的朋友们学习下。
    2018-12-12
  • OpenCV半小时掌握基本操作之角点检测

    OpenCV半小时掌握基本操作之角点检测

    这篇文章主要介绍了OpenCV基本操作之角点检测,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • 简洁的十分钟Python入门教程

    简洁的十分钟Python入门教程

    这篇文章主要介绍了简洁的十分钟Python入门教程,Python语言本身的简洁也使得网络上各种Python快门入门教程有着很高的人气,本文是国内此类其中的一篇,需要的朋友可以参考下
    2015-04-04
  • Python dict的使用误区你知道吗

    Python dict的使用误区你知道吗

    这篇文章主要为大家介绍了Python dict的使用误区,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01

最新评论