【发布时间】:2025-12-08 09:05:01
【问题描述】:
我为 notMNIST 数据集编写了一个网络,其中包含来自 A-J 的灰度 28*28 字母。
该模型在测试数据集上给出了 89% 的准确率,但是当我从训练数据集中检查单个图像的结果时(甚至不谈论自定义图像)它给出了错误的结果,而且,如果我一遍又一遍地运行相同的代码块,单个输出的结果会有所不同。
我一定是做错了什么,但我才刚刚开始学习深度学习。
我的模型 ->
batch_size = 128
#tensorflow datasets
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)
#weights and biases for layer 1
weights_l1 = tf.Variable(
tf.truncated_normal([image_size*image_size, 1024])
)
biases_l1 = tf.Variable(
tf.zeros([1024])
)
#output layer weights and biases
weights = tf.Variable(
tf.truncated_normal([1024, num_labels])
)
biases = tf.Variable(
tf.zeros([num_labels])
)
hl1 = tf.matmul(tf_train_dataset, weights_l1) + biases_l1
hl1 = tf.nn.relu(hl1)
logits = tf.matmul(hl1, weights) + biases
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)
)
optimizer = tf.train.GradientDescentOptimizer(0.05).minimize(loss)
#validation predictions
v_hl1 = tf.matmul(tf_valid_dataset, weights_l1) + biases_l1
v_hl1 = tf.nn.relu(v_hl1)
v_logits = tf.matmul(v_hl1, weights) + biases
#test predictions
t_hl1 = tf.matmul(tf_test_dataset, weights_l1) + biases_l1
t_hl1 = tf.nn.relu(t_hl1)
t_logits = tf.matmul(t_hl1, weights) + biases
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(v_logits)
test_prediction = tf.nn.softmax(t_logits)
训练步骤->
num_steps = 3001
with tf.Session() as session:
tf.global_variables_initializer().run()
for step in range(num_steps):
offset = (step*batch_size) % (train_labels.shape[0]-batch_size)
feed_dict = {tf_train_dataset: train_dataset[offset: offset+batch_size, :],
tf_train_labels: train_labels[offset: offset+batch_size, :]
}
_, l, predictions = session.run([optimizer, loss, train_prediction], feed_dict=feed_dict)
if(step%500 == 0):
print ('minibatch no.', step)
print ('current loss', l)
print("Minibatch accuracy: %.1f%%" % accuracy(predictions, train_labels[offset: offset+batch_size, :]))
print("Validation accuracy: %.1f%%" % accuracy(valid_prediction.eval(), valid_labels))
print("Test accuracy: %.1f%%" % accuracy(test_prediction.eval(), test_labels))
输出->
小批号0 当前损失 351.531 小批量准确率:7.8% 验证准确率:27.7% 小批号500 当前损失 6.78443 小批量准确率:82.0% 验证准确率:81.9% 小批号1000 当前损失 6.5816 小批量准确率:80.5% 验证准确率:81.9% 小批号1500 电流损失 4.70451 小批量准确率:81.2% 验证准确率:82.4% 小批号2000 当前损失 3.25759 小批量准确率:84.4% 验证准确率:79.1% 小批号2500 当前损失 4.18851 小批量准确率:82.8% 验证准确率:81.6% 小批号3000 当前损失 2.84338 小批量准确率:86.7% 验证准确率:83.0% 测试准确率:89.0%
测试图像->
(假设图像是“F”)
image_file = 'EE.png'
image_data = (ndimage.imread(image_file).astype(float) -
pixel_depth / 2) / pixel_depth
new_image_data = image_data.reshape([1, 784])
new_image_data = tf.convert_to_tensor(new_image_data)
new_image_data = tf.cast(new_image_data, dtype=tf.float32)
answer = tf.matmul(new_image_data, weights_l1) + biases_l1
answer = tf.nn.relu(answer)
answer = tf.matmul(answer, weights) + biases
answer = tf.nn.softmax(answer)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run(answer))
谢谢
【问题讨论】:
标签: python neural-network deep-learning