Skip to content

Bug in LogitLens Tutorial Plotting #462

Description

@oliveradk

The plotly cell used in the logit lens tutorial https://nnsight.net/notebooks/tutorials/logit_lens/ has a bug:

If there are duplicate input_words, plotly removes the duplicates, which offsets the columns and (leads to confusing results)

adding the following snip-it solves the problem (though possible there are less verbose methods)

def make_unique_with_invisible_chars(strings):
    seen = {}
    result = []
    for string in strings:
        if string in seen:
            seen[string] += 1
            modified_string = string + '\u200B' * seen[string]
            result.append(modified_string)
        else:
            seen[string] = 0
            result.append(string)
    return result
input_words = make_unique_with_invisible_chars(input_words)

(original cell)

import plotly.express as px
import plotly.io as pio

if is_colab:
    pio.renderers.default = "colab"
else:
    pio.renderers.default = "plotly_mimetype+notebook_connected+notebook"

fig = px.imshow(
    max_probs.detach().cpu().numpy(),
    x=input_words,
    y=list(range(len(words))),
    color_continuous_scale=px.colors.diverging.RdYlBu_r,
    color_continuous_midpoint=0.50,
    text_auto=True,
    labels=dict(x="Input Tokens", y="Layers", color="Probability")
)

fig.update_layout(
    title='Logit Lens Visualization',
    xaxis_tickangle=0
)

fig.update_traces(text=words, texttemplate="%{text}")
fig.show()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions