Tensorflow不会更新权重

时间:2018-07-20 08:29:44

标签: python tensorflow low-level

我正在做一个Tensoflow教程,在更新权重时遇到了一些麻烦。

from sklearn.datasets import load_digits
mnist = load_digits(2)
X,y = mnist.data, mnist.target
print("y [shape - %s]:" % (str(y.shape)), y[:10])#y [shape - (360,)]: [0 1 0 1 0 1 0 0 1 1]
print("X [shape - %s]:" % (str(X.shape)))#X [shape - (360, 64)]:

# inputs and shareds
shared_weights = tf.Variable(initial_value=tf.random_uniform([64]))#<student.code_variable()>
input_X = tf.placeholder(shape=(None,64),dtype="float32",name="features")
input_y = tf.placeholder(shape=(None,),dtype="float32",name="label")

reduced_sum=tf.reduce_sum(input_X*shared_weights/256, axis=1)
predicted_y = tf.nn.sigmoid(reduced_sum)#<predicted probabilities for input_X>
loss =tf.losses.log_loss(labels=input_y,predictions=predicted_y)#<logistic loss (scalar, mean over sample)>
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

#I don't understand how i have to implement this (I don't use it)
train_function = lambda X,y: s.run(optimizer,feed_dict={input_X:X,input_y:y})
                              #<compile function that takes X and y, returns log loss and updates weights>
predict_function =lambda X: s.run(predicted_y,feed_dict={input_X:X})  
    #<compile function that takes X and computes probabilities of y>

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y)

from sklearn.metrics import roc_auc_score
s.run(tf.global_variables_initializer())

for i in range(5):
    #print(reduced_sum.eval(feed_dict={input_X:X_train}))
    #print(shared_weights.eval())
    s.run(optimizer,feed_dict={input_X:X_train,input_y:y_train})#    <run optimizer operation>
    loss_i = loss.eval(feed_dict={input_X:X_train, input_y:y_train}) #<compute loss at iteration i>

    print("loss at iter %i:%.4f" % (i, loss_i))

    print("train auc:",roc_auc_score(y_train, predict_function(X_train)))
    print("test auc:",roc_auc_score(y_test, predict_function(X_test)))

print ("resulting weights:")
plt.imshow(shared_weights.get_value().reshape(8, -1))
plt.colorbar();

本教程的这一部分以快活的填充为指导。强制进行批次(单个minibatch)优化。 我打印了重量和重量,但它们没有变化,为什么?

2 个答案:

答案 0 :(得分:0)

不需要统一初始化权重,只需使用零即可

initial_value=tf.zeros([64])

resulting weights

答案 1 :(得分:0)

在优化程序中使用较大的步骤(例如50.0),并在迭代中使用更多的步骤(例如50)。

指向Sergey的权重是正确的。

不需要调用s.run和loss.eval替换为

UIDevice.current.setValue(self.preferredInterfaceOrientationForPresentation.rawValue, forKey: "orientation")