pytorch张量的常用算数运算
我们知道张量是一个数据的多维集合,那在进行常用算数运算时,张量是如何运算的呢?
pytorch在进行张量的算数运算时是将张量对应位置的元素进行算数运算,然后合并成一个相同大小的张量。
下面我们来看下在pytorch中如何进行张量的算数运算。加import torch x = torch.tensor([1, 2, 3, 4]) y = torch.tensor([1, 2, 3, 4]) x + y
减import torch x = torch.tensor([1, 2, 3, 4]) y = torch.tensor([1, 2, 3, 4]) x - y
乘import torch x = torch.tensor([1, 2, 3, 4]) y = torch.tensor([1, 2, 3, 4]) x * y
除import torch x = torch.tensor([1, 2, 3, 4]) y = torch.tensor([1, 2, 3, 4]) x / y
幂运算import torch x = torch.tensor([1, 2, 3, 4]) y = torch.tensor([1, 2, 3, 4]) x ** y
通过运算结果看到,张量的算数运算都是对应位置元素的运算。