2017-02-23 8 views
0

私は長すぎますし、いくつかの助けが必要です(テンソルフローなど)。私は自分のデータに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])}) 

私は常に100%の精度を得るなぜ質問はprimarelyです明らかに偽です。前もって感謝します!

答えて

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_testワンホットコードに、y_vals_trainを変換し、ネットワークを持つことになりますため出力2次元。

+0

すべてが少し鮮明になりました:)ありがとう。私の出力は浮動小数点(クエリの実行時間)なので、もしあなたがヒントを持っていれば私はすべて耳にします。しかし、あなたは明らかに元の質問に答えました。だから私は受け入れます。 – dv3

+0

平均平方和誤差は、実数の価値がある出力のための最良のメトリックです。あなたは損失のように平均絶対誤差を使用することもできます。 – indraforyou