(self)
Define the model in keras
Define the model in keras
Define the model in keras
def _define_model(self):
        """Define the model in keras"""

        # network outputs
        outputs = {}

        # inputs
        if self.parametric_embedding:
            to_x = tf.keras.layers.Input(shape=self.dims, name="to_x")
            from_x = tf.keras.layers.Input(shape=self.dims, name="from_x")
            inputs = [to_x, from_x]

            # parametric embedding
            embedding_to = self.encoder(to_x)
            embedding_from = self.encoder(from_x)

            if self.parametric_reconstruction:
                # parametric reconstruction
                if self.autoencoder_loss:
                    embedding_to_recon = self.decoder(embedding_to)
                else:
                    # stop gradient of reconstruction loss before it reaches the encoder
                    embedding_to_recon = self.decoder(tf.stop_gradient(embedding_to))

                embedding_to_recon = tf.keras.layers.Lambda(
                    lambda x: x, name="reconstruction"
                )(embedding_to_recon)

                outputs["reconstruction"] = embedding_to_recon

        else:
            # this is the sham input (its just a 0) to make keras think there is input data
            batch_sample = tf.keras.layers.Input(
                shape=(1), dtype=tf.int32, name="batch_sample"
            )

            # gather all of the edges (so keras model is happy)
            to_x = tf.squeeze(tf.gather(self.head, batch_sample[0]))
            from_x = tf.squeeze(tf.gather(self.tail, batch_sample[0]))

            # grab relevant embeddings
            embedding_to = self.encoder(to_x)[:, -1, :]
            embedding_from = self.encoder(from_x)[:, -1, :]

            inputs = [batch_sample]

        # concatenate to/from projections for loss computation
        embedding_to_from = tf.concat([embedding_to, embedding_from], axis=1)
        embedding_to_from = tf.keras.layers.Lambda(lambda x: x, name="umap")(
            embedding_to_from
        )
        outputs["umap"] = embedding_to_from

        if self.global_correlation_loss_weight > 0:
            outputs["global_correlation"] = tf.keras.layers.Lambda(
                lambda x: x, name="global_correlation"
            )(embedding_to)

        # create model

        # self.parametric_model = tf.keras.Model(inputs=inputs, outputs=outputs)
        self.parametric_model = GradientClippedModel(inputs=inputs, outputs=outputs)
def _define_model ( self ) : # network outputs outputs = { } # inputs if self . parametric_embedding : to_x = tf . keras . layers . Input ( shape = self . dims , name = "to_x" ) from_x = tf . keras . layers . Input ( shape = self . dims , name = "from_x" ) inputs = [ to_x , from_x ] # parametric embedding embedding_to = self . encoder ( to_x ) embedding_from = self . encoder ( from_x ) if self . parametric_reconstruction : # parametric reconstruction if self . autoencoder_loss : embedding_to_recon = self . decoder ( embedding_to ) else : # stop gradient of reconstruction loss before it reaches the encoder embedding_to_recon = self . decoder ( tf . stop_gradient ( embedding_to ) ) embedding_to_recon = tf . keras . layers . Lambda ( lambda x : x , name = "reconstruction" ) ( embedding_to_recon ) outputs [ "reconstruction" ] = embedding_to_recon else : # this is the sham input (its just a 0) to make keras think there is input data batch_sample = tf . keras . layers . Input ( shape = ( 1 ) , dtype = tf . int32 , name = "batch_sample" ) # gather all of the edges (so keras model is happy) to_x = tf . squeeze ( tf . gather ( self . head , batch_sample [ 0 ] ) ) from_x = tf . squeeze ( tf . gather ( self . tail , batch_sample [ 0 ] ) ) # grab relevant embeddings embedding_to = self . encoder ( to_x ) [ : , - 1 , : ] embedding_from = self . encoder ( from_x ) [ : , - 1 , : ] inputs = [ batch_sample ] # concatenate to/from projections for loss computation embedding_to_from = tf . concat ( [ embedding_to , embedding_from ] , axis = 1 ) embedding_to_from = tf . keras . layers . Lambda ( lambda x : x , name = "umap" ) ( embedding_to_from ) outputs [ "umap" ] = embedding_to_from if self . global_correlation_loss_weight > 0 : outputs [ "global_correlation" ] = tf . keras . layers . Lambda ( lambda x : x , name = "global_correlation" ) ( embedding_to ) # create model # self.parametric_model = tf.keras.Model(inputs=inputs, outputs=outputs) self . parametric_model = GradientClippedModel ( inputs = inputs , outputs = outputs )