How can I define my own loss function which required Weight and Bias parameters from previous layers in Keras?
How can I get [W1, b1, W2, b2, Wout,
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))