def train(self, data):
        """Pretrain the latent layers of the model."""
        # network parameters
        original_dim = data.shape[1]
        input_shape = (original_dim,)

        # build encoder model
        inputs = Input(shape=input_shape, name='encoder_input')
        hidden = inputs
        for i, hidden_dim in enumerate(self.hidden_dim, 1):
            hidden = Dense(hidden_dim, activation='sigmoid', name='hidden_e_{}'.format(i))(hidden)
            logger.debug("Hooked up hidden layer with %d neurons" % hidden_dim)
        z_mean = Dense(params_training.num_latent, activation=None, name='z_mean')(hidden)
        z_log_sigma = Dense(params_training.num_latent, activation=None, name='z_log_sigma')(hidden)
        z = Lambda(self.sampling, output_shape=(params_training.num_latent,), name='z')(
                [z_mean, z_log_sigma])
        encoder = Model(inputs, [z_mean, z_log_sigma, z], name='encoder')
        self.encoder_z_mean = encoder.predict(data)[0]

        # build decoder model
        latent_inputs = Input(shape=(params_training.num_latent,), name='z_sampling')
        hidden = latent_inputs
        for i, hidden_dim in enumerate(self.hidden_dim[::-1], 1):  # Reverse because decoder.
            hidden = Dense(hidden_dim, activation='sigmoid', name='hidden_d_{}'.format(i))(hidden)
            logger.debug("Hooked up hidden layer with %d neurons" % hidden_dim)
        # if hidden == latent_inputs:
        #     logger.warning("No Hidden layers hooked up.")
        outputs = Dense(original_dim, activation='sigmoid')(hidden)
        decoder = Model(latent_inputs, outputs, name='decoder')

        # Build the CVAE auto-encoder
        outputs = decoder(encoder(inputs)[2])
        cvae_model = Model(inputs, outputs, name='cvae')
        # Load the pre-trained weights.
        self.load_pretrain_weights(cvae_model)
        reconstruction_loss = binary_crossentropy(inputs, outputs) * original_dim
        kl_loss = 1 + z_log_sigma - tf.square(z_mean) - tf.exp(z_log_sigma)
        kl_loss = -0.5 * tf.reduce_sum(kl_loss, axis=-1)

        cvae_model.add_loss(tf.reduce_mean(reconstruction_loss + kl_loss))
        cvae_model.compile(optimizer='adam')
        cvae_model.summary()

        # First load the weights from the pre-training
        if self.pretrain_weights:
            cvae_model = self.load_pretrain_weights(cvae_model)

        saver = ModelCheckpoint(
                check_path(TEMPORARY_CVAE_PATH),
                save_weights_only=True,
                verbose=1
        )
        tensorboard_config = TensorBoard(log_dir=check_path(TEMPORARY_CVAE_PATH))
        # train the auto-encoder
        cvae_model.fit(data, epochs=params_training.num_epochs,
                       batch_size=params_training.batch_size,
                       callbacks=[saver, tensorboard_config])
        return self.encoder_z_mean
Beispiel #2
0
 def train(self, data):
     """Pretrain the latent layers of the model."""
     # network parameters
     original_dim = data.shape[1]
     input_shape = (original_dim, )
     batch_size = train_params.batch_size
     latent_dim = train_params.num_latent
     epochs = train_params.num_epochs
     # build encoder model
     inputs = Input(shape=input_shape, name='encoder_input')
     inputs_noisy = inputs
     z_mean = Dense(latent_dim, activation=None, name='z_mean')
     z_mean = z_mean(inputs_noisy)
     z_log_sigma = Dense(latent_dim, activation=None, name='z_log_sigma')
     z_log_sigma = z_log_sigma(inputs_noisy)
     z = Lambda(self.sampling, output_shape=(latent_dim, ),
                name='z')([z_mean, z_log_sigma])
     encoder = Model(inputs, [z_mean, z_log_sigma, z], name='encoder')
     # build decoder model
     latent_inputs = Input(shape=(latent_dim, ), name='z_sampling')
     outputs = Dense(original_dim, activation='sigmoid',
                     name="decoder_l")(latent_inputs)
     decoder = Model(latent_inputs, outputs, name='decoder')
     # Build the DAE
     outputs = decoder(encoder(inputs)[2])
     latent_model = Model(inputs, outputs, name='vae_mlp')
     reconstruction_loss = binary_crossentropy(inputs,
                                               outputs) * original_dim
     kl_loss = 1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma)
     kl_loss = K.sum(kl_loss, axis=-1)
     kl_loss *= -0.5
     vae_loss = K.mean(reconstruction_loss + kl_loss)
     latent_model.add_loss(vae_loss)
     latent_model.compile(optimizer='adam')
     saver = ModelCheckpoint(check_path(TEMPORARY_LATENT_PATH),
                             save_weights_only=True,
                             verbose=1)
     tensorboard_config = TensorBoard(
         log_dir=check_path(TEMPORARY_LATENT_PATH))
     logger.info("Model checkpoints has ben saved.")
     # train the autoencoder
     latent_model.fit(data,
                      epochs=epochs,
                      batch_size=batch_size,
                      callbacks=[saver, tensorboard_config])
     # Collect the weights for z_log_sigma and z_mean, the layers being pretrained.
     self.weights.append(
         latent_model.get_layer("encoder").get_layer(
             "z_mean").get_weights())
     self.weights.append(
         latent_model.get_layer("encoder").get_layer(
             "z_log_sigma").get_weights())
     self.de_weights.append(
         latent_model.get_layer("decoder").get_layer(
             "decoder_l").get_weights())
     logger.info("Weights has been updated successfully.")
    def train(self,
              data,
              input_dimension,
              epochs=train_params.num_epochs,
              batch_size=train_params.batch_size):
        """Pretrain the hidden layers."""
        logger.debug("Training layer with dimension {}".format(self.layer_dim))
        logger.debug("Shape of data is: {}".format(data.shape))
        enc_inp = keras.layers.Input(shape=(input_dimension, ))
        enc_inp_noisy = keras.layers.GaussianNoise(stddev=0.1)(enc_inp)
        # Default initializer is glorot uniform, and default bias initializer is 0,
        # If that changes in some version, explicitly set a kernel and bias initializer.
        layer_network = keras.layers.Dense(self.layer_dim,
                                           activation=train_params.activation,
                                           name='pretrain_hidden_{}'.format(
                                               self.layer_dim))
        encoder = layer_network(enc_inp_noisy)
        encoder = keras.models.Model(enc_inp, encoder)
        layer_network = layer_network(enc_inp_noisy)
        layer_network = keras.layers.Dense(input_dimension,
                                           activation=train_params.activation,
                                           name="de")(layer_network)
        layer_model = keras.models.Model(enc_inp, layer_network)
        layer_model.compile(optimizer=tf.optimizers.Adam(
            train_params.learning_rate_pretrain),
                            loss=train_params.loss_hidden)
        path = os.path.join(TEMPORARY_MODEL_PATH,
                            'hidden_{}_pretrain/'.format(self.layer_dim))
        saver = keras.callbacks.ModelCheckpoint(check_path(path),
                                                save_weights_only=True,
                                                verbose=1)
        tensorboard_config = TensorBoard(log_dir=check_path(path))

        layer_model.fit(data,
                        data,
                        batch_size=batch_size,
                        epochs=epochs,
                        callbacks=[saver, tensorboard_config])
        # append the weight of the layer being pre-trained.
        self.layer_weights = layer_model.get_layer("pretrain_hidden_{}".format(
            self.layer_dim)).get_weights()
        self.de_weights = layer_model.get_layer("de").get_weights()
        return encoder.predict(data, batch_size=batch_size)
Beispiel #4
0
 def save_model(self):
     """Save the model in matlab format to load later for scoring."""
     local_file_path = TEMPORARY_MODEL_PATH
     local_file_path = check_path(local_file_path)
     savemat(
         TEMPORARY_PMF_PATH, {
             "m_U": self.m_users.numpy(),
             "m_V": self.m_items.numpy(),
             "m_theta": self.m_weights
         })
    def train(self, data):
        """Pretrain the latent layers of the model."""
        # network parameters
        original_dim = data.shape[1]
        input_shape = (original_dim, )
        batch_size = params_training.batch_size
        latent_dim = params_training.num_latent
        epochs = params_training.num_epochs
        layer_num = 0

        # build encoder model
        inputs = Input(shape=input_shape, name='encoder_input')
        inputs_noisy = GaussianNoise(stddev=0.1)(inputs)
        hidden = inputs_noisy
        for i, hidden_dim in enumerate(self.hidden_dim, 1):
            hidden_layer = Dense(hidden_dim,
                                 activation='sigmoid',
                                 name='hidden_e_{}'.format(i),
                                 weights=self.pretrain[layer_num])
            hidden = hidden_layer(hidden)
            layer_num += 1
            logger.debug("Hooked up hidden layer with %d neurons" % hidden_dim)
        z_mean = Dense(latent_dim,
                       activation=None,
                       name='z_mean',
                       weights=self.pretrain[layer_num])(hidden)
        layer_num += 1
        z_log_sigma = Dense(latent_dim,
                            activation=None,
                            name='z_log_sigma',
                            weights=self.pretrain[layer_num])(hidden)
        layer_num += 1
        z = Lambda(self.sampling, output_shape=(latent_dim, ),
                   name='z')([z_mean, z_log_sigma])
        encoder = Model(inputs, [z_mean, z_log_sigma, z], name='encoder')

        # build decoder model
        latent_inputs = Input(shape=(latent_dim, ), name='z_sampling')
        hidden = latent_inputs
        for i, hidden_dim in enumerate(self.hidden_dim[::-1],
                                       1):  # Reverse because decoder.
            hidden = Dense(hidden_dim,
                           activation='sigmoid',
                           name='hidden_d_{}'.format(i),
                           weights=self.pretrain[layer_num])(hidden)
            layer_num += 1
            logger.debug("Hooked up hidden layer with %d neurons" % hidden_dim)
        outputs = Dense(original_dim, activation='sigmoid')(hidden)
        decoder = Model(latent_inputs, outputs, name='decoder')

        # Build the DAE
        outputs = decoder(encoder(inputs)[2])
        sdae = Model(inputs, outputs, name='vae_mlp')

        reconstruction_loss = binary_crossentropy(inputs,
                                                  outputs) * original_dim
        kl_loss = 1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma)
        kl_loss = K.sum(kl_loss, axis=-1)
        kl_loss *= -0.5
        vae_loss = K.mean(reconstruction_loss + kl_loss)

        sdae.add_loss(vae_loss)
        sdae.compile(optimizer='adam')
        saver = ModelCheckpoint(check_path(TEMPORARY_SDAE_PATH),
                                save_weights_only=True,
                                verbose=1)
        tensorboard_config = TensorBoard(
            log_dir=check_path(TEMPORARY_SDAE_PATH))
        logger.info("Checkpoint has been saved for SDAE.")
        # train the autoencoder
        logger.warning("Pretraining started, Don't interrupt.")
        sdae.fit(data,
                 epochs=epochs,
                 batch_size=batch_size,
                 callbacks=[saver, tensorboard_config])
        logger.info("Model has been pretrained successfully.")