Vengineerの戯言

人生は短いけど、長いです。人生を楽しみましょう!

AutoGraph


今日は、「fpgax #11 + TFUGハード部」ですね。の発表の時に質問されて、答えた「AutoGraph」。

名前と何をするのかは知っていましたが、詳細は知らなかったので調べて見ました。


AutoGraph converts Python into TensorFlow graphs <= これ、下記のコートとはかなり違うけど。

公式ドキュメント:AutoGraph: Easy control flow for graphs

サポートしている Python 構文 は、こちら

  Pythonコード

  def f(x):
    if x < 0:
      x = -x
    return x

 を AutoGraph を使って変換

  with tf.Graph().as_default():
  x = tf.constant(-1.0)

  converted_f = autograph.to_graph(f)
  y = converted_f(x)

  with tf.Session() as sess:
    print(sess.run(y))
    # Output: 1

  autograph.to_graph(f)の結果の converted_f は、

  def graph_mode_f(x):
    with tf.name_scope('f'):

      def if_true():
        with tf.name_scope('if_true'):
          x_1, = x,
          x_1 = tf.negative(x_1)
          return x_1,

      def if_false():
        with tf.name_scope('if_false'):
          x_1, = x,
          return x_1,
      x = ag__.utils.run_cond(tf.greater(x, 0), if_true, if_false)
      return x

as__.utils.run_condというメソッドが、でも、このメソッドが見つからないよ。。。

でも、to_graphメソッド はあったよ。

autograph.to_graph
autograph.to_codeというのもある模様。