gpt4 book ai didi

python - tensorflow 线性分类器未训练

转载 作者:行者123 更新时间:2023-11-30 22:37:38 35 4
gpt4 key购买 nike

我正在尝试为 MNIST 数据创建一个简单的线性分类器,但我无法降低损失。可能是什么问题呢?这是我的代码:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

class LinearClassifier(object):
def __init__(self):
print("LinearClassifier loading MNIST")
self._mnist = input_data.read_data_sets("mnist_data/", one_hot = True)
self._buildGraph()

def _buildGraph(self):
self._tf_TrainX = tf.placeholder(tf.float32, [None, self._mnist.train.images.shape[1]])
self._tf_TrainY = tf.placeholder(tf.float32, [None, self._mnist.train.labels.shape[1]])

self._tf_Weights = tf.Variable(tf.random_normal([784,10]), tf.float32)
self._tf_Bias = tf.Variable(tf.zeros([10]), tf.float32)
self._tf_Y = tf.nn.softmax(tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias)

self._tf_Loss = tf.reduce_mean(-tf.reduce_sum(self._tf_TrainY * tf.log(self._tf_Y), reduction_indices=[1]))
self._tf_TrainStep = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(self._tf_Loss)

self._tf_CorrectGuess = tf.equal(tf.argmax(self._tf_Y, 1), tf.arg_max(self._tf_TrainY, 1))
self._tf_Accuracy = tf.reduce_mean(tf.cast(self._tf_CorrectGuess, tf.float32))

self._tf_Initializers = tf.global_variables_initializer()

def train(self, epochs, batch_size):
self._sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
self._sess.run(self._tf_Initializers)

for i in range(epochs):
batchX, batchY = self._mnist.train.next_batch(batch_size)
self._loss, _, self._accurracy = self._sess.run([self._tf_Loss, self._tf_TrainStep, self._tf_Accuracy], feed_dict ={self._tf_TrainX: batchX, self._tf_TrainY: batchY})
print("Epoch: {0}, Loss: {1}, Accuracy: {2}".format(i, self._loss, self._accurracy))

当我通过以下方式运行此命令时:

lc = LinearClassifier()
lc.train(1000, 100)

...我得到这样的东西:

Epoch: 969, Loss: 8.19491195678711, Accuracy: 0.17999999225139618
Epoch: 970, Loss: 9.09421157836914, Accuracy: 0.1899999976158142
....
Epoch: 998, Loss: 7.865959167480469, Accuracy: 0.17000000178813934
Epoch: 999, Loss: 9.281349182128906, Accuracy: 0.10999999940395355

tf.train.GradientDescentOptimizer 未正确训练我的权重和偏差的原因可能是什么?

最佳答案

最主要的是你的学习率(0.001)太低了。我在将其更改为 0.5 后运行了此命令,就像他们在 mnist tensorflow tutorial 中所做的那样我得到的准确性和损失更像是:

Epoch: 997, Loss: 0.6437355875968933, Accuracy: 0.8999999761581421
Epoch: 998, Loss: 0.6129786968231201, Accuracy: 0.8899999856948853
Epoch: 999, Loss: 0.6442205905914307, Accuracy: 0.8999999761581421

另一件有点不寻常的事情是在你的原始代码中你有这个

self._tf_Y = tf.nn.softmax(tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias)
self._tf_Loss = tf.reduce_mean(-tf.reduce_sum(self._tf_TrainY * tf.log(self._tf_Y), reduction_indices=[1]))

在这种情况下,您将执行两次 softmax。我在更改之前确实运行过它,火车准确率约为 85%,所以它确实有所不同。另外,进行两次 softmax 理论上的解释性较差。

最后,他们在教程中提到,使用上面的softmax形式,-reduce_sum(label * log(y)),在数值上不稳定,所以最好使用内置的softmax层它计算解析上等效但数值更稳定的 softmax。应用这两项更改后,受影响的行如下所示:

self._tf_Y = tf.matmul(self._tf_TrainX, self._tf_Weights) + self._tf_Bias
self._tf_Loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=self._tf_TrainY, logits=self._tf_Y))

关于python - tensorflow 线性分类器未训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43831937/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com