我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用keras.regularizers.l1_l2()。
def regression(X, Y, epochs, reg_mode): x, y = np.array(X),np.array(Y) model = Sequential() if reg_mode == 'linear': model.add(Dense(1, input_dim=x.shape[1])) model.compile(optimizer='rmsprop', metrics=['accuracy'], loss='mse') elif reg_mode == 'logistic': model.add(Dense(1, activation='sigmoid', input_dim=x.shape[1])) model.compile(optimizer='rmsprop', metrics=['accuracy'], loss='binary_crossentropy') elif reg_mode == 'regularized': reg = l1_l2(l1=0.01, l2=0.01) model.add(Dense(1, activation='sigmoid', W_regularizer=reg, input_dim=x.shape[1])) model.compile(optimizer='rmsprop', metrics=['accuracy'], loss='binary_crossentropy') out = model.fit(x, y, nb_epoch=epochs, verbose=0, validation_split=.33) return model, out
def _create_layers(self, input_shape, n_output): """ Create the network layers :param input_shape: :param n_output: :return: self """ # Hidden layers for i, l in enumerate(self.layers): self._model.add(Dense(units=l, input_shape=[input_shape[-1] if i == 0 else None], activation=self.activation[i], kernel_regularizer=l1_l2(self.l1_reg[i], self.l2_reg[i]), bias_regularizer=l1_l2(self.l1_reg[i], self.l2_reg[i]))) if self.dropout[i] > 0: self._model.add(Dropout(rate=self.dropout[i])) # Output layer self._model.add(Dense(units=n_output, activation=self.out_activation))
def _create_layers(self, input_layer): """ Create the encoding and the decoding layers of the autoencoder. :return: self """ encode_layer = Dense(name='encoder', units=self.n_hidden, activation=self.enc_activation, kernel_regularizer=l1_l2(self.l1_reg, self.l2_reg), bias_regularizer=l1_l2(self.l1_reg, self.l2_reg))(input_layer) n_inputs = K.int_shape(input_layer)[-1] self._decode_layer = Dense(name='decoder', units=n_inputs, activation=self.dec_activation)(encode_layer)
def _create_layers(self, input_layer): """ Create the encoding and the decoding layers of the deep autoencoder. :param input_layer: Input size. :return: self """ encode_layer = input_layer for i, l in enumerate(self.n_hidden): encode_layer = Dense(units=l, name='encoder_%d' % i, activation=self.enc_activation[i], kernel_regularizer=l1_l2(self.l1_reg[i], self.l2_reg[i]), bias_regularizer=l1_l2(self.l1_reg[i], self.l2_reg[i]))(encode_layer) self._decode_layer = encode_layer for i, l in enumerate(self.n_hidden[-2:-(len(self.n_hidden)+1):-1] + [K.int_shape(input_layer)[1]]): self._decode_layer = Dense(units=l, name='decoder_%d' % i, activation=self.dec_activation[i])(self._decode_layer)
def _create_layers(self, input_shape, n_output): """ Create the finetuning model :param input_shape: :param n_output: :return: self """ # Hidden layers for i, l in enumerate(self.layers): self._model.add(Dense(input_shape=[input_shape[1] if i == 0 else None], units=l.n_hidden, weights=l.get_model_parameters()['enc'], activation=l.enc_activation, kernel_regularizer=l1_l2(l.l1_reg, l.l2_reg), bias_regularizer=l1_l2(l.l1_reg, l.l2_reg))) if self.dropout[i] > 0: self._model.add(Dropout(rate=self.dropout[i])) # Output layer self._model.add(Dense(units=n_output, activation=self.out_activation))
def build_output(self): mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) # Plug in dispersion parameters via fake dispersion layer disp = ConstantDispersionLayer(name='dispersion') mean = disp(mean) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) nb = NB(disp.theta_exp) self.loss = nb.loss self.extra_models['dispersion'] = lambda :K.function([], [nb.theta])([])[0].squeeze() self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): disp = Dense(self.output_size, activation=DispAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='dispersion')(self.decoder_output) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) output = SliceLayer(0, name='slice')([output, disp]) nb = NB(theta=disp, debug=self.debug) self.loss = nb.loss self.extra_models['dispersion'] = Model(inputs=self.input_layer, outputs=disp) self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): disp = Dense(self.output_size, activation=DispAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='dispersion')(self.last_hidden_disp) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.last_hidden_mean) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) output = SliceLayer(0, name='slice')([output, disp]) nb = NB(theta=disp, debug=self.debug) self.loss = nb.loss self.extra_models['dispersion'] = Model(inputs=self.input_layer, outputs=disp) self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def get_decoder(node_num, d, K, n_units, nu1, nu2, activation_fn): # Input y = Input(shape=(d,)) # Decoder layers y_hat = [None] * (K + 1) y_hat[K] = y for i in range(K - 1, 0, -1): y_hat[i] = Dense(n_units[i - 1], activation=activation_fn, W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y_hat[i + 1]) y_hat[0] = Dense(node_num, activation=activation_fn, W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y_hat[1]) # Output x_hat = y_hat[0] # decoder's output is also the actual output # Decoder Model decoder = Model(input=y, output=x_hat) return decoder
def test_arg_l1_reg_and_l2_reg(self, model): model._regularizer = l1_l2(0.01, 0.01) self._build_and_assert(model)
def build_output(self): self.loss = mean_squared_error mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) # keep unscaled output as an extra model self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) self.loss = poisson_loss self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): pi = Dense(self.output_size, activation='sigmoid', kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='pi')(self.decoder_output) disp = Dense(self.output_size, activation=DispAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='dispersion')(self.decoder_output) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) output = SliceLayer(0, name='slice')([output, disp, pi]) zinb = ZINB(pi, theta=disp, ridge_lambda=self.ridge, debug=self.debug) self.loss = zinb.loss self.extra_models['pi'] = Model(inputs=self.input_layer, outputs=pi) self.extra_models['dispersion'] = Model(inputs=self.input_layer, outputs=disp) self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): pi = Dense(1, activation='sigmoid', kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='pi')(self.decoder_output) disp = Dense(1, activation=DispAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='dispersion')(self.decoder_output) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) output = SliceLayer(0, name='slice')([output, disp, pi]) zinb = ZINB(pi, theta=disp, ridge_lambda=self.ridge, debug=self.debug) self.loss = zinb.loss self.extra_models['pi'] = Model(inputs=self.input_layer, outputs=pi) self.extra_models['dispersion'] = Model(inputs=self.input_layer, outputs=disp) self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): pi = Dense(self.output_size, activation='sigmoid', kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='pi')(self.decoder_output) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.decoder_output) # NB dispersion layer disp = ConstantDispersionLayer(name='dispersion') mean = disp(mean) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) zinb = ZINB(pi, theta=disp.theta_exp, ridge_lambda=self.ridge, debug=self.debug) self.loss = zinb.loss self.extra_models['pi'] = Model(inputs=self.input_layer, outputs=pi) self.extra_models['dispersion'] = lambda :K.function([], [zinb.theta])([])[0].squeeze() self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.extra_models['decoded'] = Model(inputs=self.input_layer, outputs=self.decoder_output) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def build_output(self): pi = Dense(self.output_size, activation='sigmoid', kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='pi')(self.last_hidden_pi) disp = Dense(self.output_size, activation=DispAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='dispersion')(self.last_hidden_disp) mean = Dense(self.output_size, activation=MeanAct, kernel_initializer=self.init, kernel_regularizer=l1_l2(self.l1_coef, self.l2_coef), name='mean')(self.last_hidden_mean) output = ColWiseMultLayer(name='output')([mean, self.sf_layer]) output = SliceLayer(0, name='slice')([output, disp, pi]) zinb = ZINB(pi, theta=disp, ridge_lambda=self.ridge, debug=self.debug) self.loss = zinb.loss self.extra_models['pi'] = Model(inputs=self.input_layer, outputs=pi) self.extra_models['dispersion'] = Model(inputs=self.input_layer, outputs=disp) self.extra_models['mean_norm'] = Model(inputs=self.input_layer, outputs=mean) self.model = Model(inputs=[self.input_layer, self.sf_layer], outputs=output) if self.ae: self.encoder = self.get_encoder()
def one_block_model(self, input_tensor): """ Method to model one cnn. It doesn't compile the model. :param input_tensor: tensor, to feed the two path :return: output: tensor, the output of the cnn """ # localPath loc_path = Conv2D(64, (7, 7), data_format='channels_first', padding='valid', activation='relu', use_bias=True, kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.), kernel_initializer='lecun_uniform', bias_initializer='zeros')(input_tensor) loc_path = MaxPooling2D(pool_size=(4, 4), data_format='channels_first', strides=1, padding='valid')(loc_path) loc_path = Dropout(self.dropout_rate)(loc_path) loc_path = Conv2D(64, (3, 3), data_format='channels_first', padding='valid', activation='relu', use_bias=True, kernel_initializer='lecun_uniform', bias_initializer='zeros', kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate),kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))(loc_path) loc_path = MaxPooling2D(pool_size=(2, 2), data_format='channels_first', strides=1, padding='valid')(loc_path) loc_path = Dropout(self.dropout_rate)(loc_path) # globalPath glob_path = Conv2D(160, (13, 13), data_format='channels_first', strides=1, padding='valid', activation='relu', use_bias=True, kernel_initializer='lecun_uniform', bias_initializer='zeros', kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))(input_tensor) glob_path = Dropout(self.dropout_rate)(glob_path) # concatenation of the two path path = Concatenate(axis=1)([loc_path, glob_path]) # output layer output = Conv2D(5, (21, 21), data_format='channels_first', strides=1, padding='valid', activation='softmax', use_bias=True, kernel_initializer='lecun_uniform', bias_initializer='zeros')(path) return output
def get_encoder(node_num, d, K, n_units, nu1, nu2, activation_fn): # Input x = Input(shape=(node_num,)) # Encoder layers y = [None] * (K + 1) y[0] = x # y[0] is assigned the input for i in range(K - 1): y[i + 1] = Dense(n_units[i], activation=activation_fn, W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y[i]) y[K] = Dense(d, activation=activation_fn, W_regularizer=Reg.l1_l2(l1=nu1, l2=nu2))(y[K - 1]) # Encoder model encoder = Model(input=x, output=y[K]) return encoder
def mlp(X, Y, para): if para['w_regularizer'] is 'auto': para['w_regularizer'] = [para['layers']] l1, l2 = check_w_reg(0, para['w_regularizer'], para['w_reg_values']) model = Sequential() model.add(Dense(para['neuron_count'][0], input_dim=para['dims'], activation=para['activation'], W_regularizer=l1_l2(l1=l1, l2=l2))) model.add(Dropout(para['dropout'])) j = 1 for i in range(para['layers'] - 1): l1, l2 = check_w_reg(j, para['w_regularizer'], para['w_reg_values']) model.add(Dense(para['neuron_count'][i+1], activation=para['activation'], W_regularizer=l1_l2(l1=l1, l2=l2))) model.add(Dropout(para['dropout'])) j += 1 l1, l2 = check_w_reg(para['layers'], para['w_regularizer'], para['w_reg_values']) model.add(Dense(para['neuron_last'], activation=para['activation_out'], W_regularizer=l1_l2(l1=l1, l2=l2))) model.compile(loss=para['loss'], optimizer=para['optimizer'], metrics=['accuracy']) if para['verbose'] >= 1: time.sleep(0.1) out = model.fit(X, Y, validation_split=para['validation_split'], epochs=para['epoch'], verbose=para['verbose'], batch_size=para['batch_size']) return model, out
def __init__(self, learning_rate=None, vocab_size=None, embedding_size=None, rnn_output_size=None, dropout_rate=None, bidirectional_rnn=None, rnn_type=None, rnn_layers=None, l1_reg=None, l2_reg=None, initializer=None, word_vector_init=None): """ If an arg is None, it will get its value from config.active_config. """ self._learning_rate = learning_rate or active_config().learning_rate self._vocab_size = vocab_size or active_config().vocab_size self._embedding_size = embedding_size or active_config().embedding_size self._rnn_output_size = (rnn_output_size or active_config().rnn_output_size) self._dropout_rate = dropout_rate or active_config().dropout_rate self._rnn_type = rnn_type or active_config().rnn_type self._rnn_layers = rnn_layers or active_config().rnn_layers self._word_vector_init = (word_vector_init or active_config().word_vector_init) self._initializer = initializer or active_config().initializer if self._initializer == 'vinyals_uniform': self._initializer = RandomUniform(-0.08, 0.08) if bidirectional_rnn is None: self._bidirectional_rnn = active_config().bidirectional_rnn else: self._bidirectional_rnn = bidirectional_rnn l1_reg = l1_reg or active_config().l1_reg l2_reg = l2_reg or active_config().l2_reg self._regularizer = l1_l2(l1_reg, l2_reg) self._keras_model = None if self._vocab_size is None: raise ValueError('config.active_config().vocab_size cannot be ' 'None! You should check your config or you can ' 'explicitly pass the vocab_size argument.') if self._rnn_type not in ('lstm', 'gru'): raise ValueError('rnn_type must be either "lstm" or "gru"!') if self._rnn_layers < 1: raise ValueError('rnn_layers must be >= 1!') if self._word_vector_init is not None and self._embedding_size != 300: raise ValueError('If word_vector_init is not None, embedding_size ' 'must be 300')
def build(self): self.input_layer = Input(shape=(self.input_size,), name='count') self.sf_layer = Input(shape=(1,), name='size_factors') last_hidden = self.input_layer for i, (hid_size, hid_drop) in enumerate(zip(self.hidden_size, self.hidden_dropout)): center_idx = int(np.floor(len(self.hidden_size) / 2.0)) if i == center_idx: layer_name = 'center' stage = 'center' # let downstream know where we are elif i < center_idx: layer_name = 'enc%s' % i stage = 'encoder' else: layer_name = 'dec%s' % (i-center_idx) stage = 'decoder' # use encoder-specific l1/l2 reg coefs if given if self.l1_enc_coef != 0. and stage in ('center', 'encoder'): l1 = self.l1_enc_coef else: l1 = self.l1_coef if self.l2_enc_coef != 0. and stage in ('center', 'encoder'): l2 = self.l2_enc_coef else: l2 = self.l2_coef last_hidden = Dense(hid_size, activation=None, kernel_initializer=self.init, kernel_regularizer=l1_l2(l1, l2), name=layer_name)(last_hidden) if self.batchnorm: last_hidden = BatchNormalization(center=True, scale=False)(last_hidden) # Use separate act. layers to give user the option to get pre-activations # of layers when requested try: last_hidden = Activation(self.activation, name='%s_act'%layer_name)(last_hidden) except ValueError: # fallback to advanced activations last_hidden = keras.layers.__dict__[self.activation](name='%s_act'%layer_name)(last_hidden) if hid_drop > 0.0: last_hidden = Dropout(hid_drop, name='%s_drop'%layer_name)(last_hidden) self.decoder_output = last_hidden self.build_output()
def one_block_model(self, input_tensor): """ Model for the twoPathways CNN. It doesn't compile the model. The consist of two streams, namely: local_path anc global_path joined in a final stream named path local_path is articulated through: 1st convolution 64x7x7 + relu 1st maxpooling 4x4 1st Dropout with rate: 0.5 2nd convolution 64x3x3 + relu 2nd maxpooling 2x2 2nd droput with rate: 0.5 global_path is articulated through: convolution 160x13x13 + relu dropout with rate: 0.5 path is articulated through: convolution 5x21x21 :param input_tensor: tensor, to feed the two path :return: output: tensor, the output of the cnn """ # localPath loc_path = Conv2D(64, (7, 7), padding='valid', activation='relu', use_bias=True, kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))(input_tensor) loc_path = MaxPooling2D(pool_size=(4, 4), strides=1, padding='valid')(loc_path) loc_path = Dropout(self.dropout_rate)(loc_path) loc_path = Conv2D(64, (3, 3), padding='valid', activation='relu', use_bias=True, kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))(loc_path) loc_path = MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid')(loc_path) loc_path = Dropout(self.dropout_rate)(loc_path) # globalPath glob_path = Conv2D(160, (13, 13), strides=1, padding='valid', activation='relu', use_bias=True, kernel_regularizer=regularizers.l1_l2(self.l1_rate, self.l2_rate), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))(input_tensor) glob_path = Dropout(self.dropout_rate)(glob_path) # concatenation of the two path path = Concatenate(axis=-1)([loc_path, glob_path]) # output layer output = Conv2D(5, (21, 21), strides=1, padding='valid', activation='softmax', use_bias=True)(path) return output