アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【PythonWindowsGoogle
TensorFlow公式のチュートリアルを進める(3)
POSTED BY
2023-01-12
Google TensorFlowバージョン2 の公式チュートリアルをすすめるメモ(1)
Google TensorFlowバージョン2 の公式チュートリアルをすすめるメモ(2)
【Windows10】Python統合開発環境Anaconda3メモ
【Anaconda3】指定した仮想環境でJupyter Notebookを動かす
【Windows10】NVIDIAグラフィックカードのGPUをTensorFlowに認識させて動かす

続きです。TensorFlowでGPUを認識した集大成として、TensorFlow公式チュートリアルサンプル

テンソルと演算
https://www.tensorflow.org/tutorials/customization/basics?hl=ja

をすすめてみます。Anaconda3 64-bit Jupyter Notebook(64bit)を起動し、前回のGPU仮想環境カーネルNew→Environment「conda_tf-gpu」にて新規作成します。

★出現したエラー:なし

無事エラー無く実行できました。Jupyter Notebook(Anaconda3)コンソールログではGPU利用の痕跡が見えます。本記事の最後に貼付します。

上記チュートリアルの「GPUによる高速化」「明示的デバイス配置」にて、GPUのチェックと同一の処理をCPUとGPUで明示的に指定して行ってテストできています。

GPUのチェック

x = tf.random.uniform([3, 3])

print("利用できるGPUはあるか: "),
print(tf.config.experimental.list_physical_devices("GPU"))

print("テンソルはGPU #0にあるか:  "),
print(x.device.endswith('GPU:0'))

結果

利用できるGPUはあるか: 
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
テンソルはGPU #0にあるか:  
True

CPUとGPUの比較

import time

def time_matmul(x):
  start = time.time()
  for loop in range(10):
    tf.matmul(x, x)

  result = time.time()-start

  print("10 loops: {:0.2f}ms".format(1000*result))

# CPUでの実行を強制
print("On CPU:")
with tf.device("CPU:0"):
  x = tf.random.uniform([1000, 1000])
  assert x.device.endswith("CPU:0")
  time_matmul(x)

# GPU #0があればその上での実行を強制
if tf.config.experimental.list_physical_devices("GPU"):
  print("On GPU:")
  with tf.device("GPU:0"): # 2番めのGPUなら GPU:1, 3番目なら GPU:2 など
    x = tf.random.uniform([1000, 1000])
    assert x.device.endswith("GPU:0")
    time_matmul(x)

結果

On CPU:
10 loops: 49.87ms
On GPU:
10 loops: 1.00ms

初回の実行こそライブラリのロードなどでGPUは時間がかかりCPUより遅いようでしたが、2度目以降はCPUよりはるかに速いという結果が得られました。

以下、Jupyter Notebookコンソールのログです。

[I 13:58:00.211 NotebookApp] Creating new notebook in
[I 13:58:03.410 NotebookApp] Kernel started: e0dfc74e-0ef6-4c37-b3a7-ffbf06a3d37d, name: conda_tf-gpu
[I 14:00:02.993 NotebookApp] Saving file at /Untitled4.ipynb
2021-01-12 14:54:44.132264: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-01-12 14:54:58.697013: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-01-12 14:54:58.725958: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.455GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2021-01-12 14:54:58.726091: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-01-12 14:54:59.476740: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-01-12 14:54:59.956291: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-01-12 14:55:00.030618: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-01-12 14:55:00.345922: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-01-12 14:55:00.654046: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-01-12 14:55:01.215301: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-01-12 14:55:01.215529: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2021-01-12 14:55:01.217412: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2021-01-12 14:55:01.224931: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1dd92508e20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-01-12 14:55:01.225021: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2021-01-12 14:55:01.225609: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 Ti computeCapability: 6.1
coreClock: 1.455GHz coreCount: 6 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2021-01-12 14:55:01.226249: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-01-12 14:55:01.226679: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-01-12 14:55:01.227056: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-01-12 14:55:01.227449: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-01-12 14:55:01.227813: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-01-12 14:55:01.228172: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-01-12 14:55:01.228506: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-01-12 14:55:01.228872: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2021-01-12 14:55:01.675882: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-01-12 14:55:01.675991: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      0
2021-01-12 14:55:01.676978: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0:   N
2021-01-12 14:55:01.677505: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2988 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
2021-01-12 14:55:01.680267: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x1dda93e8fe0 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2021-01-12 14:55:01.680383: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce GTX 1050 Ti, Compute Capability 6.1
[I 14:56:02.981 NotebookApp] Saving file at /Untitled4.ipynb
2021-01-12 14:56:56.632067: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
[I 14:58:02.977 NotebookApp] Saving file at /Untitled4.ipynb
[I 15:00:03.824 NotebookApp] Saving file at /Untitled4.ipynb
※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】