1

Hello There I hope you guys are in good health. I am new to deep learning and havning some problem in traning my deep neural network on Cifar10 dataset. I am getting value error setting an array element with sequence I am sharing my traning function.

code

def traning_neuralNetwork(x_train,y_train):
    total_epochs=10
    total_loss=0
    epoch_loss=0
    batch_size=200
    num_batch = int(np.ceil(48000/batch_size))
    prediction=neural_network(x)
    cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
    optimizer=tf.train.AdamOptimizer().minimize(cost)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range (total_epochs):

            total_loss=0
            for _ in range (num_batch):
                x_train,y_train=next_batch(batch_size,x_train,y_train)
                _,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
                total_loss+=epoch_loss
            print('Epoch ',epoch, " loss = ",total_loss)

        print("Traning Complete!")
        correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
        accuracy=tf.reduce_mean(tf.cast(correct,'float'))
        print('accuracy',accuracy.eval({x:x_test,y :y_test}))

Error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-53-aeb4ef85487e> in <module>()
----> 1 traning_neuralNetwork(x_train,y_train)

<ipython-input-52-432dcd8eb993> in traning_neuralNetwork(x_train, y_train)
     67             for _ in range (num_batch):
     68                 x_train,y_train=next_batch(batch_size,x_train,y_train)
---> 69                 _,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
     70                 total_loss+=epoch_loss
     71             print('Epoch ',epoch, " loss = ",total_loss)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    906     try:
    907       result = self._run(None, fetches, feed_dict, options_ptr,
--> 908                          run_metadata_ptr)
    909       if run_metadata:
    910         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1110             feed_handles[subfeed_t] = subfeed_val
   1111           else:
-> 1112             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
   1113 
   1114           if (not is_tensor_handle_feed and

/opt/conda/lib/python3.6/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    490 
    491     """
--> 492     return array(a, dtype, copy=False, order=order)
    493 
    494 

ValueError: setting an array element with a sequence.
Madhan Varadhodiyil
  • 2,086
  • 1
  • 14
  • 20
Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
  • It seems that `x` and `y` are `tf.placeholder` defined outside `traning_neuralNetwork` function. if `next_batch` is generator function defined by `yield`, you should define `batch_i = next_batch(batch_size,x_train,y_train)` in outer loop(#epoch) , then ` x_train,y_train=next(batch_i)` to get next batch data in inner loop (#batch). – BugKiller Aug 05 '18 at 04:28
  • what is the purpose of next(batch_i) here. I am using only next_batch(batch_size,x_train,y_train) for generating next batch – Sohaib Anwaar Aug 05 '18 at 06:18
  • and I define x and y inside the function but error is same. – Sohaib Anwaar Aug 05 '18 at 06:25
  • The code in the generator function only executes when next() is called on the generator object. assuming `batch_i` is a generator object. every time you call `next`, code run unitill `yield`, next time `next` invoke code after `yield`. https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do – BugKiller Aug 05 '18 at 06:51

0 Answers0