1

신경망을 학습하여 notMNIST 데이터 집합을 사용하여 문자를 인식하지만 일단 실행하면 각 반복 후에 그 정확도가 비교적 일정하게 유지됩니다.신경망이 정확성을 향상시키지 못합니다

나는 학습 속도를 낮추려고 시도했지만 그다지 다르지 않았다. 무엇이 문제일까요?

나는 문제가 tf.nn.relu() 메소드의 구현에있을 수 있습니다 생각하고, 내가 예측을 계산 어떻게 여기

가있다 텐서 흐름 및 신경망

에서 비교적 새로운 해요 이후 내 프로그램 실행의 스크린 샷 및 훈련 세트, 검증 세트 및 테스트 세트에 대한 정확성은 모두 내가 생각

enter image description here

num_steps=801 

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 session: 
    #this is a one-time operation which ensure the parameters get initialized 
    #we described in the graph: random weights for the matrix, zeros for the 
    #biases. 
    tf.global_variables_initializer().run() 
    print("initialized") 
    for step in range(num_steps): 
     #run the computations. we tell .run() that we want to run the optimizer, 
     #and get the loss value and the training predictions returned as numpy 
     #arrays. 
     _, l, predictions = session.run([optimizer,loss, train_prediction]) 
     if (step % 100 ==0): 
      print("loss at step %d: %f" % (step,l)) 
      print("Training accuracy: %.1f%%" % accuracy(
       predictions, train_labels[:train_subset,:])) 
      #calling .eval() on valid_prediction is basically like calling run(), but 
      #just to get that one numpy array. Note that it recomputes all its graph 
      #dependencies. 
      print("Validation accuracy: %.1f%%" % accuracy(
       valid_prediction.eval(), valid_labels)) 
      print("test accuracy: %.1f%%" % accuracy(test_prediction.eval(),test_labels)) 

batch_size = 128 
hidden_nodes = 1024 
graph = tf.Graph() 
with graph.as_default(): 
    #input data. For the training data, we use a placeholder that will be fed 
    #at run time with a training minibatch 
    tf_train_dataset = tf.placeholder(tf.float32, 
            shape=(batch_size, image_size*image_size), name="td") 
    tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels), name="tl") 
    tf_valid_dataset = tf.constant(valid_dataset) 
    tf_test_dataset = tf.constant(test_dataset) 

    #variables 
    weights1 = tf.Variable(
     tf.truncated_normal([image_size*image_size, hidden_nodes])) 
    biases1 = tf.Variable(tf.zeros([hidden_nodes])) 
    weights2 =tf.Variable(
     tf.truncated_normal([hidden_nodes, num_labels])) 
    biases2 = tf.Variable(tf.zeros([num_labels])) 

    #training computation. 
    relu1 = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1) 
    relu_out= tf.nn.relu(tf.matmul(relu1, weights2) + biases2) 

    loss = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(logits=relu_out,labels=tf_train_labels)) 

    #optimizer 
    optimizer = tf.train.GradientDescentOptimizer(0.25).minimize(loss) 

    #predictions for the training, validation, and test data 
    train_prediction = relu_out 
    valid_prediction = tf.nn.relu(tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1), weights2) + biases2) 
    test_prediction = tf.nn.relu(tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) 

num_steps = 3001 

with tf.Session(graph=graph) as session: 
    tf.global_variables_initializer().run() 
    print("initialized") 
    for step in range(num_steps): 
     #pick an offset within the training data, which has been randomized. 
     #note: we could use better randomization across epochs. 
     offset = (step * batch_size) % (train_labels.shape[0] - batch_size) 
     #generate a minibatch. 
     batch_data = train_dataset[offset:(offset + batch_size), :] 
     batch_labels = train_labels[offset:(offset + batch_size), :] 
     #prepare a dictionary telling the session where to feed the minibatch. 
     #the key of the dictionary is the placeholder node of the graph to be fed, 
     #and the value is the numpy array to feed to it 
     feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels} 
     _, l, predictions = session.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)) 

답변

0

아주 나쁜 것을 볼 수 있습니다, 문제는의 구현이었다 relu() 메소드.

계산 세그먼트에서 relu()를 두 번 사용했는데 한 번만 사용해야했습니다. 변경 후 그것은 이렇게 보였다.

logits_1 = tf.matmul(tf_train_dataset, weights1) + biases1 
relu1 = tf.nn.relu(logits_1) 
logits_2 = tf.matmul(relu1, weights2) + biases2 

그리고 loss 변수의 매개 변수 logits를 relu_out에서 logits_2로 변경했습니다.

loss = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(logits=logits_2,labels=tf_train_labels)) 

마지막으로 예측 변수를 변경하여 logits_2를 사용하고 relu_out을 사용하지 않았습니다.

train_prediction = tf.nn.softmax(logits_2) 
    valid_prediction = tf.nn.softmax(
     tf.matmul(tf.nn.relu(tf.matmul(tf_valid_dataset,weights1) +biases1), weights2) + biases2) 
    test_prediction = tf.nn.softmax(
     tf.matmul(tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1), weights2) + biases2) 

정확도는 relu() 메서드를 두 번 문제였다 구현하는 이유는 아직 확실하지 않다하더라도 약 90 %

enter image description here

을 얻을 당신이 볼 수 있듯이. 내가 잘못하지 않으면 relu() 메서드는 0 또는 주어진 매개 변수의 값을 반환하므로 동일하지 않아야합니다.

누구나 자유롭게 대답 할 수 있다면