TensorFlow2:TensorBoardのグラフがうまく表示されず困った件(余談)
前回の記事のおまけ。
余談と言いつつ、重要そうなことも入っている気がする。
余談:モデルがmain graphに表示されない件
TensorBoardでは、グラフが複雑になるのを避けるため、入出力が多いノード(原文では"high-degree nodes")は、Auxiliary Nodes
側に配置される。
"入出力が多いノードは、だいたい保存とかそんな感じのノードなので、外しても問題ないっしょ。メイングラフには〇も置いとくし"(超意訳)らしい。モデルなんだがなぁ…。
Reference: TensorBoard: Graph Visualization
グラフを見ていると、レイヤ内の各weightの定義が、ただのVariableではなく、ReadVariableOpになっていることが原因のようだ。 しかしweightの定義はkeras内でやっているので、手が出せない。
調べてみると、issueが上がっていたが解決していない(放置されている?)。
[TF 2.0] Inconvenient graph visualization #29215
この件は、今のところ手動でmain graphに移動させることで回避。
余談:test側のグラフ出力がエラーになる
前回の変更2では@tf.function
を付与する関数をtrainのみにして対処したが、testのみにした場合はうまく動かない。
この場合、最初と同じでGraph visualization failed. Error: Malformed GraphDef.
が表示されてしまう。
TensorBoardのコンソールには以下のエラーが表示される。
ValueError: Cannot combine GraphDefs because nodes share a name but contents are different: images
よく見てみると、今度は入力であるimages
がエラーの原因となっている。
いやいやテスト中はループでぐるぐる回してるだけで、処理は変わってないし! とりあえず本筋でないから放置するか。。。
と思ってぼーっとコード見ていたら、なんとなくバッチが気になってきた。
結果、データ読み込みの部分をこんな感じにすると、うまく動いた。
def load_dataset(batch_size=32):
(x_train, y_train), (x_test, y_test) = tfk.datasets.mnist.load_data()
# normalize 0.0 ~ 1.0
x_train, x_test = x_train / 255.0, x_test / 255.0
# add newaxis for channel.
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]
# build pipeline: shuffle -> batch.
""" build new graph if remainder is exist, so avoiding this, add `drop_remainder=True` to `batch`.
ds_train = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(batch_size)
ds_test = tf.data.Dataset.from_tensor_slices(
(x_test, y_test)).batch(batch_size)
"""
ds_train = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(batch_size, drop_remainder=True)
ds_test = tf.data.Dataset.from_tensor_slices(
(x_test, y_test)).batch(batch_size, drop_remainder=True)
# return dataset.
return ds_train, ds_test
batch(batch_size, drop_remainder=True)
の、drop_remainder
がポイント。
train用のデータは60000 % 32 == 0
で、test用のデータは10000 % 32 == 16
で、後者は端数が出る。
ということで、入力画像のバッチサイズ部分に変化があると、別グラフとして扱われるようだ…。
なんかハマりそうなので、デフォルトでTrueにしておくのが安全かなー。
コメント
コメントを投稿