Use a pretrained model as a loss

issamou: <p>The goal is to train a multi feature generative model using the guidance of another model, an score estimator in this example.</p>
<h1><a name="data-1" class="anchor" href="#data-1"></a>Data</h1>
<p>Categorical dataset consisting of N features. Each feature has its own set attributes.<br>
A numerical feature acts as the score of the combination of feature attributes.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/e/fe2042e76122bd14e99cd0db6fe981802b2bd2c4.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fe2042e76122bd14e99cd0db6fe981802b2bd2c4" title="IMG_20231117_071708"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe2042e76122bd14e99cd0db6fe981802b2bd2c4_2_690x158.jpeg" alt="IMG_20231117_071708" data-base62-sha1="Ag6oCsIyzzkGxjLWUuS8oYMR9s0" width="690" height="158" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe2042e76122bd14e99cd0db6fe981802b2bd2c4_2_690x158.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe2042e76122bd14e99cd0db6fe981802b2bd2c4_2_1035x237.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/e/fe2042e76122bd14e99cd0db6fe981802b2bd2c4_2_1380x316.jpeg 2x" data-dominant-color="959594"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">IMG_20231117_071708</span><span class="informations">1945×447 112 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<h1><a name="pretrained-estimator-model-2" class="anchor" href="#pretrained-estimator-model-2"></a>Pretrained Estimator Model</h1>
<p>I trained a regression model on this data. Such as it takes as input the whole row and outputs the estimated score of it.<br>
The whole row is represented as the concatenation of the embeddings of each feature attribute.</p>
<pre data-code-wrap="python"><code class="lang-python">estimator = EstimatorNet()

row = torch.concat([embeddings["feature_1"], embeddings["feature_2"],...])

estimated_score = estimator(row)
</code></pre>
<p>The goal here is to have the estimator capture the complex feature interactions as well as predict the associated score.</p>
<h1><a name="to-train-generator-model-3" class="anchor" href="#to-train-generator-model-3"></a>To train Generator Model</h1>
<p>The goal is to generate features given a context, such that the score is optimized. Kind of a recommendation problem where you can’t afford to just run the estimator on every possible feature combination and then do a top k ranking. So we have to extract ‘‘scoring’’ knowledge from the estimator to guide the generator during its training and push it to generate high scoring combinations.</p>
<p>I already trained such generator and it works okay. The last layers of my generator are as follows</p>
<pre data-code-wrap="python"><code class="lang-python">latent = first_layers(context)
feature1_logits = fully_connected_1(latent)
feature2_logits = fully_connected_2(latent)

generated_feature1 = feature1_logits.argmax()
generated_feature2 = feature2_logits.argmax()
</code></pre>
<p>I used logits and argmax for demonstration purposes even though in reality I used softmax and then argmax.</p>
<p>My idea during training is that when the generator encounters a high scoring row, it tries to reproduce it. And when it encounters a bad scoring row it tries to avoid it AND come up with one that is estimated to be high scoring.</p>
<h2><a name="reproduction-4" class="anchor" href="#reproduction-4"></a>Reproduction</h2>
<p>I used cross entropy loss with softmax between row_feature and generated_feature then summed over all the features. Weighted by the inverse estimated score to give high loss for low scoring generations.</p>
<pre data-code-wrap="python"><code class="lang-python">loss1 = CrossEntropyLoss(feature1_logits.softmax(), row_feature1)
loss2 = CrossEntropyLoss(feature2_logits.softmax(), row_feature2)

reproductionloss = (loss1 + loss2)
loss = reproductionloss/estimator(row_features)
</code></pre>
<h1><a name="the-problem-5" class="anchor" href="#the-problem-5"></a>The problem</h1>
<p>So far so good. Even though the estimator’s first layer is an embedding layer ( breaks the gradients flow ) as well as having argmax in the last layer of the generator. The loss used to trained the generator is differentiable.</p>
<p>However, the feature interactions are so complex that a single feature swap converts the whole row from good scoring to bad scoring. Therefore, naive summation of cross entropy losses fails miserably in such cases as it gives almost the same reproductionloss. The only thing that changes is the estimated score, which is not differentiable.</p>
<p>My idea is to learn a new metric such that it is capable of detecting if the feature swap is meaningful enough.</p>
<p>I trained row_features aligner such that it projects the embeddings from the estimator in a new representation in which we can use the cosine similarity to define if a given row is similar to another in terms of behaviour ( not only in terms of absolute feature attribute matching )</p>
<pre data-code-wrap="python"><code class="lang-python">class Aligner(nn.Module):
    def __init__(self, estimator_model):
        self.estimator_model = estimator_model
        self.projection_head = nn.Linear(64, 64)
    def forward(row1, row2):
        latent1 = self.estimator_model.to_latent(row1)
        latent2 = self.estimator_model.to_latent(row2)
        projection1 = self.projection_head(latent1)
        projection2 = self.projection_head(latent2)
        CosineSimilarity(projection1, projection2)
       
aligner = Aligner(estimator)
cosine = aligner(row1, row2)
</code></pre>
<p>This Aligner model actually captures differences better than the sum of cross entropies.</p>
<p>However, I have no idea how to plug it to the generator model during training without breaking the gradients flow. Since this Aligner also starts with an embedding layer ( from the Pretrained Estimator ), and if I completely remove the cross entropy loss from the generator loss then nothing will be differentiable.</p>
<p>My current Idea is to feed softmaxes directly from the generator to the aligner, and use the softmax as a weighted sum of each feature embeddings in the aligner.</p>
<p>I tried a straight through estimator approach to bypass the argmax+embedding block, the gradients do flow but I suspect it is meaningless ( since the gradients at the embeddings level indicate how each dimension in the embeddings should move and not which other embedding to switch to )</p>
<p>I also tried to code my own version of embeddings with one hot indexing ( linear layer instead of lookup table of embeddings ) coupled with a differentiable version of argmax / gumbell softmax.</p>
<p>Do you people have any other suggestions on how you would approach such?</p>