<jupyter_start><jupyter_text>Training<jupyter_code>iter = 0
checkpoint = 0

if train:
    for epoch in range(num_epochs):
        with torch.enable_grad():
            for vector in train_iterator:
                optimizer.zero_grad()
                # Grab labels.
                target = torch.zeros((batch_size, 5))
                target[torch.arange(batch_size), vector.label] = 1
                # Grab other data for multimodal sentiment analysis.
                multimodal_data = torch.cat((vector.upvote.unsqueeze(dim=1),
                                             vector.change.unsqueeze(dim=1)), dim=1) # Upvotes + past week change
                # Apply model
                y = model(vector, multimodal_data)
                target = target.to(device)
                loss_function = nn.BCEWithLogitsLoss()
                loss = loss_function(y, target)
                loss_val = loss.item()

                # Backward
                loss.backward()
                nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
                optimizer.step()
                #scheduler.step(step // batch_size)
                if iter % print_every == 0:
                    print('Epoch:{}, Iter: {}, Loss:{:.4}'.format(epoch, iter, loss.item()))
                iter += 1

            torch.save(model, save_dir)
            if checkpoint % 3 == 0:
                print("evaluating on dev split...")
                loss_val, accuracy = evaluate(model, test_iterator, device)
                print("dev loss: ", loss_val, "dev accuracy: ", accuracy)
                checkpoint += 1<jupyter_output>Epoch:0, Iter: 0, Loss:0.7637
Epoch:0, Iter: 50, Loss:0.7625
Epoch:0, Iter: 100, Loss:0.7601<jupyter_text>Testing<jupyter_code>print("testing data, loading from path" + save_dir + " ...")
    model = torch.load(save_dir)
    loss_val, accuracy = evaluate(model, test_iterator, criterion=nn.BCEWithLogitsLoss())
    print("test loss: ", loss_val, "test accuracy: ", accuracy)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11439842209072978, 'fraction_numerical': 0.01824457593688363, 'mean_word_length': 1.927849927849928, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '91951', 'license': 'MIT', 'n_tokens_mistral': 636, 'n_tokens_neox': 578, 'n_words': 124, 'path': 'train_model.ipynb', 'repo_name': 'ejin8/cs229_project', 'size': '2028'}