我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用keras.layers.pooling.GlobalAveragePooling1D()。
def fGRU_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_SENTS+1, MAX_WORDS), name="wordInputs", dtype='float32') wordInp = Flatten()(wordInputs) wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInp) hij = Bidirectional(GRU(WORDGRU, return_sequences=True), name='gru1')(wordEmbedding) head = GlobalAveragePooling1D()(hij) v6 = Dense(1, activation="sigmoid", name="dense")(head) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def fGlove_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_SENTS+1, MAX_WORDS), name="wordInputs", dtype='float32') wordInp = Flatten()(wordInputs) wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInp) head = GlobalAveragePooling1D()(wordEmbedding) v6 = Dense(1, activation="sigmoid", name="dense")(head) model = Model(inputs=[wordInputs] , outputs=[v6]) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) return model
def BiGRU(X_train, y_train, X_test, y_test, gru_units, dense_units, input_shape, \ batch_size, epochs, drop_out, patience): model = Sequential() reg = L1L2(l1=0.2, l2=0.2) model.add(Bidirectional(GRU(units = gru_units, dropout= drop_out, activation='relu', recurrent_regularizer = reg, return_sequences = True), input_shape = input_shape, merge_mode="concat")) model.add(BatchNormalization()) model.add(TimeDistributed(Dense(dense_units, activation='relu'))) model.add(BatchNormalization()) model.add(Bidirectional(GRU(units = gru_units, dropout= drop_out, activation='relu', recurrent_regularizer=reg, return_sequences = True), merge_mode="concat")) model.add(BatchNormalization()) model.add(Dense(units=1)) model.add(GlobalAveragePooling1D()) print(model.summary()) early_stopping = EarlyStopping(monitor="val_loss", patience = patience) model.compile(loss='mse', optimizer= 'adam') history_callback = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs,\ verbose=2, callbacks=[early_stopping], validation_data=[X_test, y_test], shuffle = True) return model, history_callback
def fhan2_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_WORDS,), name="wordInputs", dtype='float32') wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInputs) hij = Bidirectional(GRU(WORDGRU, return_sequences=True), name='gru1')(wordEmbedding) Si = GlobalAveragePooling1D()(hij) wordEncoder = Model(wordInputs, Si) # ----------------------------------------------------------------------------------------------- docInputs = Input(shape=(None, MAX_WORDS), name='docInputs' ,dtype='float32') #sentenceMasking = Masking(mask_value=0.0, name='sentenceMasking')(docInputs) sentEncoding = TimeDistributed(wordEncoder, name='sentEncoding')(docInputs) hi = Bidirectional(GRU(WORDGRU, return_sequences=True), merge_mode='concat', name='gru2')(sentEncoding) Vb = GlobalAveragePooling1D()(hi) v6 = Dense(1, activation="sigmoid", kernel_initializer = 'glorot_uniform', name="dense")(Vb) model = Model(inputs=[docInputs] , outputs=[v6]) sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) return model, wordEncoder
def test_globalpooling_1d(): layer_test(pooling.GlobalMaxPooling1D, input_shape=(3, 4, 5)) layer_test(pooling.GlobalAveragePooling1D, input_shape=(3, 4, 5))
def build_hcnn_model(opts, vocab_size=0, maxnum=50, maxlen=50, embedd_dim=50, embedding_weights=None, verbose=False): N = maxnum L = maxlen logger.info("Model parameters: max_sentnum = %d, max_sentlen = %d, embedding dim = %s, nbfilters = %s, filter1_len = %s, filter2_len = %s, drop rate = %s, l2 = %s" % (N, L, embedd_dim, opts.nbfilters, opts.filter1_len, opts.filter2_len, opts.dropout, opts.l2_value)) word_input = Input(shape=(N*L,), dtype='int32', name='word_input') x = Embedding(output_dim=embedd_dim, input_dim=vocab_size, input_length=N*L, weights=embedding_weights, name='x')(word_input) drop_x = Dropout(opts.dropout, name='drop_x')(x) resh_W = Reshape((N, L, embedd_dim), name='resh_W')(drop_x) z = TimeDistributed(Convolution1D(opts.nbfilters, opts.filter1_len, border_mode='valid'), name='z')(resh_W) avg_z = TimeDistributed(AveragePooling1D(pool_length=L-opts.filter1_len+1), name='avg_z')(z) # shape= (N, 1, nbfilters) resh_z = Reshape((N, opts.nbfilters), name='resh_z')(avg_z) # shape(N, nbfilters) hz = Convolution1D(opts.nbfilters, opts.filter2_len, border_mode='valid', name='hz')(resh_z) # avg_h = MeanOverTime(mask_zero=True, name='avg_h')(hz) avg_hz = GlobalAveragePooling1D(name='avg_hz')(hz) y = Dense(output_dim=1, activation='sigmoid', name='output')(avg_hz) model = Model(input=word_input, output=y) if verbose: model.summary() start_time = time.time() model.compile(loss='mse', optimizer='rmsprop') total_time = time.time() - start_time logger.info("Model compiled in %.4f s" % total_time) return model
def build_model(opts, vocab_size=0, maxnum=50, maxlen=50, embedd_dim=50, embedding_weights=None, verbose=False, init_mean_value=None): N = maxnum L = maxlen logger = get_logger("Build model") logger.info("Model parameters: max_sentnum = %d, max_sentlen = %d, embedding dim = %s, lstm_units = %s, drop rate = %s, l2 = %s" % (N, L, embedd_dim, opts.lstm_units, opts.dropout, opts.l2_value)) word_input = Input(shape=(N*L,), dtype='int32', name='word_input') x = Embedding(output_dim=embedd_dim, input_dim=vocab_size, input_length=N*L, weights=embedding_weights, name='x')(word_input) drop_x = Dropout(opts.dropout, name='drop_x')(x) resh_W = Reshape((N, L, embedd_dim), name='resh_W')(drop_x) z = TimeDistributed(LSTM(opts.lstm_units, return_sequences=True), name='z')(resh_W) avg_z = TimeDistributed(GlobalAveragePooling1D(), name='avg_z')(z) hz = LSTM(opts.lstm_units, return_sequences=True, name='hz')(avg_z) # TODO, random drop sentences drop_hz = Dropout(opts.dropout, name='drop_hz')(hz) avg_hz = GlobalAveragePooling1D(name='avg_hz')(drop_hz) y = Dense(output_dim=1, activation='sigmoid', name='output')(avg_hz) model = Model(input=word_input, output=y) if opts.init_bias and init_mean_value: logger.info("Initialise output layer bias with log(y_mean/1-y_mean)") bias_value = (np.log(init_mean_value) - np.log(1 - init_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) if verbose: model.summary() start_time = time.time() model.compile(loss='mse', optimizer='rmsprop') total_time = time.time() - start_time logger.info("Model compiled in %.4f s" % total_time) return model
def build_bidirectional_model(opts, vocab_size=0, maxnum=50, maxlen=50, embedd_dim=50, embedding_weights=None, verbose=False, init_mean_value=None): N = maxnum L = maxlen logger = get_logger("Build bidirectional model") logger.info("Model parameters: max_sentnum = %d, max_sentlen = %d, embedding dim = %s, lstm_units = %s, drop rate = %s, l2 = %s" % (N, L, embedd_dim, opts.lstm_units, opts.dropout, opts.l2_value)) word_input = Input(shape=(N*L,), dtype='int32', name='word_input') x = Embedding(output_dim=embedd_dim, input_dim=vocab_size, input_length=N*L, weights=embedding_weights, name='x')(word_input) drop_x = Dropout(opts.dropout, name='drop_x')(x) resh_W = Reshape((N, L, embedd_dim), name='resh_W')(drop_x) z_fwd = TimeDistributed(LSTM(opts.lstm_units, return_sequences=True), name='z_fwd')(resh_W) z_bwd = TimeDistributed(LSTM(opts.lstm_units, return_sequences=True, go_backwards=True), name='z_bwd')(resh_W) z_merged = merge([z_fwd, z_bwd], mode='concat', name='z_merged') avg_z = TimeDistributed(GlobalAveragePooling1D(), name='avg_z')(z_merged) hz_fwd = LSTM(opts.lstm_units, return_sequences=True, name='hz_fwd')(avg_z) hz_bwd = LSTM(opts.lstm_units, return_sequences=True, go_backwards=True, name='hz_bwd')(avg_z) hz_merged = merge([hz_fwd, hz_bwd], mode='concat', name='hz_merged') # avg_h = MeanOverTime(mask_zero=True, name='avg_h')(hz) avg_hz = GlobalAveragePooling1D(name='avg_hz')(hz_merged) y = Dense(output_dim=1, activation='sigmoid', name='output')(avg_hz) model = Model(input=word_input, output=y) if opts.init_bias and init_mean_value: logger.info("Initialise output layer bias with log(y_mean/1-y_mean)") bias_value = (np.log(init_mean_value) - np.log(1 - init_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) if verbose: model.summary() start_time = time.time() model.compile(loss='mse', optimizer='rmsprop') total_time = time.time() - start_time logger.info("Model compiled in %.4f s" % total_time) return model
def build_attention_model(opts, vocab_size=0, maxnum=50, maxlen=50, embedd_dim=50, embedding_weights=None, verbose=False, init_mean_value=None): N = maxnum L = maxlen logger = get_logger('Build attention pooling model') logger.info("Model parameters: max_sentnum = %d, max_sentlen = %d, embedding dim = %s, lstm_units = %s, drop rate = %s, l2 = %s" % (N, L, embedd_dim, opts.lstm_units, opts.dropout, opts.l2_value)) word_input = Input(shape=(N*L,), dtype='int32', name='word_input') x = Embedding(output_dim=embedd_dim, input_dim=vocab_size, input_length=N*L, weights=embedding_weights, name='x')(word_input) drop_x = Dropout(opts.dropout, name='drop_x')(x) resh_W = Reshape((N, L, embedd_dim), name='resh_W')(drop_x) z = TimeDistributed(LSTM(opts.lstm_units, return_sequences=True), name='z')(resh_W) avg_z = TimeDistributed(GlobalAveragePooling1D(), name='avg_z')(z) hz = LSTM(opts.lstm_units, return_sequences=True, name='hz')(avg_z) # avg_h = MeanOverTime(mask_zero=True, name='avg_h')(hz) # avg_hz = GlobalAveragePooling1D(name='avg_hz')(hz) attent_hz = Attention(name='attent_hz')(hz) y = Dense(output_dim=1, activation='sigmoid', name='output')(attent_hz) model = Model(input=word_input, output=y) if opts.init_bias and init_mean_value: logger.info("Initialise output layer bias with log(y_mean/1-y_mean)") bias_value = (np.log(init_mean_value) - np.log(1 - init_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) if verbose: model.summary() start_time = time.time() model.compile(loss='mse', optimizer='rmsprop') total_time = time.time() - start_time logger.info("Model compiled in %.4f s" % total_time) return model
def build_attention2_model(opts, vocab_size=0, maxnum=50, maxlen=50, embedd_dim=50, embedding_weights=None, verbose=False, init_mean_value=None): N = maxnum L = maxlen logger = get_logger('Build attention pooling model') logger.info("Model parameters: max_sentnum = %d, max_sentlen = %d, embedding dim = %s, lstm_units = %s, drop rate = %s, l2 = %s" % (N, L, embedd_dim, opts.lstm_units, opts.dropout, opts.l2_value)) word_input = Input(shape=(N*L,), dtype='int32', name='word_input') x = Embedding(output_dim=embedd_dim, input_dim=vocab_size, input_length=N*L, weights=embedding_weights, name='x')(word_input) drop_x = Dropout(opts.dropout, name='drop_x')(x) resh_W = Reshape((N, L, embedd_dim), name='resh_W')(drop_x) z = TimeDistributed(LSTM(opts.lstm_units, return_sequences=True), name='z')(resh_W) att_z = TimeDistributed(Attention(name='att_z'))(z) hz = LSTM(opts.lstm_units, return_sequences=True, name='hz')(att_z) # avg_h = MeanOverTime(mask_zero=True, name='avg_h')(hz) # avg_hz = GlobalAveragePooling1D(name='avg_hz')(hz) attent_hz = Attention(name='attent_hz')(hz) y = Dense(output_dim=1, activation='sigmoid', name='output')(attent_hz) model = Model(input=word_input, output=y) if opts.init_bias and init_mean_value: logger.info("Initialise output layer bias with log(y_mean/1-y_mean)") bias_value = (np.log(init_mean_value) - np.log(1 - init_mean_value)).astype(K.floatx()) model.layers[-1].b.set_value(bias_value) if verbose: model.summary() start_time = time.time() model.compile(loss='mse', optimizer='rmsprop') total_time = time.time() - start_time logger.info("Model compiled in %.4f s" % total_time) return model
def setup_model(embeddings, seq_len, vocab_size): # Add input inputs = Input(shape=(seq_len, ), dtype='int32', name='inputs') # Add word vector embeddings embedding = Embedding(input_dim=vocab_size, output_dim=embedding_size, input_length=seq_len, name='embedding', trainable=True)(inputs) h = GlobalAveragePooling1D()(embedding) # Add output layer output = Dense(units=output_size, activation='sigmoid', kernel_initializer='he_normal', # kernel_regularizer=regularizers.l2(l2_reg_lambda), # kernel_constraint=maxnorm(max_norm), # bias_constraint=maxnorm(max_norm), name='output')(h) # build the model model = Model(inputs=inputs, outputs=output) model.compile(loss={'output':'binary_crossentropy'}, optimizer=Adam(lr=base_lr, epsilon=1e-6, decay=decay_rate), metrics=["accuracy"]) return model
def __call__(self, inputs): x = self.model(inputs) avg_x = GlobalAveragePooling1D()(x) max_x = GlobalMaxPooling1D()(x) x = concatenate([avg_x, max_x]) x = BatchNormalization()(x) return x
def fhan3_avg(MAX_NB_WORDS, MAX_WORDS, MAX_SENTS, EMBEDDING_DIM, WORDGRU, embedding_matrix, DROPOUTPER): wordInputs = Input(shape=(MAX_WORDS,), name="wordInputs", dtype='float32') wordEmbedding = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, weights=[embedding_matrix], mask_zero=False, trainable=True, name='wordEmbedding')(wordInputs) hij = Bidirectional(GRU(WORDGRU, return_sequences=True), name='gru1')(wordEmbedding) #alpha_its, Si = AttentionLayer(name='att1')(hij) wordDrop = Dropout(DROPOUTPER, name='wordDrop')(hij) word_pool = GlobalAveragePooling1D()(wordDrop) wordEncoder = Model(wordInputs, word_pool) # ----------------------------------------------------------------------------------------------- docInputs = Input(shape=(None, MAX_WORDS), name='docInputs' ,dtype='float32') #sentenceMasking = Masking(mask_value=0.0, name='sentenceMasking')(docInputs) sentEncoding = TimeDistributed(wordEncoder, name='sentEncoding')(docInputs) hi = Bidirectional(GRU(WORDGRU, return_sequences=True), merge_mode='concat', name='gru2')(sentEncoding) #alpha_s, Vb = AttentionLayer(name='att2')(hi) sentDrop = Dropout(DROPOUTPER, name='sentDrop')(hi) sent_pool = GlobalAveragePooling1D()(sentDrop) Vb = Reshape((1, sent_pool._keras_shape[1]))(sent_pool) #----------------------------------------------------------------------------------------------- headlineInput = Input(shape=(MAX_WORDS,), name='headlineInput',dtype='float32') headlineEmb = Embedding(MAX_NB_WORDS, EMBEDDING_DIM, mask_zero=False, name='headlineEmb')(headlineInput) #Vb = Masking(mask_value=0.0, name='Vb')(Vb) headlineBodyEmb = concatenate([headlineEmb, Vb], axis=1, name='headlineBodyEmb') h3 = Bidirectional(GRU(WORDGRU, return_sequences=True), merge_mode='concat', name='gru3')(headlineBodyEmb) #a3, Vn = AttentionLayer(name='att3')(h3) headDrop = Dropout(DROPOUTPER, name='3Drop')(h3) head_pool = GlobalAveragePooling1D()(headDrop) v6 = Dense(1, activation="sigmoid", kernel_initializer = 'he_normal', name="dense")(head_pool) model = Model(inputs=[docInputs, headlineInput] , outputs=[v6]) sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) return model, wordEncoder
def cnn_melspect_1D(input_shape): kernel_size = 3 #activation_func = LeakyReLU() activation_func = Activation('relu') inputs = Input(input_shape) # Convolutional block_1 conv1 = Conv1D(32, kernel_size)(inputs) act1 = activation_func(conv1) bn1 = BatchNormalization()(act1) pool1 = MaxPooling1D(pool_size=2, strides=2)(bn1) # Convolutional block_2 conv2 = Conv1D(64, kernel_size)(pool1) act2 = activation_func(conv2) bn2 = BatchNormalization()(act2) pool2 = MaxPooling1D(pool_size=2, strides=2)(bn2) # Convolutional block_3 conv3 = Conv1D(128, kernel_size)(pool2) act3 = activation_func(conv3) bn3 = BatchNormalization()(act3) # Global Layers gmaxpl = GlobalMaxPooling1D()(bn3) gmeanpl = GlobalAveragePooling1D()(bn3) mergedlayer = concatenate([gmaxpl, gmeanpl], axis=1) # Regular MLP dense1 = Dense(512, kernel_initializer='glorot_normal', bias_initializer='glorot_normal')(mergedlayer) actmlp = activation_func(dense1) reg = Dropout(0.5)(actmlp) dense2 = Dense(512, kernel_initializer='glorot_normal', bias_initializer='glorot_normal')(reg) actmlp = activation_func(dense2) reg = Dropout(0.5)(actmlp) dense2 = Dense(10, activation='softmax')(reg) model = Model(inputs=[inputs], outputs=[dense2]) return model