Exemple #1
0
    def _create_model(self):
        print("\n*** HIERARHICAL SEQ2SEQ translates from {} -> {} -> {} ***".
              format(self.feats_dict[self._first][2],
                     self.feats_dict[self._second][2],
                     self.feats_dict[self._third][2]))

        # --------------------Seq2Seq network params--------------------------------
        n_samples = self.train_first.shape[0]
        input_length = self.train_first.shape[1]
        input_dim = self.train_first.shape[2]
        output_length = self.train_second.shape[1]
        output_dim = self.train_second.shape[2]

        # --------------- MODEL TRANSLATION DEFINITION -----------------------------
        print("Creating LEVEL 1 TRANSLATION SEQ2SEQ model ...")
        inputs, encoded_seq, decoded_seq, cycled_decoded_seq = \
          mctn_model(output_dim=output_dim,
                     hidden_dim=self.configs['translation1']['hidden_dim'],
                     output_length=output_length,
                     input_dim=input_dim,
                     input_length=input_length,
                     depth=self.configs['translation1']['depth'],
                     bidirectional=self.configs['translation1']['is_bidirectional'],
                     is_cycled=self.is_cycled)
        # -------------

        input_length_2 = input_length  # == encoded_seq.shape[1]
        input_dim_2 = self.configs['translation1']['hidden_dim']
        output_length_2 = self.train_third.shape[1]
        output_dim_2 = self.train_third.shape[2]

        print("Creating LEVEL 2 TRANSLATION SEQ2SEQ model ...")
        inputs_2, encoded_seq_2, decoded_seq_2 = \
          mctn_level2_model(input=encoded_seq,
                            output_dim=output_dim_2,
                            hidden_dim=self.configs['translation2']['hidden_dim'],
                            output_length=output_length_2,
                            input_dim=input_dim_2,
                            input_length=input_length_2,
                            depth=self.configs['translation2']['depth'],
                            bidirectional=self.configs['translation2'][
                              'is_bidirectional'])

        # ---------------- MODEL REGRESSION DEFINITION -----------------------------
        print("Creating REGRESSION model ...")
        regression_score = \
          create_regression_model(
            n_hidden=self.configs['regression']['reg_hidden_dim'],
            input=encoded_seq_2,
            l2_factor=self.configs['regression']['l2_factor'])

        # ------------------ E2E REGRESSION DEFINITION -----------------------------
        print("BUILDING A JOINT END-TO-END MODEL")
        if self.is_cycled:
            outputs = [
                decoded_seq, cycled_decoded_seq, decoded_seq_2,
                regression_score
            ]
            losses = [
                self.configs['translation1']['loss_type'],
                self.configs['translation1']['cycle_loss_type'],
                self.configs['translation2']['loss_type'],
                self.configs['regression']['loss_type']
            ]
            losses_weights = [
                self.configs['translation1']['loss_weight'],
                self.configs['translation1']['cycle_loss_weight'],
                self.configs['translation2']['loss_weight'],
                self.configs['regression']['loss_weight']
            ]
        else:
            outputs = [decoded_seq, decoded_seq_2, regression_score]
            losses = [
                self.configs['translation1']['loss_type'],
                self.configs['translation2']['loss_type'],
                self.configs['regression']['loss_type']
            ]
            losses_weights = [
                self.configs['translation1']['loss_weight'],
                self.configs['translation2']['loss_weight'],
                self.configs['regression']['loss_weight']
            ]

        end2end_model = Model(inputs=inputs, outputs=outputs)
        print("Compiling model")
        optimizer = get_optimizers(opt=self.configs['general']['optim'],
                                   init_lr=self.configs['general']['init_lr'])
        end2end_model.compile(loss=losses,
                              loss_weights=losses_weights,
                              optimizer=optimizer)
        print("Model summary:")
        print(end2end_model.summary())
        print("END2END MODEL CREATED!")

        self.model = end2end_model
Exemple #2
0
  def _create_model(self):
    print("\n***SEQ2SEQ translates from {} to {}***".
          format(self.feats_dict[self._in][2],
                 self.feats_dict[self._out][2]))

    input_dims = self.input_train.shape
    output_dims = self.output_train.shape
    print("Input Dims = {}".format(input_dims))
    print("Output (Translation) Dims = {}".format(output_dims))
    print("Output (Regression) Dims = {}\n".format(self.feats_dict[
                                                     'train_labels'].shape))
    # --------------------Seq2Seq network params--------------------------------
    n_samples = input_dims[0]
    input_length = input_dims[1]
    input_dim = input_dims[2]
    output_length = output_dims[1]
    output_dim = output_dims[2]

    # --------------- MODEL TRANSLATION DEFINITION -----------------------------
    print("Creating TRANSLATION SEQ2SEQ model ...")
    inputs, encoded_seq, decoded_seq, cycled_decoded_seq = \
      mctn_model(output_dim=output_dim,
                 hidden_dim=self.configs['translation']['hidden_dim'],
                 output_length=output_length,
                 input_dim=input_dim,
                 input_length=input_length,
                 depth=self.configs['translation']['depth'],
                 bidirectional=self.configs['translation']['is_bidirectional'],
                 is_cycled=self.is_cycled,
                 dropout=0.8
                 )

    # ---------------- MODEL REGRESSION DEFINITION -----------------------------
    print("Creating REGRESSION model ...")
    regression_score = \
      create_regression_model(
        n_hidden=self.configs['regression']['reg_hidden_dim'],
        input=encoded_seq,
        l2_factor=self.configs['regression']['l2_factor'],
      input_hidden=self.configs['translation']['hidden_dim'],
      dropout=0.8)

    # ------------------ E2E REGRESSION DEFINITION -----------------------------
    print("BUILDING A JOINT END-TO-END MODEL")
    if self.is_cycled:
      outputs = [decoded_seq, cycled_decoded_seq, regression_score]
      losses = [self.configs['translation']['loss_type'],
                self.configs['translation']['cycle_loss_type'],
               self.configs['regression']['loss_type']
                ]
      losses_weights = [self.configs['translation']['loss_weight'],
                        self.configs['translation']['cycle_loss_weight'],
                        self.configs['regression']['loss_weight']
                        ]
    else:
      outputs = [decoded_seq, regression_score]
      losses = [self.configs['translation']['loss_type'],
                self.configs['regression']['loss_type']
                ]
      losses_weights = [self.configs['translation']['loss_weight'],
                        self.configs['regression']['loss_weight']
                        ]

    end2end_model = Model(inputs=inputs,
                          outputs=outputs)
    print("Compiling model")
    optimizer = get_optimizers(opt=self.configs['general']['optim'],
                               init_lr=self.configs['general']['init_lr'])
    end2end_model.compile(loss=losses,
                          loss_weights=losses_weights,
                          optimizer=optimizer,
                          metrics=[ 'categorical_accuracy'])
    print("Model summary:")
    print(end2end_model.summary())
    print("END2END MODEL CREATED!")

    self.model = end2end_model
    def _create_model(self):

        input_dim = 200
        output_dim = 200

        # --------------------Seq2Seq network params--------------------------------

        input_length = 100
        output_length = 100

        inputs = Input(shape=(input_length, ))
        embeddings = Embedding(20000, input_dim)(inputs)
        # --------------- MODEL TRANSLATION DEFINITION -----------------------------
        print("Creating TRANSLATION SEQ2SEQ model ...")
        encoded_seq, decoded_seq, cycled_decoded_seq = \
          mctn_model(input=embeddings,output_dim=output_dim,
                     hidden_dim=self.configs['translation']['hidden_dim'],
                     output_length=output_length,
                     input_dim=input_dim,
                     input_length=input_length,
                     depth=self.configs['translation']['depth'],
                     bidirectional=self.configs['translation']['is_bidirectional'],
                     is_cycled=self.is_cycled,
                     dropout=0.5
                     )

        # ---------------- MODEL REGRESSION DEFINITION -----------------------------
        print("Creating REGRESSION model ...")
        regression_score = \
          create_regression_model(
            n_hidden=self.configs['regression']['reg_hidden_dim'],
            input=encoded_seq,
            l2_factor=self.configs['regression']['l2_factor'],
          input_hidden=self.configs['translation']['hidden_dim'],
          dropout=0.5)

        # ------------------ E2E REGRESSION DEFINITION -----------------------------
        print("BUILDING A JOINT END-TO-END MODEL")
        if self.is_cycled:
            outputs = [decoded_seq, cycled_decoded_seq, regression_score]
            losses = [
                self.configs['translation']['loss_type'],
                self.configs['translation']['cycle_loss_type'],
                self.configs['regression']['loss_type']
            ]
            losses_weights = [
                self.configs['translation']['loss_weight'],
                self.configs['translation']['cycle_loss_weight'],
                self.configs['regression']['loss_weight']
            ]
        else:
            outputs = [decoded_seq, regression_score]
            losses = [
                self.configs['translation']['loss_type'],
                self.configs['regression']['loss_type']
            ]
            losses_weights = [
                self.configs['translation']['loss_weight'],
                self.configs['regression']['loss_weight']
            ]

        end2end_model = Model(inputs=inputs, outputs=outputs)
        print("Compiling model")
        optimizer = get_optimizers(opt=self.configs['general']['optim'],
                                   init_lr=self.configs['general']['init_lr'])
        end2end_model.compile(loss=losses,
                              loss_weights=losses_weights,
                              optimizer=optimizer,
                              metrics=['categorical_accuracy'])
        # print("Model summary:")
        print(end2end_model.summary())
        # print("END2END MODEL CREATED!")

        self.model = end2end_model
        self.embeding_model = Model(inputs=inputs, outputs=[embeddings])