Pytorch——Broadcasting and Element-wise Operations with PyTorch

Element-wise tensor operations for deep learning

An element-wise operation is an operation between two tensors that operates on corresponding elements within the respective tensors.元素操作是两个张量之间的操作,它对各自张量内的对应元素进行操作。
An element-wise operation operates on corresponding elements between tensors
Two elements are said to be corresponding if the two elements occupy the same position within the tensor. The position is determined by the indexes used to locate each element.两个元素如果在相同的位置上,则说这两个元素对应。

> t1 = torch.tensor([
    [1,2],
    [3,4]
], dtype=torch.float32)

> t2 = torch.tensor([
    [9,8],
    [7,6]
], dtype=torch.float32)

# Example of the first axis
> print(t1[0])
tensor([1., 2.])


# Example of the second axis
> print(t1[0][0])
tensor(1.)

> t1[0][0]
tensor(1.)


> t2[0][0]
tensor(9.)

This allows us to see that the corresponding element for the 1 in t1 is the 9 in t2.
The correspondence is defined by the indexes.

Addition is an element-wise operation

> t1 + t2
tensor([[10., 10.],
        [10., 10.]])

Arithmetic operations are element-wise operations

> print(t1 + 2)
tensor([[3., 4.],
        [5., 6.]])

> print(t1 - 2)
tensor([[-1.,  0.],
        [ 1.,  2.]])

> print(t1 * 2)
tensor([[2., 4.],
        [6., 8.]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TonyHsuM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值