我正在使用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]
答案 0 :(得分:0)
原来MLP是正常的。那时我应该使用卷积神经网络。问题解决了。