1

I was stuck on training process with ValueError. The following are the details:

I made flags as below.

flags = tf.app.flags
FLAGS = flags.FLAGS

flags.DEFINE_string('train', 'train.txt', 'File name of train data')
flags.DEFINE_string('test', 'test.txt', 'File name of train data')
flags.DEFINE_string('train_dir', '/tmp/data', 'Directory to put the training data.')
flags.DEFINE_integer('max_steps', 200, 'Number of steps to run trainer.')
flags.DEFINE_integer('batch_size', 10, 'Batch size'
                     'Must divide evenly into the dataset sizes.')
flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')

Moreover training process as below:

if 1==1:
        # Tensor for images
    images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
        # Tensor for labels
        labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
        # dropout rate
        keep_prob = tf.placeholder("float")

        # call inference() 
        logits = inference(images_placeholder, keep_prob)
        # call loss()
        loss_value = loss(logits, labels_placeholder)
        # call training()
        train_op = training(loss_value, FLAGS.learning_rate)
        # calculate accuract
        acc = accuracy(logits, labels_placeholder)

        # prepare for saving
        saver = tf.train.Saver()
        # make Session
        sess = tf.Session()
        # initialize variables
        sess.run(tf.initialize_all_variables())
        # values on TensorBoard
        summary_op = tf.merge_all_summaries()
        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph_def)

        # Training process
        for step in range(FLAGS.max_steps):
            for i in range(len(train_image)/FLAGS.batch_size):
                # batch_size
                batch = FLAGS.batch_size*i
                # define data in placeholder by feed dict
                sess.run(train_op, feed_dict={
                images_placeholder:train_image[batch:batch+FLAGS.batch_size],
        labels_placeholder: train_label[batch:batch+FLAGS.batch_size],
                keep_prob: 0.5})

When I run this code I faced following error. How to solve this problem?

File "CNN_model.py", line 230, in <module>
    images_placeholder: train_image[batch:batch+FLAGS.batch_size],labels_placeholder: train_label[batch:batch+FLAGS.batch_size],keep_prob: 0.5})
File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 334, in run
    np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
ValueError: setting an array element with a sequence.

I add code around train_image and train_label as following.    

 

NUM_CLASSES = 5
IMAGE_SIZE = 599
IMAGE_PIXELS = IMAGE_SIZE*1*128

  

f = open("song_features.json")
 data = json.load(f)
 data = np.array(data)

 flatten_data = []
 flatten_label = []


 for line in range(len(data)):
     for_flat = np.array(data[line])
     flatten_data.append(for_flat.flatten().tolist())

     #label made as 1-of-K
     tmp = np.zeros(NUM_CLASSES)
     tmp[int(random.randint(0,4))] = 1
        flatten_label.append(tmp)

  #1 line training data
  train_image = np.asarray(flatten_data)
  train_label = np.asarray(flatten_label)

I constructing this model.

enter image description here

1 Answer 1

3

This exception is raised when TensorFlow converts the values in the feed_dict into dense NumPy ndarrays, and will depend on what's in your train_image and train_label objects.

A common cause of these errors is when feed value is a ragged list: i.e. a list of lists, where the sublists have different sizes. For example:

>>> train_image = [[1., 2., 3.], [4., 5.]]
>>> np.array(train_image, dtype=np.float32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

EDIT: Thanks for sharing your code to build train_image and train_label. I suspect the problem is in the creation of train_image, if the elements of flatten_data can have different lengths for each example. Try making the following modification to confirm this: train_image = np.asarray(flatten_data, dtype=np.float32). If you get the same ValueError, you will need to pad or crop the individual rows so that they have IMAGE_PIXELS elements.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your support. I added code around train_image and train_label.
Thanks for sharing the extra code! I updated the answer with some suggestions.
I tried train_image = np.asarray(flatten_data, dtype=np.float32) however, I got the same error. This time I added code around NUM_PIXELS.
Try adding assert for_flat.flatten().shape == (NUM_PIXELS,) before appending to flatten_data. That should find any images that are the wrong size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.