2017-02-20 5 views
0

私は現在、TensorFlowとDeep Learningの初心者です。私は、隠されたレイヤのReLUアクティベーション機能と、出力レイヤーのsoftmaxという非常に単純な2レイヤーニューラルネットワークを作りようとしていました。特に、私はMNISTと全く同じ形をしていますが、より難しい例がある、よく知られたnotMNISTデータセットを訓練していました。これは、(TensorFlow v1.0.0を使用して)私はそれを解決する方法だった:この方法では、単純なランナーでTensorFlow + Batch Gradient Descent:各バッチで0%の精度ですが、ネットワークは収束しますか?

batch_size = 128 
hidden_nodes = 1024 

graph = tf.Graph() 
with graph.as_default(): 
    tf_train_dataset = tf.placeholder(tf.float32, 
            shape=(batch_size, image_size * image_size)) 
    tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) 
    tf_valid_dataset = tf.constant(valid_dataset) 
    tf_test_dataset = tf.constant(test_dataset) 

    weights_ih = tf.Variable(tf.truncated_normal([image_size * image_size, hidden_nodes])) 
    biases_ih = tf.Variable(tf.ones([hidden_nodes])/10) 
    weights_ho = tf.Variable(tf.truncated_normal([hidden_nodes, num_labels])) 
    biases_ho = tf.Variable(tf.zeros([num_labels])) 

    logits = tf.matmul(tf_train_dataset, weights_ih) + biases_ih 
    hidden_layer_output = tf.nn.relu(logits) 

    output = tf.matmul(hidden_layer_output, weights_ho) + biases_ho 
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=tf_train_labels)) 

    optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss) 

    train_prediction = tf.nn.softmax(hidden_layer_output) 
    valid_prediction = tf.nn.softmax(tf.matmul(
     tf.nn.relu(tf.matmul(tf_valid_dataset, weights_ih) + biases_ih), 
        weights_ho) + biases_ho) 
    test_prediction = tf.nn.softmax(tf.matmul(
     tf.nn.relu(tf.matmul(tf_test_dataset, weights_ih) + biases_ih), 
        weights_ho) + biases_ho) 

num_steps = 5000 

def accuracy(predictions, labels): 
    return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))/predictions.shape[0]) 

with tf.Session(graph=graph) as sess: 
    tf.global_variables_initializer().run() 
    print("Initialized") 
    for step in range(num_steps): 
     offset = (step * batch_size) % (train_labels.shape[0] - batch_size) 
     batch_data = train_dataset[offset:(offset + batch_size), :] 
     batch_labels = train_labels[offset:(offset + batch_size), :] 
     feed_dict = {tf_train_dataset: batch_data, tf_train_labels: batch_labels} 
     _, l, predictions =\ 
      sess.run([optimizer, loss, train_prediction], feed_dict=feed_dict) 
     if (step % 500 == 0): 
      print("Minibatch loss at step %d: %f" % (step, l)) 
      print("Minibatch accuracy: %.1f%%" % accuracy(predictions, batch_labels)) 
      print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), 
                  valid_labels)) 
    print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels)) 

私はこれらの結果を得る:

Initialized 
Minibatch loss at step 0: 281.805603 
Minibatch accuracy: 0.0% 
Validation accuracy: 21.9% 
Minibatch loss at step 500: 18.725670 
Minibatch accuracy: 0.0% 
Validation accuracy: 81.0% 
Minibatch loss at step 1000: 13.720121 
Minibatch accuracy: 0.0% 
Validation accuracy: 81.2% 
Minibatch loss at step 1500: 16.521467 
Minibatch accuracy: 0.0% 
Validation accuracy: 81.3% 
Minibatch loss at step 2000: 4.905802 
Minibatch accuracy: 0.0% 
Validation accuracy: 80.7% 
Minibatch loss at step 2500: 1.040669 
Minibatch accuracy: 0.0% 
Validation accuracy: 82.4% 
Minibatch loss at step 3000: 2.731811 
Minibatch accuracy: 0.0% 
Validation accuracy: 80.6% 
Minibatch loss at step 3500: 1.011298 
Minibatch accuracy: 0.0% 
Validation accuracy: 81.9% 
Minibatch loss at step 4000: 1.432833 
Minibatch accuracy: 0.0% 
Validation accuracy: 82.7% 
Minibatch loss at step 4500: 0.934623 
Minibatch accuracy: 0.0% 
Validation accuracy: 82.5% 
Test accuracy: 89.6% 

見ることができるようにミニブッチの精度は、常に0%ですが、ミニバスの損失は減少しており、検証の精度は向上していますp。モデルは「うまくいく」と思われますが、何か他のことが起こっていると、それはより大きな問題を示すものです。 500エポック後の急激なジャンプも疑わしい。私はこれについて多くの直感を持っていないので、学習率やバッチサイズを変更するなど、さまざまな面白いことを試みましたが、0%の精度で何もしませんでした。

TensorFlowでもっと経験豊富な人が私にこれを引き起こしているかもしれないと言うことができるなら、本当に感謝します。私は将来それを避けることを学ぶことができます。

ありがとうございます!

答えて

1

train_prediction = tf.nn.softmax(output) 

代わりの

train_prediction = tf.nn.softmax(hidden_layer_output) 

を試してみて、それが動作するはずです。

ところで、私はあなたがロジットと呼ぶものをロジットと呼んでいません。あなたの出力はロジットと呼ばれますが、それはちょうど命名の問題です...

+0

優雅な、これは私が今までに作ったことができない愚かなミスでした!ありがとう、そんなに。 – naiveai

関連する問題