PyTorch怎么创建数据及实现数学运算
Admin 2022-06-07 群英技术资讯 1430 次浏览
这篇文章将为大家详细讲解有关“PyTorch怎么创建数据及实现数学运算”的知识,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创建一个空张量矩阵.
格式:
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(*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(*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(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'>
创建一个 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]])

返回相加的张量.
格式:
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]])
注: 相加的张量形状必须一致, 否则会报错.
返回相减的张量.
例子:
# 张量相减 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]])
例子:
# 张量矩阵相乘 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.]])
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了Python数据分析之 Pandas Dataframe条件筛选遍历详情,查询Pandas Dataframe数据时,经常会筛选出符合条件的数据,关于其使用方式,需要的小伙伴可以参考一下下面文章内容
大家好,本篇文章主要讲的是Python中最神秘missing()函数介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
这篇文章主要分享了6 个值得收藏的 Python 代码,希望队长正在学习的你有所帮助,需要的小伙伴也可以参考一下
TensorFlow使用的求导方法称为自动微分(Automatic Differentiation),它既不是符号求导也不是数值求导,而类似于将两者结合的产物。最基本的原理就是链式法则,关键思想是在基本操作(op)的水平上应用符号求导,并保持中间结果(grad)。
这篇文章主要介绍了通过几个机器学习中最常用的概率分布为例,来看看如何从一个概率分布中采样,文章中的代码对我们的工作或学习具有一定价值,感兴趣的朋友可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008