Keras和错误:使用序列设置数组元素 [英] Keras and Error: Setting an array element with a sequence

查看:165
本文介绍了Keras和错误:使用序列设置数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在神经网络中输入多个数据源时遇到问题.我的数据框是:

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

输入与前4列相关,输出为最后一列. 训练神经网络时,我得到Setting an array element with a sequence.

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 (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.

这是由于某些列中有数组,而其他列中有整数的事实吗?我以为Keras可以解决这个问题,但也许我错了.我不清楚如何处理来自多个来源的串联数据. 谢谢!

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!

推荐答案

在插入之前,需要将numpy数组中的列表弄平.

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

array在OP实现中为[[False, False, True], 4, -10, [False, True, False], 1]

,应展平为[False, False, True, 4, -10, False, True, False, 1].

这里是一个正在运行的jupyter笔记本,证明了这一点.

Here is a working jupyter notebook demonstrating this.

这篇关于Keras和错误:使用序列设置数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆