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

时间:2018-03-20 08:30:01

标签: python numpy tensorflow deep-learning

我正在尝试创建一个神经网络模型来预测签名是真实的还是假的。我创建了带有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: setting an array element with a sequence." TensorFlow

Value Error while feeding in Neural Network

ValueError: setting an array element with a sequence

我尝试了他们在答案中指定的内容,但它对我不起作用。

有人可以帮帮我吗

提前谢谢

编辑1: 在张贴这个之后我在另一个链接上遇到了类似的问题。 Tensorflow "ValueError: setting an 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)'

我做错了什么?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

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