https://github.com/garethjns/tf2factorizationmachine
Factorization machine implemented in TensorFlow 2
https://github.com/garethjns/tf2factorizationmachine
factorization-machines latent-variable-models recommender-system supervised-learning tensorflow
Last synced: about 2 months ago
JSON representation
Factorization machine implemented in TensorFlow 2
- Host: GitHub
- URL: https://github.com/garethjns/tf2factorizationmachine
- Owner: garethjns
- Created: 2019-08-06T20:53:04.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-24T22:33:54.000Z (about 3 years ago)
- Last Synced: 2025-06-09T22:05:08.582Z (about 1 year ago)
- Topics: factorization-machines, latent-variable-models, recommender-system, supervised-learning, tensorflow
- Language: Python
- Homepage:
- Size: 2.13 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# Factorization machine in TensorFlow 2
Example implementing a factorization machine in TensorFlow 2, along with a framework for generating user-item ratings for testing.
Based on the theory and tf-1 code in [this article](http://nowave.it/factorization-machines-with-tensorflow.html) by Gabriele Modena and the paper by [Factorization Machines](https://www.csie.ntu.edu.tw/~b97053/paper/Rendle2010FM.pdf) by Steffen Rendle. Please refer to these articles for much more detailed descriptions of the model, and the maths that avoid O(p2).
Code is work in progress so expect bugs and inconveniences. See examples/ directory for more detailed examples.
# Install
````bash
git clone https://github.com/garethjns/TF2FactorizationMachine.git
````
# Example
## TensorFlow 2 model
For now the current model only implements .\_\_init__ .\_\_call__ and follows a similar structure as the [TensorFlow linear models example](https://www.tensorflow.org/beta/tutorials/eager/custom_training). Training is done in a loop with a function (train_step) to update the parameters; these will be moved to .fit at some point.
See also [examples/1_model_development.ipynb](https://github.com/garethjns/TF2FactorizationMachine/blob/master/examples/1_model_development.ipynb) for mode details on model development and [examples/2_movie_lens_data.ipynb](https://github.com/garethjns/TF2FactorizationMachine/blob/master/examples/2_movie_lens_data.ipynb) for an example running it on the MovieLens dataset.
````Python
import numpy as np
import matplotlib.pyplot as plt
from fmachine.model import FactorizationMachine
from fmachine.helpers import train_step, l2_loss
# Features
x = np.array([[1, 0, 0, 1, 0, 0, 0, 0.3, 0.3, 0.3, 0.0, 13, 0, 0, 0, 0 ],
[1, 0, 0, 0, 1, 0, 0, 0.3, 0.3, 0.3, 0.0, 14, 1, 0, 0, 0 ],
[1, 0, 0, 0, 0, 1, 0, 0.3, 0.3, 0.3, 0.0, 16, 0, 1, 0, 0 ],
[0, 1, 0, 0, 0, 1, 0, 0.0, 0.0, 0.5, 0.5, 5, 0, 0, 0, 0 ],
[0, 1, 0, 0, 0, 0, 1, 0.0, 0.0, 0.5, 0.5, 8, 0, 0, 1, 0 ],
[0, 0, 1, 1, 0, 0, 0, 0.5, 0.0, 0.5, 0.0, 9, 0, 0, 0, 0 ],
[0, 0, 1, 0, 0, 1, 0, 0.5, 0.0, 0.5, 0.0, 12, 1, 0, 0, 0 ]])
# Targets (explicit rating)
y = np.array([5, 3, 1, 4, 5, 1, 5])
y.shape = (7, 1)
mod = FactorizationMachine(m=16)
# Training
epochs = 200
bs, ws, vs, losses = [], [], [], []
for e in range(epochs):
cur_loss = train_step(mod=mod,
x=x,
y_true=y,
lr=0.0025,
loss_f=l2_loss)
# PLot loss
plt.plot(losses)
plt.title('Loss history')
plt.xlabel('Epoch')
plt.ylabel('Loss')
````
## Keras interface
Todo
## Data generator
Todo