gpt4 book ai didi

python - 使用 MNIST 训练模型的 Tensorflow 总是打印错误的数字

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

我正在使用 Tensorflow 和 Python 进行文本识别,但是当我尝试进行一些数字识别时,训练很好,但是当我恢复模型并使用它时,没有错误,但总是错误的预测,这是我的训练和使用模型的代码。有人能指出这件事是怎么回事吗?

培训:

import input_data
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
mn = input_data.read_data_sets("tmp/data", one_hot=True)
training_epoch = 10000
learning_rate = 0.001
batch_size = 20000
display_step = 1
n_hidden1 = 512
n_hidden2 = 512
input_size = 784
n_class = 10
x = tf.placeholder("float", [None, input_size])
y = tf.placeholder("float", [None, n_class])
h = tf.Variable(tf.random_normal([input_size, n_hidden1]))
layer1_bias = tf.Variable(tf.random_normal([n_hidden1]))
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,h),layer1_bias))
w = tf.Variable(tf.random_normal([n_hidden1, n_hidden2]))
layer2_bias = tf.Variable(tf.random_normal([n_hidden2]))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,w),layer2_bias))
output = tf.Variable(tf.random_normal([n_hidden2, n_class]))
bias_output = tf.Variable(tf.random_normal([n_class]))
output_layer = tf.matmul(layer_2, output) + bias_output
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=output_layer))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
avg_set = []
epoch_set = []
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
saver = tf.train.Saver()
for epoch in range(training_epoch):
avg_cost = 0.
batch_total = int(mn.train.num_examples/batch_size)
for i in range(batch_total):
batch_x, batch_y = mn.train.next_batch(batch_size)
print(batch_x.shape)
sess.run(optimizer, feed_dict={x:batch_x, y:batch_y})
avg_cost += sess.run(cost, feed_dict={x:batch_x, y:batch_y})/batch_total
if(epoch % display_step == 0):
print("Epoch:%d " % (epoch), "cost:", "{:.9f}".format(avg_cost))
avg_set.append(avg_cost)
epoch_set.append(epoch+1)
print("Training finished")
plt.plot(epoch_set,avg_set, 'o', label='MLP Training phase')
plt.ylabel('cost')
plt.xlabel('epoch')
plt.legend()
plt.show()
correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Model Accuracy:", accuracy.eval({x: mn.test.images, y: mn.test.labels}))
saver.save(sess, "model-batchsize-20000-epoch-10000-learningrate-0.001/tf_mlp_model.ckpt")

测试:

import numpy as np
import tensorflow as tf
import input_data
import cv2
import os
dir = os.path.dirname(os.path.realpath(__file__))
img = cv2.imread('6-1.png')
img.astype("float")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img.flatten()
img = np.expand_dims(img, axis=0)
n_hidden1 = 512
n_hidden2 = 512
input_size = 784
n_class = 10
x = tf.placeholder("float", [1, input_size])
y = tf.placeholder("float", [None, n_class])
h = tf.Variable(tf.random_normal([input_size, n_hidden1]))
layer1_bias = tf.Variable(tf.random_normal([n_hidden1]))
layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x,h),layer1_bias))
w = tf.Variable(tf.random_normal([n_hidden1, n_hidden2]))
layer2_bias = tf.Variable(tf.random_normal([n_hidden2]))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1,w),layer2_bias))
output = tf.Variable(tf.random_normal([n_hidden2, n_class]))
bias_output = tf.Variable(tf.random_normal([n_class]))
output_layer = tf.matmul(layer_2, output) + bias_output
with tf.Session() as sess:
tf.train.Saver()
tf.train.Saver().restore(sess, dir + "/model-batchsize-20000-epoch-10000- learningrate-0.001/tf_mlp_model.ckpt")
pred = tf.argmax(sess.run(output_layer, feed_dict={x:img}), 1)
print(pred.eval())

测试输出:

[2]

sess.run(output_layer, feed_dict={x:img}):

[[ -4.48937702e+00  -8.70745659e+00   2.27353687e+01   2.25894527e+01
1.72218680e-02 1.78360157e+01 2.39438486e+00 5.72816038e+00
-2.13753247e+00 4.05950975e+00]]

编辑1:我忘了说,真正的值是 6,而不是 2,这是我的 MacBook 上的 28x28 屏幕截图,链接:6-1.png

编辑2:尝试了数字2,返回:

[3]

最佳答案

事实证明,MLP 是正常的。那时我应该使用卷积神经网络。问题解决了。

关于python - 使用 MNIST 训练模型的 Tensorflow 总是打印错误的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43011842/

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