Keras and Error: Setting an array element with a sequence

Related searches

I have a problem with the input of multiple data sources in my neural network. My dataframe is:

                           0  1  2                   3   4  
0        [True, True, False]  3 -1  [False, True, True]  1

The input is related to the first 4 columns, the output is the last one. When I train my neural network I get Setting an array element with a sequence.

def network():
        model = Sequential()
        model.add(Dense(output_dim=50, activation='relu', input_dim=4))
        model.add(Dense(output_dim=50, activation='relu'))
        model.add(Dense(output_dim=50, activation='relu'))
        model.add(Dense(output_dim=1, activation='softmax'))
        opt = RMSprop(lr=0.00025)
        model.compile(loss='mse', optimizer=opt)
        return model

    data = pd.DataFrame()
    state = [0]*3
    for i in range(3):
        state[i]= random.choice([True, False])
    move = random.randint(1,4)
    reward = random.choice([-1, -10, 10])
    future_state = [0]*3
    for i in range(3):
        future_state[i] = random.choice([True, False])
    Q = 1
    array = [state, move, reward, future_state, Q]

    data = data.append([array])
    training = data.drop([4], axis = 1)
    target = data[4]
    model = network()
    model.fit(training,target,epochs=2)

Python traceback:

Traceback (most recent call last):
  File "D:/Documents/PycharmProjects/SnakeGA/try.py", line 33, in <module>
    model.fit(training,target,epochs=2)
  File "D:\Anaconda3\lib\site-packages\keras\models.py", line 845, in fit
    initial_epoch=initial_epoch)
  File "D:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1485, in fit
    initial_epoch=initial_epoch)
  File "D:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1140, in _fit_loop
    outs = f(ins_batch)
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2075, in __call__
    feed_dict=feed_dict)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1104, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
  File "D:\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

Is this due to the fact that I have arrays in some columns, and integers in other columns? I thought Keras could handle that, but maybe I'm wrong. It's not clear to me how to handle concatenated data from multiple sources. Thank you!

The list inside the numpy array needs to be flattened before insertion.

array is [[False, False, True], 4, -10, [False, True, False], 1] in the OP implementation,

and should be flattened to [False, False, True, 4, -10, False, True, False, 1].

Here is a working jupyter notebook demonstrating this.

ValueError: setting an array element with a sequence while using , ValueError: setting an array element with a sequence while using sparse I got exactly the same error with Keras 2 and tensorflow 1.0.1 . I am trying to perform a binary classification on vectors of word frequencies, ie. a bag of words. When I try to run the code I receive the following error: ValueError: setting an array element with a sequence. My X_train and Y_train variables are numpy arrays. X_train is a 2D numpy array where each row represents a single document's word

First of all, convert the input array into numpy array and convert the categorical boolean inputs into numbers. Then, give input dimension = 8 instead of 4.

ValueError: setting an array element with a sequence. � Issue #4865 , ValueError: setting an array element with a sequence. It looks like the error occurs when I compile my model at: model.fit(X_train, Y_train, or join the Keras Slack channel and ask there instead of filing a GitHub issue. [from keras.models import Sequential from keras.layers import (Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D) from keras.utils import np_utils from keras import backend as K K.set_image_dim_ordering('th') import json, pylab import cv2 import numpy as np. np.set_printoptions(threshold=np.nan) some model and data processing constants

You are trying to input 2 different types of data to the neuron of a neural network. Neural networks isn't a magical box to throw random information into it and expect it to give a reasonable output.

NN's take only numbers as input. When you flatten your data

[False, False, True, 4, -10, False, True, False, 1] to this format, what you are effectively doing is converting it into this [0,0,1,4,-10,0,1,0,1].

I am not really sure what you want from this data but, if you want only 5 features, you can take the majority outcome for those with binary values.

arr = [[False, False, True], 4, -10, [False, True, False], 1]

can be converted to

arr = [False,4,-10,False,1]

which effectively means your input is

arr=[0,4,-10,0,1]

But, before you do this, be sure what you're trying to do makes sense. You need to be able to answer questions like "what does each value represent?", "do i need to normalize the data?", "Would True/False in this dataset make sense?".

How to fix setting an array element with a sequence error?, I am trying to make a keras functional model that solves Sentiment Analysis through the amazon database. I am trying to split the input data into� Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Hello! I found that using sparse matrix would cause ValueError: setting an array element with a sequence. Version: Keras (1.2.0) tensorflow (0.12.1) Input data X <9516x28934 sparse matrix of type '<type 'numpy.float64'>' with 946932 stored elements in Compressed Sparse Row format> y numpy.ndarray (9516,)

Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

This error usually occurs when the Numpy array is not in sequence. While programming in Python, especially Numpy a library in Python, programmers encounter an error called ValueError: setting an array element with a sequence.

Comments
  • The error is produced inside numpy, not keras. It is unclear which part of your code produces it, you should include full code and the python traceback.
  • You are right, I edited including the full code and the traceback. I hope it's more clear now.
  • @MauroComi I'm having this same issue! Seems to be a problem when rows in the training set contain values mixed between numeric and array...if I remove the columns that contain array values then it works
  • Why would you want to mix numeric and array in the same column? For example, what would it mean if the 1st row is "3" and the 2nd row is "[0,1,0]"?
  • @JLewkovich You and Mauro are misunderstanding the concept of input values (and neurons in a NN): basically the input to the models are always a set of numerical values. It corresponds to a set of neurons where each neuron has a single numerical value as its output. So this means that you can't consider [True, True, False] or [False, True, True] as an individual input value. Rather, they both consist of 3 separate values (or you can convert them to one or multiple values). As @shadi has pointed out in her answer, one approach is to flatten the array so you have 8 input values (not 4).
  • It doesn't work, the error is the same. After converting, the numpy array contains [list[1,0,1], 3, -1, [0,1,1]] and it gives the same error