<commit_before># <NAME> (<EMAIL>)
# A (modified) implementation of RankNet as described in [1].
#   [1] http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf
#   [2] https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/MSR-TR-2010-82.pdf

import numpy as np

from keras import backend
from keras.layers import Add, Dense, Input, Lambda
from keras.models import Model

# Model.
h_1 = Dense(128, activation = "relu")
h_2 = Dense(64, activation = "relu")
h_3 = Dense(32, activation = "relu")
s = Dense(1)

# Relevant document score.
INPUT_DIM = 50
rel_doc = Input(shape = (INPUT_DIM, ), dtype = "float32")
h_1_rel = h_1(rel_doc)
h_2_rel = h_2(h_1_rel)
h_3_rel = h_3(h_2_rel)
rel_score = s(h_3_rel)

# Irrelevant document score.
irr_doc = Input(shape = (INPUT_DIM, ), dtype = "float32")
h_1_irr = h_1(irr_doc)
h_2_irr = h_2(h_1_irr)
h_3_irr = h_3(h_2_irr)
irr_score = s(h_3_irr)

# Subtract scores.
negated_irr_score = Lambda(lambda x: -1 * x, output_shape = (1, ))(irr_score)
diff = Add()([rel_score, negated_irr_score])

# Pass difference through sigmoid function.
prob = Dense(1, activation = "sigmoid", weights = [np.array([[1]])], use_bias = False, trainable = False)(diff)

# Build model.
model = Model(inputs = [rel_doc, irr_doc], outputs = prob)
model.compile(optimizer = "adadelta", loss = "binary_crossentropy")

# Fake data.
N = 100
X_1 = 2 * np.random.randn(N, INPUT_DIM)
X_2 = np.random.randn(N, INPUT_DIM)
y = np.ones((X_1.shape[0], 1))

# Train model.
NUM_EPOCHS = 10
BATCH_SIZE = 10
history = model.fit([X_1, X_2], y, epochs = NUM_EPOCHS, verbose = 1)

# Generate scores from document/query features.
get_score = backend.function([rel_doc], rel_score)
get_score([X_1])
get_score([X_2])
<commit_msg>Make outputs of get_score a list to make backend agnostic.<commit_after># <NAME> (<EMAIL>)
# A (modified) implementation of RankNet as described in [1].
#   [1] http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf
#   [2] https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/MSR-TR-2010-82.pdf

import numpy as np

from keras import backend
from keras.layers import Add, Dense, Input, Lambda
from keras.models import Model

# Model.
h_1 = Dense(128, activation = "relu")
h_2 = Dense(64, activation = "relu")
h_3 = Dense(32, activation = "relu")
s = Dense(1)

# Relevant document score.
INPUT_DIM = 50
rel_doc = Input(shape = (INPUT_DIM, ), dtype = "float32")
h_1_rel = h_1(rel_doc)
h_2_rel = h_2(h_1_rel)
h_3_rel = h_3(h_2_rel)
rel_score = s(h_3_rel)

# Irrelevant document score.
irr_doc = Input(shape = (INPUT_DIM, ), dtype = "float32")
h_1_irr = h_1(irr_doc)
h_2_irr = h_2(h_1_irr)
h_3_irr = h_3(h_2_irr)
irr_score = s(h_3_irr)

# Subtract scores.
negated_irr_score = Lambda(lambda x: -1 * x, output_shape = (1, ))(irr_score)
diff = Add()([rel_score, negated_irr_score])

# Pass difference through sigmoid function.
prob = Dense(1, activation = "sigmoid", weights = [np.array([[1]])], use_bias = False, trainable = False)(diff)

# Build model.
model = Model(inputs = [rel_doc, irr_doc], outputs = prob)
model.compile(optimizer = "adadelta", loss = "binary_crossentropy")

# Fake data.
N = 100
X_1 = 2 * np.random.randn(N, INPUT_DIM)
X_2 = np.random.randn(N, INPUT_DIM)
y = np.ones((X_1.shape[0], 1))

# Train model.
NUM_EPOCHS = 10
BATCH_SIZE = 10
history = model.fit([X_1, X_2], y, epochs = NUM_EPOCHS, verbose = 1)

# Generate scores from document/query features.
get_score = backend.function([rel_doc], [rel_score])
get_score([X_1])
get_score([X_2])

{'fraction_non_alphanumeric': 0.15098094967301678, 'fraction_numerical': 0.04663065112311629, 'mean_word_length': 4.952622673434856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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}}
{'dir': 'git-commits-cleaned', 'id': '163810', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1470, 'n_words': 333}