PyTorch torch._assert 関数
torch._assertは PyTorch でアサーションチェックに使用される関数です。条件が False の場合、RuntimeError をスローします。
関数定義
torch._assert(condition, message)
使用例
実例
import torch
# サンプルコード
condition = True
torch._assert(condition, "Assertion failed!")
print("Assertion passed!")
# 失敗例
try:
torch._assert(False, "This will fail")
except RuntimeError as e:
print(f"Caught error: {e}")
# サンプルコード
condition = True
torch._assert(condition, "Assertion failed!")
print("Assertion passed!")
# 失敗例
try:
torch._assert(False, "This will fail")
except RuntimeError as e:
print(f"Caught error: {e}")
その他の拡張
Pytorch torch リファレンスマニュアル