I define an embedding object with embeddings_constraint:
from tensorflow.keras.layers import Embedding
from tensorflow.keras.constraints import UnitNorm
. . .
emb = Embedding(input_dim, output_dim, name='embedding_name', embeddings_constraint=UnitNorm(axis=1))
. . .
Later on in the code, when I want to train my model which contains emb, I get an exception from the function model.fit:
RuntimeError: Cannot use a constraint function on a sparse variable.
When I don't impose the embeddings constraint on emb, however, the code does not throw an error. Moreover, I tried this with TF 1, and there it worked fine as well (with and without embeddings_constraint). According to a GitHub discussion, this appears to be TF 2 bug, though no working solution is proposed.
Any ideas how to solve this?