Skip to content

Commit

Permalink
FIX: val_dataset for fit_with_gd
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAuriau committed Dec 27, 2024
1 parent f8c8297 commit f1e980a
Showing 1 changed file with 7 additions and 41 deletions.
48 changes: 7 additions & 41 deletions choice_learn/models/latent_class_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def compute_batch_utility(
utilities.append(model_utilities)
return utilities

def fit(self, choice_dataset, sample_weight=None, verbose=0):
def fit(self, choice_dataset, sample_weight=None, val_dataset=None, verbose=0):
"""Fit the model on a ChoiceDataset.
Parameters
Expand All @@ -245,6 +245,8 @@ def fit(self, choice_dataset, sample_weight=None, verbose=0):
Dataset to be used for coefficients estimations
sample_weight : np.ndarray, optional
sample weights to apply, by default None
val_dataset: ChoiceDataset
Validation dataset for MLE Gradient Descent Optimization
verbose : int, optional
print level, for debugging, by default 0
Expand Down Expand Up @@ -277,7 +279,10 @@ def fit(self, choice_dataset, sample_weight=None, verbose=0):
self.optimizer = tf.keras.optimizers.Adam(self.lr)

return self._fit_with_gd(
choice_dataset=choice_dataset, sample_weight=sample_weight, verbose=verbose
choice_dataset=choice_dataset,
sample_weight=sample_weight,
verbose=verbose,
val_dataset=val_dataset,
)

raise ValueError(f"Fit method not implemented: {self.fit_method}")
Expand Down Expand Up @@ -762,45 +767,6 @@ def _fit_with_gd(
# self.callbacks.on_train_end(logs=temps_logs)
return losses_history

def _nothing(self, inputs):
"""_summary_.
Parameters
----------
inputs : _type_
_description_
Returns
-------
_type_
_description_
"""
latent_probas = tf.clip_by_value(
self.latent_logits - tf.reduce_max(self.latent_logits), self.minf, 0
)
latent_probas = tf.math.exp(latent_probas)
# latent_probas = tf.math.abs(self.logit_latent_probas) # alternative implementation
latent_probas = latent_probas / tf.reduce_sum(latent_probas)
proba_list = []
avail = inputs[4]
for q in range(self.n_latent_classes):
combined = self.models[q].compute_batch_utility(*inputs)
combined = tf.clip_by_value(
combined - tf.reduce_max(combined, axis=1, keepdims=True), self.minf, 0
)
combined = tf.keras.layers.Activation(activation=tf.nn.softmax)(combined)
# combined = tf.keras.layers.Softmax()(combined)
combined = combined * avail
combined = latent_probas[q] * tf.math.divide(
combined, tf.reduce_sum(combined, axis=1, keepdims=True)
)
combined = tf.expand_dims(combined, -1)
proba_list.append(combined)
# print(combined.get_shape()) # it is useful to print the shape of tensors for debugging

proba_final = tf.keras.layers.Concatenate(axis=2)(proba_list)
return tf.math.reduce_sum(proba_final, axis=2, keepdims=False)

def _expectation(self, choice_dataset):
predicted_probas = [model.predict_probas(choice_dataset) for model in self.models]
latent_probabilities = self.get_latent_classes_weights()
Expand Down

0 comments on commit f1e980a

Please sign in to comment.