<jupyter_start><jupyter_text>If you want to do finetuning then set requires_grad = False Remove these two lines if you want to train entire model<jupyter_code># vanila way
# for param in model.parameters():
#    param.requires_grad = False

# freeze layers according to the number of layer
layer = 0
for param in model.parameters():
    if layer <= 30 :
        param.requires_grad = False
    layer = layer + 1<jupyter_output><empty_output><jupyter_text>for k,v in model.named_parameters():     if k!='XXX':         v.requires_grad=False固定参数for k,v in model.named_parameters():    if k!='xxx.weight' and k!='xxx.bias' :            print(v.requires_grad)理想状态下，所有值都是False  Sometimes we will change datasets, so changing some parameter of ther network is necessary<jupyter_code>model.avgpool = Identity() ## .avgpool is the name of a layer in VGG
model.classifier = nn.Sequential(
    nn.Linear(512, 100), nn.ReLU(), nn.Linear(100, num_classes)
)
model.to(target_device)<jupyter_output><empty_output><jupyter_text>load dataset<jupyter_code>train_dataset = datasets.CIFAR10(root="dataset/", train=True, transform=transforms.ToTensor(), download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)<jupyter_output>Files already downloaded and verified<jupyter_text>Loss and optimizer<jupyter_code>loss_func = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
for epoch in range(num_epochs):
    for batch_idx , ( data, targets ) in enumerate( tqdm( train_loader ) ):
        # Get data to cuda if possible
        data = data.to( device = target_device )
        targets = targets.to( device = target_device )

        # Get to correct shape
        # data.shape[0] usually is the number of examples in a batch
        # in this case, just set each sample to a long bar
        # data = data.reshape(data.shape[0], -1) this is done before fully connected layer
        # not in CNN

        # forward
        predictions = model(data)
        #targets are true labels
        loss = loss_func(predictions , targets)

        # backward
        optimizer.zero_grad() # set the gradients to zero, so that we are ready for the next loop
        loss.backward()

        # gradient descent or adam step
        optimizer.step()
def check_accuracy(loader, model):
    num_correct = 0
    num_samples = 0
    model.eval()

    with torch.no_grad():
        for x, y in loader:
            x = x.to(device=target_device)
            y = y.to(device=target_device)

            scores = model(x)
            _, predictions = scores.max(1)
            num_correct += (predictions == y).sum()
            num_samples += predictions.size(0)

    model.train()
    return num_correct/num_samples

print(f"Accuracy on training set: {check_accuracy(train_loader, model):.2f}")<jupyter_output>Accuracy on training set: 0.63
{'fraction_non_alphanumeric': 0.09735744089012517, 'fraction_numerical': 0.009040333796940195, 'mean_word_length': 3.385670731707317, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '314200', 'license': 'Apache-2.0', 'n_tokens_mistral': 910, 'n_tokens_neox': 873, 'n_words': 254, 'path': '5_Finetuning&tansfer_learning.ipynb', 'repo_name': 'JiaheXu/Pytorch_tutorial', 'size': '2876'}