in tensorflow_ranking/python/keras/canned/gam.py [0:0]
def score(self, context_features=None, example_features=None, training=True):
"""Univariate scoring of context and one example to generate a score.
Args:
context_features: (dict) context feature names to 2D tensors of shape
[batch_size, ...].
example_features: (dict) example feature names to 2D tensors of shape
[batch_size, ...].
training: (bool) whether in training or inference mode.
Returns:
(tf.Tensor) A score tensor of shape [batch_size, 1].
"""
example_feature_names = sorted(list(self.example_feature_columns.keys()))
context_feature_names = sorted(list(self.context_feature_columns.keys()))
context_input = [
tf.keras.layers.Flatten()(context_features[name])
for name in context_feature_names
]
example_input = [
tf.keras.layers.Flatten()(example_features[name])
for name in example_feature_names
]
# Construct a tower for each example feature.
sub_logits_list = []
with tf.name_scope('example_feature_towers'):
for name, input_tensor in zip(example_feature_names, example_input):
with tf.name_scope('{}_tower'.format(name)):
cur = input_tensor
layers = self._per_example_feature_layers[name]
for layer in layers:
cur = layer(cur, training=training)
sub_logits = tf.identity(
cur, name='{}_{}'.format(name, _SUBSCORE_POSTFIX))
sub_logits_list.append(sub_logits)
sub_weights_list = []
if context_input:
# Construct a tower for each context feature.
with tf.name_scope('context_feature_towers'):
for name, input_tensor in zip(context_feature_names, context_input):
with tf.name_scope('{}_tower'.format(name)):
cur = input_tensor
layers = self._per_context_feature_layers[name]
for layer in layers:
cur = layer(cur, training=training)
cur = tf.keras.layers.Softmax()(cur)
sub_weights = tf.identity(
cur, name='{}_{}'.format(name, _SUBWEIGHT_POSTFIX))
sub_weights_list.append(sub_weights)
# Construct an additive model from the outputs of all example feature towers
# weighted by outputs of all context feature towers.
# Note that these layers do not have any trainable variables, hence we
# are not defining them in init but defining them here, similar to Flatten.
if sub_weights_list:
sub_logits = tf.keras.layers.Concatenate(axis=-1)(sub_logits_list)
sub_weights = (
tf.keras.layers.Add()(sub_weights_list)
if len(sub_weights_list) > 1 else sub_weights_list[0])
logits = tf.keras.backend.sum(sub_logits * sub_weights, axis=-1)
else:
logits = tf.keras.layers.Add()(
sub_logits_list) if len(sub_logits_list) > 1 else sub_logits_list[0]
return logits