ValueError:检查时出错:预期input_2具有形状(162,)但是具有形状的数组(1,)

时间:2018-03-23 05:33:06

标签: numpy keras theano

我在Keras中定义了以下模型:

[Enter]

稍后我尝试使用它进行预测,如下所示:

init_weights = he_normal()
main_input = Input(shape=(FEATURE_VECTOR_SIZE,)) #size 54
aux_input = Input(shape=(AUX_FEATURE_VECTOR_SIZE,)) #size 162
merged_input = concatenate([main_input, aux_input])

shared1 = Dense(164, activation='relu', kernel_initializer=init_weights)(merged_input)
shared2 = Dense(150, activation='relu', kernel_initializer=init_weights)(shared1)

main_output = Dense(NUM_ACTIONS, activation='linear', kernel_initializer=init_weights, name='main_output')(shared2)
aux_output = Dense(1, activation='linear', kernel_initializer=init_weights, name='aux_output')(shared2)

rms = RMSprop(lr=ALPHA)
model = Model(inputs=[main_input, aux_input], outputs=[main_output, aux_output])
model.compile(optimizer=rms, loss='mse')

然而,我得到一个错误,抱怨辅助输入的形状不正确(Keras声称它应该是形状(162,)并且它实际上是形状(1,))

但是当我打印出形状时,我得到的确正是它所要求的(见下文)。

(162,)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Traceback (most recent call last):
  File "grid_exp.py", line 94, in 
    RL_episode(max_steps)
  File "/Users/ZerkTheMighty/Code/RL2/project/Gridworld/rl_glue.py", line 220, in RL_episode
    rl_step_result = RL_step()
  File "/Users/ZerkTheMighty/Code/RL2/project/Gridworld/rl_glue.py", line 151, in RL_step
    last_action = agent.agent_step(result['reward'],result['state'])
  File "/Users/ZerkTheMighty/Code/RL2/project/Gridworld/grid_agent.py", line 170, in agent_step
    q_vals, _ = model.predict([encode_1_hot(next_state), aux_dummy], batch_size=1)
  File "/Users/ZerkTheMighty/Code/RL2/lib/python2.7/site-packages/keras/engine/training.py", line 1817, in predict
    check_batch_axis=False)
  File "/Users/ZerkTheMighty/Code/RL2/lib/python2.7/site-packages/keras/engine/training.py", line 123, in _standardize_input_data
    str(data_shape))
ValueError: Error when checking : expected input_2 to have shape (162,) but got array with shape (1,)

我不知道应该改变什么才能让它发挥作用,但我怀疑我忽略了一些显而易见的事情。建议?

我正在使用Keras 2.1.5,Theano 1.0.1,numpy 1.14.2和python 2.7.12

2 个答案:

答案 0 :(得分:1)

尝试

aux_dummy = np.zeros(shape=(1,AUX_FEATURE_VECTOR_SIZE,))

需要第一个维度来指定给模型的示例数量。

答案 1 :(得分:0)

尝试一下:

 q_vals, _ = model.predict([[encode_1_hot(next_state), aux_dummy]], batch_size=1)

[[]]就像您提供的表格