TensorFlow Core tutorial で学んでみた(1) [AI]
TensorFlow の Getting Started With TensorFlow の最初の章、"TensorFlow Core Tutorial" で TensorFlow の基本を学んでみました。
https://www.tensorflow.org/get_started/get_started
まず最初に定数の簡単な例から。コメントが英語なのはご容赦を。
こちらを実行した結果がこちらです。特に説明の必要もなさそうですね。
次に、いわゆる関数を定義します。CやJavaなどと違って行列を変数として与えられることができます。これは便利ですね。
こちらの実行結果がこちらです。
最後に初期値付きの変数を使ったサンプルです。少し小難しいですが差分二乗和を変数と行列でそれぞれで算出しています。reduce_sum という TensorFlow の埋め込み関数も使っています。
こちらを実行してみた結果がこちらです。
ちょっと独特ですが、そんなに難しくなさそうです。次は同じ Core tutorial にある trainAPI について学習してみたいと思います。
(^^)/~
https://www.tensorflow.org/get_started/get_started
まず最初に定数の簡単な例から。コメントが英語なのはご容赦を。
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf # define constans node1 = tf.constant(3.0, dtype=tf.float32) node2 = tf.constant(4.0) # execute add sess = tf.Session() node3 = tf.add(node1, node2) print("node3:", node3) print("sess.run(node3):", sess.run(node3))
こちらを実行した結果がこちらです。特に説明の必要もなさそうですね。
C:\Users\Taro\TensorFlow>python constant.py node3: Tensor("Add:0", shape=(), dtype=float32) sess.run(node3): 7.0
次に、いわゆる関数を定義します。CやJavaなどと違って行列を変数として与えられることができます。これは便利ですね。
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf # set variables a and b a = tf.placeholder(tf.float32) b = tf.placeholder(tf.float32) # define formula adder_node = a + b # create execution instance sess = tf.Session() # execute '3'+'4.5' print(sess.run(adder_node, {a: 3, b: 4.5})) # execute matrix operation [1, 3] + [2, 4] print(sess.run(adder_node, {a: [1, 3], b: [2, 4]})) # define formula add_and_triple = adder_node * 3.0 # execute operation print(sess.run(add_and_triple, {a: 3, b: 4.5})) # execute matrix operation print(sess.run(add_and_triple, {a: [1, 3], b: [2, 4]}))
こちらの実行結果がこちらです。
C:\Users\Taro\TensorFlow>python placeholder.py 7.5 [ 3. 7.] 22.5 [ 9. 21.]
最後に初期値付きの変数を使ったサンプルです。少し小難しいですが差分二乗和を変数と行列でそれぞれで算出しています。reduce_sum という TensorFlow の埋め込み関数も使っています。
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf # define variables with initial value W = tf.Variable([0.3], dtype=tf.float32) b = tf.Variable([-0.3], dtype=tf.float32) # define variable x = tf.placeholder(tf.float32) # define formula linear_model = W * x + b # create exection instance sess = tf.Session() # initialize variables init = tf.global_variables_initializer() sess.run(init) # execute linear_model print(sess.run(linear_model, {x: [1, 2, 3, 4]})) # define complex formura: # squared_deltas = (W * x + b) - y) * ((W * x + b) - y ) # loss = SUM(squared_deltas) y = tf.placeholder(tf.float32) squared_deltas = tf.square(linear_model - y) loss = tf.reduce_sum(squared_deltas) # I don't know why "reduce"? # calculate each element [1, 2, 3, 4] by squared_deltas print(sess.run(squared_deltas, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})) # calculate each element [1, 2, 3, 4] by loss print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})) # replace values of W, b fixW = tf.assign(W, [-1.0]) fixb = tf.assign(b, [1.0]) sess.run([fixW, fixb]) # reflect the changes print(sess.run(squared_deltas, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})) print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
こちらを実行してみた結果がこちらです。
C:\Users\Taro\TensorFlow>python variable.py [ 0. 0.30000001 0.60000002 0.90000004] [ 0. 1.68999982 6.75999928 15.21000099] 23.66 [ 0. 0. 0. 0.] 0.0
ちょっと独特ですが、そんなに難しくなさそうです。次は同じ Core tutorial にある trainAPI について学習してみたいと思います。
(^^)/~
ゼロから作るDeep Learning ―Pythonで学ぶディープラーニングの理論と実装
- 作者: 斎藤 康毅
- 出版社/メーカー: オライリージャパン
- 発売日: 2016/09/24
- メディア: 単行本(ソフトカバー)
TensorFlow機械学習クックブック Pythonベースの活用レシピ60+ (impress top gear)
- 作者: Nick McClure
- 出版社/メーカー: インプレス
- 発売日: 2017/08/14
- メディア: 単行本(ソフトカバー)
Machine Learning With Tensorflow
- 作者: Nishant Shukla
- 出版社/メーカー: Manning Pubns Co
- 発売日: 2017/09/30
- メディア: ペーパーバック
コメント 0