<p>As someone who has published on the question, this has been attempted many times in many different ways but doesn't work.</p>
<p>Please see the ICLR paper on the subject <a href="https://openreview.net/pdf?id=BJgza6VtPB" rel="nofollow noreferrer">https://openreview.net/pdf?id=BJgza6VtPB</a> &quot;Language GANs Falling Short&quot;</p>
<p>This is not something specific to HuggingFace, but it is specific to the mathematical fact that words are discrete objects, and that you can't slightly modify a word and slightly change the meaning of a sentence, like you can an image. Words are discrete.</p>
<p>A way to do achieve what you are trying to do would be with plug-in play language models.</p>
<p><a href="https://www.uber.com/blog/pplm/" rel="nofollow noreferrer">https://www.uber.com/blog/pplm/</a> <a href="https://arxiv.org/abs/1912.02164" rel="nofollow noreferrer">https://arxiv.org/abs/1912.02164</a></p>
<p>What they do is that they train classifiers for some desirable or undesirable characteristics, and add (or subtract) the probability of that class to the probability of each next potential token (score computed for each potential sentences including that token). That way, you either increase or decrease the probability of generating text with that characteristic.</p>
<p>Controllable text generation is a whole field, I recommend that you do a literature review.</p>
Differentially generate sentences with Huggingface Library for adversarial training (GANs)
<p>I have the following goal, which I have been trying to achieve with the Huggingface Library but I encountered some roadblocks.</p>
<p><strong>The Problem:</strong></p>
<p>I want to generate sentences in a differentiable way at training time. Why am I doing this? I want to apply a discriminator to this output to generate sentences with certain properties, which are &quot;enforced&quot; by the discriminator. These sentences will also be conditioned on a input sentence, so I need a Encoder Decoder Model.</p>
<p>To get around the non differentiability of argmax, I simply take the softmax output of the decoder and multiply it with my embedding matrix. Then I am taking this embedded input and feed it into a transformer discriminator, which simply classifies the input as original/fake. Then I backpropagate through the encoder decoder. Just as one would do it with a normal GAN.</p>
<p>So far I have tried to use the <code>EncoderDecoderModel</code> from Huggingface. This class has a method named generate, which generates sentences in a non differentiable way (greedy or beam-search). So I dug through the source code and tried to build my own differentiable generate method. I didn't get it to work though.</p>
<p><strong>Questions:</strong></p>
<ul>
<li>Is there a reasonably easy way to do this with the Huggingface Library, as I really want to use the pretrained models and everything else that comes with it?</li>
<li>Is there a way to invoke the forward method of the decoder and only generate one new token, not the whole sequence again?</li>
</ul>
<p>Thanks for your help, I would really appreciate it, I have been stuck on this for quiet a while now.</p>