[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-116985":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":11,"openIssues":12,"contributorsCount":12,"subscribersCount":12,"size":12,"stars1d":12,"stars7d":12,"stars30d":12,"stars90d":12,"forks30d":12,"starsTrendScore":12,"compositeScore":13,"rankGlobal":9,"rankLanguage":9,"license":14,"archived":15,"fork":15,"defaultBranch":16,"hasWiki":15,"hasPages":15,"topics":17,"createdAt":9,"pushedAt":24,"updatedAt":25,"readmeContent":26,"aiSummary":9,"trendingCount":12,"starSnapshotCount":12,"syncStatus":27,"lastSyncTime":28,"discoverSource":29},116985,"imbalanced-dataset-sampler","garge33limer\u002Fimbalanced-dataset-sampler","garge33limer","A PyTorch sampler that rebalances class frequencies during training by resampling.",null,"Python",51,0,40,"MIT License",false,"main",[18,19,20,21,22,23],"deep-learning","imbalanced-data","machine-learning","python","pytorch","sampler","2026-11-05 14:15:55","2026-11-12 20:25:35","# Imbalanced Dataset Sampler\r\n\r\n![license](https:\u002F\u002Fimg.shields.io\u002Fgithub\u002Flicense\u002Fprabeakarve\u002Fimbalanced-dataset-sampler.svg)\r\n\r\n\u003Ca name=\"Introduction\"\u002F>\r\n\r\n## Introduction\r\n\r\nIn many machine learning applications, we often come across datasets where some types of data may be seen more than other types. Take identification of rare diseases for example, there are probably more normal samples than disease ones. In these cases, we need to make sure that the trained model is not biased towards the class that has more data. As an example, consider a dataset where there are 5 disease images and 20 normal images. If the model predicts all images to be normal, its accuracy is 80%, and F1-score of such a model is 0.88. Therefore, the model has high tendency to be biased toward the ‘normal’ class.\r\n\r\nTo solve this problem, a widely adopted technique is called resampling. It consists of removing samples from the majority class (under-sampling) and \u002F or adding more examples from the minority class (over-sampling). Despite the advantage of balancing classes, these techniques also have their weaknesses (there is no free lunch). The simplest implementation of over-sampling is to duplicate random records from the minority class, which can cause overfitting. In under-sampling, the simplest technique involves removing random records from the majority class, which can cause loss of information.\r\n\r\n![resampling](https:\u002F\u002Fuser-images.githubusercontent.com\u002F2270240\u002F40656410-e0baa230-6376-11e8-8904-c092fb38fcdc.png)\r\n\r\nIn this repo, we implement an easy-to-use PyTorch sampler `ImbalancedDatasetSampler` that is able to\r\n- rebalance the class distributions when sampling from the imbalanced dataset\r\n- estimate the sampling weights automatically\r\n- avoid creating a new balanced dataset\r\n- mitigate overfitting when it is used in conjunction with data augmentation techniques\r\n\r\n\u003Cp align=\"center\">\r\n  \u003Cimg src=\"https:\u002F\u002Fuser-images.githubusercontent.com\u002F2270240\u002F40677251-b08f504a-63af-11e8-9653-f28e973a5664.png\">\r\n\u003C\u002Fp>\r\n\r\n## Usage\r\n\r\nFor a simple start install the package via one of following ways:\r\n\r\n```bash\r\npip install torchsampler\r\n```\r\n\r\n\r\nSimply pass an `ImbalancedDatasetSampler` for the parameter `sampler` when creating a `DataLoader`.\r\nFor example:\r\n\r\n```python\r\nfrom torchsampler import ImbalancedDatasetSampler\r\n\r\ntrain_loader = torch.utils.data.DataLoader(\r\n    train_dataset,\r\n    sampler=ImbalancedDatasetSampler(train_dataset),\r\n    batch_size=args.batch_size,\r\n    **kwargs\r\n)\r\n```\r\n\r\nThen in each epoch, the loader will sample the entire dataset and weigh your samples inversely to your class appearing probability.\r\n\r\n## Example: Imbalanced MNIST Dataset\r\n\r\nDistribution of classes in the imbalanced dataset:\r\n\u003Cp align=\"center\">\r\n  \u003Cimg src=\"https:\u002F\u002Fuser-images.githubusercontent.com\u002F2270240\u002F40678060-03c6eb9a-63b2-11e8-9a81-c61c665240e3.png\">\r\n\u003C\u002Fp>\r\n\r\nWith Imbalanced Dataset Sampler:\r\n\u003Cp align=\"center\">\r\n  \u003Cimg src=\"https:\u002F\u002Fuser-images.githubusercontent.com\u002F2270240\u002F40678100-1e543f12-63b2-11e8-9c05-3b168c3fb39f.png\">\r\n  (left: test acc in each epoch; right: confusion matrix)\r\n\u003C\u002Fp>\r\n\r\nWithout Imbalanced Dataset Sampler:\r\n\u003Cp align=\"center\">\r\n  \u003Cimg src=\"https:\u002F\u002Fuser-images.githubusercontent.com\u002F2270240\u002F40678099-1e0a481c-63b2-11e8-9ec8-f2ac4bf7a637.png\">\r\n  (left: test acc in each epoch; right: confusion matrix)\r\n\u003C\u002Fp>\r\n\r\nNote that there are significant improvements for minor classes such as `2` `6` `9`, while the accuracy of the other classes is preserved.\r\n\r\n## Contributing\r\n\r\nWe appreciate all contributions. If you are planning to contribute back bug-fixes, please do so without any further discussion. If you plan to contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us.\r\n\r\n## Licensing\r\n\r\n[MIT licensed](https:\u002F\u002Fgithub.com\u002Fprabeakarve\u002Fimbalanced-dataset-sampler\u002Fblob\u002Fmaster\u002FLICENSE).\r\n",2,"2026-11-24 04:54:47","CREATED_QUERY"]