2024年大数据最新PyTorch 一小时学会基本操作_pytorch sub


参数:


* 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]])


## 数学运算


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210626075252758.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjI3NDE2OA==,size_16,color_FFFFFF,t_70#pic_center)


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


![img](https://img-blog.csdnimg.cn/img_convert/e3f23a5ccc97a6c7180e645779a90df1.png)
![img](https://img-blog.csdnimg.cn/img_convert/cfab0c2f9895aad098e1ea1ea2b4f158.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

中...(img-yuQTaoya-1714656405174)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值