2016-09-15 9 views
0

Python APIで変数を共有するTensorFlowに問題があります。TensorFlow、Python、変数を共有する、最上部で初期化する

私は公式の文書(https://www.tensorflow.org/versions/r0.10/how_tos/variable_scope/index.html)を読んだが、何が起こっているのかまだ分かりません。

私はこの問題を説明するための最小限の実例を以下に書いています。一言で言えば

、私は以下のコードを希望次に行います

1))一つの変数の初期化、私はセッションを作成した直後に「FC1を/ワット」

2 NPYを作成します。

3)変数 "fc1/w"がすでに作成されていることを認識する操作 "y"を実行し、その変数値を使用します新しいものを初期化して)、その出力を計算します。

4)関数内の変数のスコープ 『リニア』に「真=リユース、」私はフラグを追加したことに注意してください、私はエラーを取得しておくので、それは、助けていないようしてください:

ValueError: Variable fc1/w does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope? 
私が働いていることに注意してください)

ValueError: Variable fc1/w already exists, disallowed. Did you mean to set reuse=True in VarScope? 

5:

これは私がフラグを削除し、その後、TensorFlow変数が存在しないことを私に言うだろう「リユース=真」​​であるかのため、かなり混乱していますより大きいコードベースであり、私は実際にwを出すのではなく、共有変数機能を使用できるようにしたいと思います私は以下に書いた特定のサンプルコードを解決するかもしれない共有変数を使わずにハックしましたが、一般化はできません。

6)最後に、評価からグラフの作成を分けておきたいと思います。特に、私は "tf.InteractiveSession()"を使用したり、セッションスコープで "y"を作成することは望ましくありません: "with tf.Session()as sess:"。

これは私の最初の投稿であり、TensorFlowの新機能です。質問が完全にはっきりしない場合は、謝罪してください。いずれにしても、私はより詳細な情報を提供したり、あらゆる側面をさらに明確にすることができれば幸いです。

ありがとうございます。

import tensorflow as tf 
import numpy as np 


def linear(x_, output_size, non_linearity, name): 
    with tf.variable_scope(name, reuse=True): 
     input_size = x_.get_shape().as_list()[1] 
     # If doesn't exist, initialize "name/w" randomly: 
     w = tf.get_variable("w", [input_size, output_size], tf.float32, 
          tf.random_normal_initializer()) 
     z = tf.matmul(x_, w) 
     return non_linearity(z) 


def init_w(name, w_initializer): 
    with tf.variable_scope(name): 
     w = tf.get_variable("w", initializer=w_initializer) 
     return tf.initialize_variables([w]) 


batch_size = 1 
fc1_input_size = 7 
fc1_output_size = 5 

# Initialize with zeros 
fc1_w_initializer = tf.zeros([fc1_input_size, fc1_output_size]) 

# 
x = tf.placeholder(tf.float32, [None, fc1_input_size]) 

# 
y = linear(x, fc1_output_size, tf.nn.softmax, "fc1") 

with tf.Session() as sess: 

    # Initialize "fc1/w" with zeros. 
    sess.run(init_w("fc1", fc1_w_initializer)) 

    # Create npy array to feed into placeholder x 
    x_npy = np.arange(batch_size * fc1_input_size, dtype=np.float32).reshape((batch_size, fc1_input_size)) 

    # Run y, and print result. 
    print(sess.run(y, dict_feed={x: x_npy})) 

答えて

0

(tf.variable_scopeの呼び出しのように思える)/ wの変数のスコープを使用すると、空のセッションでそれを実行しても検出します。デモするためにコードを整理しました。

import tensorflow as tf 
import numpy as np 


def shared_layer(x, output_size, non_linearity, name): 
    with tf.variable_scope(name): 
     input_size = x.get_shape().as_list()[1] 
     # If doesn't exist, initialize "name/w" randomly: 
     w = tf.get_variable("w", [input_size, output_size], tf.float32, 
          tf.random_normal_initializer()) 
     z = tf.matmul(x, w) 
     return non_linearity(z) 

def shared_init(sess, scope, var, initializer): 
    with tf.variable_scope(scope, reuse=True): 
     w = tf.get_variable(var, initializer=initializer) 
     sess.run(tf.initialize_variables([w])) 

layer_input_size = 2 
layer_output_size = 2 

w_init = tf.zeros([layer_input_size, layer_output_size]) 

x = tf.placeholder(tf.float32, [None, layer_input_size]) 
y = shared_layer(x, layer_output_size, tf.nn.softmax, "scope") 

with tf.Session() as sess: 
    shared_init(sess, "scope", "w", w_init) 
    with tf.variable_scope("scope", reuse=True): 
     print sess.run(tf.get_variable("w", [2,2])) 
関連する問題