Recovering token ids from normalized input?

username: hatbossman
cooked: <p>Trying to figure out conceptually what is wrong here. I have a flow that does the following:</p>
<p>Text → Produce Token Ids → <strong>Normalize Ids</strong> → AutoEncoder → Calculate CosineEmbeddingLoss.</p>
<p>This process seems to work and ultimately completes the task but I cannot reproduce any of the inputs as the token ids are normalized so <code>tokenizer.decode()</code> does not work. Is there a better way to do this?</p>
<p>Relevant code:</p>
<pre><code class="lang-auto">class AE(nn.Module): 
  def __init__(self):
    super().__init__()
    self.encoder = torch.nn.Sequential(
      torch.nn.Linear(512, 512), # Input is in the format (Batchx512) 
      torch.nn.ReLU(),
      torch.nn.Linear(512, 256),
      torch.nn.ReLU(),
    )
    self.decoder = torch.nn.Sequential(
      torch.nn.Linear(256, 512),
      torch.nn.ReLU(),
      torch.nn.Linear(512, 512),
      torch.nn.Sigmoid(),
    )

  def forward(self, x):
    x = self.encoder(x)
    x = self.decoder(x)
    return x
</code></pre>
<p>And training</p>
<pre><code class="lang-auto">  def training_step(self, batch, batch_idx):
    x = batch
    x_hat = self.net(x)
    loss_fn = nn.CosineEmbeddingLoss()
    loss = loss_fn(x_hat, x, torch.Tensor([1.]))
    return loss
</code></pre>
<p>I was thinking to do <code>F.normalize</code> in the encoder but again I am not sure how to undo that transform witht he decoder or how I would emit outputs. Or do I need to swap <code>nn.Sigmoid</code> with <code>nn.ReLU</code>? (Seems CosineSim is scaling sensitive, so not sure if I’d need to swap my loss)</p>

username: InnovArul
cooked: <p>Could you pl. elaborate a bit about what happens in <strong>Normalize Ids</strong> step?</p>

username: hatbossman
cooked: <p>Sure. I am using the <code>RobertaTokenizer</code> from HuggingFace. From my text examples I will load the input_ids via <code>dataset['train'] = tokenizer(dataset['train'], padding='max_length', truncation=True).input_ids</code> and then normalize that data.</p>
<p>Ex: <code>dataset['train'] = F.normalize(dataset['train'].float())</code>. I’ve tried mapping the input_ids to their float equivalent from after normalization but this does not work b/c the Sigmoid can output <em>any</em> arbitrary float not just those of normalized input_ids.</p>
<p>Additionally, if I try using ReLU as my final output activation I find the cosine scores look ok but seem to produce small values (0.6…) rather than (0, 32100) which is my range of input ids.</p>

username: InnovArul
cooked: <p>I am not sure about your usecase.<br>
But if you want to reconstruct the original values from normalized values, you can just multiply the normalized values by the norm.<br>
i.e., store <code>torch.norm(dataset['train'].float())</code> and later multiply by it.</p>

username: hatbossman
cooked: <p>That almost did the trick!</p>
<p>I am trying to do token reconstruction with an AutoEncoder. So the task is… given my model’s output, minimize the cosine distance between the generated and real text. The problem when I multiply by norm is that the predicted values are sometimes greater than my maximum vocab size (ex: 32100). It also seems that if I try to validate this by recovering my offset its slightly lossy in that 4-5 tokens might get mangled (im guessing due to long-&gt; float conversion)</p>
<p>Ex:</p>
<pre><code class="lang-auto">test_norm = torch.linalg.norm(tokenized_test['code'].float(), dim=1)
...
tokenized_test = F.normalize(tokenized_test['code'].float())
...
decode_text = test_example[0] * test_norm[0].to(device) # where test_example[0] is my first input_id sequence
tokenizer.decode(decode_text) 
</code></pre>
<p>This works well and gets me 99% of my tokenized_test string back. However if I make a prediction its unclear what norm id use. Would I use the norm of the row which was used for the example input? Or the mean of test/train?</p>
<p>My prediction output has a range of 0, 1 (I now clamp the last output with Sigmoid vs ReLU bc I realized this would cause numeric instability since I’ve normalized). When I look at the two tensors I see the following:</p>
<pre><code class="lang-auto"># Test Tensor
tensor(0., device='cuda:0')
tensor(0.2775, device='cuda:0')
tensor(0.0109, device='cuda:0')

# Prediction Tensor
tensor(0., device='cuda:0', grad_fn=&lt;UnbindBackward0&gt;)
tensor(0.9348, device='cuda:0', grad_fn=&lt;UnbindBackward0&gt;)
tensor(0.0211, device='cuda:0', grad_fn=&lt;MeanBackward0&gt;)
</code></pre>
<p>So if I scale this tensor by test_example[0]'s norm I will get values well above my 32100 vocab size (<code>91774...</code>).</p>
<p>I imagine I am just doing something wrong with my translation of data and that perhaps I should use an encoding that is in the range of (0,1) vs (0, 32100)?</p>

username: InnovArul
cooked: <p>When dealing with words (and its tokens) and similar discrete value scenarios, I have come across architectures that mostly use softmax final layer. i.e., the prediction ends up as a K-way classifier, where K = number of words/possible discrete values &amp; trained with cross-entropy loss.<br>
Is there any reason for you not following such an approach?</p>

username: hatbossman
cooked: <p>I see. I figured since my manual validation would be cosine similarity (1.0 = both tensors are the same), that should be my training metric. Because my data is normalized I’d need BCELoss + Sigmoid right? This method seems to work but hits the token recovery issue again.</p>
<p>Re: Softmax + Cross-Entropy. It seems like if I do not use normalized data (allowing inputs to range from (0, 32100)) training does not converge beyond a loss of <code>4e+6</code>. It also seems like my outputs typically range from (0,1) rather than the full (0,32100) spectrum. Or am I misunderstanding and still need to normalize the input token ids?</p>
<pre><code class="lang-auto">    super().__init__()
    self.encoder = torch.nn.Sequential(
      torch.nn.Linear(512, 512), # Input is in the format (64x512) (Batch Size, Shape)
      torch.nn.ReLU(),
      torch.nn.Linear(512, 500), # I see many people do not apply activation on the output of encoder, is this ok?
    )
    self.decoder = torch.nn.Sequential(
      torch.nn.Linear(500, 512),
      torch.nn.ReLU(),
      torch.nn.Linear(512, 512), # Ditto since I am using softmax.
    )
</code></pre>
<p>And</p>
<pre><code class="lang-auto">  def training_step(self, batch, batch_idx):
    x = batch.float()
    x_hat = self.net(x)
    loss_fn = nn.CrossEntropyLoss()
    loss = loss_fn(x_hat, x)
</code></pre>
<p>I think my output is incorrect. I see negatives and max values &lt; 1.0. If I use MSE loss explodes (I assume because of the input values) and does not ever settle below <code>1e+7</code>.</p>
<hr>
<p>With normalizing It’s also interesting that this works.</p>
<pre><code class="lang-auto">test = example * test_norm[0]
test = test.long()
print(tokenizer.decode(test, skip_special_tokens=True))
</code></pre>
<p>But I cannot recover <code>example</code> with a discrete mapping from 0, 32100 mapped to (0,1). Much less the prediction outputs. Ex:</p>
<pre><code class="lang-auto">reconstructed_tokens = []
lookup = list(M.keys()) # Where M holds a mapping from (0,1) -&gt; (0,32100)

for p in tx:
  reconstructed_tokens.append(bisect.bisect(lookup, p.detach()))

res = torch.clamp(torch.tensor(reconstructed_tokens), 0, vocab_size-1)
tokenizer.decode(res)
</code></pre>

username: kearneymw
cooked: <p>Do you have any idea as to why many of these multi class CrossEntropy models include sigmoid? It seems like guidance typically says to pass raw linear values into the loss function, but I have also run into this sigmoid layer and have yet to figure out why. If anything, it seems, to me at least, it would more quickly introduce rounding error, but maybe there is some logic I’m missing?</p>

username: hatbossman
cooked: <p>This is because we want to truncate the result. If we have a linear output we could have exploding weights within the network whereas we want to say given N classes what is the probability of activation.</p>
<p>In this case (Cross Entropy) we have 2 options Cross Entropy or Cross Entropy with logits. Passing your raw values into CE is fine bc sigmoid will be applied, but CE w/ logits will not be needed if you already have applied sigmoid at the end. I’d imagine there is some performance implications of unbounded activation pre-CE calculation, so if you know you will be using CE, sigmoid makes sense.</p>