使用Keras,如何使用回调函数为每个纪元记录每个图层的每个输出?

时间:2019-06-20 20:44:50

标签: python keras keras-layer tf.keras

我正在尝试记录每个时期每个图层的所有输出。我找到了以下解决方案:Keras/Tensorflow: Get predictions or output of all layers efficiently 但我不明白如何将其应用于我的代码。 是这样做的唯一方法还是可以使用回调函数?如果是,如何将其应用于代码? 我希望使用回调函数,因为我想使用可变数量的图层。 最好是有一个不依赖于输入数据的通用解决方案。如果有帮助,则测试输入数据的尺寸为4092,12。

我尝试以记录权重的相同方式实现回调函数,但是出现以下错误“层密集1没有入站节点”。 尝试这种方式:Keras/Tensorflow: Get predictions or output of all layers efficiently “层顺序未连接,无输入返回”

#function definition
#records weight after each epoch and stores in dictionary. input 
"weights" is object
def record_weights(weights, epoch):
    index = 0
    name_nr = 0
#itterates through each layer and gets weights if dense or cov
    for layer in model.layers:
        if("dense" in layer.name or "cov" in layer.name):
            # 1st element are weights, second is bias input
            layer_name = create_layer_name(layer, name_nr)
            weights.dic[(epoch, name_nr)] = [layer.get_weights()[0], 
layer_name]
            if(name_nr not in weights.flat_weights.keys()):
                 weights.flat_weights[name_nr] = layer.get_weights() 
[0]
            else:
                weights.flat_weights[name_nr] = 
np.append(weights.flat_weights[name_nr], layer.get_weights()[0])
            name_nr += 1
        index += 1

#tries to mimick record_weights. record each layer outputs in a 
dictionary. input "outputs" is object
def record_layer_outputs(outputs, epoch):
     index = 0
     name_nr = 0
     for layer in model.layers:
         if("dense" in layer.name or "cov" in layer.name):
             layer_name = create_layer_name(layer, name_nr)
             outputs.dic[(epoch, name_nr)] = [layer.get_output() 
[0], 
 layer_name]
             if(name_nr not in weights.flat_weights.keys()):
                  outputs.flat_outputs[name_nr] = layer.get_output() 
[0]
             else:
                 outputs.flat_outputs[name_nr] = 
 np.append(outputs.flat_outputs[name_nr], layer.get_output()[0])
             name_nr += 1
         index += 1





 #model definition
 model = Sequential()

 model.add(Dense(8))
 model.add(Activation("tanh"))

 model.add(Dense(6))
 model.add(Activation("tanh"))

 model.add(Flatten())
 model.add(Dense(1))
 model.add(Activation("sigmoid"))

 #callback functions
 weight_recording = LambdaCallback(on_epoch_end=lambda epoch, logs: 
 record_weights(weights, epoch))
 output_recording = LambdaCallback(on_epoch_end=lambda epoch, logs: 
 record_layer_outputs(outputs, epoch))

 # compile model
 sgd = optimizers.SGD(lr=0.0004)
 adam = optimizers.Adam(lr=0.0004)
 model.compile(loss="binary_crossentropy",
           optimizer=adam,
           metrics=["binary_accuracy"])

 history = model.fit(data.data, data.labels[:,0], epochs=2, 
 batch_size=512, validation_split=0.2,
                 callbacks = [weight_recording, output_recording])

预期将是模型将输出存储在对象的字典中,如重量记录回调函数 “层密集1没有入站节点。”

0 个答案:

没有答案