Sashaank
Sashaank

Reputation: 964

Value Error while running neural network code

I am trying to create a neural network model to predict whether a signature is authentic or fake. I created the data set with 1044 signatures with authentic and fake signatures. This is the code for preprocessing the images

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)

I then split the data into training and test set using this code

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 now contains 835 images and train_y contains the respective labels ([1,0] for authentic and [0,1] for fake). the shape of each image inside train_x is (150, 70). the shpae of train_y is (835, )

I then created the neural network with this code

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}))

The shape of batch_x is (100, ) and the shape of batch_y is (100, ). when I run the program I get the following error

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.

What am i doing wrong? Note that i am a novice developer and have just started learning about neural networks. I looked online on the particular error and found these following links.

"ValueError: setting an array element with a sequence." TensorFlow

Value Error while feeding in Neural Network

ValueError: setting an array element with a sequence

I tried doing what they specified in the answers but it did not work for me.

Can someone please help me out

Thank you in advance

EDIT 1: just after posting this i loked at another link with a similar problem. Tensorflow "ValueError: setting an array element with a sequence." in sess.run() I tried making the changes in the answer but now am getting a different error.

---------------------------------------------------------------------------
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)'

What am i doing wrong?

Upvotes: 1

Views: 641

Answers (2)

Eric
Eric

Reputation: 91

Without the data to run the code myself I have to guess. But the ValueError indicates that the dimension of your input from x_batch (100, 150, 70) does not match the shape of the placeholder (None, 150).

If I understand your code correctly, each image you are trying to classify has 150x70 pixels. If that's true then I would flatten each of those into a vector and use that vector's length as the placeholder x's dimension (None, 150x70).

Also, it seems, y doesn't have a specified shape. In this case it should be (None, 2). If there is no particular reason why you encode your two labels "fake" and "authentic" as two dimensional vectors, you could also use a just one dimensional vector.

Upvotes: 1

gratio
gratio

Reputation: 83

The error message simply states that you are feeding the wrong dimensions to the placeholder y while running the optimization algorithm and cost function (through feed_dict). Check whether the dimensions are correct.

Upvotes: 2

Related Questions