Own Loss Function in KERAS

前端 未结 1 1941
面向向阳花
面向向阳花 2021-01-16 13:05
  1. How can I define my own loss function which required Weight and Bias parameters from previous layers in Keras?

  2. How can I get [W1, b1, W2, b2, Wout,

相关标签:
1条回答
  • 2021-01-16 13:51

    To answer your second part, I used the following code to get the norm of every layer in my model for visualization purposes:

    for layer in model.layers:
        if('Convolution' in str(type(layer))):
            i+=1
            layer_weight = []
            for feature_map in layer.get_weights()[0]:
                layer_weight.append(linalg.norm(feature_map) / np.sqrt(np.prod(feature_map.shape)))
            l_weights.append((np.sum(layer_weight)/len(layer_weight), layer.name, i))
            weight_per_layer.append(np.sum(layer_weight)/len(layer_weight))
            conv_weights.append(layer_weight)
    

    Now to use this in a loss function I would try something like this:

    def get_loss_function(weights):
       def loss(y_pred, y_true):
           return (y_pred - y_true) * weights # or whatever your loss function should be
       return loss
    model.compile(loss=get_loss_function(conv_weights), optimizer=SGD(lr=0.1))
    
    0 讨论(0)
提交回复
热议问题