<jupyter_start><jupyter_text>**Calculating the loss**<jupyter_code>preds = network(images)
loss = F.cross_entropy(preds,labels)
loss.item()<jupyter_output><empty_output><jupyter_text>**Calculating gradients**<jupyter_code>print(network.conv1.weight)
loss.backward()  # Calculating gradients
network.conv1.weight.grad.shape<jupyter_output><empty_output><jupyter_text>**Updating the Weights**<jupyter_code>optimizer = optim.Adam(network.parameters(), lr=0.001)
loss.item()<jupyter_output><empty_output><jupyter_text>**TensorBoard**<jupyter_code># tb = SummaryWriter()

# network = Net()
# images, labels = next(iter(train_data_loader))
# grid = torchvision.utils.make_grid(images)

# tb.add_image('images', grid)
# tb.add_graph(network,images)
# tb.close()<jupyter_output><empty_output><jupyter_text>**train the model**<jupyter_code>def get_num_correct(preds,labels):
    return preds.argmax(dim=1).eq(labels).sum().item()

# for epoch in range(1,25):
#     total_loss = 0
#     total_correct = 0
#     for batch in train_data_loader:
#         images,labels = batch

#         preds = network(images)
#         loss = F.cross_entropy(preds,labels) # Calculate Loss

#         optimizer.zero_grad()
#         loss.backward()
#         optimizer.step()

#         total_loss+=loss.item()
#         total_correct+=get_num_correct(preds, labels)
#     print("epoch:", epoch, "total_correct:", total_correct, "loss:", total_loss)

get_num_correct(preds,labels)
preds = network(images)
loss = F.cross_entropy(preds,labels)
loss.item()
get_num_correct(preds,labels)
preds.shape
preds
preds.argmax(dim=1)
preds.argmax(dim=1).eq(labels)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")<jupyter_output><empty_output><jupyter_text>**Transfer learning**<jupyter_code>model = models.resnet18(pretrained=True)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)

def accuracy(out, labels):
    _,pred = torch.max(out, dim=1)
    return torch.sum(pred==labels).item()

num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 16)
model.to(device)
model = models.resnet50(pretrained=True)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)

def accuracy(out, labels):
    _,pred = torch.max(out, dim=1)
    return torch.sum(pred==labels).item()

num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 16)
model.to(device)
n_epochs = 1
print_every = 10
valid_loss_min = np.Inf
val_loss = []
val_acc = []
train_loss = []
train_acc = []
total_step = len(train_data_loader)
for epoch in range(1, n_epochs+1):
    running_loss = 0.0
    correct = 0
    total=0
    print(f'Epoch {epoch}\n')
    for batch_idx, (data_, target_) in enumerate(train_data_loader):
        data_, target_ = data_.to(device), target_.to(device)
        optimizer.zero_grad()
        
        outputs = model(data_)
        loss = criterion(outputs, target_)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        _,pred = torch.max(outputs, dim=1)
        correct += torch.sum(pred==target_).item()
        total += target_.size(0)
        if (batch_idx) % 20 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch, n_epochs, batch_idx, total_step, loss.item()))
    train_acc.append(100 * correct / total)
    train_loss.append(running_loss/total_step)
    print(f'\ntrain-loss: {np.mean(train_loss):.4f}, train-acc: {(100 * correct/total):.4f}')
    batch_loss = 0
    total_t=0
    correct_t=0
    with torch.no_grad():
        model.eval()
        for data_t, target_t in (test_data_loader):
            data_t, target_t = data_t.to(device), target_t.to(device)
            outputs_t = model(data_t)
            loss_t = criterion(outputs_t, target_t)
            batch_loss += loss_t.item()
            _,pred_t = torch.max(outputs_t, dim=1)
            correct_t += torch.sum(pred_t==target_t).item()
            total_t += target_t.size(0)
        val_acc.append(100 * correct_t/total_t)
        val_loss.append(batch_loss/len(test_data_loader))
        network_learned = batch_loss < valid_loss_min
        print(f'validation loss: {np.mean(val_loss):.4f}, validation acc: {(100 * correct_t/total_t):.4f}\n')

        
        if network_learned:
            valid_loss_min = batch_loss
            torch.save(model.state_dict(), 'resnet.pt')
            print('Improvement-Detected, save-model')
    model.train()


# evaluate the model
def evaluate_model(test_dl, model):
    predictions, actuals = list(), list()
    for i, (inputs, targets) in enumerate(test_dl):
        # evaluate the model on the test set
        yhat = model(inputs)
        # retrieve numpy array
        yhat = yhat.detach().numpy()
        actual = targets.numpy()
        # convert to class labels
        yhat = np.argmax(yhat, axis=1)
        # reshape for stacking
        actual = actual.reshape((len(actual), 1))
        yhat = yhat.reshape((len(yhat), 1))
        # store
        predictions.append(yhat)
        actuals.append(actual)
    predictions, actuals = np.vstack(predictions), np.vstack(actuals)
    # calculate accuracy
    acc = accuracy_score(actuals, predictions)
    return acc
# evaluate the model
acc = evaluate_model(test_data_loader, network)
print('Accuracy: %.3f' % acc)<jupyter_output>Accuracy: 0.408
{'fraction_non_alphanumeric': 0.13998136067101585, 'fraction_numerical': 0.014352283317800559, 'mean_word_length': 3.582408198121264, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '460557', 'license': 'Apache-2.0', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1870, 'n_words': 324, 'path': 'src/notebooks/main.ipynb', 'repo_name': 'AnushavanAleksanyan/Computer_Vision_project', 'size': '5365'}