{"id":13936472,"url":"https://github.com/jmhessel/fmpytorch","last_synced_at":"2025-07-22T04:35:27.903Z","repository":{"id":93779388,"uuid":"100334831","full_name":"jmhessel/fmpytorch","owner":"jmhessel","description":"A PyTorch implementation of a Factorization Machine module in cython.","archived":false,"fork":false,"pushed_at":"2018-03-12T18:12:44.000Z","size":295,"stargazers_count":173,"open_issues_count":0,"forks_count":18,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-27T04:31:05.920Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jmhessel.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}},"created_at":"2017-08-15T03:30:10.000Z","updated_at":"2024-07-30T09:44:52.000Z","dependencies_parsed_at":"2023-06-11T22:44:55.161Z","dependency_job_id":null,"html_url":"https://github.com/jmhessel/fmpytorch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jmhessel/fmpytorch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmhessel%2Ffmpytorch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmhessel%2Ffmpytorch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmhessel%2Ffmpytorch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmhessel%2Ffmpytorch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jmhessel","download_url":"https://codeload.github.com/jmhessel/fmpytorch/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jmhessel%2Ffmpytorch/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266428526,"owners_count":23927012,"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-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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-08-07T23:02:42.345Z","updated_at":"2025-07-22T04:35:27.842Z","avatar_url":"https://github.com/jmhessel.png","language":"Python","funding_links":[],"categories":["Python","Paper implementations｜论文实现","Paper implementations"],"sub_categories":["Other libraries｜其他库:","Other libraries:"],"readme":"# fmpytorch\n\nA library for factorization machines in pytorch. A factorization\nmachine is like a linear model, except multiplicative interaction\nterms between the variables are modeled as well.\n\nThe input to a factorization machine layer is a vector, and the output\nis a scalar. Batching is fully supported.\n\nThis is a work in progress. Feedback and bugfixes welcome! Hopefully you\nfind the code useful.\n\n\n## Usage\n\nThe factorization machine layers in fmpytorch can be used just like any other built-in module. Here's a simple feed-forward model using a factorization machine that takes in a 50-D input, and models interactions using `k=5` factors.\n\n```python\nimport torch\nfrom fmpytorch.second_order.fm import FactorizationMachine\n\nclass MyModel(torch.nn.Module):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.linear = torch.nn.Linear(100, 50)\n        self.dropout = torch.nn.Dropout(.5)\n\t# This makes a fm layer mapping from 50-D to 1-D.\n\t# The number of factors is 5.\n        self.fm = FactorizationMachine(50, 5)\n\n    def forward(self, x):\n        x = self.linear(x)\n        x = self.dropout(x)\n        x = self.fm(x)\n        return x\n```\n\nSee examples/toy.py or examples/regression.py for fuller examples.\n\n## Installation\n\nThis package requires pytorch, numpy, and cython.\n\nTo install, you can run:\n\n```\ncd fmpytorch\nsudo python setup.py install\n```\n\n## Factorization Machine brief intro\n\nA linear model, given a vector `x` models its output `y` as\n\n\u003cp\u003e\n\u003ca href=\"url\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/jmhessel/fmpytorch/master/images/linear_model.png\" width=\"250\" align=\"center\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nwhere `w` are the learnable weights of the model.\n\nHowever, the interactions between the input variables `x_i` are purely additive. In some cases, it might be useful to model the interactions between your variables, e.g., `x_i * x_j`. You could add terms into your model like\n\n\n\u003cp\u003e\n\u003ca href=\"url\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/jmhessel/fmpytorch/master/images/second_order.png\" width=\"400\" align=\"center\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nHowever, this introduces a large number of `w2` variables. Specifically, there are `O(n^2)` parameters introduced in this formulation, one for each interaction pair. A factorization machine approximates `w2` using low dimensional factors, i.e.,\n\u003cp\u003e\n\u003ca href=\"url\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/jmhessel/fmpytorch/master/images/fm.png\" width=\"400\" align=\"center\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nwhere each `v_i` is a low-dimensional vector. This is the forward pass of a second order factorization machine. This low-rank re-formulation has reduced the number of additional parameters for the factorization machine to `O(k*n)`. Magically, the forward (and backward) pass can be reformulated so that it can be computed in `O(k*n)`, rather than the naive `O(k*n^2)` formulation above.\n\n## Currently supported features\n\nCurrently, only a second order factorization machine is supported. The\nforward and backward passes are implemented in cython. Compared to the\nautodiff solution, the cython passes run several orders of magnitude\nfaster. I've only tested it with python 2 at the moment.\n\n## TODOs\n\n0. Support for sparse tensors.\n1. More interesting useage examples\n2. More testing, e.g., with python 3, etc.\n3. Make sure all of the code plays nice with torch-specific stuff, e.g., GPUs\n4. Arbitrary order factorization machine support\n5. Better organization/code cleaning\n\n## Thanks to\n\nVlad Niculae (@vene) for his sage wisdom.\n\nThe original factorization machine citation, which this layer is based off of, is\n\n```\n@inproceedings{rendle2010factorization,\n\t       title={Factorization machines},\n    \t       author={Rendle, Steffen},\n      \t       booktitle={ICDM},\n               pages={995--1000},\n\t       year={2010},\n\t       organization={IEEE}\n}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmhessel%2Ffmpytorch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjmhessel%2Ffmpytorch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjmhessel%2Ffmpytorch/lists"}