簡體   English   中英

tensorflow模型中的精度未增加

[英]accuracy not increasing in tensorflow model

我正在嘗試在張量流中編寫一層神經網絡以對MNIST數據進行分類。 采取的隱藏層的大小為30(我也嘗試過更改它,但問題仍然存在)。

問題是:當我不使用任何隱藏層並直接執行X*w + b ,我得到85%的精度,但是當我按如下方式增加層時,精度保持為0.113,交叉熵損失為2.3。 我確信這將是一個愚蠢的錯誤。 有人可以指出代碼有什么問題嗎?

import os
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time

learning_rate = 0.01
batch_size = 128
n_epochs = 10

X = tf.placeholder(tf.float32, shape=(batch_size, 784))
Y = tf.placeholder(tf.float32, shape=(batch_size, 10))

w1 =  tf.Variable(tf.zeros( [X.shape[1], 30]))
b1 =  tf.Variable(tf.zeros([1, 30]))

z = tf.matmul(X,w1) + b1
a = tf.nn.relu(z)
w2 = tf.Variable(tf.zeros( [30, 10]))
b2  =  tf.Variable(tf.zeros([1, 10]))
logits = tf.matmul(a,w2) + b2

entropy = tf.nn.softmax_cross_entropy_with_logits(logits = logits, labels = Y)
loss = tf.reduce_mean(entropy)


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

with tf.Session() as sess:
    start_time = time.time()
    sess.run(tf.global_variables_initializer()) 
    n_batches = int(mnist.train.num_examples/batch_size)
    for i in range(n_epochs): # train the model n_epochs times
        total_loss = 0

        for _ in range(n_batches):
            X_batch, Y_batch = mnist.train.next_batch(batch_size)

            _, loss_batch = sess.run([optimizer, loss], feed_dict={X: X_batch, Y:Y_batch})
            total_loss += loss_batch
        print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))
        print('Optimization Finished!') # should be around 0.35 after 25 epochs
        preds = tf.nn.softmax(logits)
        correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y, 1))
        accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
        n_batches = int(mnist.test.num_examples/batch_size)
        total_correct_preds = 0
        for i in range(n_batches):
            X_batch, Y_batch = mnist.test.next_batch(batch_size)
            _, accuracy_batch = sess.run([correct_preds, accuracy], feed_dict={X: X_batch, Y:Y_batch}) 
            total_correct_preds += accuracy_batch   
        print('Accuracy {0}'.format(total_correct_preds/mnist.test.num_examples))

嘗試使用隨機值而不是零初始化權重,如下所述:

https://www.tensorflow.org/get_started/mnist/pros#weight_initialization

w1 =  tf.Variable(tf.truncated_normal([784, 30], stddev=0.1))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM