{"id":18439498,"url":"https://github.com/idiap/wmil-sgd","last_synced_at":"2025-10-15T12:05:05.754Z","repository":{"id":95740855,"uuid":"83420595","full_name":"idiap/wmil-sgd","owner":"idiap","description":"Weighted multiple-instance learning algorithm based on stochastic gradient descent","archived":false,"fork":false,"pushed_at":"2019-02-22T17:26:32.000Z","size":1415,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-07T21:41:18.130Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idiap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-28T10:39:04.000Z","updated_at":"2025-01-20T01:38:33.000Z","dependencies_parsed_at":"2023-04-25T12:47:22.213Z","dependency_job_id":null,"html_url":"https://github.com/idiap/wmil-sgd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/idiap/wmil-sgd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiap%2Fwmil-sgd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiap%2Fwmil-sgd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiap%2Fwmil-sgd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiap%2Fwmil-sgd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idiap","download_url":"https://codeload.github.com/idiap/wmil-sgd/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idiap%2Fwmil-sgd/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279077248,"owners_count":26098234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T06:25:11.483Z","updated_at":"2025-10-15T12:05:05.748Z","avatar_url":"https://github.com/idiap.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp\u003e\u003cb\u003ewmil-sgd\u003c/b\u003e —\nThis repository contains a Python implementation of the weighted multiple-instance learning (\u003ca href=\"https://github.com/nik0spapp/wmil\"\u003ewmil\u003c/a\u003e) algorithm based on stochastic gradient descent which was presented at JAIR 2017 paper [\u003ca href=\"http://publications.idiap.ch/downloads/papers/2017/Pappas_JAIR_2017.pdf\"\u003e1\u003c/a\u003e]. This algorithm, which was originally proposed at EMNLP 2014 paper [\u003ca href=\"http://publications.idiap.ch/downloads/papers/2014/Pappas_EMNLP14_2014.pdf\"\u003e2\u003c/a\u003e], is a weakly supervised learning model, which jointly learns to focus on relevant parts of a document according to the context along with a classifier for the target categories. The model takes as input a document Bi (bag), which consists of multiple input vectors \u003ci\u003eb\u003csub\u003eij\u003c/sub\u003e\u003c/i\u003e (instances), possibly from a neural network. The model learns to compute a weighted average of these vectors by estimating the weights \u003ci\u003eψ\u003csub\u003eij\u003c/sub\u003e\u003c/i\u003e for each document \u003ci\u003eB\u003csub\u003ei\u003c/sub\u003e\u003c/i\u003e and its target categories \u003ci\u003ey\u003csub\u003ei\u003c/sub\u003e\u003c/i\u003e \u003ci\u003e∈ R\u003csup\u003ek\u003c/sup\u003e\u003c/i\u003e.\n\u003cp align=\"center\"\u003e\n\u003cimg align=\"center\" src=\"images/overview.png\" alt=\"Explicit document modeling using weighted multiple-instance learning\" width=\"650\"/\u003e\n\u003c/p\u003e\n\u003c/p\u003e\n\n```\n@ARTICLE{Pappas_JAIR_2017,\n         author = {Pappas, Nikolaos and Popescu-Belis, Andrei},\n          title = {Explicit Document Modeling through Weighted Multiple-Instance Learning},\n        journal = {Journal of Artificial Intelligence Research},\n           year = {2017},\n            pdf = {http://publications.idiap.ch/downloads/papers/2017/Pappas_JAIR_2017.pdf}     \n} \n```\n\nInstalling dependencies\n------------\nThe available code requires Python programming language and pip package manager to run. \nFor detailed instructions on how to install it along with a package manager please refer \nto the following links: http://www.python.org/getit/ and http://www.pip-installer.org/en/latest/.\n\nNext, you should be able to install the following packages: \u003cbr /\u003e\n```bash\n$ pip install numpy \n$ pip install scikit-learn\n$ pip install scipy\n```\n\nTraining and testing the model\n------------\nThe code extends BaseEstimator class from scikit-learn package, so you should be able to use it as a common sklearn estimator (check more details on http://scikit-learn.org/stable/). For example:\n```bash\n$ python\n\u003e\u003e\u003e import pickle\n\u003e\u003e\u003e from wmil_sgd import SGDWeights\n\u003e\u003e\u003e from sklearn.metrics import mean_absolute_error\n\u003e\u003e\u003e data = pickle.load(open('features/ted_comments.p'))\n\u003e\u003e\u003e size = len(data['X'])\n\u003e\u003e\u003e k = int(size*0.5)\n\u003e\u003e\u003e x_train = data['X'][:k]\n\u003e\u003e\u003e y_train = data['Y'][:k]\n\u003e\u003e\u003e x_test = data['X'][k:]\n\u003e\u003e\u003e y_test = data['Y'][k:]\n\u003e\u003e\u003e model = SGDWeights(alpha=0.4, momentum=0.0, minib=50)\n\u003e\u003e\u003e model.fit(x_train, y_train)\n[+] Training...\nepoch -\u003e 0 / mae: 0.193162 (0.26 sec)\nepoch -\u003e 1 / mae: 0.188571 (0.26 sec)\nepoch -\u003e 2 / mae: 0.185567 (0.27 sec)\nepoch -\u003e 3 / mae: 0.182778 (0.27 sec)\nepoch -\u003e 4 / mae: 0.180144 (0.27 sec)\nepoch -\u003e 5 / mae: 0.177637 (0.28 sec)\nepoch -\u003e 6 / mae: 0.175247 (0.28 sec)\nepoch -\u003e 7 / mae: 0.172964 (0.29 sec)\nepoch -\u003e 8 / mae: 0.170796 (0.27 sec)\nepoch -\u003e 9 / mae: 0.168738 (0.28 sec)\n...\n\u003e\u003e\u003e mean_absolute_error(model.predict(x_train),y_train) # training error\n0.08907894272487181\n\u003e\u003e\u003e mean_absolute_error(model.predict(x_test), y_test) # testing error\n0.16175043443941697\n```\nTo have access to the estimated sentence relevance weights of one or more input documents you can use the function \u003ccode\u003epredict_weights()\u003c/code\u003e. The weights for a single input document can be obtained as follows:\n```\n\u003e\u003e\u003e weights, preds = model.predict_weights([x_test[14]])\n\u003e\u003e\u003e print weights[0]\narray([ 0.43081474,  0.31438322,  0.25480204])\n\u003e\u003e\u003e print preds[0]\n0.84218349080557298\n\u003e\u003e\u003e print y_test[14].tolist()[0]\n0.80000000000000000\n```\nFor multiple input documents the relevance weights can be obtained as follows:\n```\n\u003e\u003e\u003e weights, preds = model.predict_weights(x_test)\n\u003e\u003e\u003e print weights[14]\narray([ 0.43081474,  0.31438322,  0.25480204])\n\u003e\u003e\u003e print preds[14]\n0.84218349080557298\n```\n\n\nVisualizing the relevance weights\n------------\nThe relevance weights estimated by the weighted multiple-instance learning model are useful for interpreting the decision made by the classifier for a particular task. For instance, the following example visualizes the estimated relevance weights (shown in percentage) of each sentence by the model in the task of sentiment analysis of comments. For more examples please check the online \u003ca href=\"http://nik0spapp.github.io/demos/wmil-sa/\"\u003ewmil-sa demo\u003c/a\u003e.\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"http://nik0spapp.github.io/demos/wmil-sa/\"\u003e\u003cimg src=\"images/wmil_vis_example.png\" alt=\"Example of assigned weights by multiple-instance regression on sentiment analysis of comments.\" width=\"750\"/\u003e\u003c/a\u003e\n\u003c/p\u003e \n\nEvaluating the relevance weights\n------------\nFor an intrinsic evaluation of the relevance weights we created and used the \u003ca href=\"http://www.idiap.ch/paper/hatdoc/\"\u003eHATDOC dataset\u003c/a\u003e. HATDOC contains human attention scores to sentences when attributing specific categories (aspect ratings) to documents (audiobook reviews). The annotations were obtained using at least 4 qualified judges per sentence using a crowdsourcing platform, namely 100 audiobook reviews with 1,662 sentences and three aspects: story, performance, and overall quality. In addition, a dataset of 50K audiobook reviews with document-level aspect ratings can be used for weakly supervised training. The data are suitable for intrinsic evaluation of explicit document modeling models with attention mechanisms in the task of aspect sentiment analysis and summarization of reviews.\n\n## Contact:\nnpappas@idiap.ch\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidiap%2Fwmil-sgd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidiap%2Fwmil-sgd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidiap%2Fwmil-sgd/lists"}