<p>It seems a bit crazy but it seems to work : instead of creating a custom loss function that I would pass in model.compile, the network computes the loss (Eq. 1 from <a href="http://arxiv.org/pdf/1708.04729.pdf" rel="nofollow noreferrer">arxiv.org/pdf/1708.04729.pdf</a>) in a function that I call with Lambda :</p>

<pre><code>loss = Lambda(lambda x: similarity(x[0], x[1], x[2]))([X_hat, X, embedding_matrix])    
</code></pre>

<p>And the network has two outputs: <code>X_hat</code> and <code>loss</code>, but I weight <code>X_hat</code> to have 0 weight and loss to have all the weight :</p>

<pre><code>model = Model(input_sequence, [X_hat, loss])
model.compile(loss=mean_squared_error,
              optimizer=optimizer,
              loss_weights=[0., 1.])
</code></pre>

<p>When I train the model : </p>

<pre><code>for i in range(epochs):
    for j in range(num_data):
        input_embedding = model.layers[1].get_weights()[0][[data[j:j+1]]]
        y = [input_embedding, 0] #The embedding of the input
        model.fit(data[j:j+1], y, batch_size=1, ...)
</code></pre>

<p>That way, the model is trained to tend <code>loss</code> toward 0, and when I want to use the trained model's prediction I use the first output which is the reconstruction <code>X_hat</code></p>
Keras : How to use weights of a layer in loss function?
<p>I am implementing a custom loss function in keras. The model is an <code>autoencoder</code>. The first layer is an Embedding layer, which embed an input of size <code>(batch_size, sentence_length)</code> into <code>(batch_size, sentence_length, embedding_dimension)</code>. Then the model compresses the embedding into a vector of a certain dimension, and finaly must reconstruct the embedding <code>(batch_size, sentence_lenght, embedding_dimension)</code>. </p>

<p>But the embedding layer is trainable, and the loss must use the weights of the embedding layer (I have to sum over all word embeddings of my vocabulary).</p>

<p>For exemple, if I want to train on the toy exemple : "the cat". The <code>sentence_length is 2</code> and suppose <code>embedding_dimension is 10</code> and the <code>vocabulary size is 50</code>, so the embedding matrix has shape <code>(50,10)</code>. The Embedding layer's output <code>X</code> is of shape <code>(1,2,10)</code>. Then it passes in the model and the output <code>X_hat</code>, is also of shape <code>(1,2,10)</code>. The model must be trained to maximize the probability that the vector <code>X_hat[0]</code> representing 'the' is the most similar to the vector <code>X[0]</code> representing 'the' in the Embedding layer, and same thing for 'cat'. But the loss is such that I have to compute the cosine similarity between <code>X</code> and <code>X_hat</code>, normalized by the sum of cosine similarity of <code>X_hat</code> and every embedding (50, since the vocabulary size is 50) in the embedding matrix, which are the columns of the weights of the embedding layer. </p>

<p>But How can I access the weights in the embedding layer at each iteration of the training process?</p>

<p>Thank you !</p>