Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.inner function

PyTorch torch.inner function


PyTorch torch reference manual 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:

Return Value:


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

)

print

(

"Vector a:"

,

a

)

print

(

"Vector b:"

,

b

)

print

(

"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

)

print

(

"A shape:"

,

A.

shape

)

print

(

"B shape:"

,

B.

shape

)

print

(

"Result shape:"

,

result.

shape

)


PyTorch torch reference manual PyTorch torch reference manual