<jupyter_start><jupyter_text>Matrix Factorization (MF) ExampleDemonstrates matrix factorization with MXNet on the [MovieLens 100k](http://grouplens.org/datasets/movielens/100k/) dataset.<jupyter_code>import mxnet as mx
from movielens_data import get_data_iter, max_id
from matrix_fact import train
train_test_data = get_data_iter(batch_size=50)
max_user, max_item = max_id('./ml-100k/u.data')
(max_user, max_item)<jupyter_output><empty_output><jupyter_text>Linear MF<jupyter_code>def plain_net(k):
    # input
    user = mx.symbol.Variable('user')
    item = mx.symbol.Variable('item')
    score = mx.symbol.Variable('score')
    # user feature lookup
    user = mx.symbol.Embedding(data = user, input_dim = max_user, output_dim = k) 
    # item feature lookup
    item = mx.symbol.Embedding(data = item, input_dim = max_item, output_dim = k)
    # predict by the inner product, which is elementwise product and then sum
    pred = user * item
    pred = mx.symbol.sum_axis(data = pred, axis = 1)
    pred = mx.symbol.Flatten(data = pred)
    # loss layer
    pred = mx.symbol.LinearRegressionOutput(data = pred, label = score)
    return pred

net1 = plain_net(64)
mx.viz.plot_network(net1)
results1 = train(net1, train_test_data, num_epoch=15, learning_rate=0.02)<jupyter_output><empty_output><jupyter_text>Neural Network (non-linear) MF<jupyter_code>def get_one_layer_mlp(hidden, k):
    # input
    user = mx.symbol.Variable('user')
    item = mx.symbol.Variable('item')
    score = mx.symbol.Variable('score')
    # user latent features
    user = mx.symbol.Embedding(data = user, input_dim = max_user, output_dim = k)
    user = mx.symbol.Activation(data = user, act_type='relu')
    user = mx.symbol.FullyConnected(data = user, num_hidden = hidden)
    # item latent features
    item = mx.symbol.Embedding(data = item, input_dim = max_item, output_dim = k)
    item = mx.symbol.Activation(data = item, act_type='relu')
    item = mx.symbol.FullyConnected(data = item, num_hidden = hidden)
    # predict by the inner product
    pred = user * item
    pred = mx.symbol.sum_axis(data = pred, axis = 1)
    pred = mx.symbol.Flatten(data = pred)
    # loss layer
    pred = mx.symbol.LinearRegressionOutput(data = pred, label = score)
    return pred

net2 = get_one_layer_mlp(64, 64)
mx.viz.plot_network(net2)
results2 = train(net2, train_test_data, num_epoch=15, learning_rate=0.02)<jupyter_output><empty_output><jupyter_text>Visualizing resultsNow let's draw a single chart that compares the learning curves of the three different models.We'll use the bokeh library since it gives nice interactive charting.<jupyter_code>import bokeh
import bokeh.io
import bokeh.plotting
bokeh.io.output_notebook()
import pandas as pd

def viz_lines(fig, results, legend, color):
    df = pd.DataFrame(results._data['eval'])
    fig.line(df.elapsed,df.RMSE, color=color, legend=legend, line_width=2)
    df = pd.DataFrame(results._data['train'])
    fig.line(df.elapsed,df.RMSE, color=color, line_dash='dotted', alpha=0.1)

fig = bokeh.plotting.Figure(x_axis_type='datetime', x_axis_label='Training time', y_axis_label='RMSE')
viz_lines(fig, results1, "Linear MF", "orange")
viz_lines(fig, results2, "MLP", "blue")

bokeh.io.show(fig)<jupyter_output><empty_output><jupyter_text>AcknowledgementThis tutorial is based on examples from [xlvector/github](https://github.com/xlvector/).<jupyter_code># What if we let the linear model train for as long?
results1 = train(net1, train_test_data, num_epoch=30, learning_rate=0.02)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11801242236024845, 'fraction_numerical': 0.01383399209486166, 'mean_word_length': 4.954621848739496, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '258437', 'license': 'Apache-2.0', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1244, 'n_words': 276, 'path': 'example/recommenders/demo1-MF.ipynb', 'repo_name': 'Abusnina/mxnet', 'size': '3542'}