gpt4 book ai didi

machine-learning - 为什么我的带有 ReLU 的 1 隐藏层神经网络在 notMNIST 数据集上的准确率没有超过 18%?

转载 作者:行者123 更新时间:2023-11-30 08:33:12 24 4
gpt4 key购买 nike

我正在尝试使用 Tensorflow 实现一个具有修正线性单元和 1024 个隐藏节点的 1 隐藏层神经网络。

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

batch_size = 128

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))
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)

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

# Training computation.
logits = tf.matmul(tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1), weights2) + biases2
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits))

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

# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
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)


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))

这是我得到的输出:

Initialized
Minibatch loss at step 0: 208.975021
Minibatch accuracy: 11.7%
Validation accuracy: 10.0%
Minibatch loss at step 500: 0.000000
Minibatch accuracy: 100.0%
Validation accuracy: 10.2%
Minibatch loss at step 1000: 0.000000
Minibatch accuracy: 100.0%
Validation accuracy: 14.6%
Minibatch loss at step 1500: 0.000000
Minibatch accuracy: 100.0%
Validation accuracy: 10.2%
Minibatch loss at step 2000: 0.000000
Minibatch accuracy: 100.0%
Validation accuracy: 17.7%
Minibatch loss at step 2500: 2.952326
Minibatch accuracy: 93.8%
Validation accuracy: 26.6%
Minibatch loss at step 3000: 0.000000
Minibatch accuracy: 100.0%
Validation accuracy: 17.5%
Test accuracy: 18.1%

看起来好像过度拟合了。它在训练数据上的准确率接近 100%,但在验证和测试数据上的准确率只有 20% 左右。

这是实现具有修正线性单元的 1 隐藏层神经网络的正确方法吗?如果是这样,我怎样才能提高准确性?

最佳答案

以下是一些可以提高准确性的建议:

首先,您的隐藏层大小为 1024,看起来太大了。这可能会导致过度拟合。我会尝试将其减少到大约 50-100 左右,看看是否有所改善并从那里继续。

此外,关于这一行:

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

0.5 的学习率可能太高,尝试降低它(到 0.01、0.001 左右),看看会发生什么。最后,您还可以尝试使用 tf.train.AdamOptimizer 而不是 tf.train.GradientDescentOptimizer,因为在许多情况下它的性能更好。

关于machine-learning - 为什么我的带有 ReLU 的 1 隐藏层神经网络在 notMNIST 数据集上的准确率没有超过 18%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728359/

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