geffy / tffm

TensorFlow implementation of an arbitrary order Factorization Machine
MIT License
780 stars 176 forks source link

Interaction factors #33

Closed NehKulkarni closed 6 years ago

NehKulkarni commented 6 years ago

Hi, Is it possible to display all the interaction factors between the variables?

peterewills commented 6 years ago

Here's how I do it, on the MNIST data set. Expose the weights and interaction matrix via w,V = model.weights. You can just insert this cell into the notebook example.ipynb.

model = TFFMClassifier(
        order=2, 
        rank=10, 
        optimizer=tf.train.AdamOptimizer(learning_rate=0.001), 
        n_epochs=50, 
        batch_size=1024,
        init_std=0.001,
        reg=0.01,
        input_type='dense',
        seed=42
    )

model.fit(X_tr, y_tr, show_progress=True)
predictions = model.predict(X_te)

# visualize weights
w,V = model.weights

plt.imshow(w.reshape(28,28));
plt.title('Weight (Linear Term)');

for idx,col in enumerate(V.T):
    plt.figure();
    plt.imshow(col.reshape(28,28))
    plt.title('Interaction Feature {}'.format(idx))

model.destroy()