예제 #1
0
 def __init__(self,
              config,
              model_ctor=CollabFilteringModel,
              name="Recommender"):
     super().__init__(config, name=name)
     self._history_length = config["history_length"]
     self._num_docs = config.get("num_docs")
     self._num_topics = config.get("num_topics")
     self._model = model_ctor(self._num_users, self._num_docs, 32,
                              self._history_length)
     doc_history_model = estimation.FiniteHistoryStateModel(
         history_length=self._history_length,
         observation_shape=(),
         batch_shape=(self._num_users, ),
         dtype=tf.int32)
     self._doc_history = dynamic.NoOPOrContinueStateModel(doc_history_model,
                                                          batch_ndims=1)
     ctime_history_model = estimation.FiniteHistoryStateModel(
         history_length=self._history_length,
         observation_shape=(),
         batch_shape=(self._num_users, ),
         dtype=tf.float32)
     self._ctime_history = dynamic.NoOPOrContinueStateModel(
         ctime_history_model, batch_ndims=1)
     self._document_sampler = selector_lib.IteratedMultinormialLogitChoiceModel(
         self._slate_size, (self._num_users, ),
         -np.Inf * tf.ones(self._num_users))
     # Call model to create weights
     ctime_history = self._ctime_history.initial_state().get("state")
     docid_history = self._doc_history.initial_state().get("state")
     self._model(docid_history, ctime_history)
예제 #2
0
    def test_noop_model(self):
        class DummyModelWithRandomness(self.DummyDynamicStateModel):
            def initial_state(self, parameters=None):
                det_i_value = super().initial_state(parameters=parameters)
                return Value(
                    state=det_i_value.get('state'),
                    state2=ed.Categorical(logits=det_i_value.get('state')))

            def next_state(self, old_state, inputs, parameters=None):
                det_n_value = super().next_state(old_state, inputs, parameters)
                return Value(
                    state=det_n_value.get('state'),
                    state2=ed.Categorical(logits=det_n_value.get('state')))

            def specs(self):
                return super().specs().union(Value(state2=FieldSpec()))

        state_model = dynamic.NoOPOrContinueStateModel(
            DummyModelWithRandomness(3.14), batch_ndims=2)
        i_state = state_model.initial_state()
        self.assertAllClose(i_state.get('state'),
                            tf.ones((4, 3, 2), dtype=tf.float32) * 3.14)
        self.assertIsInstance(
            i_state.get('tbranch.state2').distribution, tfd.Categorical)
        self.assertIsInstance(
            i_state.get('fbranch.state2').distribution, tfd.Categorical)
        selector = tf.ones((4, 3)) < 0
        next_state = state_model.next_state(
            i_state, Value(inputs=1.0, condition=selector))
        self.assertAllClose(next_state.get('state'),
                            tf.ones((4, 3, 2), dtype=tf.float32) * 6.28)
        self.assert_log_prob_shape_compliance(i_state, next_state)
        selector = tf.constant([[True, False, True], [False, True, False],
                                [True, True, False], [False, False, True]])
        next_next_state = state_model.next_state(
            next_state, Value(inputs=1.0, condition=selector))
        noopers = tf.boolean_mask(next_next_state.get('state'), selector)
        self.assertAllClose(noopers,
                            6.28 * tf.ones_like(noopers, dtype=tf.float32))
        evolvers = tf.boolean_mask(next_next_state.get('state'),
                                   tf.math.logical_not(selector))
        self.assertAllClose(evolvers,
                            9.42 * tf.ones_like(evolvers, dtype=tf.float32))
        self.assert_log_prob_shape_compliance(next_state, next_next_state)
예제 #3
0
 def __init__(
         self,
         config,
         affinity_model_ctor=affinity_lib.TargetPointSimilarity,
         choice_model_ctor=selector_lib.MultinormialLogitChoiceModel,
         no_click_mass=0.,
         # Step size for updating user interests based on consumed documents
         # (small!). We may want to have different values for different interests
         # to represent how malleable those interests are, e.g., strong dislikes
         # may be less malleable).
         interest_step_size=0.1,
         reset_users_if_timed_out=False,
         interest_update_noise_scale=None,
         initial_interest_generator=None,
         max_user_affinity=10.0):
     super().__init__(config)
     self._config = config
     self._max_user_affinity = max_user_affinity
     self._affinity_model = affinity_model_ctor(
         (self._num_users, ), config['slate_size'], 'negative_euclidean')
     self._choice_model = choice_model_ctor(
         (self._num_users, ), no_click_mass * tf.ones(self._num_users))
     self._interest_generator = initial_interest_generator
     if interest_update_noise_scale is None:
         interest_noise = None
     else:
         interest_noise = interest_update_noise_scale * tf.ones(
             self._num_users, dtype=tf.float32)
     interest_model = dynamic.ControlledLinearScaledGaussianStateModel(
         dim=self._num_topics,
         transition_scales=None,
         control_scales=interest_step_size *
         tf.ones(self._num_users, dtype=tf.float32),
         noise_scales=interest_noise,
         initial_dist_scales=tf.ones(self._num_users, dtype=tf.float32))
     self._interest_model = dynamic.NoOPOrContinueStateModel(interest_model,
                                                             batch_ndims=1)