CPUとGPUとMPS:デバイスを選ぶ
CUDAやApple MPSを検出し、利用できなければCPUに切り替えます
「CPUとGPUとMPS:デバイスを選ぶ」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Where Math Happens
Every tensor lives on a device: the CPU, a GPU, or Apple's MPS. The device decides how fast your model trains. ⚡
The CPU Always Works
Your CPU is the safe default. It runs every PyTorch operation, just more slowly on the heavy matrix math that big networks demand.
The GPU Goes Fast
A GPU does thousands of multiplications in parallel, making it far faster for deep learning. PyTorch reaches NVIDIA GPUs through CUDA.
Apple Silicon Has MPS
On a Mac with Apple Silicon, MPS taps the built-in GPU. It is the fast path for M-series chips without any NVIDIA hardware.
Detect CUDA
Ask PyTorch whether an NVIDIA GPU is available before you use one. cuda.is_available returns True or False so you never assume.
torch.cuda.is_available()Detect MPS
On a Mac, check the Apple backend the same way. mps.is_available tells you if the M-series GPU is ready to use.
torch.backends.mps.is_available()Pick the Best Device
A clean pattern tries CUDA first, then MPS, then falls back to CPU. This device string adapts to whatever machine runs your code.
device = 'cuda' if torch.cuda.is_available() else 'cpu'Move a Tensor
Send data to the chosen device with .to(device). The tensor's numbers are unchanged, but its math now runs there.
x = torch.tensor([1.0, 2.0]).to(device)Keep Everything Together
Your model and your data must share one device. Mixing CPU and GPU tensors throws a device mismatch error during the forward pass.
Check Where a Tensor Lives
Not sure where data sits? Read the .device attribute. It prints cpu, cuda, or mps so you can confirm placement at a glance.
print(x.device)When to Use Which
Use the GPU for real training, and the CPU for quick tests or tiny data. The fastest device is the one your hardware actually has.
Quick Check
Pick the right call for a Mac with Apple Silicon.
Recap
You learned to detect CUDA, MPS, or CPU, pick the best one, and move tensors there. Same code, fastest hardware available. 🚀
よくある質問
「CPUとGPUとMPS:デバイスを選ぶ」レッスンは無料ですか?
はい。「CPUとGPUとMPS:デバイスを選ぶ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「CPUとGPUとMPS:デバイスを選ぶ」で何を学びますか?
CUDAやApple MPSを検出し、利用できなければCPUに切り替えます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「CPUとGPUとMPS:デバイスを選ぶ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- PyTorchをインストールしてインポートを確認する
- CPUとGPUとMPS:デバイスを選ぶ
- ノートブック、スクリプト、再現可能な乱数シード
- はじめてのtorch.tensor