def __init__(self,
              latent_dim=100,
              sparsity_weight=1,
              sparsity_objective=0.1):
     """
     Create a sparse shallow AE with the custom kl divergence regularizer.
     Arguments:
         sparsity_weight: positive float - the weight of the sparsity cost.
         sparsity_objective: float between 0 and 1 - the sparsity parameter
     """
     self.latent_dim = latent_dim
     self.sparsity_weight = sparsity_weight
     self.sparsity_objective = sparsity_objective
     input_img = Input(shape=(
         28, 28,
         1))  # adapt this if using `channels_first` image data format
     x = Flatten()(input_img)
     encoded = Dense(
         latent_dim,
         activation='sigmoid',
         activity_regularizer=custom_regularizers.KL_divergence_sum(
             beta=self.sparsity_weight, rho=self.sparsity_objective))(x)
     self.encoder = Model(input_img, encoded, name='encoder')
     encoded_img = Input(shape=(self.latent_dim, ))
     x = Dense(28 * 28)(encoded_img)
     x = LeakyReLU(alpha=0.1)(x)
     decoded = Reshape((28, 28, 1))(x)
     self.decoder = Model(encoded_img, decoded, name='decoder')
     encoded = self.encoder(input_img)
     decoded = self.decoder(encoded)
     self.autoencoder = Model(input_img, decoded)
     self.autoencoder.compile(optimizer='adadelta',
                              loss='mean_squared_error')
Exemplo n.º 2
0
 def __init__(self, latent_dim=100, nb_rows=28, nb_columns=28, nb_input_channels=1, one_channel_output=True, 
                 sparsity_weight=0.1, sparsity_objective=0.1):
     """
     Create a sparse shallow AE with the custom kl divergence regularizer, enforcing weights non negativity with Keras NonNeg constraint.
     Arguments:
         sparsity_weight: positive float - the weight of the sparsity cost.
         sparsity_objective: float between 0 and 1 - the sparsity parameter.
     """
     self.latent_dim = latent_dim
     self.nb_rows=nb_rows
     self.nb_columns=nb_columns
     self.nb_input_channels=nb_input_channels
     if one_channel_output:
         self.nb_output_channels=1
     else:
         self.nb_output_channels=nb_input_channels
     self.sparsity_weight = sparsity_weight
     self.sparsity_objective = sparsity_objective
     input_img = Input(shape=(self.nb_rows, self.nb_columns, nb_input_channels))  # adapt this if using `channels_first` image data format
     x = Flatten()(input_img)
     encoded = Dense(latent_dim, activation='sigmoid', 
                         activity_regularizer=custom_regularizers.KL_divergence_sum(beta=self.sparsity_weight, 
                                                                                     rho=self.sparsity_objective))(x)
     self.encoder = Model(input_img, encoded, name='encoder')
     encoded_img = Input(shape=(self.latent_dim,))  
     x = Dense(self.nb_rows*self.nb_columns*self.nb_output_channels, 
                         kernel_constraint=constraints.non_neg())(encoded_img)
     x = LeakyReLU(alpha=0.1)(x)
     decoded = Reshape((self.nb_rows,self.nb_columns,self.nb_output_channels))(x)
     self.decoder = Model(encoded_img, decoded, name='decoder')
     encoded = self.encoder(input_img)
     decoded = self.decoder(encoded)
     self.autoencoder = Model(input_img, decoded)
     self.autoencoder.compile(optimizer='adadelta', loss='mean_squared_error', metrics=['mse'])
Exemplo n.º 3
0
 def __init__(self, latent_dim=100, nb_rows=28, nb_columns=28, nb_input_channels=1, one_channel_output=True,
                 sparsity_weight=0.1, sparsity_objective=0.1, dropout_rate=None):
     """
     Create and initialize an autoencoder.
     """
     self.latent_dim = latent_dim
     self.nb_input_channels=nb_input_channels
     self.nb_rows=nb_rows
     self.nb_columns=nb_columns
     self.sparsity_weight = sparsity_weight
     self.sparsity_objective = sparsity_objective
     if one_channel_output:
         self.nb_output_channels=1
     else:
         self.nb_output_channels=nb_input_channels
     input_img = Input(shape=(self.nb_rows, self.nb_columns, nb_input_channels))  # adapt this if using `channels_first` image data format
     x = Flatten()(input_img)
     encoded = Dense(latent_dim, activation='sigmoid', 
                     activity_regularizer=custom_regularizers.KL_divergence_sum(beta=self.sparsity_weight, 
                                                                                 rho=self.sparsity_objective))(x)
     self.encoder = Model(input_img, encoded, name='encoder')
     encoded_img = Input(shape=(self.latent_dim,))
     if dropout_rate is None:
         x = MaxPlusDense(self.nb_rows*self.nb_columns*self.nb_output_channels, use_bias=False,
                             kernel_constraint=custom_constraints.Between_0_and_1())(encoded_img)
     else:
         x = Dropout(dropout_rate)(encoded_img)
         x = MaxPlusDense(self.nb_rows*self.nb_columns*self.nb_output_channels, use_bias=False,
                             kernel_constraint=custom_constraints.Between_0_and_1())(x)
     decoded = Reshape((self.nb_rows,self.nb_columns,self.nb_output_channels))(x)
     self.decoder = Model(encoded_img, decoded, name='decoder')  
     encoded = self.encoder(input_img)
     decoded = self.decoder(encoded)
     self.autoencoder = Model(input_img, decoded)
     self.autoencoder.compile(optimizer='adadelta', loss='mean_squared_error', metrics=['mse'])