Keras retrieve value of node before activation function

前端 未结 5 1245
感动是毒
感动是毒 2021-02-07 00:48

Imagine a fully-connected neural network with its last two layers of the following structure:

[Dense]
    units = 612
    activation = softplus

[Dense]
    unit         


        
5条回答
  •  日久生厌
    2021-02-07 01:39

    I can see a simple way just changing a little the model structure. (See at the end how to use the existing model and change only the ending).

    The advantages of this method are:

    • You don't have to guess if you're doing the right calculations
    • You don't need to care about the dropout layers and how to implement a dropout calculation
    • This is a pure Keras solution (applies to any backend, either Theano or Tensorflow).

    There are two possible solutions below:

    • Option 1 - Create a new model from start with the proposed structure
    • Option 2 - Reuse an existing model changing only its ending

    Model structure

    You could just have the last dense separated in two layers at the end:

    [Dense]
        units = 612
        activation = softplus
    
    [Dense]
        units = 1
        #no activation
    
    [Activation]
        activation = sigmoid
    

    Then you simply get the output of the last dense layer.

    I'd say you should create two models, one for training, the other for checking this value.

    Option 1 - Building the models from the beginning:

    from keras.models import Model
    
    #build the initial part of the model the same way you would
    #add the Dense layer without an activation:
    
    #if using the functional Model API
        denseOut = Dense(1)(outputFromThePreviousLayer)    
        sigmoidOut = Activation('sigmoid')(denseOut)    
    
    #if using the sequential model - will need the functional API
        model.add(Dense(1))
        sigmoidOut = Activation('sigmoid')(model.output)
    

    Create two models from that, one for training, one for checking the output of dense:

    #if using the functional API
        checkingModel = Model(yourInputs, denseOut)
    
    #if using the sequential model:
        checkingModel = model   
    
    trainingModel = Model(checkingModel.inputs, sigmoidOut)   
    

    Use trianingModel for training normally. The two models share weights, so training one is training the other.

    Use checkingModel just to see the outputs of the Dense layer, using checkingModel.predict(X)

    Option 2 - Building this from an existing model:

    from keras.models import Model
    
    #find the softplus dense layer and get its output:
    softplusOut = oldModel.layers[indexForSoftplusLayer].output
        #or should this be the output from the dropout? Whichever comes immediately after the last Dense(1)
    
    #recreate the dense layer
    outDense = Dense(1, name='newDense', ...)(softPlusOut)
    
    #create the new model
    checkingModel = Model(oldModel.inputs,outDense)
    

    It's important, since you created a new Dense layer, to get the weights from the old one:

    wgts = oldModel.layers[indexForDense].get_weights()
    checkingModel.get_layer('newDense').set_weights(wgts)
    

    In this case, training the old model will not update the last dense layer in the new model, so, let's create a trainingModel:

    outSigmoid = Activation('sigmoid')(checkingModel.output)
    trainingModel = Model(checkingModel.inputs,outSigmoid)
    

    Use checkingModel for checking the values you want with checkingModel.predict(X). And train the trainingModel.

提交回复
热议问题