現在、tensorflowの勉強を進めており、一通りのコードの作法(session, runの役割など)等は学んだので、より深くステップごとに関数(及びメソッド)がどういう役割を果たしているかを見ていくことにしました。
今回質問させていただきたいのは以下のような線形問題です。
import tensorflow as tf import numpy as np # 乱数を生成。(あとで代入する) np.random.seed(1337) xdata = np.linspace(-1, 1, 1000) ydata = 2 * xdata + 0.3 * np.random.randn(1000) # グラフを作成 with tf.Graph().as_default() as Linearregression: with tf.name_scope('multiplication') as scope: w = tf.Variable(0.1, name="w") x = tf.placeholder("float", name="x") #実際のデータを入れる変数をtf.placeholderで宣言 y = tf.placeholder("float", name="y") ymodel = tf.multiply(x, w, name="multiplication") # プロセスを作成 with tf.name_scope('objective') as scope: objective = tf.reduce_mean(tf.square(y - ymodel), name="objective") #平均 with tf.name_scope('optimizer') as scope: optimizer = tf.train.GradientDescentOptimizer(0.5) #勾配降下法で最適化する。引数は学習率 training = optimizer.minimize(objective) # 実行 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(50): sess.run(training, feed_dict={x: xdata, y: ydata}) obj, w_ = sess.run([objective, w], feed_dict={x: xdata, y: ydata}) print("objective: %f, Gradient: %f" % (obj, w_)) # evaluation w1 = sess.run(w) w2 = sum(ydata * xdata) / sum(xdata**2) # analytic solution print("Fitted value: %f" % w1) print("Analytic solution: %f" % w2) tf.summary.FileWriter('./Linearregression', sess.graph)
この出力は以下のようになっています。
objective: 0.620617, Gradient: 0.734022 objective: 0.323566, Gradient: 1.156280 objective: 0.191808, Gradient: 1.437504 objective: 0.133365, Gradient: 1.624798 objective: 0.107443, Gradient: 1.749537 objective: 0.095945, Gradient: 1.832612 objective: 0.090845, Gradient: 1.887940 objective: 0.088583, Gradient: 1.924789 objective: 0.087580, Gradient: 1.949330 objective: 0.087135, Gradient: 1.965674 objective: 0.086937, Gradient: 1.976560 objective: 0.086850, Gradient: 1.983809 objective: 0.086811, Gradient: 1.988638 objective: 0.086794, Gradient: 1.991853 objective: 0.086786, Gradient: 1.993995 objective: 0.086783, Gradient: 1.995421 objective: 0.086781, Gradient: 1.996371 objective: 0.086780, Gradient: 1.997004 objective: 0.086780, Gradient: 1.997425 objective: 0.086780, Gradient: 1.997706 objective: 0.086780, Gradient: 1.997893 objective: 0.086780, Gradient: 1.998017 objective: 0.086780, Gradient: 1.998100 objective: 0.086780, Gradient: 1.998155 objective: 0.086780, Gradient: 1.998192 objective: 0.086780, Gradient: 1.998216 objective: 0.086780, Gradient: 1.998233 objective: 0.086780, Gradient: 1.998244 objective: 0.086780, Gradient: 1.998251 objective: 0.086780, Gradient: 1.998256 objective: 0.086780, Gradient: 1.998259 objective: 0.086780, Gradient: 1.998261 objective: 0.086780, Gradient: 1.998262 objective: 0.086780, Gradient: 1.998263 objective: 0.086780, Gradient: 1.998264 objective: 0.086780, Gradient: 1.998264 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 objective: 0.086780, Gradient: 1.998265 Fitted value: 1.998265 Analytic solution: 1.998265
私の理解が正しければ、1ステップでやっていることは、以下のようにまとめられると思います。
- すべての変数を初期化:
sess.run(tf.global_variables_initializer())
xdata
,ydata
の値をすべてx
,y
に入れる.x
,w
の積を取ってymodel = wx
を計算:ymodel = tf.multiply(x, w, name="multiplication")
ymodel
とy
の関係からobjective(損失関数を計算)を計算:objective = tf.reduce_mean(tf.square(y - ymodel), name="objective")
- 損失関数が最小となるような
(W,b)
の組をGradient Decentによって見つける。optimizer.minimize(objective)
- objectiveを出力する。(実際はsess.runの実行後に実際の値を計算することで値が出て、それを出力している。)
ただ、2ステップ以降で、wがどのように最適化されて行くのかがわかりません。xとwの積を取ったあと、ymodelとydataを比較して最適化していくのでしょうが、wはここでどのように関わってくるのでしょうか?
以上何卒よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。