{"id":21352047,"url":"https://github.com/icetube23/ideal_words","last_synced_at":"2025-03-16T04:41:49.596Z","repository":{"id":246674922,"uuid":"821728496","full_name":"icetube23/ideal_words","owner":"icetube23","description":"A PyTorch implementation of ideal word computation.","archived":false,"fork":false,"pushed_at":"2025-03-15T20:47:52.000Z","size":49,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T21:30:36.437Z","etag":null,"topics":["compositionality","deep-learning","ideal-words","interpretability","linear-spaces-of-meaning","pytorch","vision-language-model"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/icetube23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-06-29T09:11:06.000Z","updated_at":"2025-03-15T20:17:24.000Z","dependencies_parsed_at":"2024-06-29T15:48:39.336Z","dependency_job_id":"f4be832b-25a8-49a6-842f-44c3481ed222","html_url":"https://github.com/icetube23/ideal_words","commit_stats":null,"previous_names":["icetube23/ideal_words"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icetube23%2Fideal_words","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icetube23%2Fideal_words/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icetube23%2Fideal_words/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icetube23%2Fideal_words/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icetube23","download_url":"https://codeload.github.com/icetube23/ideal_words/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243809873,"owners_count":20351407,"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","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":["compositionality","deep-learning","ideal-words","interpretability","linear-spaces-of-meaning","pytorch","vision-language-model"],"created_at":"2024-11-22T03:12:24.097Z","updated_at":"2025-03-16T04:41:49.591Z","avatar_url":"https://github.com/icetube23.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ideal Words\n\nThis package provides a PyTorch implementation of ideal word computation which was proposed by Trager et al. in the paper [Linear Spaces of Meanings: Compositional Structures in Vision-Language Models](https://arxiv.org/abs/2302.14383). Ideal words can be seen as a compositional approximation to a given set of embedding vectors. This package allows computing these ideal words given a factored set of concepts $\\mathcal{Z} = \\mathcal{Z}_1 \\times \\dots \\times \\mathcal{Z}_k$ (e.g., $\\\\{\\mathrm{blue}, \\mathrm{red}\\\\} \\times \\\\{\\mathrm{car}, \\mathrm{bike}\\\\}$) and a embedding function $f : \\mathcal{Z} \\to \\mathbb{R}^n$. Additionally, it allows to  quantify compositionality using the ideal word, real word, and average scores from the paper (see Table 6 and 7 for details).\n\n## Usage\n\nYou can install the package using:\n```\npip install ideal_words\n```\n\nConsider you have a text encoder, a tokenizer, and a set of factors. You can then compute ideal words as follows:\n```python\nfrom ideal_words import FactorEmbedding, IdealWords\n\n# tokenizer and encoder whose embeddings we want to approximate with ideal words\ntxt_encoder = MyTextEncoder()\ntokenizer = MyTokenizer()\n\n# the factors we want to consider\nZ1 = ['blue', 'red']\nZ2 = ['car', 'bike']\n\n# factor embedding is a embedding function with some additional logic\nfe = FactorEmbedding(txt_encoder, tokenizer)\n# compute ideal words from factor embedding and factors\niw = IdealWords(fe, [Z1, Z2])\n\n# retrieve ideal word representation for a specific element of a factor\nprint(f'Ideal word for \"blue\": {iw.get_iw(\"blue\")}')\n# retrieve ideal word approximation for a combination of factor elements\nprint(f'Ideal word approximation for \"red car\": {iw.get_uz((\"red\", \"car\"))}')\n# directly access the ideal word representation of a certain factor element\ni, j = 1, 0  # freely adjustable, as long as i \u003c= num_factors, j \u003c= len_factor_i\nprint(f'Ideal word for the {j}-th element of the {i}-th factor: {iw.ideal_words[i][j]}')\n```\n\nIf you have a CUDA-capable GPU, it will be automatically used. If you prefer to use the CPU, you can pass `device='cpu'` when creating the `FactorEmbedding` object.\n\n## Advanced example\n\nYou can also customize the behaviour of the `FactorEmbedding` class if your use-case is different (e.g., you are not using a plain text encoder but a CLIP model). [This example](examples/clip_vit_large_14.py) shows how you can compute ideal words and the scores from the paper for the factors from the MIT-States and the UT Zappos datasets using a CLIP model (compare Table 6 and 7 from the paper).\n\nYou can run this example locally by using:\n```\ngit clone https://github.com/icetube23/ideal_words.git\ncd ideal_words\npip install .[demo]  # it is recommended to do this in a virtual environment\npython examples/clip_vit_large_14.py\n```\n\n## Scalability\n\nFor small numbers of factors and/or small datasets, computing ideal words is really fast. The [example](examples/clip_vit_large_14.py) from the previous section computes ideal words using a CLIP ViT-L-14 model on two datasets and runs in less than a minute on a recently modern GPU.\n\nHowever, the approach does not scale well with an increasing number of factors. The computational complexity is at least exponential in the number of factors $\\mathcal{\\Omega}(\\vert\\mathcal{Z_1}\\vert \\times \\dots \\times \\vert\\mathcal{Z_k}\\vert)$.\n\n## Contributing\n\nThe code is roughly tested but there still might be some bugs and/or inefficiencies. If you find anything, feel free to create an issue or to submit a pull request. If you want to contribute to this package, you should install it with the additional development dependencies:\n```\ngit clone https://github.com/icetube23/ideal_words.git\ncd ideal_words\npip install -e .[dev]  # it is recommended to do this in a virtual environment\n```\n\n## Acknowledgement\n\nThe ideal word approach was proposed by Trager et al. in https://arxiv.org/abs/2302.14383. Please make sure to appropriately credit their idea by citing their paper if you use this code in research.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficetube23%2Fideal_words","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficetube23%2Fideal_words","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficetube23%2Fideal_words/lists"}