Python tf.global_variables_initializer()的位置

Python tf.global_variables_initializer()的位置,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我是一个深度学习的初学者,一直坚持这个问题 import tensorflow as tf import numpy as np import pandas as pd from sklearn.preprocessing import LabelEncoder from sklearn.utils import shuffle from sklearn.model_selection import train_test_split #define the one hot encode fu

我是一个深度学习的初学者,一直坚持这个问题

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import  shuffle
from sklearn.model_selection import train_test_split

#define the one hot encode function
def one_hot_encode(labels):
  n_labels = len(labels)
  n_unique_labels = len(np.unique(labels))
  one_hot_encode = np.zeros((n_labels,n_unique_labels))
  one_hot_encode[np.arange(n_labels), labels] = 1
  return one_hot_encode

#Read the sonar dataset
df = pd.read_csv('sonar.csv')
print(len(df.columns))
X = df[df.columns[0:60]].values
y=df[df.columns[60]]
#encode the dependent variable containing categorical values
encoder = LabelEncoder()
encoder.fit(y)
y = encoder.transform(y)
Y = one_hot_encode(y)

#Transform the data in training and testing
X,Y = shuffle(X,Y,random_state=1)
train_x,test_x,train_y,test_y = train_test_split(X,Y,test_size=0.20,       random_state=42)


#define and initialize the variables to work with the tensors
learning_rate = 0.1
training_epochs = 1000

 #Array to store cost obtained in each epoch
 cost_history = np.empty(shape=[1],dtype=float)

 n_dim = X.shape[1]
 n_class = 2

x = tf.placeholder(tf.float32,[None,n_dim])
W = tf.Variable(tf.zeros([n_dim,n_class]))
b = tf.Variable(tf.zeros([n_class]))

#initialize all variables.


#define the cost function
y_ = tf.placeholder(tf.float32,[None,n_class])
y = tf.matmul(x, W)+ b
 init = tf.global_variables_initializer()#wrong position
cost_function =       tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y,labels=y_))

training_step = tf.train.AdamOptimizer(learning_rate).minimize(cost_function)
 init = tf.global_variables_initializer()#correct position
 #initialize the session

 sess = tf.Session()

  sess.run(init)
  mse_history = []

  #calculate the cost for each epoch
 for epoch in range(training_epochs):
sess.run(training_step,feed_dict={x:train_x,y_:train_y})
cost = sess.run(cost_function,feed_dict={x: train_x,y_: train_y})
cost_history = np.append(cost_history,cost)
print('epoch : ', epoch,  ' - ', 'cost: ', cost)

 pred_y = sess.run(y, feed_dict={x: test_x})
 print(pred_y) 
#Calculate Accuracy
 correct_prediction = tf.equal(tf.argmax(pred_y,1), tf.argmax(test_y,1))
 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 print(sess.run(accuracy))
 sess.close()
在上面的代码中,如果我使用init=tf.global\u variables\u initializer() 上面AdamOptimizer则给出错误,但如果我在之后使用它 AdamOptimizer那么它工作正常。原因是什么? 尽管它在两个位置都可以使用GradientDescentOptimizer

查看
init=tf.global\u variables\u initializer()
init=tf.variables\u initializer(tf.global\u variables())相同。

需要初始化一些内部变量(均值统计等)

<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
<tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
<tf.Variable 'x/Adam:0' shape=(2, 1) dtype=float32_ref>    # 1st moment vector
<tf.Variable 'x/Adam_1:0' shape=(2, 1) dtype=float32_ref>  # 2nd moment vector
你会得到

Attempting to use uninitialized value beta1_power
它告诉您,它试图访问尚未初始化的

所以

这是唯一正确的方法。您可以检查,哪些变量可以通过放置

for variable in tf.global_variables():
    print(variable)
在源代码中

考虑最小化二次型的例子
0.5x'Ax+bx+c
。在TensorFlow中,这将是

import tensorflow as tf
import numpy as np

x = tf.Variable(np.random.rand(2, 1), dtype=tf.float32, name="x")
# we already make clear, that we are not going to optimize these variables
b = tf.constant([[5], [6]], dtype=tf.float32, name="b")
A = tf.constant([[9, 2], [2, 10]], dtype=tf.float32, name="A")

cost_function = 0.5 * tf.matmul(tf.matmul(tf.transpose(x), A), x) - tf.matmul(tf.transpose(b), x) + 42

for variable in tf.global_variables():
    print('before ADAM: global_variables_initializer would init {}'.format(variable))

optimize_op = tf.train.AdamOptimizer(0.1).minimize(cost_function)

for variable in tf.global_variables():
    print('after ADAM: global_variables_initializer would init 
{}.格式(变量))

输出是

before ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x/Adam:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x/Adam_1:0' shape=(2, 1) dtype=float32_ref>

因此,在使用优化器之前和之后都没有任何变化

根据我的经验,
init=tf.global\u variables\u initializer()
只会初始化它之前声明的变量

例如,考虑下面的代码:

variable_1 = tf.get_variable("v_1",[5,5],tf.float32,initializer=tf.zeros_initializer)
init = tf.global_variables_initializer()
variable_2 = tf.get_variable("v_2",[5,5],tf.float32,initializer=tf.zeros_initializer)
以下代码将在
变量_1
中打印数字(5x5,全部为零):

with tf.Session() as sess:
    sess.run(init) 
    print(sess.run(variable_1))
但是,以下代码将产生“尝试使用未初始化值”错误:


总之,在大多数情况下,只需将
init=tf.global\u variables\u initializer()
放在所有其他变量之后。

但如果我在AdamopOptimizer上面定义init,仍然会出现一些错误。你可以检查一下你是否想要有区别。请看一下我的最新答案。
before ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'beta2_power:0' shape=() dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x/Adam:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x/Adam_1:0' shape=(2, 1) dtype=float32_ref>
before ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
after ADAM global_variables_initializer would init <tf.Variable 'x:0' shape=(2, 1) dtype=float32_ref>
variable_1 = tf.get_variable("v_1",[5,5],tf.float32,initializer=tf.zeros_initializer)
init = tf.global_variables_initializer()
variable_2 = tf.get_variable("v_2",[5,5],tf.float32,initializer=tf.zeros_initializer)
with tf.Session() as sess:
    sess.run(init) 
    print(sess.run(variable_1))
with tf.Session() as sess:
    sess.run(init) 
    print(sess.run(variable_2))