我在TensorFlow中使用Python API共享變量時遇到問題。TensorFlow,Python,共享變量,初始化在頂部
我讀過官方文檔(https://www.tensorflow.org/versions/r0.10/how_tos/variable_scope/index.html),但我仍然無法弄清楚發生了什麼。
我在下面寫了一個最小的工作示例來說明問題。
概括地說,我想下面的代碼做到以下幾點:
1)初始化一個變量「FC1/W」後,立即創建會話,
2)創建NPY將數組「x_npy」加入佔位符「x」中,
3)運行一個操作「y」,它應該意識到已經創建了變量「fc1/w」,然後使用該變量值(而不是初始化新的)來計算它的輸出。
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?
這是因爲如果我要刪除標誌「再利用= true」,則TensorFlow會告訴我該變量確實存在相當混亂:
ValueError: Variable fc1/w already exists, disallowed. Did you mean to set reuse=True in VarScope?
5)請注意,我的工作一個更大的代碼庫,我真的希望能夠使用共享變量功能,而不是w如果不使用共享變量,可能會解決我在下面編寫的特定示例代碼,但可能無法很好地推廣。 6)最後,請注意,我真的想保持圖形的創建與評估的分離。特別是,我不想在會話範圍內使用「tf.InteractiveSession()」或創建「y」,即在「with tf.Session()as sess:」下面。
這是我在Stack Overflow上的第一篇文章,我對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}))