Kuroyagi飼育日誌

学んだことの備忘録

深層学習の学習【その1】[追記]

ずっと放置してきたPythonと深層学習を始めようかと思います。

きっかけは先日のInterfaceできゅうりの選別システムを深層学習で作ろうといった記事でやっぱり便利だ!使えるようにしたい!と思ったのがきっかけです。

Interface(インターフェース) 2017年 03 月号 | |本 | 通販 | Amazon

さて、とは言うもののtensorflowはインストールやらなんやらやってみたはものの、いろいろと初歩的なことでひっかかっていたので頓挫していました。前にやったときのことも記録に残していないという…

そこで、今回は開発環境の構築から実際のサンプルの実行までをひとまずやっていきます。


OS: Ubuntu 16.04 LTS

基本的には以下の手順通り進めればインストールはできると思います。

【記事1】
Installing TensorFlow on Ubuntu  |  TensorFlow

こちらの記事もいいかもしれません。

【記事2】
TensorFlowで Hello Worldを動かしてみた&その解説 | Developers.IO


簡単な流れは以下の通りです。

Pythonのバージョン確認
pipのインストール
Virtualenvのインストール
Tensorflowのインストール

以下参考記事です。

【記事3】
Python: Python基礎講座(1 Pythonとは) - Qiita
【記事4】
virturalenv:今日のPython: VIRTUALENV について



さてインストールできたところで、ちゃんとインストールできているか?使えるかを確認します。【記事2】にあるサンプルコードを実行しました。

ソースコード1】

# hello-tf.py
import tensorflow as tf
import multiprocessing as mp
 
core_num = mp.cpu_count()
config = tf.ConfigProto(
    inter_op_parallelism_threads=core_num,
    intra_op_parallelism_threads=core_num )
sess = tf.Session(config=config)
 
hello = tf.constant('hello, tensorflow!')
print sess.run(hello)
 
a = tf.constant(10)
b = tf.constant(32)
print sess.run(a+b)

するとこんなエラーが出ました。

  File "/home/******/Documents/error.py", line 12
    print sess.run(hello)
             ^
SyntaxError: invalid syntax

表記がおかしいのかもしれないのでちょっと違うソースを書いて試してみます。参考としたのは以下の【記事5】です。似たようなことをやっているのですが、表記法が違っていたので試してみました。

【記事5】
TensorFlowのキーコンセプト: Opノード、セッション、変数 - Qiita

内容をまとめたソースコードは以下の通りです。

ソースコード2】

import tensorflow as tf 
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
result = sess.run(hello)
print(result)

結果は以下の通り成功しました。

b'Hello, TensorFlow!'

ということで、エラーに出ていたsessの問題点はprint文のところの表記にあるようです。試しに先のエラーが出た【ソースコード1】のprint文を訂正した【ソースコード3】実行すると…

ソースコード3】

# hello-tf.py
import tensorflow as tf
import multiprocessing as mp
 
core_num = mp.cpu_count()
config = tf.ConfigProto(
    inter_op_parallelism_threads=core_num,
    intra_op_parallelism_threads=core_num )
sess = tf.Session(config=config)
 
hello = tf.constant('hello, tensorflow!')
result1 = sess.run(hello)
print(result1)
 
a = tf.constant(10)
b = tf.constant(32)
result2 = sess.run(a+b)
print(result2)

結果は以下の通り成功です。

b'hello, tensorflow!'
42

ただ、この結果が出るときにこんな表記が出てきてしまうのですが、これは正常か否か確認したいと思います。

W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
b'hello, tensorflow!'
42

[追記2017.02.27]
【記事6】
Tensorflow 1.0でGPU利用 with AWS - Qiita

記事6ではW〜というログが出ている状態を正常と言っているので、先の実行結果は問題ないようです。

ということでまずは第一歩が終わりです。