Python keras.layers.Conv1D() Examples
The following are 30
code examples of keras.layers.Conv1D().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
keras.layers
, or try the search function
.
Example #1
Source File: recurrent.py From keras-anomaly-detection with MIT License | 18 votes |
def create_model(time_window_size, metric): model = Sequential() model.add(Conv1D(filters=256, kernel_size=5, padding='same', activation='relu', input_shape=(time_window_size, 1))) model.add(MaxPooling1D(pool_size=4)) model.add(LSTM(64)) model.add(Dense(units=time_window_size, activation='linear')) model.compile(optimizer='adam', loss='mean_squared_error', metrics=[metric]) # model.compile(optimizer='adam', loss='mean_squared_error', metrics=[metric]) # model.compile(optimizer="sgd", loss="mse", metrics=[metric]) print(model.summary()) return model
Example #2
Source File: models.py From delft with Apache License 2.0 | 9 votes |
def cnn(maxlen, embed_size, recurrent_units, dropout_rate, recurrent_dropout_rate, dense_size, nb_classes): #inp = Input(shape=(maxlen, )) input_layer = Input(shape=(maxlen, embed_size), ) #x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp) x = Dropout(dropout_rate)(input_layer) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) x = MaxPooling1D(pool_size=2)(x) x = GRU(recurrent_units)(x) x = Dropout(dropout_rate)(x) x = Dense(dense_size, activation="relu")(x) x = Dense(nb_classes, activation="sigmoid")(x) model = Model(inputs=input_layer, outputs=x) model.summary() model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
Example #3
Source File: train_ann.py From subsync with Apache License 2.0 | 7 votes |
def ann_model(input_shape): inp = Input(shape=input_shape, name='mfcc_in') model = inp model = Conv1D(filters=12, kernel_size=(3), activation='relu')(model) model = Conv1D(filters=12, kernel_size=(3), activation='relu')(model) model = Flatten()(model) model = Dense(56)(model) model = Activation('relu')(model) model = BatchNormalization()(model) model = Dropout(0.2)(model) model = Dense(28)(model) model = Activation('relu')(model) model = BatchNormalization()(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) return model
Example #4
Source File: plot_model_struct.py From speaker_recognition with Apache License 2.0 | 6 votes |
def construct_model(classe_nums): model = Sequential() model.add( Conv1D(filters=256, kernel_size=3, strides=1, activation='relu', input_shape=(99, 40), name='block1_conv1')) model.add(MaxPool1D(pool_size=2, name='block1_pool1')) model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, axis=1)) model.add(Conv1D(filters=256, kernel_size=3, strides=1, activation='relu', name='block1_conv2')) model.add(MaxPool1D(pool_size=2, name='block1_pool2')) model.add(Flatten(name='block1_flat1')) model.add(Dropout(0.5, name='block1_drop1')) model.add(Dense(512, activation='relu', name='block2_dense2')) model.add(MaxoutDense(512, nb_feature=4, name="block2_maxout2")) model.add(Dropout(0.5, name='block2_drop2')) model.add(Dense(512, activation='relu', name='block2_dense3', kernel_regularizer=l2(1e-4))) model.add(MaxoutDense(512, nb_feature=4, name="block2_maxout3")) model.add(Dense(classe_nums, activation='softmax', name="predict")) # plot_model(model, to_file='model_struct.png', show_shapes=True, show_layer_names=False) model.summary()
Example #5
Source File: weather_model.py From Deep_Learning_Weather_Forecasting with Apache License 2.0 | 6 votes |
def CausalCNN(n_filters, lr, decay, loss, seq_len, input_features, strides_len, kernel_size, dilation_rates): inputs = Input(shape=(seq_len, input_features), name='input_layer') x=inputs for dilation_rate in dilation_rates: x = Conv1D(filters=n_filters, kernel_size=kernel_size, padding='causal', dilation_rate=dilation_rate, activation='linear')(x) x = BatchNormalization()(x) x = Activation('relu')(x) #x = Dense(7, activation='relu', name='dense_layer')(x) outputs = Dense(3, activation='sigmoid', name='output_layer')(x) causalcnn = Model(inputs, outputs=[outputs]) return causalcnn
Example #6
Source File: cnn_rnn_crf.py From Jtyoui with MIT License | 6 votes |
def create_model(): inputs = Input(shape=(length,), dtype='int32', name='inputs') embedding_1 = Embedding(len(vocab), EMBED_DIM, input_length=length, mask_zero=True)(inputs) bilstm = Bidirectional(LSTM(EMBED_DIM // 2, return_sequences=True))(embedding_1) bilstm_dropout = Dropout(DROPOUT_RATE)(bilstm) embedding_2 = Embedding(len(vocab), EMBED_DIM, input_length=length)(inputs) con = Conv1D(filters=FILTERS, kernel_size=2 * HALF_WIN_SIZE + 1, padding='same')(embedding_2) con_d = Dropout(DROPOUT_RATE)(con) dense_con = TimeDistributed(Dense(DENSE_DIM))(con_d) rnn_cnn = concatenate([bilstm_dropout, dense_con], axis=2) dense = TimeDistributed(Dense(len(chunk_tags)))(rnn_cnn) crf = CRF(len(chunk_tags), sparse_target=True) crf_output = crf(dense) model = Model(input=[inputs], output=[crf_output]) model.compile(loss=crf.loss_function, optimizer=Adam(), metrics=[crf.accuracy]) return model
Example #7
Source File: __init__.py From transformer-word-segmenter with Apache License 2.0 | 6 votes |
def __output(self, dec_output): output_dropout_layer = Dropout(self.output_dropout) output_layer = Conv1D(self.tgt_vocab_size + 1, kernel_size=1, activation=gelu, kernel_regularizer=regularizers.l2(self.l2_reg_penalty), name='output_layer') output_softmax_layer = Softmax(name="word_predictions") if self.use_crf: return output_layer(output_dropout_layer(dec_output)) else: return output_softmax_layer(output_layer(output_dropout_layer(dec_output)))
Example #8
Source File: transfer_learning.py From hyperspectral_deeplearning_review with GNU General Public License v3.0 | 6 votes |
def get_model_compiled(args, inputshape, num_class): model = Sequential() if args.arch == "CNN1D": model.add(Conv1D(20, (24), activation='relu', input_shape=inputshape)) model.add(MaxPooling1D(pool_size=5)) model.add(Flatten()) model.add(Dense(100)) elif "CNN2D" in args.arch: model.add(Conv2D(50, kernel_size=(5, 5), input_shape=inputshape)) model.add(Activation('relu')) model.add(Conv2D(100, (5, 5))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(100)) elif args.arch == "CNN3D": model.add(Conv3D(32, kernel_size=(5, 5, 24), input_shape=inputshape)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Conv3D(64, (5, 5, 16))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling3D(pool_size=(2, 2, 1))) model.add(Flatten()) model.add(Dense(300)) if args.arch != "CNN2D": model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Dense(num_class, activation='softmax')) model.compile(loss=categorical_crossentropy, optimizer=Adam(args.lr1), metrics=['accuracy']) return model
Example #9
Source File: graph.py From Keras-TextClassification with MIT License | 6 votes |
def shortcut_pool(inputs, output, filters=256, pool_type='max', shortcut=True): """ ResNet(shortcut连接|skip连接|residual连接), 这里是用shortcut连接. 恒等映射, block+f(block) 再加上 downsampling实现 参考: https://github.com/zonetrooper32/VDCNN/blob/keras_version/vdcnn.py :param inputs: tensor :param output: tensor :param filters: int :param pool_type: str, 'max'、'k-max' or 'conv' or other :param shortcut: boolean :return: tensor """ if shortcut: conv_2 = Conv1D(filters=filters, kernel_size=1, strides=2, padding='SAME')(inputs) conv_2 = BatchNormalization()(conv_2) output = downsampling(output, pool_type=pool_type) out = Add()([output, conv_2]) else: out = ReLU(inputs) out = downsampling(out, pool_type=pool_type) if pool_type is not None: # filters翻倍 out = Conv1D(filters=filters*2, kernel_size=1, strides=1, padding='SAME')(out) out = BatchNormalization()(out) return out
Example #10
Source File: graph.py From Keras-TextClassification with MIT License | 6 votes |
def downsampling(inputs, pool_type='max'): """ In addition, downsampling with stride 2 essentially doubles the effective coverage (i.e., coverage in the original document) of the convolution kernel; therefore, after going through downsampling L times, associations among words within a distance in the order of 2L can be represented. Thus, deep pyramid CNN is computationally efficient for representing long-range associations and so more global information. 参考: https://github.com/zonetrooper32/VDCNN/blob/keras_version/vdcnn.py :param inputs: tensor, :param pool_type: str, select 'max', 'k-max' or 'conv' :return: tensor, """ if pool_type == 'max': output = MaxPooling1D(pool_size=3, strides=2, padding='SAME')(inputs) elif pool_type == 'k-max': output = k_max_pooling(top_k=int(K.int_shape(inputs)[1]/2))(inputs) elif pool_type == 'conv': output = Conv1D(kernel_size=3, strides=2, padding='SAME')(inputs) else: output = MaxPooling1D(pool_size=3, strides=2, padding='SAME')(inputs) return output
Example #11
Source File: train_nets.py From subtitle-synchronization with GNU Lesser General Public License v3.0 | 6 votes |
def model_lstm(input_shape): inp = Input(shape=input_shape) model = inp if input_shape[0] > 2: model = Conv1D(filters=24, kernel_size=(3), activation='relu')(model) # if input_shape[0] > 0: model = TimeDistributed(Conv1D(filters=24, kernel_size=3, activation='relu'))(model) model = LSTM(16)(model) model = Activation('relu')(model) model = Dropout(0.2)(model) model = Dense(16)(model) model = Activation('relu')(model) model = BatchNormalization()(model) model = Dense(1)(model) model = Activation('sigmoid')(model) model = Model(inp, model) return model # %% # Conv-1D architecture. Just one sample as input
Example #12
Source File: models.py From delft with Apache License 2.0 | 6 votes |
def cnn2(maxlen, embed_size, recurrent_units, dropout_rate, recurrent_dropout_rate, dense_size, nb_classes): #inp = Input(shape=(maxlen, )) input_layer = Input(shape=(maxlen, embed_size), ) #x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp) x = Dropout(dropout_rate)(input_layer) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = GRU(recurrent_units, return_sequences=False, dropout=dropout_rate, recurrent_dropout=dropout_rate)(x) #x = Dropout(dropout_rate)(x) x = Dense(dense_size, activation="relu")(x) x = Dense(nb_classes, activation="sigmoid")(x) model = Model(inputs=input_layer, outputs=x) model.summary() model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
Example #13
Source File: models.py From delft with Apache License 2.0 | 6 votes |
def conv(maxlen, embed_size, recurrent_units, dropout_rate, recurrent_dropout_rate, dense_size, nb_classes): filter_kernels = [7, 7, 5, 5, 3, 3] #inp = Input(shape=(maxlen, )) input_layer = Input(shape=(maxlen, embed_size), ) #x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp) conv = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[0], border_mode='valid', activation='relu')(input_layer) conv = MaxPooling1D(pool_length=3)(conv) conv1 = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[1], border_mode='valid', activation='relu')(conv) conv1 = MaxPooling1D(pool_length=3)(conv1) conv2 = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[2], border_mode='valid', activation='relu')(conv1) conv3 = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[3], border_mode='valid', activation='relu')(conv2) conv4 = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[4], border_mode='valid', activation='relu')(conv3) conv5 = Conv1D(nb_filter=recurrent_units, filter_length=filter_kernels[5], border_mode='valid', activation='relu')(conv4) conv5 = MaxPooling1D(pool_length=3)(conv5) conv5 = Flatten()(conv5) z = Dropout(0.5)(Dense(dense_size, activation='relu')(conv5)) #x = GlobalMaxPool1D()(x) x = Dense(nb_classes, activation="sigmoid")(z) model = Model(inputs=input_layer, outputs=x) model.summary() model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model # LSTM + conv
Example #14
Source File: keras_bert_classify_text_cnn.py From nlp_xiaojiang with MIT License | 6 votes |
def build_model_text_cnn(self): ######### text-cnn ######### # bert embedding bert_inputs, bert_output = KerasBertEmbedding().bert_encode() # text cnn bert_output_emmbed = SpatialDropout1D(rate=self.keep_prob)(bert_output) concat_out = [] for index, filter_size in enumerate(self.filters): x = Conv1D(name='TextCNN_Conv1D_{}'.format(index), filters=int(self.embedding_dim/2), kernel_size=self.filters[index], padding='valid', kernel_initializer='normal', activation='relu')(bert_output_emmbed) x = GlobalMaxPooling1D(name='TextCNN_MaxPool1D_{}'.format(index))(x) concat_out.append(x) x = Concatenate(axis=1)(concat_out) x = Dropout(self.keep_prob)(x) # 最后就是softmax dense_layer = Dense(self.label, activation=self.activation)(x) output_layers = [dense_layer] self.model = Model(bert_inputs, output_layers)
Example #15
Source File: iscx2012_cnn_rnn_5class.py From DeepTraffic with Mozilla Public License 2.0 | 6 votes |
def byte_block(in_layer, nb_filter=(64, 100), filter_length=(3, 3), subsample=(2, 1), pool_length=(2, 2)): block = in_layer for i in range(len(nb_filter)): block = Conv1D(filters=nb_filter[i], kernel_size=filter_length[i], padding='valid', activation='tanh', strides=subsample[i])(block) # block = BatchNormalization()(block) # block = Dropout(0.1)(block) if pool_length[i]: block = MaxPooling1D(pool_size=pool_length[i])(block) # block = Lambda(max_1d, output_shape=(nb_filter[-1],))(block) block = GlobalMaxPool1D()(block) block = Dense(128, activation='relu')(block) return block
Example #16
Source File: models.py From delft with Apache License 2.0 | 6 votes |
def cnn2_best(maxlen, embed_size, recurrent_units, dropout_rate, recurrent_dropout_rate, dense_size, nb_classes): #inp = Input(shape=(maxlen, )) input_layer = Input(shape=(maxlen, embed_size), ) #x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp) x = Dropout(dropout_rate)(input_layer) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = Conv1D(filters=recurrent_units, kernel_size=2, padding='same', activation='relu')(x) #x = MaxPooling1D(pool_size=2)(x) x = GRU(recurrent_units, return_sequences=False, dropout=dropout_rate, recurrent_dropout=dropout_rate)(x) #x = Dropout(dropout_rate)(x) x = Dense(dense_size, activation="relu")(x) x = Dense(nb_classes, activation="sigmoid")(x) model = Model(inputs=input_layer, outputs=x) model.summary() model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
Example #17
Source File: architecture_features.py From temporalCNN with GNU General Public License v3.0 | 6 votes |
def conv_bn(X, **conv_params): nbunits = conv_params["nbunits"]; kernel_size = conv_params["kernel_size"]; strides = conv_params.setdefault("strides", 1) padding = conv_params.setdefault("padding", "same") kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-6)) kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal") Z = Conv1D(nbunits, kernel_size=kernel_size, strides = strides, padding=padding, kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)(X) return BatchNormalization(axis=-1)(Z) #-- CHANNEL_AXIS (-1) #-----------------------------------------------------------------------
Example #18
Source File: models.py From neurowriter with MIT License | 6 votes |
def gatedblock(dilation, dropout, kernels, kernel_size): """Keras compatible Dilated convolution layer Includes Gated activation, skip connections, batch normalization and dropout """ def f(input_): norm = BatchNormalization()(input_) # Dropout of inputs drop = Dropout(dropout)(norm) # Normal activation normal_out = Conv1D(kernels, kernel_size, dilation_rate=dilation, activation='tanh', padding='same')(drop) # Gate gate_out = Conv1D(kernels, kernel_size, dilation_rate=dilation, activation='sigmoid', padding='same')(drop) # Point-wise nonlinear · gate merged = multiply([normal_out, gate_out]) # Activation after gate skip_out = Conv1D(kernels, 1, activation='tanh')(merged) # Residual connections: allow the network input to skip the # whole block if necessary out = add([skip_out, input_]) return out, skip_out return f
Example #19
Source File: model.py From BERT with Apache License 2.0 | 5 votes |
def __init__(self, n_state: int, n_head: int, attention_dropout: float, use_attn_mask: bool, layer_id: int) -> None: assert n_state % n_head == 0 self.c_attn = Conv1D(3 * n_state, 1, name='layer_{}/c_attn'.format(layer_id)) self.attn = MultiHeadAttention(n_head, n_state, attention_dropout, use_attn_mask, name='layer_{}/self_attention'.format(layer_id)) self.c_attn_proj = Conv1D(n_state, 1, name='layer_{}/c_attn_proj'.format(layer_id))
Example #20
Source File: seriesnet.py From seriesnet with MIT License | 5 votes |
def DC_CNN_Block(nb_filter, filter_length, dilation, l2_layer_reg): def f(input_): residual = input_ layer_out = Conv1D(filters=nb_filter, kernel_size=filter_length, dilation_rate=dilation, activation='linear', padding='causal', use_bias=False, kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=42), kernel_regularizer=l2(l2_layer_reg))(input_) layer_out = Activation('selu')(layer_out) skip_out = Conv1D(1,1, activation='linear', use_bias=False, kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=42), kernel_regularizer=l2(l2_layer_reg))(layer_out) network_in = Conv1D(1,1, activation='linear', use_bias=False, kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=42), kernel_regularizer=l2(l2_layer_reg))(layer_out) network_out = Add()([residual, network_in]) return network_out, skip_out return f
Example #21
Source File: model.py From speaker_recognition with Apache License 2.0 | 5 votes |
def construct_model(classe_nums): model = Sequential() model.add( Conv1D(filters=256, kernel_size=3, strides=1, activation='relu', input_shape=(99, 40), name='block1_conv1')) model.add(MaxPool1D(pool_size=2, name='block1_pool1')) model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, axis=1)) model.add(Conv1D(filters=256, kernel_size=3, strides=1, activation='relu', name='block1_conv2')) model.add(MaxPool1D(pool_size=2, name='block1_pool2')) model.add(Flatten(name='block1_flat1')) model.add(Dropout(0.5, name='block1_drop1')) model.add(Dense(512, activation='relu', name='block2_dense2')) model.add(MaxoutDense(512, nb_feature=4, name="block2_maxout2")) model.add(Dropout(0.5, name='block2_drop2')) model.add(Dense(512, activation='relu', name='block2_dense3', kernel_regularizer=l2(1e-4))) model.add(MaxoutDense(512, nb_feature=4, name="block2_maxout3")) model.summary() model_input = Input(shape=(99, 40)) features = model(model_input) extract_feature_model = Model(inputs=model_input, outputs=features) category_predict = Dense(classe_nums, activation='softmax', name="predict")(features) sr_model = Model(inputs=model_input, outputs=category_predict) plot_model(sr_model, to_file='model.png', show_shapes=True, show_layer_names=False) return extract_feature_model, sr_model
Example #22
Source File: model.py From polyaxon-examples with Apache License 2.0 | 5 votes |
def train(experiment, max_features, maxlen, embedding_size, kernel_size, optimizer, filters, pool_size, lstm_output_size, log_learning_rate, batch_size, epochs): model = Sequential() model.add(Embedding(max_features, embedding_size, input_length=maxlen)) model.add(Dropout(0.25)) model.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1)) model.add(MaxPooling1D(pool_size=pool_size)) model.add(LSTM(lstm_output_size)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(OPTIMIZERS[optimizer](lr=10 ** log_learning_rate), loss='binary_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), callbacks=[PolyaxonKerasCallback(run=experiment)]) score, accuracy = model.evaluate(x_test, y_test, batch_size=batch_size) return score, accuracy
Example #23
Source File: models.py From SeqGAN with MIT License | 5 votes |
def DiscriminatorConv(V, E, filter_sizes, num_filters, dropout): ''' Another Discriminator model, currently unused because keras don't support masking for Conv1D and it does huge influence on training. # Arguments: V: int, Vocabrary size E: int, Embedding size filter_sizes: list of int, list of each Conv1D filter sizes num_filters: list of int, list of each Conv1D num of filters dropout: float # Returns: discriminator: keras model input: word ids, shape = (B, T) output: probability of true data or not, shape = (B, 1) ''' input = Input(shape=(None,), dtype='int32', name='Input') # (B, T) out = Embedding(V, E, name='Embedding')(input) # (B, T, E) out = VariousConv1D(out, filter_sizes, num_filters) out = Highway(out, num_layers=1) out = Dropout(dropout, name='Dropout')(out) out = Dense(1, activation='sigmoid', name='FC')(out) discriminator = Model(input, out) return discriminator
Example #24
Source File: model.py From BERT with Apache License 2.0 | 5 votes |
def __init__(self, n_state: int, d_hid: int, layer_id: int) -> None: self.c_fc = Conv1D(d_hid, 1, name='layer_{}/c_fc'.format(layer_id)) self.activation = Gelu(name='layer_{}/gelu'.format(layer_id)) self.c_ffn_proj = Conv1D(n_state, 1, name='layer_{}/c_ffn_proj'.format(layer_id))
Example #25
Source File: models.py From delft with Apache License 2.0 | 5 votes |
def dpcnn(maxlen, embed_size, recurrent_units, dropout_rate, recurrent_dropout_rate, dense_size, nb_classes): #input_layer = Input(shape=(maxlen, )) input_layer = Input(shape=(maxlen, embed_size), ) #X = Embedding(max_features, embed_size, weights=[embedding_matrix], # trainable=False)(input_layer) # first block X_shortcut1 = input_layer X = Conv1D(filters=recurrent_units, kernel_size=2, strides=3)(X_shortcut1) X = Activation('relu')(X) X = Conv1D(filters=recurrent_units, kernel_size=2, strides=3)(X) X = Activation('relu')(X) # connect shortcut to the main path X = Activation('relu')(X_shortcut1) # pre activation X = Add()([X_shortcut1,X]) X = MaxPooling1D(pool_size=3, strides=2, padding='valid')(X) # second block X_shortcut2 = X X = Conv1D(filters=recurrent_units, kernel_size=2, strides=3)(X) X = Activation('relu')(X) X = Conv1D(filters=recurrent_units, kernel_size=2, strides=3)(X) X = Activation('relu')(X) # connect shortcut to the main path X = Activation('relu')(X_shortcut2) # pre activation X = Add()([X_shortcut2,X]) X = MaxPooling1D(pool_size=2, strides=2, padding='valid')(X) # Output X = Flatten()(X) X = Dense(nb_classes, activation='sigmoid')(X) model = Model(inputs = input_layer, outputs = X, name='dpcnn') model.summary() model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model
Example #26
Source File: keras_bert_classify_text_cnn.py From nlp_xiaojiang with MIT License | 5 votes |
def build_model_r_cnn(self): ######### RCNN ######### # bert embedding bert_inputs, bert_output = KerasBertEmbedding().bert_encode() # rcnn bert_output_emmbed = SpatialDropout1D(rate=self.keep_prob)(bert_output) if args.use_lstm: if args.use_cudnn_cell: layer_cell = CuDNNLSTM else: layer_cell = LSTM else: if args.use_cudnn_cell: layer_cell = CuDNNGRU else: layer_cell = GRU x = Bidirectional(layer_cell(units=args.units, return_sequences=args.return_sequences, kernel_regularizer=regularizers.l2(args.l2 * 0.1), recurrent_regularizer=regularizers.l2(args.l2) ))(bert_output_emmbed) x = Dropout(args.keep_prob)(x) x = Conv1D(filters=int(self.embedding_dim / 2), kernel_size=2, padding='valid', kernel_initializer='normal', activation='relu')(x) x = GlobalMaxPooling1D()(x) x = Dropout(args.keep_prob)(x) # 最后就是softmax dense_layer = Dense(self.label, activation=self.activation)(x) output_layers = [dense_layer] self.model = Model(bert_inputs, output_layers)
Example #27
Source File: keras_bert_classify_text_cnn.py From nlp_xiaojiang with MIT License | 5 votes |
def build_model_avt_cnn(self): #########text-cnn######### # bert embedding bert_inputs, bert_output = KerasBertEmbedding().bert_encode() # text cnn bert_output_emmbed = SpatialDropout1D(rate=self.keep_prob)(bert_output) concat_x = [] concat_y = [] concat_z = [] for index, filter_size in enumerate(self.filters): conv = Conv1D(name='TextCNN_Conv1D_{}'.format(index), filters=int(self.embedding_dim/2), kernel_size=self.filters[index], padding='valid', kernel_initializer='normal', activation='relu')(bert_output_emmbed) x = GlobalMaxPooling1D(name='TextCNN_MaxPooling1D_{}'.format(index))(conv) y = GlobalAveragePooling1D(name='TextCNN_AveragePooling1D_{}'.format(index))(conv) z = AttentionWeightedAverage(name='TextCNN_Annention_{}'.format(index))(conv) concat_x.append(x) concat_y.append(y) concat_z.append(z) merge_x = Concatenate(axis=1)(concat_x) merge_y = Concatenate(axis=1)(concat_y) merge_z = Concatenate(axis=1)(concat_z) merge_xyz = Concatenate(axis=1)([merge_x, merge_y, merge_z]) x = Dropout(self.keep_prob)(merge_xyz) # 最后就是softmax dense_layer = Dense(self.label, activation=self.activation)(x) output_layers = [dense_layer] self.model = Model(bert_inputs, output_layers)
Example #28
Source File: punctuator.py From keras-punctuator with MIT License | 5 votes |
def createModel(wordIndex=None): sys.stderr.write('Creating model.' + "\n") model = Sequential() model.add(createEmbeddingLayer(wordIndex)) model.add(Conv1D(512, 3, activation='relu')) if wordIndex is not None: model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(LABELS_COUNT, activation='softmax')) # alternative optimizer: rmsprop, adam model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['acc', precision, recall, fbeta_score]) return model
Example #29
Source File: idcnn.py From nlp_toolkit with MIT License | 5 votes |
def forward(self): word_ids = Input(shape=(self.maxlen,), dtype='int32', name='token') input_data = [word_ids] embed = Token_Embedding(word_ids, self.nb_tokens, self.embedding_dim, self.token_embeddings, False, self.maxlen, self.embed_dropout_rate, name='token_embeddings') layerInput = Conv1D( self.nb_filters, self.conv_kernel_size, padding='same', name='conv_first')(embed) dilation_layers = [] totalWidthForLastDim = 0 for j in range(self.repeat_times): for i in range(len(self.dilation_rate)): islast = True if i == len(self.dilation_rate) - 1 else False conv = Conv1D(self.nb_filters, self.conv_kernel_size, use_bias=True, padding='same', dilation_rate=self.dilation_rate[i], name='atrous_conv_%d_%d' % (j, i))(layerInput) conv = Activation('relu')(conv) if islast: dilation_layers.append(conv) totalWidthForLastDim += self.nb_filters layerInput = conv dilation_conv = concatenate( dilation_layers, axis=-1, name='dilated_conv') if self.drop_rate > 0: enc = Dropout(self.drop_rate)(dilation_conv) outputs, self._loss, self._acc = sl_output_logits( enc, self.nb_classes, self.use_crf) self.model = Model(inputs=input_data, outputs=outputs)
Example #30
Source File: seriesnet.py From seriesnet with MIT License | 5 votes |
def DC_CNN_Model(length): input = Input(shape=(length,1)) l1a, l1b = DC_CNN_Block(32,2,1,0.001)(input) l2a, l2b = DC_CNN_Block(32,2,2,0.001)(l1a) l3a, l3b = DC_CNN_Block(32,2,4,0.001)(l2a) l4a, l4b = DC_CNN_Block(32,2,8,0.001)(l3a) l5a, l5b = DC_CNN_Block(32,2,16,0.001)(l4a) l6a, l6b = DC_CNN_Block(32,2,32,0.001)(l5a) l6b = Dropout(0.8)(l6b) #dropout used to limit influence of earlier data l7a, l7b = DC_CNN_Block(32,2,64,0.001)(l6a) l7b = Dropout(0.8)(l7b) #dropout used to limit influence of earlier data l8 = Add()([l1b, l2b, l3b, l4b, l5b, l6b, l7b]) l9 = Activation('relu')(l8) l21 = Conv1D(1,1, activation='linear', use_bias=False, kernel_initializer=TruncatedNormal(mean=0.0, stddev=0.05, seed=42), kernel_regularizer=l2(0.001))(l9) model = Model(input=input, output=l21) adam = optimizers.Adam(lr=0.00075, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss='mae', optimizer=adam, metrics=['mse']) return model