PyTorch torch.cuda.empty_cache 関数
torch.cuda.empty_cachePyTorch で CUDA キャッシュをクリアするための関数です。未使用のキャッシュメモリを解放し、再割り当てできるようにします。
関数定義
torch.cuda.empty_cache()
使用例
実例
import torch
# CUDA キャッシュをクリア
if torch.cuda.is_available():
# いくつかのテンソルを作成
x = torch.randn(1000, 1000).cuda()
y = torch.randn(1000, 1000).cuda()
# キャッシュをクリア
torch.cuda.empty_cache()
print("CUDA cache cleared")
# メモリを確認
print(f"Memory allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
else:
print("CUDA not available")
# CUDA キャッシュをクリア
if torch.cuda.is_available():
# いくつかのテンソルを作成
x = torch.randn(1000, 1000).cuda()
y = torch.randn(1000, 1000).cuda()
# キャッシュをクリア
torch.cuda.empty_cache()
print("CUDA cache cleared")
# メモリを確認
print(f"Memory allocated: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")
else:
print("CUDA not available")
その他の拡張
Pytorch torch リファレンスマニュアル