Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras and Error: Setting an array element with a sequence

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!

like image 419
maurock Avatar asked Oct 29 '25 14:10

maurock


1 Answers

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.

like image 80
Shadi Avatar answered Oct 31 '25 02:10

Shadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!