2017-02-23 88 views
0

我被卡住的時間太長,需要一些幫助(對於tensorflow等非常新)。我根據自己的數據修改了一個MNIST示例,但即使在2個紀元後仍保持100%的準確性。
我的X是(類似於MNIST)a [18,1] - 向量和y是一個float32。
變量:Tensorflow:獲取NN的正確精度

n_nodes_hl1 = 100 
n_nodes_hl2 = 100 
n_nodes_hl3 = 50 
x = tf.placeholder(shape=[None, 18], dtype=tf.float32) 
y = tf.placeholder(shape=[None, 1], dtype=tf.float32) 
x_vals_train = np.array([]) 
y_vals_train = np.array([]) 
x_vals_test = np.array([]) 
y_vals_test = np.array([]) 
loss_vec = [] 

我的模型:

def neural_net_model(data): 
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([18,n_nodes_hl1])), 
        'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} 
    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])), 
        'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))} 
    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])), 
        'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3,1])), 
    'biases':tf.Variable(tf.random_normal([1]))} 

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']),hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) 
    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']),hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 
    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']),hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases'] 

    return output 

會議:

def train_neural_network(x): 
    prediction = neural_net_model(x) 
    cost = tf.reduce_mean(tf.abs(y - prediction)) 
    optimizer = tf.train.AdamOptimizer(0.01).minimize(cost) 

    with tf.Session() as sess: 
     sess.run(tf.global_variables_initializer()) 
     for i in range(10): 
      temp_loss = 0 

      rand_index = np.random.choice(len(x_vals_train), 50) 
      rand_x = x_vals_train[rand_index] 
      rand_y = np.transpose([y_vals_train[rand_index]]) 
      _, temp_loss = sess.run(optimizer, feed_dict={x: rand_x, y: rand_y}) 

      if (i+1)%100==0: 
      print('Generation: ' + str(i+1) + '. Loss = ' + str(temp_loss)) 

     # evaluate accuracy 
     correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1)) 
     accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
     print "accuracy %.5f'" % accuracy.eval(feed_dict={x: x_vals_test, y: np.transpose([y_vals_test])}) 

問題是primarely爲什麼我總是得到100%的準確率,這是顯然是假的。提前致謝!

回答

2

MNIST通常具有單熱編碼輸出。在這種情況下,correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))是有意義的,因爲tf.argmax將一次性代碼轉換爲實際類,然後通過tf.equal進行比較。

但由於輸出尺寸你的情況是1tf.argmax輸出0這是唯一有效的索引兩者,因此,它們是相等的,因此100%的準確度。

您需要重新定義適合您的案例的準確度。假設輸出是一個二進制值(精度爲指標纔有意義爲二進制的情況),可以使用以下內容:

correct_prediction = tf.equal(tf.round(prediction), y) 

在這裏,你被四捨五入prediction然後比較y。對於這個工作,你需要有乙狀結腸作爲最終層激活output = tf.nn.sigmoid(output)或者你需要剪輯的預言:

correct_prediction = tf.equal(tf.round(tf.clip_by_value(prediction,0,1)), y) 

其他選項,供您將轉換y_vals_trainy_vals_test一個熱碼,並具備網絡輸出2維。

+0

現在一切看起來更清晰:)謝謝。我的輸出是一個浮點數(查詢的執行時間) - 所以如果你有任何提示,我全部都是耳朵。但你清楚地回答了原來的問題..所以我接受 – dv3

+0

平均方差是實際價值輸出的最佳指標。你也可以使用平均絕對誤差,就像你在損失中一樣。 – indraforyou