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

Input lct #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 61 additions & 36 deletions src/evaluation/finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def get_perf_stats(labels, measures):
def main(args):
t0 = time.time()
# set up results directory

options = Options.load(args.option_file)
args.output_dim = options.emb_dim

Expand Down Expand Up @@ -181,10 +182,13 @@ def main(args):
print("aggregation method: sum", file=logfile, flush=True)
else:
raise ValueError("No aggregation method specified")
if args.finetune:
print("finetuning (jjepa weights not frozen)", file=logfile, flush=True)
if not args.from_input:
if args.finetune:
print("finetuning (jjepa weights not frozen)", file=logfile, flush=True)
else:
print("lct (jjepa weights frozen)", file=logfile, flush=True)
else:
print("lct (jjepa weights frozen)", file=logfile, flush=True)
print("lct from input features", file=logfile, flush=True)

# define the global base device
world_size = torch.cuda.device_count()
Expand Down Expand Up @@ -220,23 +224,32 @@ def main(args):
)

# initialise the network
model = load_model(options, args.load_jjepa_path, args.device)
net = model.target_transformer
if not args.from_input:
model = load_model(options, args.load_jjepa_path, args.device)
net = model.target_transformer

# initialize the MLP projector
if args.from_input:
args.output_dim = 20 * 30 * 4
finetune_mlp_dim = args.output_dim
if args.finetune_mlp:
finetune_mlp_dim = f"{args.output_dim}-{args.finetune_mlp}"
proj = Projector(2, finetune_mlp_dim).to(args.device)
print(f"finetune mlp: {proj}", flush=True, file=logfile)
if args.finetune:
optimizer = optim.Adam(
[{"params": proj.parameters()}, {"params": net.parameters(), "lr": 1e-6}],
lr=1e-4,
)
net.train()
if not args.from_input:
if args.finetune:
optimizer = optim.Adam(
[
{"params": proj.parameters()},
{"params": net.parameters(), "lr": 1e-6},
],
lr=1e-4,
)
net.train()
else:
net.eval()
optimizer = optim.Adam(proj.parameters(), lr=1e-4)
else:
net.eval()
optimizer = optim.Adam(proj.parameters(), lr=1e-4)

loss = nn.CrossEntropyLoss(reduction="mean")
Expand Down Expand Up @@ -272,19 +285,22 @@ def main(args):
y = labels.to(args.device)
x = x.view(x.shape[0], x.shape[1], -1)
x = x.to(args.device)
batch = {"particles": x.to(torch.float32)}
reps = net(
batch,
subjet_mask.to(args.device),
subjets_meta=subjets.to(args.device),
split_mask=None,
)
if args.flatten:
reps = reps.view(reps.shape[0], -1)
elif args.sum:
reps = reps.sum(dim=1)
if not args.from_input:
batch = {"particles": x.to(torch.float32)}
reps = net(
batch,
subjet_mask.to(args.device),
subjets_meta=subjets.to(args.device),
split_mask=None,
)
if args.flatten:
reps = reps.view(reps.shape[0], -1)
elif args.sum:
reps = reps.sum(dim=1)
else:
raise ValueError("No aggregation method specified")
else:
raise ValueError("No aggregation method specified")
reps = x.view(x.shape[0], -1) # flatten the input features
out = proj(reps)
batch_loss = loss(out, y.long()).to(args.device)
batch_loss.backward()
Expand All @@ -307,19 +323,22 @@ def main(args):
y = labels.to(args.device)
x = x.view(x.shape[0], x.shape[1], -1)
x = x.to(args.device)
batch = {"particles": x.to(torch.float32)}
reps = net(
batch,
subjet_mask.to(args.device),
subjets_meta=subjets.to(args.device),
split_mask=None,
)
if args.flatten:
reps = reps.view(reps.shape[0], -1)
elif args.sum:
reps = reps.sum(dim=1)
if not args.from_input:
batch = {"particles": x.to(torch.float32)}
reps = net(
batch,
subjet_mask.to(args.device),
subjets_meta=subjets.to(args.device),
split_mask=None,
)
if args.flatten:
reps = reps.view(reps.shape[0], -1)
elif args.sum:
reps = reps.sum(dim=1)
else:
raise ValueError("No aggregation method specified")
else:
raise ValueError("No aggregation method specified")
reps = x.view(x.shape[0], -1)
out = proj(reps)
batch_loss = loss(out, y.long()).detach().cpu().item()
losses_e_val.append(batch_loss)
Expand Down Expand Up @@ -474,6 +493,12 @@ def main(args):
action="store",
help="keep the transformer frozen and only train the MLP head",
)
parser.add_argument(
"--from-input",
type=int,
action="store",
help="do LCT with input features",
)
parser.add_argument(
"--train-dataset-path",
type=str,
Expand Down