Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Extending the metaphor to regularization #133

Open
chrico-bu-uab opened this issue Oct 17, 2024 · 0 comments
Open

[Enhancement] Extending the metaphor to regularization #133

chrico-bu-uab opened this issue Oct 17, 2024 · 0 comments

Comments

@chrico-bu-uab
Copy link

This function pushes "weights" (prompts) to be "small" (short).

class TokenCountLoss(Module):
    def __init__(self, max_tokens):
        super().__init__()
        self.call_fn = StringBasedFunction(
            fn=partial(self._eval_df, max_tokens=max_tokens),
            function_purpose="evaluating the token count of the model's response",
        )

    @classmethod
    def _eval_df(cls, input_val: tg.Variable, max_tokens: int) -> str:
        tc = get_token_count(input_val.value)
        out = f"token count: {tc}; "
        if tc > max_tokens:
            return out + f"exceeded max tokens by {tc - max_tokens}"
        return out + "within limits"

    def forward(self, **kwargs):
        return self.call_fn(inputs=kwargs)

...
reg_loss = TokenCountLoss(max_tokens)(input_val=model.system_prompt)
losses.append(reg_loss)
total_loss = tg.sum(losses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant