我是深入学习的初学者,并坚持这个问题。在
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_variables_初始值设定项() 上面AdamOptimizer那么它会给出错误,但是如果我在之后使用它的话 AdamOptimizer那么它工作正常。原因是什么? 尽管在这两个位置都使用GradientDescentOptimizer,它都能很好地工作。在
根据我的经验,
init = tf.global_variables_initializer()
只会初始化在其之前声明的变量。在例如,考虑以下代码:
以下代码将在
^{pr2}$variable_1
中打印数字(5x5,全部为零):但是,以下代码将生成“尝试使用未初始化的值”错误:
最后,在大多数情况下,只需将
init = tf.global_variables_initializer()
放在所有其他变量之后。在看一下documentation,}是一样的
init = tf.global_variables_initializer()
和{^{} 需要初始化一些内部变量(平均值统计等)
documentation告诉您如何应用更新。在
相反,香草梯度下降优化器^{} 不依赖于任何变量。有区别。
现在,在^{} 可以使用其变量之前,需要在某个时刻初始化这些变量。在
要创建初始化所有所需变量的操作} 之后。在
init
,此操作init
需要知道运行程序所需的变量。因此,它需要放在^{如果你把} 之前
^{pr2}$init = tf.global_variables_initializer()
放在^{你会得到
它告诉您,^{} 试图访问尚未初始化的
<tf.Variable 'beta1_power:0' shape=() dtype=float32_ref>
。在所以
是唯一正确的方法。您可以检查,哪些变量可以通过
源代码中。在
考虑最小化二次型
0.5x'Ax + bx + c
的例子。在TensorFlow中{}'。格式(变量)
输出是
因此,当在ADAM定义}时,
tf.train.AdamOptimizer
之前放置{tf.global_variables_initializer()
看不到ADAM所需的变量。使用GradientDescentOptimizer时,值为所以优化器前后没有任何变化。在
相关问题 更多 >
编程相关推荐