<jupyter_start><jupyter_text>Discrete Choice Problem<jupyter_code>%%capture --no-stderr
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from csrank import FATEDiscreteChoiceFunction<jupyter_output>Using TensorFlow backend.
/home/pritha/anaconda3/envs/linenv/lib/python3.7/site-packages/sklearn/externals/joblib/__init__.py:15: DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.
  warnings.warn(msg, category=DeprecationWarning)<jupyter_text>The Medoid DC Problem In the medoid problem the goal of the discrete choice algorithms for the medoid problem is to find the most central object for the given set.This problem is inspired by solving the task of finding a good representation of the given data using the most central point of the data pointsWe will generate a random dataset where each instance contains 5 objects and 2 features for easy plotting.<jupyter_code>from csrank import DiscreteChoiceDatasetGenerator
seed = 123
n_train = 10000
n_test = 10000
n_features = 2
n_objects = 5
gen = DiscreteChoiceDatasetGenerator(dataset_type='medoid', random_state=seed,
                                n_train_instances=n_train,
                                n_test_instances=n_test,
                                n_objects=n_objects,
                                n_features=n_features)
X_train, Y_train, X_test, Y_test = gen.get_single_train_test_split()<jupyter_output><empty_output><jupyter_text>Let us plot a random instance. The medoid is marked as M.<jupyter_code>def get_name(d):
    if d ==0:
        return ""
    else:
        return "M"
fig, ax = plt.subplots(figsize=(5,5))
inst = np.random.choice(n_train)
ax.scatter(X_train[inst][:, 0], X_train[inst][:, 1])
for i in range(n_objects):
    ax.text(X_train[inst, i, 0]+0.01,
            X_train[inst, i, 1]+0.01,
            s=get_name(int(Y_train[inst, i])))<jupyter_output><empty_output><jupyter_text>The FATE networkThe first-aggregate-then-evaluate approach learns an embedding of each object and then aggregates that into a _context_:\begin{equation}	\mu_{C(\vec{x})} = \frac{1}{|C(\vec{x})|} \sum_{\vec{y} \in C(\vec{x})} \phi(\vec{y})\end{equation}and then scores each object $\vec{x}$ using a generalized utility function $U (\vec{x}, \mu_{C(\vec{x})})$.<jupyter_code>from keras.optimizers import SGD
from csrank import FATEObjectRanker
from csrank.losses import smooth_rank_loss
fate = FATEObjectRanker(
    n_object_features=n_features,
    loss_function=smooth_rank_loss,
    optimizer=SGD(lr=1e-4, nesterov=True, momentum=0.9))<jupyter_output><empty_output><jupyter_text>We will run the training for only 10 epochs to get an idea of the convergence:<jupyter_code>fate.fit(X_train, Y_train, verbose=True, epochs=10)
scores = fate.predict_scores(X_test)
from csrank.metrics_np import categorical_accuracy_np
from keras import backend as K
categorical_accuracy_np(Y_test, scores)<jupyter_output><empty_output><jupyter_text>Not converged yet, but let us visualize the scores it assigns to test instances:<jupyter_code>fig, ax = plt.subplots(figsize=(5,5))
inst = np.random.choice(n_test)
ax.scatter(X_test[inst][:, 0], X_test[inst][:, 1])
for i in range(n_objects):
    ax.text(X_test[inst, i, 0]+0.01,
            X_test[inst, i, 1]+0.01,
            s=int(Y_test[inst, i]))
    ax.text(X_test[inst, i, 0]+0.01,
            X_test[inst, i, 1]-0.05,
            s='{:.1f}'.format(scores[inst][i]),
            color='b')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10591983010353066, 'fraction_numerical': 0.019644279267321475, 'mean_word_length': 4.413793103448276, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '194684', 'license': 'Apache-2.0', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1239, 'n_words': 335, 'path': 'docs/notebooks/FATE-Net-DC.ipynb', 'repo_name': 'helegraf/cs-ranking', 'size': '3767'}