Own Loss Function in KERAS

2019-08-06 14:17发布

问题:

  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, bout] from every layer? Here, we need to pass few more variable than usual (y_true, y_pred). I have attached two images for your reference.

I need to implement this loss function. enter image description here

enter image description here

回答1:

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))