PyTorch基本操作的示例分析

发布时间:2021-09-08 13:42:49 作者:小新
来源:亿速云 阅读:114

这篇文章主要介绍PyTorch基本操作的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创建数据

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

参数:

例子:

# 创建一个形状为[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

参数:

例子:

# 创建一个形状为[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

参数:

例子:

# 创建一个形状为[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

参数:

例子:

# 通过数据创建张量
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

参数:

例子:

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

输出结果:

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

数学运算

PyTorch基本操作的示例分析

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) 可以帮助我们快速的找到张量中的特定信息.

PyTorch基本操作的示例分析

例子:

# 简单的索引操作
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基本操作的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. XML文档基本操作的示例分析
  2. 基于Pytorch SSD模型的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pytorch

上一篇:TensorFlow2之Fashion Mnist的示例分析

下一篇:python线程通信Condition的实例用法介绍

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》