Posts

Showing posts with the label Pytorch

Confusion Matrix And Test Accuracy For PyTorch Transfer Learning Tutorial

Answer : Answer given by ptrblck of PyTorch community. Thanks a lot! nb_classes = 9 confusion_matrix = torch.zeros(nb_classes, nb_classes) with torch.no_grad(): for i, (inputs, classes) in enumerate(dataloaders['val']): inputs = inputs.to(device) classes = classes.to(device) outputs = model_ft(inputs) _, preds = torch.max(outputs, 1) for t, p in zip(classes.view(-1), preds.view(-1)): confusion_matrix[t.long(), p.long()] += 1 print(confusion_matrix) To get the per-class accuracy: print(confusion_matrix.diag()/confusion_matrix.sum(1)) Here is a slightly modified(direct) approach using sklearn's confusion_matrix:- from sklearn.metrics import confusion_matrix nb_classes = 9 # Initialize the prediction and label lists(tensors) predlist=torch.zeros(0,dtype=torch.long, device='cpu') lbllist=torch.zeros(0,dtype=torch.long, device='cpu') with torch.no_grad(): for i, (inputs, classes) in enumerate(da...

Cross Validation For MNIST Dataset With Pytorch And Sklearn

Image
Answer : I think you're confused! Ignore the second dimension for a while, When you've 45000 points, and you use 10 fold cross-validation, what's the size of each fold? 45000/10 i.e. 4500. It means that each of your fold will contain 4500 data points, and one of those fold will be used for testing, and the remaining for training i.e. For testing: one fold => 4500 data points => size: 4500 For training: remaining folds => 45000-4500 data points => size: 45000-4500=40500 Thus, for first iteration, the first 4500 data points (corresponding to indices) will be used for testing and the rest for training. (Check below image) Given your data is x_train: torch.Size([45000, 784]) and y_train: torch.Size([45000]) , this is how your code should look like: for train_index, test_index in kfold.split(x_train, y_train): print(train_index, test_index) x_train_fold = x_train[train_index] y_train_fold = y_train[train_index] x_test_fold = x_train[test_i...

Adding L1/L2 Regularization In PyTorch?

Answer : Following should help for L2 regularization: optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-5) This is presented in the documentation for PyTorch. Have a look at http://pytorch.org/docs/optim.html#torch.optim.Adagrad. You can add L2 loss using the weight decay parameter to the Optimization function. For L2 regularization, l2_lambda = 0.01 l2_reg = torch.tensor(0.) for param in model.parameters(): l2_reg += torch.norm(param) loss += l2_lambda * l2_reg References: https://discuss.pytorch.org/t/how-does-one-implement-weight-regularization-l1-or-l2-manually-without-optimum/7951. http://pytorch.org/docs/master/torch.html?highlight=norm#torch.norm.