Skip to content

Commit

Permalink
small modifs
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAuriau committed Jan 3, 2024
1 parent 11f513a commit c7079cf
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions notebooks/custom_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,17 @@
"### *Utility Computation*\n",
"\n",
"In the method compute_utility, we need to define how to estimate each item utility for each session using the features and initialized weights.\n",
"The arguments of the function are each features type of the ChoiceDataset class:\n",
"The arguments of the function are a batch of each features type of the ChoiceDataset class:\n",
"\n",
"| Order | Argument | shape | Features for ModeCanada| \n",
"|---|---|---|---|\n",
"| 1 | items_features | (n_items, n_items_features) | Items OneHot vectors | \n",
"| 2 | sessions_features | (n_sessions, n_sessions_features) | Customer Income | \n",
"| 3 | sessions_items_features | (n_sessions, n_items, n_sessions_items_features) | Cost, Freq, Ivt, Ovt values of each mode | \n",
"| 4 | sessions_items_availabilities | (n_sessions, n_items) | Not Used | \n",
"| 5 | choices | (n_sessions, ) | Not Used | \n",
"| 2 | sessions_features | (batch_size, n_sessions_features) | Customer Income | \n",
"| 3 | sessions_items_features | (batch_size, n_items, n_sessions_items_features) | Cost, Freq, Ivt, Ovt values of each mode | \n",
"| 4 | sessions_items_availabilities | (batch_size, n_items) | Not Used | \n",
"| 5 | choices | (batch_size, ) | Not Used | \n",
"\n",
"batch_size represents the number of sessions given in the batch.\n",
"The method needs to return the utilities, in the form of a matrix of shape (n_sessions, n_items), reprenting the utility of each item for each session."
]
},
Expand Down Expand Up @@ -163,8 +164,10 @@
" # Create model weights. Basically is one weight by feature + one for intercept\n",
" beta_inter = tf.Variable(tf.random_normal_initializer(0.0, 0.02, seed=42)(shape=(1, 3)),\n",
" name=\"beta_inter\")\n",
" beta_freq_cost_ovt = tf.Variable(tf.random_normal_initializer(0.0, 0.02, seed=42)(shape=(1, 3)),\n",
" name=\"beta_freq_cost_ovt\")\n",
" beta_freq_cost_ovt = tf.Variable(\n",
" tf.random_normal_initializer(0.0, 0.02, seed=42)(shape=(1, 3)),\n",
" name=\"beta_freq_cost_ovt\"\n",
" )\n",
" beta_income = tf.Variable(tf.random_normal_initializer(0.0, 0.02, seed=42)(shape=(1, 3)),\n",
" name=\"beta_income\")\n",
" beta_ivt = tf.Variable(tf.random_normal_initializer(0.0, 0.02, seed=42)(shape=(1, 4)),\n",
Expand All @@ -174,18 +177,25 @@
" self.weights = [beta_inter, beta_freq_cost_ovt, beta_income, beta_ivt]\n",
"\n",
"\n",
" def compute_utility(self, items_batch, sessions_batch, sessions_items_batch, availabilities_batch, choices_batch):\n",
" def compute_utility(self,\n",
" items_batch,\n",
" sessions_batch,\n",
" sessions_items_batch,\n",
" availabilities_batch,\n",
" choices_batch):\n",
" \"\"\"Method that defines how the model computes the utility of a product.\n",
"\n",
" MNL, here U =\n",
"\n",
" Parameters\n",
" ----------\n",
" items_batch : tuple of np.ndarray (items_features)\n",
" Fixed-Item-Features: formatting from ChoiceDataset: a matrix representing the products constant features.\n",
" Fixed-Item-Features: formatting from ChoiceDataset: a matrix representing the products\n",
" constant features.\n",
" Shape must be (n_items, n_items_features)\n",
" sessions_batch : tuple of np.ndarray (sessions_features)\n",
" Time-Features. Not used as not conditional MNL, means it is the same for all products and is not implicated in utility computation.\n",
" Time-Features. Not used as not conditional MNL, means it is the same for all products\n",
" and is not implicated in utility computation.\n",
" Shape must be (n_sessions, n_sessions_features)\n",
" sessions_items_batch : tuple of np.ndarray (sessions_items_features)\n",
" Time-Item-Features\n",
Expand All @@ -203,7 +213,10 @@
" Utility of each product for each session.\n",
" Shape must be (n_sessions, n_items)\n",
" \"\"\"\n",
" # We use the fact that items_features is OneHot of the item, letting us selecting the right beta when needed (through dot)\n",
" _ = (availabilities_batch, choices_batch) # Avoid unused variable warning\n",
"\n",
" # We use the fact that items_features is OneHot of the item, letting us selecting the right\n",
" # Beta when needed (through dot)\n",
" # Utility from items features + intercept\n",
"\n",
" # Concatenation to reach right shape for dot product\n",
Expand Down Expand Up @@ -297,7 +310,7 @@
"ivt(0, air) & ivt(0, bus) & ivt(0, car) & ivt(0,train) \\\\\n",
"ivt(1, air) & ivt(1, bus) & ivt(1, car) & ivt(1,train) \\\\\n",
"... & ... & ... & ... \\\\\n",
"ivt(batch_size, air) & ivt(batch_size, bus) & ivt(batch_size, car) & ivt(batch_size,train) \\\\\n",
"ivt(batch\\_size, air) & ivt(batch\\_size, bus) & ivt(batch\\_size, car) & ivt(batch\\_size,train) \\\\\n",
"\\end{array}\\right)$\n",
"\n",
"\n",
Expand Down Expand Up @@ -383,10 +396,16 @@
" self.dense_1 = Dense(units=10, activation=\"elu\")\n",
" # Second linear layer\n",
" self.dense_2 = Dense(units=1, activation=\"linear\")\n",
" # We do not forget to specify self.weights with all coefficients that need to be estimated. Easy with TensorFlow.Layer\n",
" # We do not forget to specify self.weights with all coefficients that need to be estimated.\n",
" # Easy with TensorFlow.Layer\n",
" self.weights = self.dense_1.trainable_variables + self.dense_2.trainable_variables\n",
" \n",
" def compute_utility(self, items_batch, sessions_batch, sessions_items_batch, availabilities_batch, choices_batch):\n",
" def compute_utility(self,\n",
" items_batch,\n",
" sessions_batch,\n",
" sessions_items_batch,\n",
" availabilities_batch,\n",
" choices_batch):\n",
" # We apply the neural network to all sessions_items_features for all the items\n",
" # We then concatenate the utilities of each item of shape (n_sessions, 1) into a single one of shape (n_sessions, n_items)\n",
" u = tf.concat([self.dense_2(self.dense_1(sessions_items_batch[0][:, i])) for i in range(sessions_items_batch[0].shape[1])], axis=1)\n",
Expand Down

0 comments on commit c7079cf

Please sign in to comment.