AWSにTensorflow GPUをセットアップする
AWS管理コンソールにログインし、以下の画面を表示します。
![AWS管理コンソール画面](https://spjai.com/wp-content/uploads/2019/08/console2.png)
「EC2」を選択してください。
![Launch Instance](https://spjai.com/wp-content/uploads/2019/08/console3-e1565862837899.png)
「Launch Instance」をクリックしてください。
2つの選択肢があります。
- ゼロからインスタンスを作成する (あまり簡単ではありません)
- 既存のAMIを利用する
Amazonが提供するディープラーニングのためのAMIを使いましょう。
![AMI](https://spjai.com/wp-content/uploads/2019/08/ami.png)
パブリックAMIからインスタンスを作成する
サイドバーから「Spot Requests」をクリックします。
![Spot Requests](https://spjai.com/wp-content/uploads/2019/08/spot.png)
これは安価なオプションです。
詳細は https://aws.amazon.com/ec2/spot/ を参照してください。
![](https://spjai.com/wp-content/uploads/2019/08/spot3.png)
「Request Spot Instance」をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/ami1.png)
“AMI” Selectを選択します。
![](https://spjai.com/wp-content/uploads/2019/08/ami2.png)
「Community AMIs」を選択し、あなたのリージョンのAMIコードをタイプして、「Select」をクリックします。
“Instance type”は、 SelectをクリックしてGPU Instanceを選択します。
![](https://spjai.com/wp-content/uploads/2019/08/gpu-1.png)
“GPU Instances” でフィルターをかけ、“g2.2xlarge”を選択します。
“Allocation strategy”は‘Lowest price’に、“Maximum price”は‘Use automated bidding’ にしておきます。
Nextをクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/gpu2.png)
“Create new key pair” をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/key.png)
“.pem”ファイルをダウンロードし、安全な場所に保存します。
このキー無しではインスタンスを起動することができません。
また、キーのコピーは無効となるのでオリジナルのみを保管してください。
![](https://spjai.com/wp-content/uploads/2019/08/1-1.png)
“Create new Security Group”をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/2-1.png)
セキュリティー設定を上記と同様に記入します。
Port‘8888’はJupyter notebookをホストするポートです。
他のフィールドはそのままにしておきます。
Reviewをクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/3-1.png)
最後に”Launch” をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/4-1.png)
1~2分間で、‘active’に変わるはずです。
サイドバーからInstancesをクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/5.png)
ご覧の様に、’Demo_ GPU ‘が’running’のステータスになっています。
インスタンスが完全に起動しました。
次のステップではインスタンスに接続します。
SSHを使用してインスタンスに接続する
active Instanceを選択し、“Connect”をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/6.png)
ターミナルを開きます。(Mac/Linuxの場合)
Windowsの場合はPuTTYを使用します。
.pem ファイルのあるディレクトリに移動します。
私の場合は “~/Documents/AWS” でした。
![](https://spjai.com/wp-content/uploads/2019/08/7.png)
“sudo chmod 400 demo-key.pem”と入力します。
例の中からSSHコマンドを入力します。私の場合は以下でした。
sudo ssh -i “demo-key.pem” ubuntu@ec2–54–203–11–42.us-west-2.compute.amazonaws.com
“root -> ubuntu”に置き換える事を忘れないでください。
![](https://spjai.com/wp-content/uploads/2019/08/8.png)
yes と入力します。
![](https://spjai.com/wp-content/uploads/2019/08/9.png)
SSHで仮想マシンに接続されました。
次のステップでは、Jupyter notebookにブラウザ経由でアクセスできるようにします。
Jupyter notebookにブラウザ経由でアクセスする
始めにJupyter Notebookをインストールします。
sudo pip install jupyter
“ipython”と入力します。
![](https://spjai.com/wp-content/uploads/2019/08/10.png)
from IPython.lib import passwd
passwd()
‘sha1:005e7c81ba50:75d71db1cbafb1f9fe78fc59d9a42590d716f3c5’
Sha1を安全な場所に保管します。後で必要になります。
下記の通り入力します。
jupyter notebook --generate-config
ウェブからアクセスするためには、Jupyter サーバーのための証明書を作成する必要があります。
私はCertsディレクトリに証明書を保管しようと思います。
mkdir certs
cd certs
sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout democert.pem -out democert.pem
![](https://spjai.com/wp-content/uploads/2019/08/11.png)
”Democert.pem” が取得されたはずです。
これからJupyter コンフィグレーション・ファイルを編集します。
cd ~/.jupyter/
vi jupyter_notebook_config.py
“i”を押して編集を始めます。
![](https://spjai.com/wp-content/uploads/2019/08/12.png)
# Configuration file for jupyter-notebook.
c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
# Notebook config
#location of your certificate key
c.NotebookApp.certfile = u'/home/ubuntu/certs/democert.pem' #location of your certificate file
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False #so that the ipython notebook does not opens up a browser by default
c.NotebookApp.password =
u'sha1:005e7c81ba50:75d71db1cbafb1f9fe78fc59d9a42590d716f3c5' #the encrypted password we generated above
# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 8888
編集を終了するには“esc”、ファイルを保存してvim editor を終了するには”:wq” をコマンドします。
ホームディレクトリの中に“Notebooks”フォルダーを作成し、Jupyter notebookを保存します。
cd ~
mkdir Notebooks
cd Notebooks
Type
nohup jupyter notebook
to start the server.
“nohup” ensures that the server is running even if you close ssh connection to the server
![](https://spjai.com/wp-content/uploads/2019/08/13.png)
ターミナルを直接終了することにより、接続を切断できます。
新しいターミナルインスタンス、新しいタブ、SSHサーバーを開き、Notebookディレクトリに移動します。
cat nohup.out
![](https://spjai.com/wp-content/uploads/2019/08/14.png)
The Jupyter Notebook is running at: https://[all ip addresses on your system]:8888/
あなたのパブリックIPアドレスがAWSコンソールから確認できます。
![](https://spjai.com/wp-content/uploads/2019/08/21.png)
ブラウザに以下の様に入力します。
https://ec2–54–203–11–42.us-west-2.compute.amazonaws.com:8888
![](https://spjai.com/wp-content/uploads/2019/08/15.png)
“Add Exception”をクリックし、続いて“Confirm Security Exception”をクリックします。
![](https://spjai.com/wp-content/uploads/2019/08/16.png)
パスワードを入力します。
新しいiPython notebookを作成してtensorflowをテストしましょう。
![](https://spjai.com/wp-content/uploads/2019/08/17.png)
![](https://spjai.com/wp-content/uploads/2019/08/18.png)
ご覧の様に‘tensorflow’がインポートされました。
アウトプットにCUDAライブラリが含まれているので‘nohup.out’はGPUが使用中である事を示しています
もしサーバーを終了したかったり、またはport:8888が他のサーバーによって使用中の場合
![](https://spjai.com/wp-content/uploads/2019/08/19.png)
Port 8888 already in use
既に稼働しているサーバーを終了するには
lsof nohup.out
kill -9 <PID>
![](https://spjai.com/wp-content/uploads/2019/08/20.png)
作業が終わったら、”Spot Request”のキャンセルを忘れないでください。
Spot Requests -> Select the request -> Action -> Cancel Request
原文
https://chatbotslife.com/aws-setup-for-deep-learning-fda04db6df75
チャットボットライフとの提携により、翻訳し掲載しています。
チャットボットライフとは、最新のボット、AI、NLP、ツール等を扱うメディアです。