在tensorflow模型中精度没有增加

2024-05-17 10:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个在tensorflow中有一层的神经网络来对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))

Tags: importsizetimetfbatchbatchesvariableexamples