minterp

scripts/steer.py

376 lines · View on GitHub · Linked from 7. Steer

1"""
2Step 7: does adding a direction change the answer as the description predicts? See docs/07-steer.md.
3
4Two things are steered, both at a name's position after block 4, with the
5directions the earlier steps suggest.
6
7who acts    On a clean prompt (A acts, B is due), add a direction that
8            should turn "A acts" into "B acts", and see whether the answer
9            becomes the flipped prompt's. Unless it says otherwise, a
10            direction is built on four sentence patterns and used on the
11            fifth:
12              own            the difference between the prompt's own two
13                             streams, flipped minus clean: at dose 1 this
14                             is a patch of step 3, the most a direction
15                             can do
16              pair           the same difference for the same two names,
17                             averaged over the other four patterns
18              names          `pair` with a term for the first-mentioned
19                             name, a term for the second-mentioned one,
20                             and nothing that belongs to the two together
21              means          the mean stream when B acts minus the mean
22                             when A acts, over both versions of the prompt
23              order          the mean of `pair` over all pairs: what every
24                             flipped prompt has that its clean twin lacks,
25                             whoever the names are
26              means + order  the sum of the last two
27              reader         the direction in which a reader of the acting
28                             name (step 2) raises B's score over A's, at
29                             the length of `means`: a reader has no length
30                             of its own
31              latents        step 6's latent for B at its usual value and
32                             A's taken away, through the dictionary's
33                             decoder. The values are from four patterns;
34                             the choice of latent is step 6's, on all five
35            and, as controls:
36              kind           the mean stream over the "She" names minus
37                             the mean over the "He" names, A and B left
38                             out, towards B's kind: nothing where A and B
39                             are of one kind
40              other names    `means` for two names that are not in the
41                             prompt, of the same kinds as B and A
42              random         a random direction of the length of `means`
43              random, long   a random direction of the length of `pair`
44              means + random `means` plus a random direction of the length
45                             of `order`
46              wipe           the mean stream of the prompt's own pattern,
47                             over all pairs and both versions, in place of
48                             the prompt's stream at dose 1
49            A direction can lower the answer by damaging what the position
50            holds, so the doses run below zero as well.
51
52kind        After a sentence about one name the model goes on with " She"
53            or " He". Add to a "He" name the direction that should make it
54            a "She" name, and take it from a "She" name. Unless it says
55            otherwise, a direction is built from the other five sentences
56            and without the name it is used on:
57              means          the mean stream over "She" names minus the
58                             mean over "He" names
59              rebuilt        the part of `means` that the dictionary
60                             rebuilds
61              missed         the part it misses
62              rebuilt, without the shared latent
63                             and its two halves: the latents that are
64                             higher on "He" names, and those higher on
65                             "She" names
66              latent         the one latent of step 6 that the six "She"
67                             names share, at its mean value over the other
68                             five sentences, through the decoder
69              random         a random direction of the length of `means`
70
71For every dose it records the effect, and how far the whole next-token
72distribution is from the one the steering aims at, outside the tokens that
73were meant to move (a KL divergence, with the aimed-at distribution as the
74reference).
75
76    uv run scripts/steer.py
77    uv run scripts/steer.py --model seed1
78"""
79
80import argparse
81import json
82
83import torch
84
85from minterp import probe, steer, tasks
86from minterp import sae as S
87from minterp.common import eot, load, rounded, save
88from minterp.tap import Tap
89
90parser = argparse.ArgumentParser()
91parser.add_argument("--model", default="seed0", help="seed0, seed1 or seed2: the steps before must have run on it")
92parser.add_argument("--block", type=int, default=4)
93parser.add_argument("--dict", default="", help="another dictionary of the same place, by the name scripts/sae.py gave it (step 8)")
94args = parser.parse_args()
95
96model, tok, meta = load(args.model)
97device = next(model.parameters()).device
98sae = S.load(S.path(meta["name"], args.block, args.dict), device)
99tag = f"-{args.dict}" if args.dict else ""  # the records of another dictionary are kept beside the first
100key = ("resid", args.block)
101with open(f"runs/features/features-{meta['name']}{tag}.json") as f:
102    step6 = json.load(f)
103head_latent = {x["name"]: x["the head's latent"] for x in step6["names"]}
104# The latent the veto head rests on most for the "She" names: one for all six in the first dictionary. Another dictionary
105# may give one name a latent of its own, so it is the one most of them share.
106theirs = [head_latent[q] for q in step6["names that go on with She"]]
107shared = max(sorted(set(theirs)), key=theirs.count)
108people = tasks.names(tok)
109k = len(people)
110name_ids = torch.tensor([tok.encode_ordinary(" " + p)[0] for p in people], device=device)
111she = torch.tensor([p in step6["names that go on with She"] for p in people], device=device)
112DOSES = (-2.0, -1.0, -0.5, 0.0, 0.25, 0.5, 0.75, 1.0, 1.5, 2.0)
113generator = torch.Generator().manual_seed(0)  # the two controls called `random`
114more = torch.Generator().manual_seed(1)  # the other random controls, so that adding one does not move the first
115result = {"model": meta, "block": args.block, "dictionary": args.dict or "first", "doses": DOSES, "names that go on with She": step6["names that go on with She"]}
116series = lambda values: rounded(torch.tensor(values))
117cosine = lambda a, b: torch.nn.functional.cosine_similarity(a, b, dim=1)
118
119
120@torch.no_grad()
121def stream_at(tokens, position):
122    with Tap(model, keep=("resid",)) as tap:
123        logits, _ = model(tokens)
124    return tap[key][:, position].float(), logits[:, -1].float()
125
126
127# ---------------------------------------------------------------- who acts
128batches = tasks.batches(tok, device, people=people)
129A, B, C = (torch.tensor([people.index(t[i]) for t in batches[0].names], device=device) for i in range(3))
130rows = torch.arange(len(A), device=device)
131acts = torch.stack([A, B])  # (versions, prompts): who acts on the clean and on the flipped prompt
132# Two names that are not in the prompt, of the same kinds as B and A: what `means` does when it is about other people.
133D, E = steer.stand_ins(A, B, C, she)
134# The yardstick for a name: the names of its kind that are in no version of the prompt and are not the control's stand-ins.
135apart = torch.ones(len(A), k, dtype=torch.bool, device=device)
136for names in (A, B, C, D, E):
137    apart[rows, names] = False
138own_kind = {"A": apart & (she[None] == she[A][:, None]), "B": apart & (she[None] == she[B][:, None])}
139# A term for the first-mentioned name, a term for the second, and a constant: the projection that keeps only those.
140additive = steer.term_per_name(A, B, k)  # (prompts, prompts)
141
142at_name, plain, third = [], [], []
143for batch in batches:
144    per = [stream_at(getattr(batch, v), batch.pos["S"]) for v in ("clean", "flipped")]
145    at_name.append(torch.stack([x for x, _ in per]))
146    plain.append(torch.stack([l for _, l in per]))
147    third.append(stream_at(batch.neutral, batch.pos["S"])[1])
148at_name, plain = torch.stack(at_name), torch.stack(plain)  # (patterns, versions, prompts, dim) and (..., vocab)
149kinds = {"both She": she[A] & she[B], "both He": ~she[A] & ~she[B], "one of each": she[A] != she[B]}
150NAMES = ("own", "pair", "names", "means + order", "means", "order", "reader", "latents", "kind", "other names", "random", "random, long", "means + random", "wipe")
151who = {"directions": NAMES, "pairs": {n: int(m.sum()) for n, m in kinds.items()}, "templates": []}
152OUTSIDE, FROM = "to the flipped prompt, outside the two names", "from the clean prompt, outside the two names"
153for p, batch in enumerate(batches):
154    others = [q for q in range(len(batches)) if q != p]
155    x = at_name[others]  # (patterns, versions, prompts, dim): what the directions are built from
156    actor = acts[None].expand(len(others), -1, -1)
157    mean_when = torch.stack([x[actor == i].mean(0) for i in range(k)])  # (names, dim): the mean stream when each name acts
158    pair = (x[:, 1] - x[:, 0]).mean(0)
159    order = pair.mean(0, keepdim=True).expand_as(pair)
160    means = mean_when[B] - mean_when[A]
161    directions = {"own": at_name[p, 1] - at_name[p, 0], "pair": pair, "names": additive @ pair, "means + order": means + order, "means": means, "order": order}
162    length = means.norm(dim=1, keepdim=True)
163
164    # the reader's direction: where its score for B rises fastest against its score for A
165    W, _, _, spread = probe.weights(x.reshape(-1, x.shape[-1]).cpu(), actor.reshape(-1).cpu(),
166                                    torch.tensor(others)[:, None, None].expand(len(others), 2, len(A)).reshape(-1), k)
167    grad = (W / spread.T).T.float().to(device)  # (names, dim): the gradient of each name's score with respect to the stream
168    directions["reader"] = grad[B] - grad[A]
169    directions["reader"] = directions["reader"] / directions["reader"].norm(dim=1, keepdim=True) * length
170
171    # the latents' direction: B's latent at its usual value and A's taken away. Nothing where the two share a latent.
172    with torch.no_grad():
173        z = sae.encode(x.reshape(-1, x.shape[-1]))
174    usual = torch.stack([z[actor.reshape(-1) == i, head_latent[people[i]]].mean() for i in range(k)])  # each name's latent when it acts
175    column = torch.stack([steer.writes(sae, head_latent[q]) for q in people])  # (names, dim)
176    directions["latents"] = usual[B, None] * column[B] - usual[A, None] * column[A]
177    # Step 6 set the two names' latents in the flipped run to their clean values. Its mirror, in this prompt's own two streams:
178    with torch.no_grad():
179        change = sae.encode(at_name[p, 1]) - sae.encode(at_name[p, 0])  # (prompts, width)
180    la, lb = (torch.tensor([head_latent[people[i]] for i in who_.tolist()], device=device) for who_ in (A, B))
181    only = torch.zeros_like(change)
182    only[rows, la], only[rows, lb] = change[rows, la], change[rows, lb]
183    mirror = (only @ sae.dec.detach()) * sae.scale
184    apart_ = la != lb  # where the two names share a latent there is nothing to compare
185    # the controls
186    directions["kind"] = steer.kind_only(mean_when, A, B, she)
187    directions["other names"] = mean_when[D] - mean_when[E]
188    directions["random"] = steer.like(means, generator)
189    directions["random, long"] = steer.like(pair, more)
190    directions["means + random"] = means + steer.like(order, more)
191    directions["wipe"] = at_name[p].mean((0, 1))[None] - at_name[p, 0]  # at dose 1 the stream becomes the pattern's mean
192
193    ld = lambda logits: tasks.logit_diff(logits, batch.right, batch.wrong)
194    clean, flipped = ld(plain[p, 0]), ld(plain[p, 1])
195    gap = (clean - flipped).mean()
196    names_out = torch.stack([batch.right, batch.wrong], dim=1)
197    own = {n: lambda logits, n=n, t=t: steer.against(logits[:, name_ids], t, own_kind[n]) for n, t in (("A", A), ("B", B))}
198    between = steer.elsewhere(plain[p, 1], plain[p, 0], names_out)
199    entry = {"template": batch.template, "gap": gap.item(), "stream size at the acting name": at_name[p, 0].norm(dim=1).mean().item(),
200             "pairs": {n: {"gap": (clean - flipped)[m].mean().item()} for n, m in kinds.items()},
201             "length": {name: {**{"all": d.norm(dim=1).mean().item()}, **{n: d[m].norm(dim=1).mean().item() for n, m in kinds.items()}} for name, d in directions.items()},
202             "cosine with own": {name: cosine(d, directions["own"]).mean().item() for name, d in directions.items()},
203             "cosine with means": {name: cosine(d, means).mean().item() for name, d in directions.items()},
204             "the latents' direction against the mirror of step 6's move": {
205                 "cosine": cosine(directions["latents"][apart_], mirror[apart_]).mean().item(),
206                 "length ratio": (directions["latents"][apart_].norm(dim=1).mean() / mirror[apart_].norm(dim=1).mean()).item()},
207             # What the measures read on prompts nobody steered.
208             "plain": {"a third name acts, share": ((clean - ld(third[p])).mean() / gap).item(),
209                       "the flipped prompt turned": (flipped < 0).float().mean().item(), "the clean prompt turned": (clean < 0).float().mean().item(),
210                       "from the clean prompt to the flipped, outside the two names": between.mean().item(),
211                       "the same, by pair": {n: between[m].mean().item() for n, m in kinds.items()}},
212             "steered": {}}
213    for name, d in directions.items():
214        out = {m: [] for m in ("share", "turned", "newly turned", "A against its own kind", "B against its own kind", OUTSIDE, FROM)}
215        by, far = {n: [] for n in kinds}, {n: [] for n in kinds}
216        grouped = {x: {n: [] for n in kinds} for x in ("A", "B")}
217        for dose in DOSES:
218            logits = steer.steered(model, batch.clean, args.block, d, [batch.pos["S"]], dose)
219            moved = clean - ld(logits)
220            out["share"].append((moved.mean() / gap).item())
221            out["turned"].append((ld(logits) < 0).float().mean().item())  # A is above B
222            out["newly turned"].append(((ld(logits) < 0) & (clean >= 0)).float().mean().item())  # and was not before
223            for n in ("A", "B"):
224                way = (own[n](logits) - own[n](plain[p, 0])).mean() / (own[n](plain[p, 1]) - own[n](plain[p, 0])).mean()
225                out[f"{n} against its own kind"].append(way.item())
226            short = steer.elsewhere(plain[p, 1], logits, names_out)
227            out[OUTSIDE].append(short.mean().item())
228            out[FROM].append(steer.elsewhere(plain[p, 0], logits, names_out).mean().item())
229            for n, m in kinds.items():
230                by[n].append(moved[m].mean().item())
231                far[n].append(short[m].mean().item())
232                for x in ("A", "B"):
233                    grouped[x][n].append(((own[x](logits) - own[x](plain[p, 0]))[m].mean() / (own[x](plain[p, 1]) - own[x](plain[p, 0]))[m].mean()).item())
234        entry["steered"][name] = {**{m: series(v) for m, v in out.items()}, "moved, in logits, by pair": {n: series(v) for n, v in by.items()},
235                                  f"{OUTSIDE}, by pair": {n: series(v) for n, v in far.items()},
236                                  **{f"{x} against its own kind, by pair": {n: series(v) for n, v in grouped[x].items()} for x in ("A", "B")}}
237    who["templates"].append(entry)
238result["who acts"] = who
239
240# ---------------------------------------------------------------- kind
241# Not the sentence step 5 sorted the names with.
242SENTENCES = ("Once upon a time, {N} found a red ball.", "One morning, {N} was very happy.", "Yesterday, {N} played in the garden.",
243             "After lunch, {N} saw a little dog.", "Today, {N} wanted to bake a cake.", "That night, {N} could not sleep.")
244she_id, he_id = (tok.encode_ordinary(w)[0] for w in (" She", " He"))
245# Every form of the two pronouns that is one token, with and without a space and a capital.
246forms = sorted({v for w in ("he", "she", "her", "his", "him", "hers", "himself", "herself") for v in (w, " " + w, w.capitalize(), " " + w.capitalize()) if len(tok.encode_ordinary(v)) == 1})
247LEAVE = {"the two pronouns and the names": torch.cat([torch.tensor([she_id, he_id], device=device), name_ids]),
248         "the two pronouns, His and Her, and the names": torch.cat([torch.tensor([she_id, he_id] + [tok.encode_ordinary(w)[0] for w in (" His", " Her")], device=device), name_ids]),
249         "every form of the pronouns and the names": torch.cat([torch.tensor([tok.encode_ordinary(v)[0] for v in forms], device=device), name_ids])}
250LEAVE = {n: ids[None].expand(k, -1) for n, ids in LEAVE.items()}
251gap = lambda l: l[:, she_id] - l[:, he_id]
252cases = []
253for sentence in SENTENCES:
254    tokens = torch.tensor([[eot(tok)] + tok.encode_ordinary(sentence.format(N=q)) for q in people], device=device)
255    position = (tokens[0] == name_ids[0]).nonzero()[0, 0].item()
256    assert (tokens[:, position] == name_ids).all()
257    cases.append((tokens, position, *stream_at(tokens, position)))
258streams = torch.stack([x for _, _, x, _ in cases])  # (sentences, names, dim)
259with torch.no_grad():
260    every = sae.encode(streams)  # (sentences, names, width)
261    codes = every[..., shared]  # the shared latent at the name
262    before = ((streams / sae.scale - sae.b) @ sae.enc + sae.b_enc)[..., shared]  # the same before the top-k cut and the clamp at zero
263same = she[:, None] == she[None, :]
264unlike = ~torch.eye(k, dtype=torch.bool, device=device)
265KIND = ("means", "rebuilt", "missed", "rebuilt, without the shared latent", "latent", "random", "latents higher on He names", "latents higher on She names, without the shared one")
266kind = {"directions": KIND, "sentences": SENTENCES, "the shared latent": shared, "names that share it": [q for q in step6["names that go on with She"] if head_latent[q] == shared], "names per kind": int(she.sum()), "order": people, "pronoun forms left out": forms,
267        "the latent fires on": {"She names": (codes[:, she] > 0).float().mean().item(), "He names": (codes[:, ~she] > 0).float().mean().item()},
268        "the latent on She names": {"mean": codes[:, she].mean().item(), "largest": codes[:, she].max().item()},
269        # Why the dictionary reads back less than was written: its encoder's reading of one unit of the latent's own direction,
270        # and where that reading starts on a "He" name, against the latent's usual value on "She" names.
271        "the encoder's reading of one unit of the latent's direction": (sae.dec[shared] @ sae.enc[:, shared]).item(),
272        "the reading before the cut on He names, against the usual value": (before[:, ~she].mean() / codes[:, she].mean()).item(), "templates": []}
273for s, (tokens, position, x, logits) in enumerate(cases):
274    rest = torch.tensor([q for q in range(len(SENTENCES)) if q != s], device=device)
275    pool, coded = streams[rest], every[rest]
276    means, rebuilt, without, lower, higher = [], [], [], [], []
277    for i in range(k):
278        keep = torch.ones(k, dtype=torch.bool, device=device)
279        keep[i] = False
280        means.append(pool[:, keep & she].mean((0, 1)) - pool[:, keep & ~she].mean((0, 1)))
281        differ = coded[:, keep & she].mean((0, 1)) - coded[:, keep & ~she].mean((0, 1))  # (width,): the same difference in latents
282        rebuilt.append((differ @ sae.dec.detach()) * sae.scale)
283        differ[shared] = 0
284        without.append((differ @ sae.dec.detach()) * sae.scale)
285        lower.append((differ.clamp(max=0) @ sae.dec.detach()) * sae.scale)  # the two halves of `without`, by sign
286        higher.append((differ.clamp(min=0) @ sae.dec.detach()) * sae.scale)
287    means, rebuilt, without, lower, higher = (torch.stack(v) for v in (means, rebuilt, without, lower, higher))  # (names, dim): towards "She"
288    usual = codes[rest][:, she].mean()
289    directions = {"means": means, "rebuilt": rebuilt, "missed": means - rebuilt, "rebuilt, without the shared latent": without,
290                  "latent": (usual * steer.writes(sae, shared)).expand(k, -1), "random": steer.like(means, generator),
291                  "latents higher on He names": lower, "latents higher on She names, without the shared one": higher}
292    sign = torch.where(she, -1.0, 1.0)[:, None]  # add to a "He" name, take from a "She" name
293
294    def between(reference, changed, ids):
295        """From each name's reference distribution to each name's changed one: (reference j, name i)."""
296        return torch.stack([steer.elsewhere(reference[j].expand(k, -1), changed, ids) for j in range(k)])
297
298    entry = {"sentence": SENTENCES[s], "stream size at the name": x.norm(dim=1).mean().item(),
299             "length": {name: d.norm(dim=1).mean().item() for name, d in directions.items()},
300             "cosine with means": {name: cosine(d, means).mean().item() for name, d in directions.items()},
301             "latents that differ by more than half a unit between the kinds": int(((coded[:, she].mean((0, 1)) - coded[:, ~she].mean((0, 1))).abs() > 0.5).sum()),
302             "the latent that differs most between the kinds": int((coded[:, she].mean((0, 1)) - coded[:, ~she].mean((0, 1))).abs().argmax()),
303             "plain": {"She names": gap(logits)[she].mean().item(), "He names": gap(logits)[~she].mean().item(),
304                       "names on the side step 5 sorted them to": ((gap(logits) > 0) == she).float().mean().item(),
305                       "between two names of one kind": {n: between(logits, logits, ids)[same & unlike].mean().item() for n, ids in LEAVE.items()},
306                       "between two names of two kinds": {n: between(logits, logits, ids)[~same].mean().item() for n, ids in LEAVE.items()}},
307             "steered": {}}
308    for name, d in directions.items():
309        out = {m: {"She names": [], "He names": []} for m in ("gap", "turned", "from the name itself")}
310        far = {n: {"She names": [], "He names": []} for n in LEAVE}
311        back, each = {"She names": [], "He names": []}, []
312        for dose in DOSES:
313            steered = steer.steered(model, tokens, args.block, sign * d, [position], dose)
314            to_other = {n: (between(logits, steered, ids) * ~same).sum(0) / (~same).sum(0) for n, ids in LEAVE.items()}
315            away = steer.elsewhere(logits, steered, LEAVE["the two pronouns and the names"])
316            g = gap(steered)
317            each.append(g)
318            with torch.no_grad():
319                seen = sae.encode(x + dose * sign * d)[:, shared]  # what the dictionary reads at the name after the change
320            for group, m, flips in (("She names", she, g < 0), ("He names", ~she, g > 0)):
321                out["gap"][group].append(g[m].mean().item())
322                out["turned"][group].append(int(flips[m].sum()))
323                out["from the name itself"][group].append(away[m].mean().item())
324                back[group].append((seen[m].mean() / usual).item())
325                for n in LEAVE:
326                    far[n][group].append(to_other[n][m].mean().item())
327        entry["steered"][name] = {**{m: {g: series(v) if m != "turned" else v for g, v in groups.items()} for m, groups in out.items()},
328                                  "to a name of the other kind": {n: {g: series(v) for g, v in groups.items()} for n, groups in far.items()},
329                                  "gap by name": rounded(torch.stack(each, dim=1), 2),  # (names, doses), names in the order of tasks.names
330                                  "the shared latent as the dictionary reads it, against its usual value": {g: series(v) for g, v in back.items()}}
331    kind["templates"].append(entry)
332result["kind"] = kind
333
334# ---------------------------------------------------------------- summary
335mean = lambda items, pick: sum(pick(t) for t in items) / len(items)
336row = lambda values, form="6.2f": " ".join(f"{v:{form}}" for v in values)
337W_T, K_T, n = who["templates"], kind["templates"], range(len(DOSES))
338at = DOSES.index(1.0)
339print("who acts: steering a clean prompt at the acting name, by dose")
340print(f"  plain: a third name acts {mean(W_T, lambda t: t['plain']['a third name acts, share']):.2f} of the way; the flipped prompt has A above B in {100 * mean(W_T, lambda t: t['plain']['the flipped prompt turned']):.0f}%, "
341      f"the clean one in {100 * mean(W_T, lambda t: t['plain']['the clean prompt turned']):.1f}%; outside the two names the clean prompt is {mean(W_T, lambda t: t['plain']['from the clean prompt to the flipped, outside the two names']):.2f} nats from the flipped ("
342      + ", ".join(f"{g} {mean(W_T, lambda t, g=g: t['plain']['the same, by pair'][g]):.2f}" for g in kinds) + ")")
343for measure in ("share", "turned", "A against its own kind", "B against its own kind", OUTSIDE):
344    print(f"  {measure}\n    dose           " + row(DOSES))
345    for name in NAMES:
346        print(f"    {name:14s} " + row([mean(W_T, lambda t, i=i: t["steered"][name][measure][i]) for i in n]))
347print("  length, all and by pair; cosine with own, with means")
348for name in NAMES:
349    print(f"    {name:14s} " + ", ".join(f"{g} {mean(W_T, lambda t, g=g: t['length'][name][g]):.1f}" for g in ("all", *kinds)) + f"; {mean(W_T, lambda t: t['cosine with own'][name]):+.2f}, {mean(W_T, lambda t: t['cosine with means'][name]):+.2f}")
350print(f"  the stream's size there {mean(W_T, lambda t: t['stream size at the acting name']):.1f}")
351print("  at dose 1 by pair: share of that pair's gap (" + ", ".join(f"{g} {mean(W_T, lambda t, g=g: t['pairs'][g]['gap']):.2f} logits, {who['pairs'][g]} pairs" for g in kinds) + "); nats from the flipped prompt")
352for name in NAMES:
353    print(f"    {name:14s} " + ", ".join(f"{g} {mean(W_T, lambda t, g=g: t['steered'][name]['moved, in logits, by pair'][g][at]) / mean(W_T, lambda t, g=g: t['pairs'][g]['gap']):+.2f}" for g in kinds)
354          + "; " + ", ".join(f"{mean(W_T, lambda t, g=g: t['steered'][name][OUTSIDE + ', by pair'][g][at]):.2f}" for g in kinds))
355
356total = kind["names per kind"] * len(SENTENCES)
357print(f"\nkind: the logit of She minus the logit of He after a sentence about one name; plain {mean(K_T, lambda t: t['plain']['She names']):+.2f} for She names, {mean(K_T, lambda t: t['plain']['He names']):+.2f} for He names; "
358      f"{100 * mean(K_T, lambda t: t['plain']['names on the side step 5 sorted them to']):.0f}% of names on the side step 5 sorted them to")
359for leave in LEAVE:
360    print(f"  outside {leave}: two names of one kind are {mean(K_T, lambda t: t['plain']['between two names of one kind'][leave]):.2f} nats apart, of two kinds {mean(K_T, lambda t: t['plain']['between two names of two kinds'][leave]):.2f}"
361          + "; means at dose 1 to a name of the other kind " + f"{(mean(K_T, lambda t: t['steered']['means']['to a name of the other kind'][leave]['He names'][at]) + mean(K_T, lambda t: t['steered']['means']['to a name of the other kind'][leave]['She names'][at])) / 2:.2f}")
362print("  dose                                  " + row(DOSES))
363for name in KIND:
364    for group in ("He names", "She names"):
365        print(f"  {name[:24]:24s} {group:9s}    " + row([mean(K_T, lambda t, i=i: t["steered"][name]["gap"][group][i]) for i in n])
366              + f"   turned of {total}: " + " ".join(f"{sum(t['steered'][name]['turned'][group][i] for t in K_T):2d}" for i in n))
367back = "the shared latent as the dictionary reads it, against its usual value"
368for name in ("latent", "means"):
369    for group in ("He names", "She names"):
370        print(f"  {name}, the shared latent read back, {group:9s} " + row([mean(K_T, lambda t, i=i: t["steered"][name][back][group][i]) for i in n]))
371print("  length: " + ", ".join(f"{name} {mean(K_T, lambda t, name=name: t['length'][name]):.1f}" for name in KIND)
372      + "; cosine with means: " + ", ".join(f"{name} {mean(K_T, lambda t, name=name: t['cosine with means'][name]):+.2f}" for name in KIND)
373      + f"; the stream's size at the name {mean(K_T, lambda t: t['stream size at the name']):.1f}; the latent on She names {kind['the latent on She names']}; "
374      f"latents that differ by more than half a unit {mean(K_T, lambda t: t['latents that differ by more than half a unit between the kinds']):.0f}; pronoun forms left out {len(forms)}")
375
376save(f"runs/steer/steer-{meta['name']}{tag}.json", result)