Posts

Showing posts with the label Deep Learning

Can Anyone Give A Real Life Example Of Supervised Learning And Unsupervised Learning?

Answer : Supervised learning: You get a bunch of photos with information about what is on them and then you train a model to recognize new photos. You have a bunch of molecules and information about which are drugs and you train a model to answer whether a new molecule is also a drug. Unsupervised learning: You have a bunch of photos of 6 people but without information about who is on which one and you want to divide this dataset into 6 piles, each with the photos of one individual. You have molecules, part of them are drugs and part are not but you do not know which are which and you want the algorithm to discover the drugs. Supervised Learning: is like learning with a teacher training dataset is like a teacher the training dataset is used to train the machine Example: Classification: Machine is trained to classify something into some class. classifying whether a patient has disease or not classifying whether an email is spam or no...

Check The Total Number Of Parameters In A PyTorch Model

Answer : PyTorch doesn't have a function to calculate the total number of parameters as Keras does, but it's possible to sum the number of elements for every parameter group: pytorch_total_params = sum(p.numel() for p in model.parameters()) If you want to calculate only the trainable parameters: pytorch_total_params = sum(p.numel() for p in model.parameters() if p.requires_grad) Answer inspired by this answer on PyTorch Forums . Note: I'm answering my own question. If anyone has a better solution, please share with us. To get the parameter count of each layer like Keras, PyTorch has model.named_paramters() that returns an iterator of both the parameter name and the parameter itself. Here is an example: from prettytable import PrettyTable def count_parameters(model): table = PrettyTable(["Modules", "Parameters"]) total_params = 0 for name, parameter in model.named_parameters(): if not parameter.requires_grad: continue...

Best Way To Save A Trained Model In PyTorch?

Answer : I've found this page on their github repo, I'll just paste the content here. Recommended approach for saving a model There are two main approaches for serializing and restoring a model. The first (recommended) saves and loads only the model parameters: torch.save(the_model.state_dict(), PATH) Then later: the_model = TheModelClass(*args, **kwargs) the_model.load_state_dict(torch.load(PATH)) The second saves and loads the entire model: torch.save(the_model, PATH) Then later: the_model = torch.load(PATH) However in this case, the serialized data is bound to the specific classes and the exact directory structure used, so it can break in various ways when used in other projects, or after some serious refactors. It depends on what you want to do. Case # 1: Save the model to use it yourself for inference : You save the model, you restore it, and then you change the model to evaluation mode. This is done because you usually have BatchNorm and Dropout layers...