TensorFlow : (r1.0) Programmer’s Guide : 変数を共有する (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
更新日時 : 09/10/2017
作成日時 : 03/04/2016
* 本家サイトのドキュメント構成の変更に伴い、本ページは以下のページをベースにするよう変更しました (09/10/2017) :
* (obsolete) 本ページは、TensorFlow の本家サイトの How To – Sharing Variables を翻訳した上で
適宜、補足説明したものです:
https://www.tensorflow.org/versions/master/how_tos/variable_scope/index.html#sharing-variables
* サンプルコードの動作確認はしておりますが、適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
変数 HowTo に記述されている方法で 単一の変数を作成し、初期化し、保存してすることができます。しかし複雑なモデルを組み立てる時には変数の大規模なセットを共有する必要が良くあり、それら全てを一ヶ所で初期化することを望むかもしれません。このチュートリアルでは tf.variable_scope() と the tf.get_variable() を使用してこれがどのようになされるかを示します。
The Problem
画像フィルタのための単純なモデルを作成することを考えてください、畳み込みニューラルネットワーク・チュートリアル モデルと類似していますが、(この例の単純化のために)2つの畳み込みだけです。変数 HowTo で説明されているように、もし tf.Variable を使用するのであれば、モデルはこのようなものになるでしょう。
def my_image_filter(input_images): conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="conv1_weights") conv1_biases = tf.Variable(tf.zeros([32]), name="conv1_biases") conv1 = tf.nn.conv2d(input_images, conv1_weights, strides=[1, 1, 1, 1], padding='SAME') relu1 = tf.nn.relu(conv1 + conv1_biases) conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]), name="conv2_weights") conv2_biases = tf.Variable(tf.zeros([32]), name="conv2_biases") conv2 = tf.nn.conv2d(relu1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME') return tf.nn.relu(conv2 + conv2_biases)
簡単に想像できますように、迅速にモデルはこれよりも非常に複雑になるでしょう、そしてこの時点でさえも既に 4 つの異なる変数を持ちます : conv1_weights、conv1_biases、conv2_weights そして conv2_biases です。
このモデルを再利用したい時には問題が起きます。貴方の画像フィルタを 2 つの異なる画像、image1 と image2 に適用したいと仮定します。同じパラメータで同じフィルタにより両方の画像が処理されることを望むでしょう。my_image_filter() を二度呼び出すことができますが、これは変数の 2 セットを作成します。
# 最初の呼び出しが変数の 1 セットを作成します。 # First call creates one set of variables. result1 = my_image_filter(image1) # 2 度目の呼び出しで他のセットが作成されます。 result2 = my_image_filter(image2)
A common way to share variables is to create them in a separate piece of code and pass them to functions that use them. For example by using a dictionary:
variables_dict = { "conv1_weights": tf.Variable(tf.random_normal([5, 5, 32, 32]), name="conv1_weights") "conv1_biases": tf.Variable(tf.zeros([32]), name="conv1_biases") ... etc. ... } def my_image_filter(input_images, variables_dict): conv1 = tf.nn.conv2d(input_images, variables_dict["conv1_weights"], strides=[1, 1, 1, 1], padding='SAME') relu1 = tf.nn.relu(conv1 + variables_dict["conv1_biases"]) conv2 = tf.nn.conv2d(relu1, variables_dict["conv2_weights"], strides=[1, 1, 1, 1], padding='SAME') return tf.nn.relu(conv2 + variables_dict["conv2_biases"]) # The 2 calls to my_image_filter() now use the same variables result1 = my_image_filter(image1, variables_dict) result2 = my_image_filter(image2, variables_dict)
While convenient, creating variables like above, outside of the code, breaks encapsulation:
- The code that builds the graph must document the names, types, and shapes of variables to create.
- When the code changes, the callers may have to create more, or less, or different variables.
One way to address the problem is to use classes to create a model, where the classes take care of managing the variables they need. For a lighter solution, not involving classes, TensorFlow provides a Variable Scope mechanism that allows to easily share named variables while constructing a graph.
Variable Scope Example
以上