Tensorflow“ ValueError:设置具有序列的数组元素”。在sess.run()中 [英] Tensorflow "ValueError: setting an array element with a sequence." in sess.run()

查看:109
本文介绍了Tensorflow“ ValueError:设置具有序列的数组元素”。在sess.run()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个程序,用于对32x32手写字符图像进行分类。到目前为止,这是我的代码:

I am currently working on a program to classify 32x32 images of handwritten characters. This is my code so far:

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教程,但是无论何时构建程序,我都会遇到错误:

I based it off of the MNIST tutorial on the Tensorflow website, however whenever build the program I get an error:

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个批次会那么准确,但是我想先弄清楚这个错误。

Please help, I've tried many way at changing how I input my feed_dict but i cant figure out whats wrong. Batch_xs should be 7x1024 and batch_ys should be 7x26. I know only having batches of 7 wont be that accurate but i want to figure this error out first.

推荐答案

大声笑,我可以大约20秒后解决了我自己的问题,只是注意到各个数组之间没有逗号。因此,每当我将数组输入到 feed_dict

lol, okay I fixed my own problem about 20 seconds later just noticing there was no commas between the individual arrays. So I added list(...) around the array whenever I inputted it to feed_dict

侧面说明:我的准确度很差,大约53%... lmao

Side note: my accuracy was pretty bad tho, 53%... lmao

更新:只需删除即可将其提高到80% y

Update: got it to 80% just by deleting the tf.nn.softmax(...) for the y

这篇关于Tensorflow“ ValueError:设置具有序列的数组元素”。在sess.run()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆