Пример #1
0
  def test_personal_matrix_factorization_trains_multiple_users_fails(self):
    num_users = 2
    num_items = 8
    num_latent_factors = 5
    personal_model = True
    add_biases = False
    l2_regularization = 0.0

    with self.assertRaises(ValueError):
      models.get_matrix_factorization_model(
          num_users,
          num_items,
          num_latent_factors,
          personal_model=personal_model,
          add_biases=add_biases,
          l2_regularization=l2_regularization)
Пример #2
0
  def test_personal_matrix_factorization_trains_keras_accuracy_threshold(self):
    num_users = 1
    num_items = 8
    num_latent_factors = 5
    personal_model = True
    add_biases = False
    l2_regularization = 0.0

    matrix_factorization_model = models.get_matrix_factorization_model(
        num_users,
        num_items,
        num_latent_factors,
        personal_model=personal_model,
        add_biases=add_biases,
        l2_regularization=l2_regularization)

    keras_model = models.build_keras_model(matrix_factorization_model)

    # Ensure number of parameters of model is as expected as a quick check.
    expected_num_params = (
        num_latent_factors  # User embeddings.
        + num_items * num_latent_factors)  # Item embeddings.
    num_params = keras_model.count_params()
    self.assertEqual(expected_num_params, num_params)
    num_trainable_params = count_trainable_params(keras_model)
    self.assertEqual(expected_num_params, num_trainable_params)

    history = keras_model.fit(
        self.train_items, self.train_preferences, batch_size=1, epochs=1)

    # Ensure the model has a valid loss after one epoch (not NaN).
    self.assertIn(_KERAS_LOSS, history.history)
    losses = history.history[_KERAS_LOSS]
    self.assertLen(losses, 1)
    self.assertFalse(np.isnan(losses[0]))

    # Ensure the model has valid number of examples after one epoch (not NaN).
    self.assertIn(_KERAS_NUM_EXAMPLES, history.history)
    num_examples = history.history[_KERAS_NUM_EXAMPLES]
    self.assertLen(num_examples, 1)
    self.assertEqual(num_examples[0], 5)

    # Ensure the model has valid number of batches after one epoch (not NaN).
    self.assertIn(_KERAS_NUM_BATCHES, history.history)
    num_batches = history.history[_KERAS_NUM_BATCHES]
    self.assertLen(num_batches, 1)
    self.assertEqual(num_batches[0], 5)

    # Ensure the model has a valid reconstruction accuracy after one epoch (not
    # NaN).
    self.assertIn(_KERAS_ACCURACY, history.history)
    accuracies = history.history[_KERAS_ACCURACY]
    self.assertLen(accuracies, 1)
    self.assertFalse(np.isnan(accuracies[0]))
Пример #3
0
  def test_matrix_factorization_trains_keras(self):
    num_users = 10
    num_items = 8
    num_latent_factors = 5
    personal_model = False
    add_biases = False
    l2_regularization = 0.0
    learning_rate = 0.5

    matrix_factorization_model = models.get_matrix_factorization_model(
        num_users,
        num_items,
        num_latent_factors,
        personal_model=personal_model,
        add_biases=add_biases,
        l2_regularization=l2_regularization)

    keras_model = models.build_keras_model(
        matrix_factorization_model, tf.keras.optimizers.SGD(learning_rate))

    # Ensure number of parameters of model is as expected as a quick check.
    expected_num_params = (
        num_users * num_latent_factors  # User embeddings.
        + num_items * num_latent_factors)  # Item embeddings.
    num_params = keras_model.count_params()
    self.assertEqual(expected_num_params, num_params)
    num_trainable_params = count_trainable_params(keras_model)
    self.assertEqual(expected_num_params, num_trainable_params)

    history = keras_model.fit([self.train_users, self.train_items],
                              self.train_preferences,
                              batch_size=1,
                              epochs=1)

    # Ensure the model has a valid loss after one epoch (not NaN).
    self.assertIn(_KERAS_LOSS, history.history)
    losses = history.history[_KERAS_LOSS]
    self.assertLen(losses, 1)
    self.assertFalse(np.isnan(losses[0]))

    # Ensure the model has valid number of examples after one epoch (not NaN).
    self.assertIn(_KERAS_NUM_EXAMPLES, history.history)
    num_examples = history.history[_KERAS_NUM_EXAMPLES]
    self.assertLen(num_examples, 1)
    self.assertEqual(num_examples[0], 5)

    # Ensure the model has valid number of batches after one epoch (not NaN).
    self.assertIn(_KERAS_NUM_BATCHES, history.history)
    num_batches = history.history[_KERAS_NUM_BATCHES]
    self.assertLen(num_batches, 1)
    self.assertEqual(num_batches[0], 5)