インストール後に実行してみましょう。
https://www.tensorflow.org/versions/r0.10/get_started/index.html
動作確認
動作確認用のPythonファイルを作成します。1 2 | (tensorflow)$ touch tf_test.py (tensorflow)$ vi tf_test.py |
サンプルソースはy = x * a + bの、aとbの値を求めるプログラムです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import tensorflow as tf import numpy as np # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 x_data = np.random.rand( 100 ).astype(np.float32) y_data = x_data * 0.1 + 0.3 # Try to find values for W and b that compute y_data = W * x_data + b # (We know that W should be 0.1 and b 0.3, but TensorFlow will # figure that out for us.) W = tf.Variable(tf.random_uniform([ 1 ], - 1.0 , 1.0 )) b = tf.Variable(tf.zeros([ 1 ])) y = W * x_data + b # Minimize the mean squared errors. loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer( 0.5 ) train = optimizer.minimize(loss) # Before starting, initialize the variables. We will 'run' this first. init = tf.initialize_all_variables() # Launch the graph. sess = tf.Session() sess.run(init) # Fit the line. for step in range ( 201 ): sess.run(train) if step % 20 = = 0 : print (step, sess.run(W), sess.run(b)) # Learns best fit is W: [0.1], b: [0.3] |
プログラムの実行。
1 | (tensorflow)$ python tf_test.py |
1 2 3 4 5 6 7 8 9 10 11 | (0, array([-0.08499005], dtype=float32), array([ 0.5096584], dtype=float32)) (20, array([ 0.03381674], dtype=float32), array([ 0.33253124], dtype=float32)) (40, array([ 0.08111919], dtype=float32), array([ 0.30928054], dtype=float32)) (60, array([ 0.09461367], dtype=float32), array([ 0.30264756], dtype=float32)) (80, array([ 0.09846338], dtype=float32), array([ 0.30075532], dtype=float32)) (100, array([ 0.09956162], dtype=float32), array([ 0.30021548], dtype=float32)) (120, array([ 0.09987493], dtype=float32), array([ 0.30006149], dtype=float32)) (140, array([ 0.09996434], dtype=float32), array([ 0.30001754], dtype=float32)) (160, array([ 0.09998985], dtype=float32), array([ 0.30000502], dtype=float32)) (180, array([ 0.09999709], dtype=float32), array([ 0.30000144], dtype=float32)) (200, array([ 0.09999915], dtype=float32), array([ 0.30000043], dtype=float32)) |
0.1 と 0.3の近似値が算出されている!!