2017-07-06 62 views
0

在我的tensorflow應用程序中,所有的預測都是真實的。我試圖將MNIST示例應用於我的問題,但是我擔心從多個類使用該技術是錯誤的,並且我有二進制分類。Tensorflow預測全部出來真實

# In[1]: 


import tensorflow as tf 
import numpy 


# In[2]: 


X = tf.placeholder(tf.float32, [None, 3], "training-data") 
W1 = tf.Variable(tf.truncated_normal([3, 2]), "W") 
b1 = tf.Variable(tf.zeros([2]), "B") # number of neurons 

W2 = tf.Variable(tf.truncated_normal([2, 1]), "W1") 
b2 = tf.Variable(tf.zeros([1]), "B1") # number of neurons 


# In[9]: 


Y = tf.nn.sigmoid(tf.matmul(X, W1) + b1) 
Y1 = tf.nn.softmax(tf.matmul(Y, W2) + b2) 
Y_ = tf.placeholder(tf.float32, [None, 1], "labels") # labels 

#cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y1)) # error function 
cross_entropy = tf.reduce_sum(tf.abs(Y1 - Y_)) 
is_correct = tf.equal(Y1, Y_) 
# All the predictions are coming out True ?!? 
accuracy = tf.reduce_sum(tf.cast(is_correct, tf.int32))/tf.size(is_correct) 

print("X", X) 
print("Y", Y) 
print("Y1", Y1) 
print("Y_", Y_) 
print("cross-entropy", cross_entropy) 
print("is-correct", is_correct) 
print("accuracy", accuracy) 


# In[10]: 


optimizer = tf.train.GradientDescentOptimizer(0.005) 
train_step = optimizer.minimize(cross_entropy) 


# In[11]: 


def load(filename): 
    filename_queue = tf.train.string_input_producer([filename]) 
    key, value = tf.TextLineReader(skip_header_lines=1).read(filename_queue) 
    col1, col2, col3, col4, col5 = tf.decode_csv(records = value, record_defaults=[[1.0], [1.0], [1.0], [1.0], [1.0]]) 
    batch_size=100 
    # A tensor for each column of the CSV 
    load_time, is_east, is_west, is_europe, labels = tf.train.shuffle_batch([col1, col2, col3, col4, col5], batch_size=batch_size, capacity=batch_size*50, min_after_dequeue=batch_size) 
    #features = tf.stack([load_time, is_east, is_west, is_europe], 1) 
    features = tf.stack([is_east, is_west, is_europe], 1) 
    return features, tf.reshape(labels, [-1, 1]) 


# In[12]: 


features, labels = load("/Users/andrew.ehrlich/Desktop/labelled_clicks.csv") 


# In[13]: 


# Run! 

test_features = numpy.loadtxt(open("/Users/andrew.ehrlich/Desktop/labelled_clicks_test.csv", "rb"), delimiter=",", skiprows=1, usecols = [1,2,3]) 
test_labels = numpy.loadtxt(open("/Users/andrew.ehrlich/Desktop/labelled_clicks_test.csv", "rb"), delimiter=",", skiprows=1, usecols = [4], ndmin = 2) 

summ = tf.reduce_sum(test_labels) 
size = tf.size(test_labels) 

with tf.Session() as sess: 
    file_writer = tf.summary.FileWriter('/Users/andrew.ehrlich/tf.log', sess.graph) 
    init = tf.global_variables_initializer() 

    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 

    sess.run(init) 
    for i in range(1000): 

     ran_features = sess.run(features) 
     ran_labels = sess.run(labels) 

     train_data = {X: ran_features, Y_: ran_labels} 
     sess.run(train_step, feed_dict=train_data) # I guess this updates the tensors behind train_step (W and b) 

     if (i % 100 == 0): 
      train_acc, train_ent = sess.run([accuracy, cross_entropy], feed_dict=train_data) 

      test_data = {X: test_features, Y_: test_labels} 
      test_acc, test_ent = sess.run([accuracy, cross_entropy], feed_dict=test_data) 

      size = sess.run(tf.size(ran_labels)) 
      print("batch size: %d [TRAIN - acc:%1.4f ent: %10.4f] [TEST - acc:%1.4f ent: %10.4f]" % (size, train_acc, train_ent, test_acc, test_ent)) 


# In[ ]: 

輸出:

batch size: 100 [TRAIN - acc:0.4100 ent: 59.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.5300 ent: 47.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.5900 ent: 41.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.4700 ent: 53.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.5200 ent: 48.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.6000 ent: 40.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.5500 ent: 45.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.6100 ent: 39.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.4100 ent: 59.0000] [TEST - acc:0.4787 ent: 9423.0000] 
batch size: 100 [TRAIN - acc:0.5300 ent: 47.0000] [TEST - acc:0.4787 ent: 9423.0000] 

精度不改變,因爲Y_的值始終都是真的,這導致了許多,只是表示在測試組正標籤的數量。請讓我知道任何反饋!我很感激!

回答

1

當您在最後一層使用softmax,然後計算cross_entropy時,將它們組合到一個數字穩定的tf.softmax_cross_entropy_with_logits。一旦你看到損失正在減少,但你的準確性不好,那麼你可以通過增加更多的層來增加網絡的複雜性。

做以下修改:

Y1 = (tf.matmul(Y, W2) + b2) 
cross_entropy = tf.reduce_mean(tf.softmax_cross_entropy_with_logits(logits=Y1, labels=Y_)) 
+0

我在文檔中看到:警告:此操作期望未縮放的logits,因爲它在內部執行softmax以提高效率。不要用softmax的輸出調用此操作,因爲它會產生不正確的結果。 https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits#autolink-21385 –

+0

我將softmax圖層更改爲softmax_cross_entropy_with_logits。我應該使用什麼損失函數?舊的報道NaN。 –

+0

更新了答案 –

0

你有一個單一的輸出,我假設你希望它是0和1的虛假和真實。如果是這樣的話,其中一個問題就是softmax函數。 Softmax適用於多個類別,因爲它的一個屬性是它使所有類別的總和等於1,並且將結果解釋爲概率分佈變得更容易。在你的情況下,它只是因爲這個屬性而將你的單個輸出設置爲1。

有兩種基本的解決方法。或者在你的輸出層上使用softmax,或者使用不同的損失函數(均方可能工作),或者讓輸出層返回兩個值,保持softmax並將結果兩個數字解釋爲一個熱點編碼,其中實際值是一個度量網絡的信心。

+0

我將最後一層更新爲2而不是1,我可以看到Y現在包含了概率。但準確性和熵保持相同的每批。使用「Y = tf.nn.softmax(tf.matmul(X,W1)+ b1)」和「loss = -tf.reduce_sum(Y_ * tf.log(Y))」 –