简体   繁体   English

在 tensorflow 2 中使用回调信息训练神经网络

[英]Use callback information for training neural network in tensorflow 2

I am currently trying to extend the code suggested in this question: How to get layer weight while training?我目前正在尝试扩展此问题中建议的代码: How to get layer weight while training?

By using the code below:通过使用下面的代码:

class CustomCallback(keras.callbacks.Callback):        
    def on_train_batch_begin(self, batch, logs=None):
        print(model.layers[2].get_weights())

history = model.fit(train_x, train_y, 
                    epochs=200,
                    validation_data=(test_x, test_y), 
                    verbose=1, 
                    callbacks=[CustomCallback()])

I can get the weight of a specific layer of the neural network while training (at the beginning of each iteration of gradient descent for each batch).我可以在训练时获得神经网络特定层的权重(在每批梯度下降的每次迭代开始时)。

My question is, can I use this information and allow loss function's access to it?我的问题是,我可以使用这些信息并允许损失函数访问它吗? I would like to use this information to define a new expression to be optimized as a part of custom loss function to be used.我想使用此信息来定义要优化的新表达式,作为要使用的自定义损失 function 的一部分。

In other words, I would like to access information obtained by callback during each training step for each batch, and feed this information to a custom loss function to define one of objective function term to be optimized.换句话说,我想访问在每个批次的每个训练步骤期间通过回调获得的信息,并将此信息提供给自定义损失 function 以定义要优化的目标 function 术语之一。

For instance, consider the following custom loss:例如,考虑以下自定义损失:

def Custom_Loss(y_true, y_pred):
    return f(y_true, y_pred)

I would like to modify the above defined loss to be as follows:我想将上述定义的损失修改如下:

def Custom_Loss(y_true, y_pred):
    return f(y_true, y_pred) + g(layer_2_weight, y_pred)

One very noob trick that I came up with (but no idea how to do so) is define a "dummy" variable which is redefined inside the callback and use this for the loss function definition, but I am not sure if this would work or if this is a right approach to follow.我想出的一个非常菜鸟的技巧(但不知道该怎么做)是定义一个“虚拟”变量,该变量在回调内部重新定义并将其用于丢失 function 定义,但我不确定这是否可行或如果这是正确的做法。

If the above-described task is achievable, would it be possible to get some example code or any post in stackoverflow (I tried to find it, but I could not find any. If there is any, please let me know)?如果上述任务是可以实现的,是否可以获得一些示例代码或stackoverflow中的任何帖子(我试图找到它,但我找不到任何东西。如果有的话,请告诉我)?

Personally, I would do one of the two things listed below,就个人而言,我会做下面列出的两件事之一,

  • Use Eager Execution ~ While TensorFlow by default now uses this, you would need to, instead of using a Callback and running model.fit() , run the model itself as a function. Use Eager Execution ~ While TensorFlow by default now uses this, you would need to, instead of using a Callback and running model.fit() , run the model itself as a function. You can get better information about eager execution on the official TensorFlow Website您可以在官方 TensorFlow 网站上获得有关 Eager Execution 的更好信息
  • Use PyTorch ~ I know this question pertains to TensorFlow, but if using PyTorch is an option for you, then such a model training would become simpler in PyTorch. Use PyTorch ~ I know this question pertains to TensorFlow, but if using PyTorch is an option for you, then such a model training would become simpler in PyTorch.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM