运行神经网络代码时出现值错误



我正在尝试创建一个神经网络模型来预测签名是真的还是假的。我创建了包含 1044 个签名的数据集,其中包含真实和虚假签名。这是用于预处理图像的代码

DATA = '../DATASET/DATA/'
IMG_BREDTH = 150
IMG_HEIGHT = 70
# helper functions
def label_img(img):
word_label = img.split('.')[-2]
if (word_label == '1') or (word_label == '2'): return [1,0]
elif word_label == 'F': return [0,1]
def create_data_set():
data = []
for img in tqdm(os.listdir(DATA)):
if img == '.DS_Store': continue
label = label_img(img)
path = os.path.join(DATA, img)
img = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (IMG_HEIGHT, IMG_BREDTH))
data.append([np.array(img), label])
shuffle(data)
np.save('data.npy', data)
return np.array(data)

然后,我使用此代码将数据拆分为训练集和测试集

data = create_data_set()
train_x = data[:835, 0]
train_y = data[:835, 1]
test_x = data[835:, 0]
test_y = data[835:, 1]

train_x现在包含 835 张图像,train_y包含相应的标签([1,0] 表示真品,[0,1] 表示假货)。 train_x内每个图像的形状为(150,70)。 train_y的 shpae 是 (835, )

然后,我用这段代码创建了神经网络

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 2
batch_size = 100
x = tf.placeholder(tf.float32, [None, len(train_x[0])])
y = tf.placeholder(tf.float32)
# neural network model
def neural_network_model(data):
hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_layer_3 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases': tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_layer_3['weights']), hidden_layer_3['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(train_x):
start = i
end = i + batch_size
batch_x = np.array(train_x[start:end], object)
batch_y = np.array(train_y[start:end], object)
assert batch_x.shape == (100, )
_, c = sess.run(fetches=[optimizer, cost], feed_dict={x: batch_x, y: batch_y})
epoch_loss += c
i += batch_size
print('Epoch', epoch, 'completed out of', hm_epochs, 'loss', epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy: ', accuracy.eval({x: test_x, y: test_y}))

batch_x的形状为 (100,),batch_y 的形状为 (100, )。 当我运行程序时,出现以下错误

train_neural_network(x)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-32-7c7cbdae9b34> in <module>()
----> 1 train_neural_network(x)
<ipython-input-31-041caea3bd1c> in train_neural_network(x)
20                 print(batch_y.shape)
21                 assert batch_x.shape == (100, )
---> 22                 _, c = sess.run(fetches=[optimizer, cost], feed_dict={x: batch_x, y: batch_y})
23                 epoch_loss += c
24 
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
776     try:
777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
779       if run_metadata:
780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
952             np_val = subfeed_val.to_numpy_array()
953           else:
--> 954             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
955 
956           if (not is_tensor_handle_feed and
~/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
529 
530     """
--> 531     return array(a, dtype, copy=False, order=order)
532 
533 
ValueError: setting an array element with a sequence.

我做错了什么?请注意,我是一名新手开发人员,刚刚开始学习神经网络。我在网上查看了特定错误,并找到了以下链接。

"ValueError:使用序列设置数组元素。 TensorFlow

在神经网络中馈送时出现值错误

值错误:使用序列设置数组元素

我尝试按照他们在答案中指定的事情做,但这对我不起作用。

有人可以帮我吗

提前谢谢你

编辑 1: 发布此内容后,我发现了另一个具有类似问题的链接。 Tensorflow "ValueError: set a array element with a sequence." in sess.run() 我尝试对答案进行更改,但现在出现不同的错误。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-7c7cbdae9b34> in <module>()
----> 1 train_neural_network(x)
<ipython-input-35-ac9b2062de7f> in train_neural_network(x)
20                 print(batch_y.shape)
21                 assert batch_x.shape == (100, )
---> 22                 _, c = sess.run(fetches=[optimizer, cost], feed_dict={x: list(batch_x), y: list(batch_y)})
23                 epoch_loss += c
24 
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
776     try:
777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
779       if run_metadata:
780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
959                 'Cannot feed value of shape %r for Tensor %r, '
960                 'which has shape %r'
--> 961                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
962           if not self.graph.is_feedable(subfeed_t):
963             raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Cannot feed value of shape (100, 150, 70) for Tensor 'Placeholder_2:0', which has shape '(?, 150)'

我做错了什么?

错误消息只是指出您在运行优化算法和成本函数(通过 feed_dict)时向占位符 y 提供错误的维度。检查尺寸是否正确。

如果没有数据来自己运行代码,我必须猜测。但ValueError表示来自x_batch(100、150、70)的输入尺寸与占位符的形状不匹配(无、150)。

如果我正确理解您的代码,您尝试分类的每个图像都有 150x70 像素。如果这是真的,那么我会将它们中的每一个展平为一个向量,并使用该向量的长度作为占位符x的维度(None,150x70)。

此外,似乎y没有指定的形状。在这种情况下,它应该是(无,2)。如果没有特别的原因将两个标签"假"和"真"编码为二维向量,您也可以只使用一维向量。

相关内容

最新更新