Python 我不知道是什么';我的TensorFlow程序有什么问题吗

Python 我不知道是什么';我的TensorFlow程序有什么问题吗,python,tensorflow,Python,Tensorflow,如果有人能给我指点我需要解决的问题,我将不胜感激。我是TensorFlow的新手,不知道出了什么问题 守则: from PIL import ImageFont, Image, ImageDraw import tensorflow as tf import random import numpy as np x = tf.placeholder(tf.float32, [None,48,48,1]) y = tf.placeholder(tf.float32, [None,26]) def

如果有人能给我指点我需要解决的问题,我将不胜感激。我是TensorFlow的新手,不知道出了什么问题

守则:

from PIL import ImageFont, Image, ImageDraw
import tensorflow as tf
import random
import numpy as np

x = tf.placeholder(tf.float32, [None,48,48,1])
y = tf.placeholder(tf.float32, [None,26])

def initW(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

w1 = initW([5,5,1,32])
w2 = initW([5,5,32,64])
w3 = initW([64*12*12,26])
keep_prob = tf.placeholder(tf.float32)

saver = tf.train.Saver()

def model(x,w1,w2,w3,keep_prob):
    l1c = tf.nn.relu(tf.nn.conv2d(x, w1, strides = [1,1,1,1], padding = "SAME"))
    l1p = tf.nn.max_pool(l1c, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME")
    l2c = tf.nn.relu(tf.nn.conv2d(l1p, w2, strides = [1,1,1,1], padding = "SAME"))
    l2p = tf.nn.max_pool(l2c, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
    l2r = tf.reshape(l2p,[-1,64*12*12])
    l2d = tf.nn.dropout(l2r, keep_prob)
    l3f = tf.matmul(l2d,w3)

    return l3f

model = model(x,w1,w2,w3,keep_prob)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = model))

op = tf.train.AdamOptimizer(0.000002).minimize(cost)

def imgData(char):
    img = Image.new("L", (48, 48), color=255)
    f = ImageFont.truetype("Arial.ttf", 50)
    ImageDraw.Draw(img).text((25 - f.getsize("W")[0] / 2, 0), "W", font=f)
    return np.reshape(img.getdata(),[48,48,1])

def oneHot(C,reshape=False):
    arr = [0] * 26
    arr[charToInt(C)] = 1
    if reshape:
        return np.reshape(arr,[1,26])
    else:
        return arr

def charToInt(C):
    return ord(C) - ord("A")

def accuracy():
    alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    testSet = np.array([imgData(C) for C in alphabet])
    testLabels = np.array([oneHot(C) for C in alphabet])
    accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(model,1),tf.argmax(y,1)),tf.float32))
    print (sess.run(accuracy,feed_dict={x:testSet,y:testLabels,keep_prob:1.0}))

def predict(char):
    probabilities = sess.run(model, feed_dict= {x:[imgData(char)], keep_prob: 0.5})[0]
    for n in range(26):
        print (chr(ord("A") + n) + ": " + "{0:.2f}".format(probabilities[n]))

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    random.shuffle(alphabet)

    train = True
    if train:
        for n in range(20):
            for C in alphabet:
                sess.run(op, feed_dict = {
                    x: [imgData(C)],
                    y: oneHot(C,reshape=True),
                    keep_prob: 0.5
                })
            print("cost: " + str(sess.run(cost,feed_dict={
                x:[imgData(C)],
                y:oneHot(C,reshape=True),
                keep_prob: 1.0
            })))
            saver.save(sess,"./model")

    else:
        saver.restore(sess,"./model")
        accuracy()
        # predict("B")
这是一个简单的卷积神经网络程序,它获取大写字母的图像并输出预测字符
x
是由
imgData(char)
生成的48x48图像的输入张量,
y
是表示大写字符的输出

cnn模型可以在
模型(x,w1,w2,w3,keep_prob)
中找到。 尽管尝试了不同的学习速度和时代数字,成本总是收敛到3.3左右

accurity()
使用测试集打印出模型的测量精度。它总是说0.0384615。我想要
predict()
显示给定图像中各种字符的概率,但现在它给出了各种奇怪的数字


非常感谢。

您的
imgData
方法有一个输入错误:如果我理解正确,您应该写出
char
,而不是
W
(请参阅的文档)。否则,您的金色标签将始终是
W
字符。这就是为什么您总是获得0.0384的精度,即1/26,即您只学会识别26个字符中的一个(这是预期的,因为除了
W
,您从不为其他字符提供标签)

我还将您的学习率提高了10倍。然后,模型在约10次迭代中过度拟合:

0, cost: 4.96586 accu 0.0769231
1, cost: 5.01186 accu 0.115385
2, cost: 3.9491 accu 0.230769
3, cost: 2.56 accu 0.384615
4, cost: 1.99173 accu 0.423077
5, cost: 2.51248 accu 0.576923
6, cost: 2.19388 accu 0.730769
7, cost: 3.79979 accu 0.769231
8, cost: 2.25762 accu 0.807692
9, cost: 1.27227 accu 0.884615
10, cost: 0.964877 accu 0.884615
11, cost: 2.15709 accu 0.923077
12, cost: 0.482007 accu 0.961538 
13, cost: 0.733133 accu 1.0

哎呀,糟了,这对我来说是个大失败:P谢谢!NP顺便说一句,你在飞行中建立训练数据的方法很有趣!如果你能添加一些噪音,那会更有趣。嘿,添加噪音的好主意。我也在考虑转换和使用不同的字体。
0, cost: 4.96586 accu 0.0769231
1, cost: 5.01186 accu 0.115385
2, cost: 3.9491 accu 0.230769
3, cost: 2.56 accu 0.384615
4, cost: 1.99173 accu 0.423077
5, cost: 2.51248 accu 0.576923
6, cost: 2.19388 accu 0.730769
7, cost: 3.79979 accu 0.769231
8, cost: 2.25762 accu 0.807692
9, cost: 1.27227 accu 0.884615
10, cost: 0.964877 accu 0.884615
11, cost: 2.15709 accu 0.923077
12, cost: 0.482007 accu 0.961538 
13, cost: 0.733133 accu 1.0