(self, inputs, level_name, is_last=False)
| 193 | |
| 194 | # single Extraction Layer |
| 195 | def cgc_model(self, inputs, level_name, is_last=False): |
| 196 | specific_expert_outputs = [] |
| 197 | # build task-specific expert layer |
| 198 | for i in range(self._num_tasks): |
| 199 | for j in range(self._specific_expert_num): |
| 200 | expert_network = self._dnn(inputs[i], dnn_hidden_units=self._expert_dnn_hidden_units, layer_name=level_name + 'task_' + self._towers[i][0] + '_expert_specific_' + str(j)) |
| 201 | specific_expert_outputs.append(expert_network) |
| 202 | |
| 203 | # build task-shared expert layer |
| 204 | shared_expert_outputs = [] |
| 205 | for k in range(self._shared_expert_num): |
| 206 | expert_network = self._dnn(inputs[-1], dnn_hidden_units=self._expert_dnn_hidden_units, layer_name=level_name + 'expert_shared_' + str(k)) |
| 207 | shared_expert_outputs.append(expert_network) |
| 208 | |
| 209 | # task_specific gate (count = num_tasks) |
| 210 | cgc_outs = [] |
| 211 | for i in range(self._num_tasks): |
| 212 | # concat task-specific expert and task-shared expert |
| 213 | cur_expert_num = self._specific_expert_num + self._shared_expert_num |
| 214 | # task_specific + task_shared |
| 215 | cur_experts = specific_expert_outputs[ |
| 216 | i * self._specific_expert_num:(i + 1) * self._specific_expert_num] + shared_expert_outputs |
| 217 | |
| 218 | expert_concat = tf.keras.layers.Lambda(lambda x: tf.stack(x, axis=1))(cur_experts) |
| 219 | |
| 220 | # build gate layers |
| 221 | gate_input = self._dnn(inputs[i], dnn_hidden_units=self._gate_dnn_hidden_units, layer_name=level_name + 'gate_specific_' + self._towers[i][0]) |
| 222 | gate_out = tf.layers.dense(gate_input, units=cur_expert_num, |
| 223 | name=level_name + 'gate_softmax_specific_' + self._towers[i][0]) |
| 224 | gate_out = tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=-1))(gate_out) |
| 225 | |
| 226 | gate_mul_expert = tf.keras.layers.Lambda(lambda x: tf.math.reduce_sum(x[0] * x[1], axis=1, keep_dims=False), |
| 227 | name=level_name + 'gate_mul_expert_specific_' + self._towers[i][0])( |
| 228 | [expert_concat, gate_out]) |
| 229 | cgc_outs.append(gate_mul_expert) |
| 230 | |
| 231 | # if not last, add a shared gate |
| 232 | if not is_last: |
| 233 | cur_expert_num = self._num_tasks * self._specific_expert_num + self._shared_expert_num |
| 234 | cur_experts = specific_expert_outputs + shared_expert_outputs # all the expert include task-specific expert and task-shared expert |
| 235 | |
| 236 | expert_concat = tf.keras.layers.Lambda(lambda x: tf.stack(x, axis=1))(cur_experts) |
| 237 | # gate layers |
| 238 | gate_input = self._dnn(inputs[-1], dnn_hidden_units=self._gate_dnn_hidden_units, layer_name=level_name + 'gate_shared') |
| 239 | gate_out = tf.layers.dense(gate_input, units=cur_expert_num, use_bias=False, activation='softmax', |
| 240 | name=level_name + 'gate_softmax_shared') |
| 241 | gate_out = tf.keras.layers.Lambda(lambda x: tf.expand_dims(x, axis=-1))(gate_out) |
| 242 | |
| 243 | gate_mul_expert = tf.keras.layers.Lambda(lambda x: tf.math.reduce_sum(x[0] * x[1], axis=1, keep_dims=False), |
| 244 | name=level_name + 'gate_mul_expert_shared')( |
| 245 | [expert_concat, gate_out]) |
| 246 | |
| 247 | cgc_outs.append(gate_mul_expert) |
| 248 | return cgc_outs |
| 249 | |
| 250 | # create model |
| 251 | def _create_model(self): |
no test coverage detected