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

PyTorch torch.solve function

PyTorch torch.solve function


PyTorch torch reference manual PyTorch torch reference manual

torch.solve is a function in PyTorch used to solve systems of linear equations. It solves AX = B, where A is the coefficient matrix and B is the right-hand side matrix or vector.

Function definition

torch.solve(B, A, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create the coefficient matrix and the right-hand-side vector

A

=

torch.

tensor

(

[

[

1.0

,

2.0

]

,

[

3.0

,

4.0

]

]

)

B

=

torch.

tensor

(

[

5.0

,

11.0

]

)

Solve AX = B

X

,

_

=

torch.

solve

(

B

,

A

)

print

(

"Coefficient matrix A:"

)

print

(

A

)

print

(

"nRight-hand-side vector B:"

)

print

(

B

)

print

(

"nSolution X:"

)

print

(

X

)

print

(

"nVerify: A @ X ="

)

print

(

A

@

X

)

The output is:

系数矩阵 A:

tensor([[1., 2.],

        [3., 4.]])

右侧向量 B:

tensor([ 5., 11.])

解 X:

tensor([1., 2.])

验证: A @ X =

tensor([ 5., 11.])


PyTorch torch reference manual PyTorch torch reference manual