1

我正在使用 Keras(带有 tensorflow 后端)并尝试在训练期间在我的训练集上获取层输出(实际激活)(使用“fit”功能)

作为 on_batch_end 回调的一部分,有什么方法可以获取用于训练的最后一批的激活?或任何其他能够访问图层输出的方式?

我在下面找到了这段代码,但它再次对新数据运行前向传递。我正在尝试利用这样一个事实,即我的网络已经作为批处理本身训练的一部分进行了前向传递,并且只是提取了当前的激活,这可能吗?

 def get_activations(model, model_inputs, print_shape_only=False, layer_name=None):
        print('----- activations -----')
        activations = []
        inp = model.input

        model_multi_inputs_cond = True
        if not isinstance(inp, list):
            # only one input! let's wrap it in a list.
            inp = [inp]
            model_multi_inputs_cond = False

        outputs = [layer.output for layer in model.layers if
                   layer.name == layer_name or layer_name is None]  # all layer outputs

        funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs]  # evaluation functions

        if model_multi_inputs_cond:
            list_inputs = []
            list_inputs.extend(model_inputs)
            list_inputs.append(0.)
        else:
            list_inputs = [model_inputs, 0.]

        # Learning phase. 0 = Test mode (no dropout or batch normalization)
        # layer_outputs = [func([model_inputs, 0.])[0] for func in funcs]
        layer_outputs = [func(list_inputs)[0] for func in funcs]
        for layer_activations in layer_outputs:
            activations.append(layer_activations)
            if print_shape_only:
                print(layer_activations.shape)
            else:
                print(layer_activations)
        return activations
4

1 回答 1

1

您可以编写自定义回调来获取每个时期的每层激活。这里的答案有助于理解如何构建回调。我添加了一些用于访问激活的行。

from keras.callbacks import LambdaCallback
from keras import backend as k

activation_list= []
def save_act(model):
    # each object is an output tensor variable for a layer:
    output_vars= [layer.output for layer in model.layers]
    # get_output backend function for each layer
    get_outputs_funcs= [k.function([model.input], [out]) for out in output_vars]
    # fit the function for each layer, obtain the actual activations:
    layers_acts= [f([X]) for f in get_outputs_funcs]
    activation_list.append(layers_acts)
# Save activations for all layers for each epoch:
activations_callback=  LambdaCallback(on_epoch_end=lambda epoch, logs:  save_act(model))

model.fit(..., callbacks= [activations_callback], ...)

# The dimensionality of the activation object: [epoch][layer][0][input][unit]

# I usually use the mean activations for each hidden layer over the epochs (to visualise and check for signs of saturation):
n_layers= 2; n_epochs =100
layer_sizes= [10,10,10]; input_size= X.shape[0]
act_layers= np.zeros((n_layers, n_epochs))
for ep in range(n_epochs):
    for layer in range(n_layers):
        layer_outs_wrt_inputs= np.zeros((layer_sizes[layer], input_size))
        for i in range(input_size):
            perlayer_outs= activation_list[ep][layer][0][i]
            layer_outs_wrt_inputs[layer, i]= np.mean(perlayer_outs)
        ave_over_inputs= np.mean(layer_outs_wrt_inputs)
        act_layers[layer, ep]= ave_over_inputs
acts_L1= act_layers[0]; acts_L2= act_layers[1]
plt.plot(epochs, acts_L1, linestyle= 'dotted', color= 'red', label= 'layer 1')
plt.plot(epochs, acts_L2, linestyle= 'dotted', color= 'blue', label= 'layer 2')
plt.legend()
plt.show()

于 2022-01-05T12:24:40.713 回答