PyTorch torch.inner function
PyTorch torch reference manual
torch.inner A function in PyTorch for computing the inner product of two tensors. For vectors, it is equivalent to the dot product; for higher-dimensional tensors, it computes the inner product along a specified dimension.
Function definition
torch.inner(input, other, out=None)
Parameters:
input(Tensor): the first input tensor.other(Tensor): The second input tensor.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the inner product result.
Usage examples
Example - vector inner product
import
torch
Create two vectors
a
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
b
=
torch.
tensor
(
[
4.0
,
5.0
,
6.0
]
)
Compute the inner product
result
=
torch.
inner
(
a
,
b
)
(
"Vector a:"
,
a
)
(
"Vector b:"
,
b
)
(
"Inner product result:"
,
result
)
The output is:
向量 a: tensor([1., 2., 3.])
向量 b: tensor([4., 5., 6.])
内积结果: tensor(32.)
Example - matrix inner product
import
torch
Create two matrices
A
=
torch.
randn
(
2
,
3
)
B
=
torch.
randn
(
2
,
3
)
Compute the inner product along the last dimension
result
=
torch.
inner
(
A
,
B
)
(
"A shape:"
,
A.
shape
)
(
"B shape:"
,
B.
shape
)
(
"Result shape:"
,
result.
shape
)