我目前正在开发一个程序,用于对手写字符的32x32图像进行分类。到目前为止,这是我的代码:
import tensorflow as tf
import numpy as np
from PIL import Image
import csv
train_list = csv.reader(open("HCR_data/HCR_train_labels.csv"), delimiter=",")
test_list = csv.reader(open("HCR_data/HCR_test_labels.csv"), delimiter=",")
x = tf.placeholder(tf.float32, [None, 1024])
w = tf.Variable(tf.zeros([1024, 26]))
b = tf.Variable(tf.zeros([26]))
y = tf.nn.softmax(tf.matmul(x, w) + b)
y_ = tf.placeholder(tf.float32, [None, 26])
train_data = []
for location, label in train_list:
image = Image.open(location).convert('L')
image = np.asarray(image).flatten()
image = [0.0 if p==255 else 1.0 for p in image]
one_hot = [0.0] * 26
one_hot[ord(label) - 65] = 1.0
train_data.append((image, one_hot))
train_data = np.asarray(train_data)
np.random.shuffle(train_data)
test_data = []
for location, label in test_list:
image = Image.open(location).convert('L')
image = np.asarray(image).flatten()
image = [0.0 if p==255 else 1.0 for p in image]
one_hot = [0.0] * 26
one_hot[ord(label) - 65] = 1.0
test_data.append((image, one_hot))
test_data = np.asarray(test_data)
np.random.shuffle(test_data)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for i in range(269):
batch_xs = train_data[(i*7):((i+1)*7),0]
batch_ys = train_data[(i*7):((i+1)*7),1]
#print(len(batch_ys)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: test_data[:,0], y_: test_data[:,1]}))
我的基础是Tensorflow网站上的MNIST教程,但是无论何时构建程序我都会收到错误:
Traceback (most recent call last):
File "C:...\HCR.py", line 57, in <module>
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 766, in run
run_metadata_ptr)
File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 937, in _run
np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
File "C:...\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
请帮助,我已经尝试了很多方法来改变我输入feed_dict
的方式,但我无法弄清楚什么是错的。 Batch_xs应为7x1024,batch_ys应为7x26。我知道只有7个批次不会那么准确,但我想首先想出这个错误。
答案 0 :(得分:0)
list(...)
时,我在数组周围添加了feed_dict
旁注:我的准确性很差,53%...... lmao
更新:只需删除tf.nn.softmax(...)
y
即可达到80%