如何在Keras模型的中间添加一个热层?

时间:2019-03-04 07:03:44

标签: tensorflow machine-learning keras one-hot-encoding

我有一个自动编码器模型 像这样

    input_img = Input(shape=(132,))

    l1 = Dense(64, activation='relu')(input_img)
    l2 = Dense(32, activation='relu')(l1)
    l3 = Dense(nb_class)(l2)
    l_select = Activation("softmax")(l3)        
    l4 = Dense(32, activation='relu')(l_select)
    l5 = Dense(64, activation='relu')(l4)
    l6 = Dense(132, activation='relu')(l5)
    self.model_3 = Model(inputs=[input_img], output=l6)

,我想在softmax层之后插入一个热层 像这样         input_img = Input(shape =(132,))

    l1 = Dense(64, activation='relu')(input_img)
    l2 = Dense(32, activation='relu')(l1)
    l3 = Dense(nb_class)(l2)
    l_select = Activation("softmax")(l3)
    l7 = Lambda(K.round)(l_select)

    l4 = Dense(32, activation='relu')(l7)
    l5 = Dense(64, activation='relu')(l4)
    l6 = Dense(132, activation='relu')(l5)
    self.model_2 = Model(inputs=[input_img], output=l7)
    self.model_3 = Model(inputs=[input_img], output=l6)

我不希望任何浮点数进入我的解码层,我希望对解码层进行一定的选择。

如果我尝试使用K.argmax,K.round,则会输出错误

     ValueError: An operation has `None` for gradient. Please make sure that 
     all of your ops have a gradient defined 
     (i.e. are differentiable). Common ops without gradient: K.argmax, 
     K.round, K.eval.

有什么方法可以在模型中间插入一个热层吗?

0 个答案:

没有答案