使用model.predict()用序列设置数组元素

时间:2018-07-22 01:58:10

标签: python keras

这是我的代码:

        bsp = Input(shape = (1,3,))
        V1 = Dense(2,activation = 'softmax')(bsp)

        # b = Dense(5, activation = "relu")(B)
        # inputs = [B]
        # merges = [b]
        S_input = Input(shape = (15,4))
        S = Reshape((15,4,1))(S_input)
        #inputs.append(S)

        x2 = inception(S)

        # merge and add
        V2 = Dense(2, activation = 'softmax')(x2)
        V = add([V1,V2])
        model = Model(inputs = [bsp,S_input], outputs = V)

        model.predict([observation[0],observation[1]])

基本上,这是一个具有2个输入和1个输出的模型。在最终计算中,将2个输入加在一起并传递到最终模型中。但是,它的错误为:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/Learning Materials/programming/python_code/RL/LabFiles_RL/stock_market_reinforcement_learning/market_pg.py", line 157, in <module>
    pg.train(verbose = 1)
  File "C:/Users/User/Desktop/Learning Materials/programming/python_code/RL/LabFiles_RL/stock_market_reinforcement_learning/market_pg.py", line 66, in train
    aprob = model.predict([observation[0],observation[1]])[0]
  File "C:\Users\User\Anaconda3\lib\site-packages\keras\engine\training.py", line 1172, in predict
    steps=steps)
  File "C:\Users\User\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 297, in predict_loop
    batch_outs = f(ins_batch)
  File "C:\Users\User\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2661, in __call__
    return self._call(inputs)
  File "C:\Users\User\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2614, in _call
    dtype=tensor.dtype.base_dtype.name))
  File "C:\Users\User\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.

正如其他类似问题中提到的那样,在我的案例中,我确保两个输入的维数相同,形状分别为(1,1,3)和(1,15,4)。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我将调查数组的数据类型,并将其设置为在构造期间浮动。这是一个小错误,在这里讨论:

ValueError: setting an array element with a sequence