2017-05-07 137 views
1

我正在嘗試爲MNIST數據創建一個簡單的線性分類器,我無法讓自己的損失下降。可能是什麼問題呢? 這裏是我的代碼:Tensorflow線性分類器未訓練

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

class LinearClassifier(object): 
    def __init__(self): 
     print("LinearClassifier loading MNIST") 
     self._mnist = input_data.read_data_sets("mnist_data/", one_hot = True) 
     self._buildGraph() 

    def _buildGraph(self): 
     self._tf_TrainX = tf.placeholder(tf.float32, [None, self._mnist.train.images.shape[1]])  
     self._tf_TrainY = tf.placeholder(tf.float32, [None, self._mnist.train.labels.shape[1]]) 

     self._tf_Weights = tf.Variable(tf.random_normal([784,10]), tf.float32) 
     self._tf_Bias = tf.Variable(tf.zeros([10]), tf.float32) 
     self._tf_Y = tf.nn.softmax(tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias) 

     self._tf_Loss = tf.reduce_mean(-tf.reduce_sum(self._tf_TrainY * tf.log(self._tf_Y), reduction_indices=[1])) 
     self._tf_TrainStep = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(self._tf_Loss) 

     self._tf_CorrectGuess = tf.equal(tf.argmax(self._tf_Y, 1), tf.arg_max(self._tf_TrainY, 1)) 
     self._tf_Accuracy = tf.reduce_mean(tf.cast(self._tf_CorrectGuess, tf.float32)) 

     self._tf_Initializers = tf.global_variables_initializer() 

    def train(self, epochs, batch_size): 
     self._sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) 
     self._sess.run(self._tf_Initializers) 

     for i in range(epochs): 
      batchX, batchY = self._mnist.train.next_batch(batch_size) 
      self._loss, _, self._accurracy = self._sess.run([self._tf_Loss, self._tf_TrainStep, self._tf_Accuracy], feed_dict ={self._tf_TrainX: batchX, self._tf_TrainY: batchY}) 
      print("Epoch: {0}, Loss: {1}, Accuracy: {2}".format(i, self._loss, self._accurracy)) 

當我通過運行這個:

lc = LinearClassifier() 
lc.train(1000, 100) 

...我GETT是這樣的:

Epoch: 969, Loss: 8.19491195678711, Accuracy: 0.17999999225139618 
Epoch: 970, Loss: 9.09421157836914, Accuracy: 0.1899999976158142 
.... 
Epoch: 998, Loss: 7.865959167480469, Accuracy: 0.17000000178813934 
Epoch: 999, Loss: 9.281349182128906, Accuracy: 0.10999999940395355 

可能是什麼原因TF .train.GradientDescentOptimizer沒有正確地訓練我的權重和偏差?

回答

3

最主要的是你的學習率(0.001)太低。我跑這將其更改爲0.5,如他們在mnist tensorflow tutorial做了之後,我越來越準確性和損失更喜歡:

Epoch: 997, Loss: 0.6437355875968933, Accuracy: 0.8999999761581421 
Epoch: 998, Loss: 0.6129786968231201, Accuracy: 0.8899999856948853 
Epoch: 999, Loss: 0.6442205905914307, Accuracy: 0.8999999761581421 

另一件事,有點不尋常的是在你原來的代碼,你有這樣的

self._tf_Y = tf.nn.softmax(tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias) 
self._tf_Loss = tf.reduce_mean(-tf.reduce_sum(self._tf_TrainY * tf.log(self._tf_Y), reduction_indices=[1])) 

在這種情況下,你會做softmax兩次。在改變之前我確實運行了它,火車的準確度大約是85%,所以它確實有所作爲。同樣做兩次softmax理論上可以解釋的更少。

最後,他們在教程中提到,使用上面的softmax形式-reduce_sum(label * log(y))在數值上不穩定,所以最好使用內置的softmax層,它可以計算分析上等效的但數值更穩定的softmax。應用這兩個更改後,受影響的行看起來像:

self._tf_Y = tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias 
self._tf_Loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=self._tf_TrainY, logits=self._tf_Y)) 
+0

謝謝。你是對的。 –