【问题标题】:ValueError: Error when checking : expected input_2 to have shape (162,) but got array with shape (1,)ValueError:检查时出错:预期 input_2 的形状为 (162,),但得到的数组的形状为 (1,)
【发布时间】:2018-09-01 17:38:39
【问题描述】:

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

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')

后来我尝试用它来做一个预测,如下:

aux_dummy = np.zeros(shape=(AUX_FEATURE_VECTOR_SIZE,))
print(aux_dummy.shape)
print(aux_dummy)
q_vals, _ = model.predict([encode_1_hot(next_state), aux_dummy], batch_size=1)

但是,我收到一条错误消息,抱怨辅助输入的形状不正确(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.] 回溯(最近一次通话最后): 文件“grid_exp.py”,第 94 行,在 RL_episode(max_steps) 文件“/Users/ZerkTheMighty/Code/RL2/project/Gridworld/rl_glue.py”,第 220 行,在 RL_episode rl_step_result = RL_step() 文件“/Users/ZerkTheMighty/Code/RL2/project/Gridworld/rl_glue.py”,第 151 行,在 RL_step last_action = agent.agent_step(result['reward'],result['state']) 文件“/Users/ZerkTheMighty/Code/RL2/project/Gridworld/grid_agent.py”,第 170 行,位于 agent_step q_vals, _ = model.predict([encode_1_hot(next_state), aux_dummy], batch_size=1) 文件“/Users/ZerkTheMighty/Code/RL2/lib/python2.7/site-packages/keras/engine/training.py”,第 1817 行,在预测中 check_batch_axis=False) _standardize_input_data 中的文件“/Users/ZerkTheMighty/Code/RL2/lib/python2.7/site-packages/keras/engine/training.py”,第 123 行 str(数据形状)) ValueError:检查时出错:预期 input_2 的形状为 (162,),但得到的数组的形状为 (1,)

我不知道我应该改变什么才能让它发挥作用,但我怀疑我忽略了一些明显的东西。有什么建议吗?

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

【问题讨论】:

  • 我的猜测是 1 元素对象 dtype 数组

标签: numpy keras theano


【解决方案1】:

试试

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

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    [[]] 就像你给的表格

    【讨论】:

    • model.predict([[数据]])。即 [[]]。
    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2018-09-30
    • 2020-01-11
    • 2019-04-20
    • 2021-06-30
    • 2020-04-11
    • 2023-03-25
    • 2020-05-17
    相关资源
    最近更新 更多