Share weights between two dense layers in keras

前端 未结 1 813
萌比男神i
萌比男神i 2021-02-06 05:46

I have a code as follows. What I want to do is to share the same weights in two dense layers.

The equation for op1 and op2 layer will be like that

op1 = w1

1条回答
  •  旧时难觅i
    2021-02-06 06:16

    This uses the same layer for both sides. (Weighs and bias are shared)

    ip_shape1 = Input(shape=(5,))
    ip_shape2 = Input(shape=(5,))
    
    dense = Dense(1, activation = "sigmoid", kernel_initializer = "ones")
    
    op1 = dense(ip_shape1)
    op2 = dense(ip_shape2)
    
    merge_layer = Concatenate()([op1, op2])
    predictions = Dense(1, activation='sigmoid')(merge_layer)
    
    model = Model(inputs=[ip_shape1, ip_shape2], outputs=predictions)
    

    0 讨论(0)
提交回复
热议问题