TensorFlowを使って算数②
上記の続き。次はTensorBoardを使ってグラフの可視化も行います。
実行環境は同じPython3です。
途中までしか書いていません。修正します。
コード
import tensorflow as tf def x2_plus_b(x, b): # 関数の第一引数 x を出力する _x という名前の定数型のopを定義 _x = tf.constant(x) # 関数の第二引数 b を出力する _b という名前の定数型のopを定義 _b = tf.constant(b) # resultに_xの2乗を代入 result = tf.square(_x) # resultに_bを加算 result = tf.add(result, _b) # resultを返す return result def monitor_calculation(x, b): # Graph のタイトルを付ける。 title = "b = {0}".format(b) # c = x^2 + b c = x2_plus_b(float(x), float(b)) # TensorBoardで表示する。第一引数でtag付け。第二引数は値 s = tf.scalar_summary(title, c) m = tf.merge_summary([s]) # if you are using some summaries, merge them return m if __name__ == '__main__': # Session オブジェクトを作成し、sessという別名をつける with tf.Session() as sess: # 計算したsummaryを、tf.train.SummaryWriterで書き出す writer に代入 # その際に、フォルダの直下に "log" ファイルが作成され、出力される。 writer = tf.train.SummaryWriter("log", graph_def=sess.graph) # -10 から 11までの数字 xaxis = range(-10, 12) # 三回ループする for b in range(3): for x in xaxis: # monitor_calculation を実行し、summary_str に代入 summary_str = sess.run(monitor_calculation(x, b)) writer.add_summary(summary_str, x)
実行結果
WARNING:tensorflow:When passing a `Graph` object, please use the `graph` named argument instead of `graph_def`.
???
どうやらgraph_defというのは非推奨らしい。 直します。
writer = tf.train.SummaryWriter("log", graph=sess.graph)
TensorBoardを実行
tensorboard --logdir=/path/to/log-directory
グラフができました。
qiitaの記事だとGRAPHを押すと、データフローが可視化されるはずですがエラーが。なぜ