tensorflow / neural-structured-learning

Training neural models with structured signals.
https://www.tensorflow.org/neural_structured_learning
Apache License 2.0
980 stars 189 forks source link

NSL with TF 2.5 custom loss #94

Closed arita37 closed 3 years ago

arita37 commented 3 years ago

Just checking if this is possible to wrap a custom loss model using @tf.function into NSL.

TF is 2.5

what are the constraints ?

chunta-lu commented 3 years ago

Could you clarify where do you want to add the custom loss? The base model used in either GraphRegularization and AdversarialRegularization can use any loss function, so I assume you want to add a custom loss function for the graph regularization loss. If this is the case, you can add it to the lib/distances.py similar to jensen_shannon_divergence, and add another option in DistanceType in the configs/configs.py.

arita37 commented 3 years ago

We have a model wichi cannot use keras fit because of custom loss.

We want onadd graph regu on top.

Thanks

On Jul 23, 2021, at 8:18, chunta-lu @.***> wrote:

 Could you clarify where do you want to add the custom loss? The base model used in either GraphRegularization and AdversarialRegularization can use any loss function, so I assume you want to add a custom loss function for the graph regularization loss. If this is the case, you can add it to the lib/distances.py similar to jensen_shannon_divergence, and add another option in DistanceType in the configs/configs.py.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

chunta-lu commented 3 years ago

Could you provide the example you of your model and the custom loss? It sounds like your base model cannot fit with your custom loss. Could you solve this issue first? The custom loss function should have the same arguments as the other Keras loss function, e.g.,

@tf.function
def custom_loss(y_true, y_pred):
 ...
arita37 commented 3 years ago

Sure.

Technically: custom_loss with @tf.function custom train step with gradient_tape with @tf.function Manual epoch loop

Should I plug the logits output into the graph regularizer low level API ?

Thanks

On Jul 23, 2021, at 10:31, chunta-lu @.***> wrote:

 Could you provide the example you of your model and the custom loss? It sounds like your base model cannot fit with your custom loss. Could you solve this issue first? The custom loss function should have the same arguments as the other Keras loss function, e.g.,

@tf.function def custom_loss(y_true, y_pred): ... — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

csferng commented 3 years ago

Hi @arita37, it looks like you are using a custom training loop. (Please correct me if this is not the case.)

To add graph regularization in a custom training loop, you can call nsl.keras.layers.PairwiseDistance to compute the graph regularization term and add it to your loss tensor in the gradient_tape scope. Alternatively, you can also call nsl.lib.pairwise_distance_wrapper or other distance functions to calculate the graph regularization term.

The inputs to the distance layer/function are a batch of "embeddings" of seed and neighbor examples, where the "embedding" can be logits or any intermediate layer's output (which your model exposes).

arita37 commented 3 years ago

Hello,

Thanks, exactly custom training loop. Also, do we need to provide the static graph node,vertex in tsv flat file format too ? (list of neightbors )

Thanks

On Jul 24, 2021, at 3:51, CS.Ferng @.***> wrote:

 Hi @arita37, it looks like you are using a custom training loop. (Please correct me if this is not the case.)

To add graph regularization in a custom training loop, you can call nsl.keras.layers.PairwiseDistance to compute the graph regularization term and add it to your loss tensor in the gradient_tape scope. Alternatively, you can also call nsl.lib.pairwise_distance_wrapper or other distance functions to calculate the graph regularization term.

The inputs to the distance layer/function are a batch of "embeddings" of seed and neighbor examples, where the "embedding" can be logits or any intermediate layer's output (which your model exposes).

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

csferng commented 3 years ago

Both nsl.lib.pairwise_distance_wrapper() and nsl.keras.layers.PairwiseDistance().call() assume the graph is already processed, and require two parameters sources and targets representing the embeddings/logits of seed and neighbor nodes.

One typical way to preprocess the graph for graph regularization is to augment training examples with their neighbor features before training. The function nsl.tool.pack_nbrs can help achieve this. This function reads a TSV-formatted graph and two TFRecord files and generates a combined TFRecord file. Then in the modeling code you can use nsl.lib.unpack_neighbor_features or nsl.keras.layers.NeighborFeatures to separate seed and neighbor features in order to compute the embeddings/logits of them.

Please see this tutorial for a concrete example of this workflow. Note that in this tutorial the wrapper nsl.keras.GraphRegularization is used for unpacking neighbor features and computing graph regularization.

csferng commented 3 years ago

Closing this issue for now. Please feel free to reopen if you have any follow-up questions.