# pytorch tensor内所有元素相乘实例

## tensor内所有元素相乘

```a = torch.Tensor([1,2,3])
print(torch.prod(a))```

tensor(6.)

## tensor乘法运算汇总与解析

### 元素一一相乘

```import torch
def element_by_element():

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])

return x * y, torch.mul(x, y)
element_by_element()```
`(tensor([ 4, 10, 18]), tensor([ 4, 10, 18]))`

```def element_by_element_broadcast():

x = torch.tensor([1, 2, 3])
y = 2

return x * y
`tensor([2, 4, 6])`

### 向量点乘

torch.matmul: If both tensors are 1-dimensional, the dot product (scalar) is returned.

```def vec_dot_product():

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])

vec_dot_product()```
`tensor(32)`

### 矩阵乘法

torch.matmul: If both arguments are 2-dimensional, the matrix-matrix product is returned.

```def matrix_multiple():

x = torch.tensor([
[1, 2, 3],
[4, 5, 6]
])
y = torch.tensor([
[7, 8],
[9, 10],
[11, 12]
])

matrix_multiple()```
```(tensor([[ 58,  64],
[139, 154]]), tensor([[ 58,  64],
[139, 154]]))```

### vector 与 matrix 相乘

torch.matmul: If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

```def vec_matrix():
x = torch.tensor([1, 2, 3])
y = torch.tensor([
[7, 8],
[9, 10],
[11, 12]
])

vec_matrix()```
`tensor([58, 64])`

### matrix 与 vector 相乘

```def matrix_vec():
x = torch.tensor([
[1, 2, 3],
[4, 5, 6]
])
y = torch.tensor([
7, 8, 9
])

matrix_vec()```
`tensor([ 50, 122])`

### 带有batch_size 的 broad cast乘法

```def batched_matrix_broadcasted_vector():
x = torch.tensor([
[
[1, 2], [3, 4]
],
[
[5, 6], [7, 8]
]
])

print(f"x shape: {x.size()} \n {x}")
y = torch.tensor([1, 3])

```x shape: torch.Size([2, 2, 2])
tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
tensor([[ 7, 15],
[23, 31]])```
```batched matrix x batched matrix
def batched_matrix_batched_matrix():
x = torch.tensor([
[
[1, 2, 1], [3, 4, 4]
],
[
[5, 6, 2], [7, 8, 0]
]
])

y = torch.tensor([
[
[1, 2],
[3, 4],
[5, 6]
],
[
[7, 8],
[9, 10],
[1, 2]
]
])

print(f"x shape: {x.size()} \n y shape: {y.size()}")
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")```
```x shape: torch.Size([2, 2, 3])
y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])
tensor([[[ 12,  16],
[ 35,  46]],
[[ 91, 104],
[121, 136]]])```

```def batched_matrix_batched_matrix_bmm():
x = torch.tensor([
[
[1, 2, 1], [3, 4, 4]
],
[
[5, 6, 2], [7, 8, 0]
]
])

y = torch.tensor([
[
[1, 2],
[3, 4],
[5, 6]
],
[
[7, 8],
[9, 10],
[1, 2]
]
])

print(f"x shape: {x.size()} \n y shape: {y.size()}")
xy = batched_matrix_batched_matrix()
print(f"xy shape: {xy.size()} \n {xy}")```
```x shape: torch.Size([2, 2, 3])
y shape: torch.Size([2, 3, 2])
xy shape: torch.Size([2, 2, 2])
tensor([[[ 12,  16],
[ 35,  46]],
[[ 91, 104],
[121, 136]]])```
```tensordot
def tesnordot():
x = torch.tensor([
[1, 2, 1],
[3, 4, 4]])
y = torch.tensor([
[7, 8],
[9, 10],
[1, 2]])
print(f"x shape: {x.size()}, y shape: {y.size()}")
```x shape: torch.Size([2, 3]), y shape: torch.Size([3, 2])