Python Tensorflow“;ValueError:使用序列设置数组元素。";在sess.run()中

Python Tensorflow“;ValueError:使用序列设置数组元素。";在sess.run()中,python,arrays,python-3.x,numpy,tensorflow,Python,Arrays,Python 3.x,Numpy,Tensorflow,我目前正在开发一个程序,对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.c

我目前正在开发一个程序,对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.
回溯(最近一次呼叫最后一次):
文件“C:…\HCR.py”,第57行,在
sess.run(train_step,feed_dict={x:batch_xs,y_u:batch_ys})
文件“C:…\AppData\Local\Programs\Python\Python35\lib\site packages\tensorflow\Python\client\session.py”,第766行,正在运行
运行_元数据_ptr)
文件“C:…\AppData\Local\Programs\Python\35\lib\site packages\tensorflow\Python\client\session.py”,第937行,正在运行
np\u val=np.asarray(subfeed\u val,dtype=subfeed\u dtype)
asarray中的文件“C:…\AppData\Local\Programs\Python\Python35\lib\site packages\numpy\core\numeric.py”,第531行
返回数组(a,数据类型,copy=False,order=order)
ValueError:使用序列设置数组元素。

请帮忙,我已经尝试了很多方法来改变我的
feed\u dict
的输入方式,但我不知道出了什么问题。批次X应为7x1024,批次Y应为7x26。我知道只有7个批次不会那么准确,但我想先找出这个错误。

哈哈,好吧,我在大约20秒后修复了自己的问题,只是注意到各个阵列之间没有逗号。因此,每当我将数组输入到
feed\u dict时,我就在数组周围添加了
list(…)

旁注:我的准确率很差,53%。。。lmao


更新:通过删除
y

tf.nn.softmax(…)
可以打印批处理的形状吗?\u xs:print(batch_xs.shape)@hars可以吗?我修复了,谢谢!