LoginSignup
6
6

More than 5 years have passed since last update.

クラウド統合開発環境Cloud9でTensorFlowを使う〜使い方の基本〜

Last updated at Posted at 2016-10-19

はじめに

Cloud9でTensorFlowのBasic Usageを確認しました。TensorFlowを使う上での基本的な考え方と使い方をまとめます。
GPUとかInteractiveUsageとかは入ってません。まずは基本的なコードができることを目的として、その他は必要に応じて勉強していこうと思っています。

環境

Cloud9
Python 2.7.6
Sample Codes : GitHub
環境構築は「クラウド統合開発環境Cloud9でTensorFlowを使う」を参照

TensorFlowの考え方

重要な考え方は以下の2点です。
- Graphs
- Sessions

Graphsは、値や計算を設定するクラスです。値は定数や変数、テンソル(2次元だと行列。多次元配列で定義します)などを持てます。その値の計算(例えば足し算や掛け算)を設定するのもGraphsになります。

Sessionsは、Graphsの値や計算を実行するクラスになります。TensorFlowはGPUを使えるようですが、Sessionsを介して自動的に使用してくれるようです。

そのためGraphsで値や計算を設定 => Sessionsで計算の実行
という流れになります。
以下で具体的な例を見ていきます。
GitHubのbasic_usage.py
の実行結果を順に解説していきます。

具体例

constant(定数)とSessions

import tensorflow as tf

# Graphs
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]])
product = tf.matmul(matrix1, matrix2)

# Sessions
sess = tf.Session()
result = sess.run(product)
print(result)
# Output: [[12.]]
sess.close()
  1. tf.constantは、定数を設定
  2. matrix1に1×2の行列、matrix2に2×1の行列を設定
  3. tf.matmulで行列の掛け算(ここまでがGraphsの値や計算を設定しているところ)
  4. tf.Session()でセッションを作成
  5. runで計算を実行
  6. close()でセッションをクローズ
  7. printで行列の計算結果を出力

Graphsを設定してから、Sessionsで計算を実行している流れが重要です。

Variable(変数)

# Graphs
state = tf.Variable(0)
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)

init_op = tf.initialize_all_variables()

# Sessions
with tf.Session() as sess:
  sess.run(init_op)
  print(sess.run(state))
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))
# Output: 0 1 2 3
  1. tf.Variableで変数を設定
  2. tf.addで足し算
  3. tf.assignで足し算の結果をstateに入れ直し
  4. tf.initialize_all_variables()で初期化(変数は初期化が必要!)
  5. Session()でセッションを作成
  6. run(init_op)で変数の初期化
  7. ループでrun(update)して、結果のstateを出力

変数が順に更新されています。

複数同時にrun

# Graphs
input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

# Sessions
with tf.Session() as sess:
  result = sess.run([mul, intermed])
  print(result)
# Output: [array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

コードの内容は今までと変わらないので理解できると思います。
重要なのは、run([mul, intermed])で複数の計算を同時に実行し、結果の出力も同時に出力されているところです。

placeholder(代入)

# Graphs
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

# Sessions
with tf.Session() as sess:
  print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# Output: [array([ 14.], dtype=float32)]

tf.placeholder(tf.float32)で値を代入できるようにしています。
run([output], feed_dict={input1:[7.], input2:[2.]})で、代入して計算しています。

おわりに

Graphsを設定してから、Sessionsで計算を実行している流れが重要だと思います。簡単なコードだと特に問題ないのですが、複雑な計算になってくるとどうなるかが未知数です。
サンプルコードを変更して色々と試してみるとさらに理解が深まると思います。

まだ勉強中なので間違いがあったらコメントにご指摘ください。
今後もTensorFlowの使い方を勉強していって、加筆・修正していきたいと思います。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6