def getPredictions(x_train, x_test, train, test): embedding_dim = 200 embedding_matrix = create_embedding_matrix('data\\glove.6B.200d.txt', tokenizer.word_index, embedding_dim) model = Sequential() model.add( layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=maxlen, trainable=True)) model.add(layers.Conv1D(64, 3, activation='relu')) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[f1, 'accuracy']) #model.summary() history = model.fit(x_train, train, epochs=20, verbose=False, validation_data=(x_test, test), batch_size=10) val = model.evaluate(x_train, train, verbose=False) val = model.evaluate(x_test, test, verbose=False) predictions = model.predict(x_test) return predictions
def __init__(self, parameters): super(CNN_MODEL, self).__init__(name='CNN-Model') # Get the required parameters vocabulary_size = parameters['vocabulary_size'] #max_length = parameters['max_length'] embedding_dimensions = parameters['embedding_dimensions'] cnn_filters = parameters['cnn_filters'] dnn_units = parameters['dnn_units'] dropout_rate = parameters['dropout_rate'] #kernel_initializer = parameters['kernel_initializer'] self.embedding = layers.Embedding(vocabulary_size, embedding_dimensions) self.cnn_layer1 = layers.Conv1D(filters=cnn_filters, kernel_size=2, padding="valid", activation="relu") self.cnn_layer2 = layers.Conv1D(filters=cnn_filters, kernel_size=3, padding="valid", activation="relu") self.cnn_layer3 = layers.Conv1D(filters=cnn_filters, kernel_size=4, padding="valid", activation="relu") self.pool = layers.GlobalMaxPool1D() self.dense_1 = layers.Dense(units=dnn_units, activation="relu") self.dropout = layers.Dropout(rate=dropout_rate) self.last_dense = layers.Dense(units=1, activation="sigmoid")
def DNN_MaxPooling1D(embedding_dim, vocab_size, maxlen): '''It is a sequential four layer neural network where first layer is an Embedding layer, second layer is maxpooling layer followed by a dense layer using relu activation and finally the last dense layer using sigmoid activation function.''' model = Sequential() # Sequential is a Keras API which allows us to create models layer-by-layer piecewise. model.add(layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maxlen)) # It is the first, embedding layer of the model which turns # positive integers(indexes) into dense vectors of fixed size. input_dim = vocab_size(4933 in # this case). Output_dimension = embedding_dim(50 in this case), is the dimension of dense embedding. # input_length = maxlen(100 in this case), is the length of input sequence. model.add(layers.GlobalMaxPool1D()) # using maxpooling layer to reduce the spatial size of the representation. model.add(layers.Dense(10, activation='relu')) # Dense is used to create densely-connected # Neural Network layers. we have taken units:10, which means output array shape will be(*, 10). input_dim is the # dimension of input fed to the layer and activation:relu, basically uses rectified linear unit to convert input # signal into output signal at a A-NN node. It has been seen that it provides better convergence and also it # also rectifies vanishing gradient problem. model.add(layers.Dense(1, activation='sigmoid')) # it is the last layer whose output shape array shape # will be(*, 1). The activation function used is Sigmoid whose range is between 0 and 1. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # compile is a method of sequential class which configures the model for training. We are using binary_crossentropy # loss function to calculate loss, and adam optimizer. we want to evaluate just the accuracy metric. model.summary() # prints a summary representation of the model. return model
def createRCNN(word_index, embedding_matrix): input_layer = layers.Input((1000, )) embedding_layer = layers.Embedding(len(word_index) + 1, 300, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) rnn_layer = layers.Bidirectional(layers.GRU( 50, return_sequences=True))(embedding_layer) conv_layer = layers.Convolution1D(100, 3, activation="relu")(rnn_layer) pooling_layer = layers.GlobalMaxPool1D()(conv_layer) output_layer1 = layers.Dense(50, activation="relu")(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1) model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy') return model
def create_cnn(inp_size): # Add an Input Layer input_layer = layers.Input((inp_size, )) # embedding layer learnt from above embedding_layer = layers.Embedding(vocab_size, 200)(input_layer) # add dropout on this layer embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Add the convolutional Layer conv_layer = layers.Convolution1D(100, 3, activation="tanh")(embedding_layer) # Add the pooling Layer pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Add the output Layers output_layer1 = layers.Dense(50, activation="relu")(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1) # Compile the model model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy', metrics=['accuracy']) return model
def cnn(xtrain, ytrain, xvalid, yvalid, epochs=3): # Add an Input Layer input_layer = layers.Input((70, )) # Add the word embedding Layer embedding_layer = layers.Embedding(len(word_index) + 1, 300, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Add the convolutional Layer conv_layer = layers.Convolution1D(100, 4, activation="relu")(embedding_layer) # Add the pooling Layer pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Add the output Layers output_layer1 = layers.Dense(50, activation="relu")(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(4, activation="softmax")(output_layer1) # Compile the model model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(xtrain, ytrain, batch_size=256, epochs=epochs) predictions = model.predict(xvalid) predictions = predictions.argmax(axis=-1) accuracy = model.evaluate(xvalid, yvalid, verbose=0) f1score = metrics.f1_score(valid_y, predictions, average='weighted') return accuracy, f1score
def create_rcnn(): # Add an Input Layer input_layer = layers.Input((70, )) # Add the word embedding Layer embedding_layer = layers.Embedding(len(word_index) + 1, 300, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Add the recurrent layer layers.Bidirectional(layers.GRU(50, return_sequences=True))(embedding_layer) # Add the convolutional Layer conv_layer = layers.Convolution1D(100, 3, activation="relu")(embedding_layer) # Add the pooling Layer pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Add the output Layers output_layer1 = layers.Dense(50, activation="relu")(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(units=max(training_label_encoded) + 1, activation="softmax", name="ouput_layer")(output_layer1) # Compile the model model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=["sparse_categorical_accuracy"]) return model
def create_cnn(): # Add an Input Layer input_layer = layers.Input((70, )) # Add the word embedding Layer embedding_layer = layers.Embedding(len(word_index) + 1, 50, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Add the convolutional Layer conv_layer = layers.Convolution1D(100, 3, activation="relu")(embedding_layer) # Add the pooling Layer pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Add the output Layers output_layer1 = layers.Dense(50, activation="relu")(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1) # Compile the model model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy') return model
def create_cnn(): # Add an Input Layer input_layer = layers.Input((100, )) # Add the word embedding Layer embedding_layer = layers.Embedding(10000, 100, trainable=True)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Add the convolutional Layer conv_layer = layers.Conv1D(100, 3, padding='valid', activation="relu", strides=1)(embedding_layer) # Add the pooling Layer pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Add the output Layers output_layer1 = layers.Dense(1, activation="sigmoid")(pooling_layer) # Compile the model model = models.Model(inputs=input_layer, outputs=output_layer1) model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy', metrics=['accuracy']) model.summary() return model
def model_3(self): embedding_matrix = self.build_myself_embedding_matrix() input = layers.Input(shape=(self.max_words,)) embedding = layers.Embedding(input_dim=embedding_matrix.shape[0], output_dim=embedding_matrix.shape[1], input_length=self.max_words, weights=[embedding_matrix]) x = layers.SpatialDropout1D(0.2)(embedding(input)) x = layers.Bidirectional(layers.GRU(400, return_sequences=True))(x) x = layers.Bidirectional(layers.GRU(400, return_sequences=True))(x) avg_pool = layers.GlobalAveragePooling1D()(x) max_pool = layers.GlobalMaxPool1D()(x) concat = layers.concatenate([avg_pool, max_pool]) x = layers.Dense(1024)(concat) x = layers.BatchNormalization()(x) x = layers.Activation(activation='relu')(x) x = layers.Dropout(0.2)(x) x = layers.Dense(512)(x) x = layers.BatchNormalization()(x) x = layers.Activation(activation='relu')(x) x = layers.Dropout(0.2)(x) output = layers.Dense(self.class_num, activation='softmax')(x) model = models.Model(input=input, output=output) print(model.summary()) return model
def trainDeepModelOnEmbeddings(self, X_train, y_train, X_test, y_test): model_eval = ModelEvaluation() embedding_dim = 50 input_dim = X_train.shape[1] model = models.Sequential() model.add( layers.Embedding(input_dim=input_dim, output_dim=embedding_dim, input_length=None)) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.summary() history = model.fit(X_train, y_train, epochs=1, verbose=False, validation_data=(X_test, y_test), batch_size=10) loss, accuracy = model.evaluate(X_train, y_train, verbose=False) print("Training Accuracy: {:.4f}".format(accuracy)) loss, accuracy = model.evaluate(X_test, y_test, verbose=False) print("Testing Accuracy: {:.4f}".format(accuracy)) model_eval.plot_history(history) return True
def black_box(vocab_size, embedding_dim, embedding_matrix, maxlen): """ Return a Keras fast-forward NN model with a embedding layer and two dense layers """ model = Sequential() if embedding_matrix is not None: model.add( layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, weights=[embedding_matrix], input_length=maxlen, trainable=trainable), ) else: model.add( layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maxlen, trainable=trainable), ) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model
def build_text_cnn_model(vocabulary_size=None, n_classes=None, embedding_size=300, max_len=None, hidden_size=128, conv_kernels=None, name=None): """Generate Text-CNN model for text classification """ assert vocabulary_size is not None assert n_classes is not None if not conv_kernels: conv_kernels = [2, 3, 4] input_ = layers.Input(shape=(max_len, )) embedding = layers.Embedding(vocabulary_size, embedding_size)(input_) x = embedding multi_convs = [] for kernel_size in conv_kernels: conv = layers.Conv1D(hidden_size, kernel_size, activation='relu')(x) conv = layers.GlobalMaxPool1D()(conv) multi_convs.append(conv) x = layers.Concatenate()(multi_convs) x = layers.Dense(n_classes, activation='softmax')(x) output = x model = Model(inputs=input_, outputs=output, name=name) return model
def myModel(vocab_size, embedding_dim, maxlen, embedding_matrix, X_train, labels_train, X_test, labels_test): model2 = Sequential() model2.add(layers.InputLayer(input_shape=(100, ))) #taking advantage of the Embedding layer of keras #to initialize an embedding layer, three parameters are requires: input_dim which is the vocab size of the text data #output_dim which is the size of the output vectors for each word #input_length which is the length of the input sequences(sentences) #since i'm using GloVe, we'll use their pretrained weights and allow them to be trained to gain higher accuracy model2.add( layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maxlen, weights=[embedding_matrix], trainable=True)) model2.add(layers.GlobalMaxPool1D()) model2.add(layers.Dense(10, activation='relu')) model2.add(layers.Dense(1, activation='sigmoid')) model2.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model2.save('model.h5') history = model2.fit(X_train, labels_train, epochs=100, verbose=False, validation_data=(X_test, labels_test), batch_size=10) model2.summary() loss, accuracy = model2.evaluate(X_train, labels_train, verbose=False) print("Training Accuracy: {:.4f}".format(accuracy)) loss, accuracy = model2.evaluate(X_test, labels_test, verbose=False) print("Testing Accuracy: {:.4f}".format(accuracy))
def DNN_PretrainedWordEmbeddingGlove(embedding_dim, vocab_size, maxlen, embedding_matrix): '''It is a sequential four layer neural network where first layer is an Embedding layer which gets weights from pretrained GloVe word-embeddings, second layer is maxpooling layer followed by a dense layer using relu activation and finally the last dense layer using sigmoid activation function.''' model = Sequential() # Sequential is a Keras API which allows us to create models layer-by-layer piecewise. # TODO: vary trainable between True and False to additionally train the word embeddings. model.add(layers.Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=maxlen, trainable=True))# It is the first, embedding layer of the model which uses pretrained # GloVe word embeddings. We can choose to train the word embeddings additionally by setting trainable=True. # input_length = maxlen(100 in this case), is the length of input sequence. model.add(layers.GlobalMaxPool1D()) # using maxpooling layer to reduce the spatial size of the representation. model.add(layers.Dense(10, activation='relu')) # Dense is used to create densely-connected # Neural Network layers. we have taken units:10, which means output array shape will be(*, 10). input_dim is the # dimension of input fed to the layer and activation:relu, basically uses rectified linear unit to convert input # signal into output signal at a A-NN node. It has been seen that it provides better convergence and also it # also rectifies vanishing gradient problem. model.add(layers.Dense(1, activation='sigmoid')) # it is the last layer whose output shape array shape # will be(*, 1). The activation function used is Sigmoid whose range is between 0 and 1. model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # compile is a method of sequential class which configures the model for training. We are using binary_crossentropy # loss function to calculate loss, and adam optimizer. we want to evaluate just the accuracy metric. model.summary() # prints a summary representation of the model. return model
def create_cnn(): # Adicione uma camada de entrada input_layer = layers.Input((70, )) # Adicione a camada de incorporação de palavras embedding_layer = layers.Embedding(len(word_index) + 1, 300, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) # Adicione a camada convolucional conv_layer = layers.Convolution1D(90, 3, activation="relu")(embedding_layer) # Adicione a camada de pooling máximo, pega maior valor resltande do mapa de ativação. pooling_layer = layers.GlobalMaxPool1D()(conv_layer) # Adicione as camadas de saÃda # camada totalmente conectada para normalização dos dados. output_layer1 = layers.Dropout(0.7)(pooling_layer) output_layer2 = layers.Dense(1, activation="sigmoid")(output_layer1) # Compile o modelo model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adamax(), loss='binary_crossentropy', metrics=['accuracy']) return model
def char_embedding_graph(input_dim, output_dim, cf, dropout_rate=0.0, regularizer=None, name='c_emb'): layers = KM.Sequential(name=name) layers.add( KL.Embedding(input_dim=input_dim, output_dim=output_dim, embeddings_initializer=init_embedding_weights( None, dropout_rate), trainable=True, embeddings_regularizer=regularizer, name=name + '_emb')) layers.add( KL.Lambda(lambda x: tf.reshape(x, (-1, cf.WORD_LEN, output_dim)), name=name + '_reshape')) layers.add( KL.Conv1D(cf.C_CONV_FILTERS, cf.C_CONV_KERNEL, activation='relu', kernel_regularizer=regularizer, name=name + '_conv1d')) layers.add(KL.GlobalMaxPool1D(name=name + '_pool1d')) return layers
def train_model(self): if len(self.X_train) == 0: self.load_data() print("Training model...") self.model = Sequential() self.model.add( layers.Embedding(self.config["vocab_size"], 50, input_length=self.config["max_len"])) self.model.add(layers.Conv1D(128, 5, activation='relu')) self.model.add(layers.GlobalMaxPool1D()) self.model.add(layers.Dense(10, activation='relu') ) # 4340*10= 43400 weights + 10 bias = 43410 params self.model.add(layers.Dense(3, activation='softmax')) self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) self.model.summary() self.model.fit(self.X_train, self.y_train, epochs=100, verbose=False, validation_data=(self.X_test, self.y_test), batch_size=15) self.model.save("data/models/cnn.h5") print("Done") return self.model
def build_model(tokens, categorical_vectorizer, hid_size=64): """ Build a model that maps three data sources to a single linear output: predicted log1p(salary) """ n_cat_features = len(categorical_vectorizer.vocabulary_) n_tokens = len(tokens) l_title = L.Input(shape=[None], name="Title") l_descr = L.Input(shape=[None], name="FullDescription") l_categ = L.Input(shape=[n_cat_features], name="Categorical") # Build your monster! # <YOUR CODE> # Embedding (size_vocabulary,embeding_dim,max_length) embedding_dim = 64 vocabulary_size = n_tokens num_filters = 32 max_length = 10 nb_filter = 16 # embedding layer title_embedding = L.Embedding(input_dim=n_tokens, output_dim=embedding_dim)(l_title) descr_embedding = L.Embedding(input_dim=n_tokens, output_dim=embedding_dim)(l_descr) # convLayer title_conv = L.Conv1D(nb_filter, max_length, activation='relu')(title_embedding) descr_conv = L.Conv1D(nb_filter, max_length, activation='relu')(descr_embedding) # MaxPooling Layer title_pool = L.GlobalMaxPool1D()(title_conv) descr_pool = L.GlobalMaxPool1D()(descr_conv) # output_layer_title = L.Conv1D(1,kernel_size = 3)(l_title) output_layer_categ = L.Dense(hid_size)(l_categ) concat = L.Concatenate(axis=-1)( [title_pool, descr_pool, output_layer_categ]) # output_layer2 = L.Dense(hid_size)(output_layer_categ) output_layer3 = L.Dense(1)(concat) # end of your code model = keras.models.Model(inputs=[l_title, l_descr, l_categ], outputs=[output_layer3]) model.compile('adam', 'mean_squared_error', metrics=['mean_absolute_error']) return model
def build_model(emb_cid, emb_advid): inp1 = layers.Input(shape=(max_len, )) inp2 = layers.Input(shape=(max_len, )) emb1 = layers.Embedding(input_dim=emb_cid.shape[0], output_dim=emb_cid.shape[1], input_length=max_len, weights=[emb_cid], trainable=False)(inp1) emb2 = layers.Embedding(input_dim=emb_advid.shape[0], output_dim=emb_advid.shape[1], input_length=max_len, weights=[emb_advid], trainable=False)(inp2) sdrop = layers.SpatialDropout1D(rate=0.2) emb1 = sdrop(emb1) emb2 = sdrop(emb2) content = layers.Concatenate()([emb1, emb2]) mha = MultiHeadAttention(head_num=16)(content) mha = layers.Dropout(0.01)(mha) mha = layers.Add()([content, mha]) mha = LayerNormalization()(mha) mha = layers.Dropout(0.01)(mha) mha_ff = FeedForward(256)(mha) mha_out = layers.Add()([mha, mha_ff]) mha_out = LayerNormalization()(mha_out) lstm = layers.Bidirectional(layers.LSTM(128, return_sequences=True))(mha_out) avg_pool = layers.GlobalAveragePooling1D()(lstm) max_pool = layers.GlobalMaxPool1D()(lstm) x = layers.Concatenate()([avg_pool, max_pool]) x = layers.Dense(128, activation='relu')(x) x = layers.BatchNormalization()(x) x = layers.Dense(64, activation='relu')(x) x = layers.BatchNormalization()(x) x = layers.Dropout(0.1)(x) out = layers.Dense(10, activation='softmax')(x) model = keras.Model(inputs=[inp1, inp2], outputs=out) model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(1e-3), metrics=['accuracy']) return model
def create_cnn(): input_layer = layers.Input((70,)) embedding_layer = layers.Embedding(len(word_index)+1, 300, weights=[embedding_matrix], trainable=False)(input_layer) embedding_layer = layers.SpatialDropout1D(0.3)(embedding_layer) conv_layer = layers.Convolution1D(100, 3, activation='relu')(embedding_layer) pooling_layer = layers.GlobalMaxPool1D()(conv_layer) output_layer1 = layers.Dense(50, activation='relu')(pooling_layer) output_layer1 = layers.Dropout(0.25)(output_layer1) output_layer2 = layers.Dense(1, activation='sigmoid')(output_layer1) model = models.Model(inputs=input_layer, outputs=output_layer2) model.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy') return model
def multipleOutput(vocabulary_size, num_income_groups): """ multiple output model :param vocabulary_size: :param num_income_groups: :return: """ post_input = layers.Input(shape=(None, ), dtype='int32', name='posts') embedded_post = layers.Embedding(input_dim=256, output_dim=vocabulary_size)(post_input) # Conv1D layer x = layers.Conv1D(filters=128, kernel_size=5, activation='relu')(embedded_post) x = layers.MaxPooling1D(pool_size=5)(x) x = layers.Conv1D(filters=256, kernel_size=5, activation='relu')(x) x = layers.Conv1D(filters=256, kernel_size=5, activation='relu')(x) x = layers.MaxPooling1D(pool_size=5)(x) x = layers.Conv1D(filters=256, kernel_size=5, activation='relu')(x) x = layers.Conv1D(filters=256, kernel_size=5, activation='relu')(x) x = layers.GlobalMaxPool1D()(x) # Dense layer # regression problem age_prediction = layers.Dense(units=1, name='age')(x) # category category problem income_prediction = layers.Dense(units=num_income_groups, activation='softmax', name='income')(x) # binary category gender_prediction = layers.Dense(units=1, activation='sigmoid', name='gender')(x) # model model = Model( inputs=post_input, outputs=[age_prediction, income_prediction, gender_prediction]) model.compile(optimizer=RMSprop(lr=1e-5), loss={ 'age': 'mse', 'income': 'categorical_crossentropy', 'gender': 'binary_crossentropy' }, loss_weights={ 'age': 0.25, 'income': 1., 'gender': 10. }) return model
def create_model(num_filters, kernel_size, vocab_size, embedding_dim, maxlen): model = Sequential() model.add(layers.Embedding(vocab_size, embedding_dim, input_length=maxlen)) model.add(layers.SpatialDropout1D(0.25)) model.add(layers.Conv1D(num_filters, kernel_size, padding='same', activation='relu')) model.add(layers.AveragePooling1D()) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dropout(0.25)) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model
def create_model_cnn(n_salida, n_oculto, em_input, em_dim, em_maxlen): from keras import layers model = Sequential() model.add( layers.Embedding(input_dim=em_input, output_dim=em_dim, input_length=em_maxlen)) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(n_oculto, activation='relu')) model.add(layers.Dense(units=n_salida, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) return model
def get_baseline_convolutional_encoder(filters, embedding_dimension, input_shape=None, dropout=0.05): encoder = Sequential() # Initial conv if input_shape is None: # In this case we are using the encoder as part of a siamese network and the input shape will be determined # automatically based on the input shape of the siamese network encoder.add( layers.Conv1D(filters, 32, padding='same', activation='relu')) else: # In this case we are using the encoder to build a classifier network and the input shape must be defined encoder.add( layers.Conv1D(filters, 32, padding='same', activation='relu', input_shape=input_shape)) encoder.add(layers.BatchNormalization()) encoder.add(layers.SpatialDropout1D(dropout)) encoder.add(layers.MaxPool1D(4, 4)) # Further convs encoder.add( layers.Conv1D(2 * filters, 3, padding='same', activation='relu')) encoder.add(layers.BatchNormalization()) encoder.add(layers.SpatialDropout1D(dropout)) encoder.add(layers.MaxPool1D()) encoder.add( layers.Conv1D(3 * filters, 3, padding='same', activation='relu')) encoder.add(layers.BatchNormalization()) encoder.add(layers.SpatialDropout1D(dropout)) encoder.add(layers.MaxPool1D()) encoder.add( layers.Conv1D(4 * filters, 3, padding='same', activation='relu')) encoder.add(layers.BatchNormalization()) encoder.add(layers.SpatialDropout1D(dropout)) encoder.add(layers.MaxPool1D()) encoder.add(layers.GlobalMaxPool1D()) encoder.add(layers.Dense(embedding_dimension)) return encoder
def char_level_token_encoder(self): charset_size = self.parameters.get('charset_size') char_embedding_size = self.parameters.get('char_embedding_size') token_embedding_size = self.parameters.get('hidden_units_size') n_highway_layers = self.parameters.get('n_highway_layers') filters = self.parameters.get('cnn_filters') token_maxlen = self.parameters.get('token_maxlen') # Input Layer, word characters (samples, words, character_indices) inputs = layers.Input(shape=( None, token_maxlen, ), dtype='int32') # Embed characters (samples, words, characters, character embedding) embedding = layers.Embedding(input_dim=charset_size, output_dim=char_embedding_size)(inputs) token_embeds = [] # Apply multi-filter 2D convolutions + 1D MaxPooling + tanh for (window_size, filters_size) in filters: convs = layers.Conv2D(filters=filters_size, kernel_size=(window_size, char_embedding_size), strides=(1, 1), padding='same')(embedding) convs = layers.TimeDistributed(layers.GlobalMaxPool1D())(convs) convs = layers.Activation(activation='tanh')(convs) convs = Camouflage(mask_value=0)(inputs=[convs, inputs]) token_embeds.append(convs) token_embeds = layers.concatenate(token_embeds) # Apply highways networks for i in range(n_highway_layers): token_embeds = layers.TimeDistributed(HighWay())(token_embeds) token_embeds = Camouflage(mask_value=0)( input=[token_embeds, inputs]) # Project to token embedding dimensionality token_embeds = layers.TimeDistributed( layers.Dense(units=token_embedding_size, activation='linear'))(token_embeds) token_embeds = Camouflage(mask_value=0)(input=[token_embeds, inputs]) token_encoder = models.Model(inputs=inputs, outputs=token_embeds, name='token_encoding') return token_encoder
def create_model(self, num_filters, kernel_size, vocab_size, embedding_dim, maxlen): #creates a CNN model with the specified params, returns the model oect itself model = Sequential() model.add( layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=maxlen)) model.add(layers.Conv1D(num_filters, kernel_size, activation='relu')) model.add(layers.GlobalMaxPool1D()) model.add(layers.Dense(10, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.summary() return model
def createModel2D(self, train_shape, output_count): model = Sequential() model.add( Dense(self.count_first_layer, input_shape=( train_shape[0], train_shape[1], ), activation="relu")) model.add(Dense(self.count_second_layer, activation="relu")) model.add(layers.GlobalMaxPool1D()) model.add(Dense(self.count_third_layer, activation=tf.nn.leaky_relu)) model.add(Dense(output_count, activation="softmax")) model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam') return model
def train(self, X_train, y_train, X_test, y_test, embedding_matrix, vocab_size): pool = [] inputs = [] for kernel_size, n_filters in zip(self.kernel_sizes, self.n_filters): input_shape = (self.text_length, self.embedding_dim) x = Input(shape=(self.text_length, )) embedding = layers.Embedding(vocab_size, self.embedding_dim, input_length=self.text_length)(x) C1D = layers.Conv1D(n_filters, kernel_size, activation='relu', input_shape=(self.text_length, self.embedding_dim))(embedding) MX1D = layers.GlobalMaxPool1D()(C1D) pool.append(MX1D) inputs.append(x) merged = pool[0] if len(self.kernel_sizes) > 1: merged = concatenate(pool) if len(self.hidden_layers) > 0: dense = layers.Dense(self.hidden_layers[0], activation='relu')(merged) for n_unit in self.hidden_layers[1:]: dense = layers.Dense(n_unit, activation='relu')(dense) #dense2 = layers.Dense(10, activation='relu')(dense1) outputs = layers.Dense(self.n_class, activation='softmax')(dense) else: outputs = layers.Dense(self.n_class, activation='softmax')(merged) self.model = Model(inputs=inputs, outputs=outputs) sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.1, nesterov=True) self.model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) self.model.summary() history = self.model.fit( [X_train] * len(self.kernel_sizes), y_train, epochs=self.epochs, verbose=True, validation_data=([X_test] * len(self.kernel_sizes), y_test), batch_size=self.batch_size) return self.model
def build_keras_model_mv03r00(emb_dims, vocab_size, max_len, emb_matrix): ip = kl.Input(shape=(max_len, )) x = kl.Embedding(vocab_size, emb_dims, weights=[emb_matrix], trainable=True, name='X_emb')(ip) x = kl.SpatialDropout1D(0.5)(x) x = kl.Bidirectional(kl.CuDNNGRU(200, return_sequences=True))(x) x = kl.Bidirectional(kl.CuDNNGRU(200, return_sequences=True))(x) x = kl.GlobalMaxPool1D()(x) x = kl.Dense(100, activation="relu")(x) x = kl.Dropout(0.5)(x) op = kl.Dense(6, activation="sigmoid")(x) model = km.Model(inputs=[ip], outputs=op) return model