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

PyTorch torch._assert Function

PyTorch torch._assert Function


PyTorch torch reference manual PyTorch torch reference manual

torch._assert is a function in PyTorch used for assertion checks. If the condition is False, a RuntimeError is raised.

Function definition



torch._assert(condition, message)


Usage examples

Example

import

torch

Example Code

condition

=

True

torch._assert

(

condition

,

"Assertion failed!"

)

print

(

"Assertion passed!"

)

Failing example

try

:

    torch._assert

(

False

,

"This will fail"

)

except

RuntimeError

as

e:

   

print

(

f

"Caught error: {e}"

)


PyTorch torch reference manual PyTorch torch reference manual