Пример #1
0
 def __init__(self,
              latent_dim=100,
              nb_rows=28,
              nb_columns=28,
              nb_input_channels=1,
              one_channel_output=True,
              decay_positive_weights=0,
              decay_negative_weights=1,
              decay_weight=1):
     """
     Create a shallow AE with a Non Negativity Constraint on the weights enforced with asymetric weight decay.
     Arguments:
         decay_positive_weights: positive float - the weight decay parameter for the positive weights.
         decay_negative_weights: positive float - the weight decay parameter for the negative weights.
         decay_weight: positive float - the weight of the whole non negativity cost.
     """
     self.latent_dim = latent_dim
     self.nb_input_channels = nb_input_channels
     self.nb_rows = nb_rows
     self.nb_columns = nb_columns
     if one_channel_output:
         self.nb_output_channels = 1
     else:
         self.nb_output_channels = nb_input_channels
     self.decay_positive_weights = decay_positive_weights
     self.decay_negative_weights = decay_negative_weights
     self.decay_weight = decay_weight
     input_img = Input(
         shape=(self.nb_rows, self.nb_columns, self.nb_input_channels
                ))  # adapt this if using `channels_first` image data format
     x = Conv2D(64, (4, 4), strides=(2, 2), padding='same')(input_img)
     x = LeakyReLU(alpha=0.1)(x)
     x = Conv2D(128, (4, 4), strides=(2, 2), padding='same')(x)
     x = BatchNormalization()(x)
     x = LeakyReLU(alpha=0.1)(x)
     x = Flatten()(x)
     x = Dense(1024)(x)
     x = BatchNormalization()(x)
     x = LeakyReLU(alpha=0.1)(x)
     encoded = Dense(self.latent_dim, activation='sigmoid')(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_regularizer=custom_regularizers.asymmetric_weight_decay(
             alpha=self.decay_positive_weights,
             beta=self.decay_negative_weights,
             lam=self.decay_weight))(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'])
Пример #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, decay_positive_weights=0, decay_negative_weights=1, decay_weight=1):
     """
     Create a sparse shallow AE with the custom kl divergence regularizer, enforcing weights non negativity with an asymmetric decay.
     Arguments:
         sparsity_weight: positive float - the weight of the sparsity cost.
         sparsity_objective: float between 0 and 1 - the sparsity parameter.
         decay_positive_weights: positive float - the weight decay parameter for the positive weights.
         decay_negative_weights: positive float - the weight decay parameter for the negative weights.
         decay_weight: positive float - the weight of the whole non negativity cost.
     """
     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
     self.decay_positive_weights = decay_positive_weights        
     self.decay_negative_weights = decay_negative_weights
     self.decay_weight = decay_weight
     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),
                         kernel_regularizer=custom_regularizers.asymmetric_weight_decay(alpha=self.decay_positive_weights, 
                                                                                         beta=self.decay_negative_weights, 
                                                                                         lam=self.decay_weight))(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_regularizer=custom_regularizers.asymmetric_weight_decay(alpha=self.decay_positive_weights, 
                                                                                         beta=self.decay_negative_weights, 
                                                                                         lam=self.decay_weight))(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'])
 def __init__(self,
              latent_dim=100,
              decay_positive_weights=0,
              decay_negative_weights=1,
              decay_weight=1):
     """
     Create a shallow AE with a Non Negativity Constraint on the weights enforced with asymetric weight decay.
     Arguments:
         decay_positive_weights: positive float - the weight decay parameter for the positive weights.
         decay_negative_weights: positive float - the weight decay parameter for the negative weights.
         decay_weight: positive float - the weight of the whole non negativity cost.
     """
     self.latent_dim = latent_dim
     self.decay_positive_weights = decay_positive_weights
     self.decay_negative_weights = decay_negative_weights
     self.decay_weight = decay_weight
     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',
         kernel_regularizer=custom_regularizers.asymmetric_weight_decay(
             alpha=self.decay_positive_weights,
             beta=self.decay_negative_weights,
             lam=self.decay_weight))(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')