出现错误:模型的输出张量必须是TensorFlow`Layer`的输出(因此保留了过去的层元数据)

时间:2019-10-02 09:25:57

标签: python tensorflow keras

我目前正在研究喀拉拉邦的NN模型,并且正在为这个错误而苦苦挣扎。关键是我的图层似乎都被包裹起来了,我不太了解发生了什么问题。

如果我使用“ prims_output”作为输出,则模型可以正确编译,但是当我尝试通过“ pol_sig”或“ pol_mu”作为输出时,则无法编译。

def prim_def(args):

    Dim_A, nb_primitives, input_vec = args

    prims_output = []
    for i in range(Dim_A+1):
        prim_vec = keras.layers.Dense(256, activation='relu')(input_vec)
        prims_output.append(keras.layers.Dense(nb_primitives, activation='linear')(prim_vec)) # mut_i sigmatU_i sigmatT_i sigmatAh_i sigmatI_i

    prims_output = np.array(prims_output)

    return prims_output


def prim_to_pol(args):

    Dim_A, prims_output, weights_output = args

    w_i_sigma_i_j = [tensorflow.divide(weights_output, prims_output[j]) for j in range(Dim_A)]
    w_i_sigma_i_jxmu_i = [tensorflow.multiply(w_i_sigma_i_j[j], prims_output[-1]) for j in range(Dim_A)]
    un_sigma_j = [tensorflow.reduce_sum(w_i_sigma_i_j[j], 2) for j in range(Dim_A)]
    mu_j_sigma_j = [tensorflow.reduce_sum(w_i_sigma_i_jxmu_i[j], 2) for j in range(Dim_A)]
    sigma_j = [tensorflow.math.reciprocal(un_sigma_j[j]) for j in range(Dim_A)]
    mu_j = [tensorflow.divide(mu_j_sigma_j[j], un_sigma_j[j]) for j in range(Dim_A)]

    sigma_j = np.array(sigma_j)
    mu_j = np.array(sigma_j)

    return sigma_j, mu_j



state_input = keras.layers.Input(shape=(timesteps, Dim_S)) # ~Vt, ~Tt, ~Aht, ~It
state_vec = keras.layers.Dense(512, activation='relu')(state_input)
state_vec = keras.layers.Dense(256, activation='relu')(state_vec)

goal_input = keras.layers.Input(shape=(timesteps, Dim_G)) # Vt+1, Tt+1, Aht+1, It+1 
goal_vec = keras.layers.Dense(512, activation='relu')(goal_input)
goal_vec = keras.layers.Dense(256, activation='relu')(goal_vec)

concat_vecs = keras.layers.Concatenate()([state_vec, goal_vec])
concat_vecs = keras.layers.Dense(256, activation='relu')(concat_vecs)
weights_output = keras.layers.Dense(nb_primitives, activation='sigmoid')(concat_vecs) # wt_1 ... wt_nb_primitives

prims_output = keras.layers.Lambda(prim_def)([Dim_A, nb_primitives, state_vec])

pol_sig, pol_mu = keras.layers.Lambda(prim_to_pol)([Dim_A, prims_output, weights_output])

model_MCP = keras.models.Model(inputs=[state_input, goal_input], outputs=[i for i in pol_sig])

如果有人可以解决,我将深表感谢。 感谢您的帮助。

0 个答案:

没有答案