Skip to main content

ayush-thakur の「PyTorchでGPUを使用する方法」

PyTorchでの深層学習モデルにGPUを使用するための簡単なチュートリアル。利用可能なGPUの確認から使用状況の可視化まで。
Created on August 1|Last edited on August 1

セクション

紹介

このレポートでは、GPUを使用し、より多くのコントロールを得る方法について説明します。
私たちはWeights and Biasesを使用して、GPUとCPUの利用メトリクスを自動的に記録します。これにより、計算リソースの使用状況を簡単に監視し、膨大なモデルをトレーニングすることができます。
続行する前に、まだしていない場合は、check if Pytorch is using your GPU

GPUの在庫を確認する

GPUにアクセスできるかを確認する最も簡単な方法は、呼び出すことです。torch.cuda.is_available()Trueが返された場合、それはシステムにNvidiaドライバーが正しくインストールされていることを意味します。
>>> import torch
>>> torch.cuda.is_available()


GPUを使用する際の注意点

  1. By default, the tensors are generated on the CPU. Even the model is initialized on the CPU. Thus one has to manually ensure that the operations are done using GPU.
  2. PyTorch provides a simple to use API to transfer the tensor generated on CPU to GPU. Luckily the new tensors are generated on the same device as the parent tensor.
  3. The same logic applies to the model.
  4. Thus data and the model need to be transferred to the GPU. Well, what's device?
  5. It's a common PyTorch practice to initialize a variable, usually named device that will hold the device we’re training on (CPU or GPU).


トーチCUDAパッケージ

パイトーチでは、torch.cudaこのパッケージには、CPUテンソルと同じ機能を実装するCUDAテンソルタイプの追加サポートが含まれており、計算にGPUを利用します。
  1. If you want a tensor to be on GPU you can call .cuda().
  2. If you have a tensor on GPU and you would like to bring it to CPU then you can call .cpu(). This is usually used to bring the output(tensor) of the model to the CPU.
  3. To get the index of the currently selected device.
  4. To get the number of GPUs available.
  5. To get the name of the device.


例とGPUメトリクスの可視化


リンクされたColabノートブックを試して、PyTorchを使用してシンプルなMNIST分類器をトレーニングしてください。ノートブックは統合されています。Weights and Biases
もしWeights & Biasesを使用してモデルを追跡している場合、GPU利用率を含むすべてのシステムメトリクスが自動的にログに記録されます。記録される最も重要なメトリクスの一部は、GPUメモリの割り当て、GPU利用率、CPU利用率などです。ログに記録されたメトリクスの全リストを見ることができます。here
以下に示すメディアパネルには、トレーニング中にW&Bによって自動的に記録されたこれらのシステムメトリクスの一部が表示されています。



Run set
0


要約

この記事では、Kerasを使用してディープラーニング研究のためにGPUを活用する方法と、Weights and Biasesを使用してリソースの消費を監視する方法について説明しました。Lambda Labsの素晴らしい記事をチェックしてください。tracking system resource utilizationWeights & Biasesを使用したトレーニング中。

重みとバイアスを試す

Weights & Biasesは、機械学習の実験を追跡するのに役立ちます。ツールを使用して、ハイパーパラメータと実行結果のメトリクスを記録し、結果を視覚化して比較し、同僚と迅速に結果を共有してください。
Get started5分でReplitで2つの簡単な実験を実行し、W&Bがどのように作業を整理するのに役立つかを確認してください。以下の指示に従ってください。
指示:
  1. Click the green "Run" button below (the first time you click Run, Replit will take approx 30-45 seconds to allocate a machine)
  2. Follow the prompts in the terminal window (the bottom right pane below)
  3. You can resize the terminal window (bottom right) for a larger view


PyTorchに興味がある人のためのおすすめの読み物