def modelC(row, col): # define LSTM model = Sequential() model.add( TimeDistributed(Conv2D(16, (2, 2), activation='relu'), input_shape=(None, row, col, 1))) model.add(Dropout(0.25)) model.add(BatchNormalization()) model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2)))) model.add(Dropout(0.25)) model.add(TimeDistributed(Flatten())) model.add(LSTM(75)) # model.add(Dropout(0.25)) model.add(BatchNormalization()) model.add(RepeatVector(4)) model.add(LSTM(50, return_sequences=True)) # model.add(Dropout(0.25)) model.add(BatchNormalization()) model.add(TimeDistributed(Dense(4, activation='softmax'))) # Replicates `model` on 8 GPUs. # This assumes that your machine has 8 available GPUs. # parallel_model = multi_gpu_model(model, gpus=[2]) # parallel_model.compile(loss='categorical_crossentropy', # optimizer='adam', metrics=['accuracy']) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def create_model(): units = 512 middle_units = 256 dropout_value = 0.3 activation_function = 'softmax' loss_function = 'categorical_crossentropy' optimizer = 'rmsprop' model = Sequential() model.add( LSTM(units, input_shape=(network_input.shape[1], network_input.shape[2]), recurrent_dropout=dropout_value, return_sequences=True)) model.add( LSTM( units, return_sequences=True, recurrent_dropout=dropout_value, )) model.add(LSTM(units)) model.add(BatchNormalization()) model.add(Dropout(dropout_value)) model.add(Dense(middle_units)) model.add(Activation('relu')) model.add(Dropout(dropout_value)) model.add(BatchNormalization()) model.add(Dropout(dropout_value)) model.add(Dense(vocab_size)) model.add(Activation(activation_function)) model.compile(loss=loss_function, optimizer=optimizer) return model
def modelStandard(input_shape, parameter=None): # define LSTM model = Sequential() model.add( TimeDistributed(Conv2D(16, (2, 2), activation='relu'), input_shape=input_shape)) model.add(Dropout(parameter['dropout'])) model.add(BatchNormalization()) model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))) model.add(Dropout(parameter['dropout'])) model.add(TimeDistributed(Flatten())) model.add(LSTM(parameter['cell1'])) # model.add(Dropout(0.25)) model.add(BatchNormalization()) model.add(RepeatVector(8)) model.add(LSTM(parameter['cell2'], return_sequences=True)) # model.add(Dropout(0.25)) model.add(BatchNormalization()) model.add(TimeDistributed(Dense(5, activation='softmax'))) # Replicates `model` on 8 GPUs. # This assumes that your machine has 8 available GPUs. #parallel_model = multi_gpu_model(model, gpus=2) #parallel_model.compile(loss='categorical_crossentropy', # optimizer='adam', metrics=['accuracy']) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def modelB(row, col, parameter=None): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') ''' x = TimeDistributed(Conv2D(16, (2, 2)))(input) x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.25)(x) ''' # tower_1 = TimeDistributed(Conv2D(16, (1, 1), padding='same', activation='relu'))(input) # tower_1 = TimeDistributed(Conv2D(16, (3, 3), padding='same', activation='relu'))(tower_1) tower_2 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(input) x = BatchNormalization()(tower_2) x = Activation('relu')(x) x = Dropout(0.25)(x) tower_2 = TimeDistributed(Conv2D(16, (5, 5), padding='same'))(x) x = BatchNormalization()(tower_2) x = Activation('relu')(x) tower_2 = Dropout(0.25)(x) tower_3 = TimeDistributed( MaxPooling2D((3, 3), strides=(1, 1), padding='same'))(input) tower_3 = TimeDistributed(Conv2D(16, (1, 1), padding='same'))(tower_3) x = BatchNormalization()(tower_3) x = Activation('relu')(x) tower_3 = Dropout(0.25)(x) concatenate_output = concatenate([tower_2, tower_3], axis=-1) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(concatenate_output) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) # convLstm = ConvLSTM2D(filters=40, kernel_size=(3, 3),padding='same', return_sequences=False)(x) lstm_output = LSTM(75)(x) lstm_output = BatchNormalization()(lstm_output) # lstm_output = BatchNormalization()(convLstm) # auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_output) # auxiliary_input = Input(shape=(4,), name='aux_input') # x = concatenate([lstm_output, auxiliary_input]) x = RepeatVector(4)(lstm_output) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(4, activation='softmax'), name='main_output')(x) model = Model(inputs=[input], outputs=[output]) model.compile(loss={'main_output': 'categorical_crossentropy'}, loss_weights={'main_output': 1.}, optimizer='adam', metrics=['accuracy']) return model
def SingleOutputCNN( input_shape, output_shape, cnns_per_maxpool=1, maxpool_layers=1, dense_layers=1, dense_units=64, dropout=0.25, regularization=False, global_maxpool=False, name='', ) -> Model: function_name = cast(types.FrameType, inspect.currentframe()).f_code.co_name model_name = f"{function_name}-{name}" if name else function_name # model_name = seq([ function_name, name ]).filter(lambda x: x).make_string("-") # remove dependency on pyfunctional - not in Kaggle repo without internet inputs = Input(shape=input_shape) x = inputs for cnn1 in range(0, maxpool_layers): for cnn2 in range(1, cnns_per_maxpool + 1): x = Conv2D(32 * cnn2, kernel_size=(3, 3), padding='same', activation='relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) x = BatchNormalization()(x) x = Dropout(dropout)(x) if global_maxpool: x = GlobalMaxPooling2D()(x) x = Flatten()(x) for nn1 in range(0, dense_layers): if regularization: x = Dense(dense_units, activation='relu', kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01))(x) else: x = Dense(dense_units, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(dropout)(x) x = Dense(output_shape, activation='softmax')(x) model = Model(inputs, x, name=model_name) # plot_model(model, to_file=os.path.join(os.path.dirname(__file__), f"{name}.png")) return model
def construct_model(self, tuned_params: Dict[str, Union[int, float]], hps: HyperParameters = None) -> Model: hpf = HyperParameterFactory(self.default_parameters_values, tuned_params, hps) dense = hpf.get_int(DENSE_NAME, 32, 128, step=8) hp_learning_rate = hpf.get_choice(LEARNING_RATE_NAME, [1e-2, 1e-3, 1e-4]) conv_2d_model = self.conv_2d_model_factory.construct_model(tuned_params, hps) conv_2d_model.load_weights(self.conv2d_weight_path) conv_2d_model.trainable = False map_view_model = self.map_view_model_factory.construct_model(tuned_params, hps) map_view_model.load_weights(self.map_view_weight_path) map_view_model.trainable = False combined_model = Conv2D(32, 2, strides=1, activation=tf.nn.relu, name='Combined_Conv2D_0')(conv_2d_model.input) combined_model = Conv2D(64, 3, strides=1, activation=tf.nn.relu, name='Combined_Conv2D_1')(combined_model) combined_model = Conv2D(128, 2, strides=1, activation=tf.nn.relu, name='Combined_Conv2D_2')(combined_model) combined_model = Flatten(name='Combined_Flatten')(combined_model) combined_model = Dropout(0.1, name='Combined_Dropout')(combined_model) combined_model = Dense(dense, name='Combined_Dense0')(combined_model) combined_model = Concatenate(name='Combined_Concat')( [conv_2d_model.output, map_view_model.output, combined_model]) combined_model = Dense(dense, name='Combined_Dense1')(combined_model) output = Dense(5, activation=tf.nn.relu)(combined_model) combined_model = Model(inputs=[conv_2d_model.input, map_view_model.input], outputs=output) loss_fn = tf.keras.losses.CategoricalCrossentropy() opt = tf.keras.optimizers.Adam(learning_rate=hp_learning_rate) combined_model.compile(optimizer=opt, loss=loss_fn, metrics=[tf.keras.metrics.categorical_accuracy]) return combined_model
def MultiOutputApplication( input_shape, output_shape: Union[List, Dict], application='NASNetMobile', weights=None, # None or 'imagenet' pooling='avg', # None, 'avg', 'max', dense_units=512, # != (1295+168+11+7), dense_layers=2, dropout=0.25) -> Model: function_name = cast(types.FrameType, inspect.currentframe()).f_code.co_name model_name = f"{function_name}-{application}" if application else function_name inputs = Input(shape=input_shape) x = inputs if application == 'NASNetMobile': application_model = tf.keras.applications.nasnet.NASNetMobile( input_shape=input_shape, input_tensor=inputs, include_top=False, weights=weights, pooling=pooling, classes=1000, ) else: raise Exception( f"MultiOutputApplication() - unknown application: {application}") x = application_model(x) x = Flatten(name='output')(x) for nn1 in range(0, dense_layers): x = Dense(dense_units, activation='relu')(x) x = BatchNormalization()(x) x = Dropout(dropout)(x) if isinstance(output_shape, dict): outputs = [ Dense(output_shape, activation='softmax', name=key)(x) for key, output_shape in output_shape.items() ] else: outputs = [ Dense(output_shape, activation='softmax', name=f'output_{index}')(x) for index, output_shape in enumerate(output_shape) ] model = Model(inputs, outputs, name=model_name) # plot_model(model, to_file=os.path.join(os.path.dirname(__file__), f"{name}.png")) return model
def build_model(): model = Sequential() model.add(Conv2D(filters=16, kernel_size=2, input_shape=(64, 64, 1), activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=32, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=64, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(Conv2D(filters=128, kernel_size=2, activation='relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.2)) model.add(GlobalAveragePooling2D()) model.add(Dense(9, activation='softmax')) return model
def get_model(X, N_class, total_words=86627, EMBEDDING_DIM=100, maxlen=53): embeddings_index = {} f = open('glove.6B/glove.6B.100d.txt') for line in f: values = line.split() word = values[0] coefs = np.asarray(values[1:], dtype='float32') embeddings_index[word] = coefs f.close() embedding_matrix = np.zeros((total_words, EMBEDDING_DIM)) for word, i in X.items(): embedding_vector = embeddings_index.get(word) if embedding_vector is not None: embedding_matrix[i] = embedding_vector inp = Input(shape=(maxlen, ), dtype='int32') embedding = Embedding(total_words, EMBEDDING_DIM, embeddings_initializer=Constant(embedding_matrix), input_length=maxlen, trainable=False)(inp) x = LSTM(300, dropout=0.25, recurrent_dropout=0.25, return_sequences=True)(embedding) x = Dropout(0.25)(x) merged = Attention_COSTUM(maxlen)(x) merged = Dense(256, activation='relu')(merged) merged = Dropout(0.25)(merged) merged = BatchNormalization()(merged) outp = Dense(N_class, activation='softmax')(merged) AttentionLSTM = Model(inputs=inp, outputs=outp) AttentionLSTM.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) return AttentionLSTM
def get_model(self): model = Sequential() model.add(Conv2D(32, kernel_size=(2, 2), activation='relu', input_shape=(self.feature_dim_1, self.feature_dim_2, self.channel))) model.add(Conv2D(64, kernel_size=(2, 2), activation='relu')) model.add(Conv2D(128, kernel_size=(2, 2), activation='relu')) model.add(MaxPool2D(pool_size=(1, 1))) model.add(Dropout(0.5)) model.add(Conv2D(128, kernel_size=(2, 2), activation='relu')) model.add(Conv2D(256, kernel_size=(2, 2), activation='relu')) model.add(MaxPool2D(pool_size=(1, 1))) model.add(Dropout(0.5)) model.add(Conv2D(128, kernel_size=(2, 2), activation='relu')) model.add(Conv2D(256, kernel_size=(4, 4), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dropout(0.5)) model.add(Dense(256, kernel_regularizer=regularizers.l2(0.2), activation='relu')) model.add(Dense(32, kernel_regularizer=regularizers.l2(0.2), activation='relu')) model.add(Dense(self.num_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=['accuracy']) return model
def modelA(row, col): # define LSTM input = Input(shape=(None, row, col, 1), name='main_input') x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input) x = Dropout(0.25)(x) x = BatchNormalization()(x) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) lstm_output = LSTM(75)(x) lstm_output = BatchNormalization()(lstm_output) auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_output) auxiliary_input = Input(shape=(4, ), name='aux_input') x = concatenate([lstm_output, auxiliary_input]) x = RepeatVector(8)(x) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(5, activation='softmax'), name='main_output')(x) model = Model(inputs=[input, auxiliary_input], outputs=[output, auxiliary_output]) model.compile(loss={ 'main_output': 'categorical_crossentropy', 'aux_output': 'binary_crossentropy' }, loss_weights={ 'main_output': 1., 'aux_output': 0.2 }, optimizer='adam', metrics=['accuracy']) return model
def modelStandardB(row, col): # define LSTM input_img = Input(shape=(None, row, col, 1), name='input') x = TimeDistributed(Conv2D(16, (2, 2), activation='relu'))(input_img) x = Dropout(0.25)(x) x = BatchNormalization()(x) x = TimeDistributed(MaxPooling2D(pool_size=(2, 2), strides=2))(x) x = Dropout(0.25)(x) x = TimeDistributed(Flatten())(x) x = LSTM(75)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) x = RepeatVector(4)(x) x = LSTM(50, return_sequences=True)(x) # model.add(Dropout(0.25)) x = BatchNormalization()(x) output = TimeDistributed(Dense(4, activation='softmax'))(x) model = Model(input_img, output) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def __init__(self, sequence_length, total_words): super().__init__() self.add( Embedding(input_dim=total_words, output_dim=10, input_length=sequence_length)) self.add(CuDNNLSTM(150, return_sequences=True)) self.add(Dropout(0.2)) self.add(CuDNNLSTM(100)) self.add(Dense(total_words, activation='softmax')) self.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print(self.summary())
def construct_model(self, tuned_params: Dict[str, Union[int, float]], hps: HyperParameters = None) -> Model: hpf = HyperParameterFactory(self.default_parameters_values, tuned_params, hps) max_pool0 = hpf.get_choice(MAXPOOL0_NAME, [1, 2, 4, 8]) max_pool1 = hpf.get_choice(MAXPOOL1_NAME, [1, 2, 4, 8]) max_pool2 = hpf.get_choice(MAXPOOL2_NAME, [1, 2, 4, 8]) filter_0 = hpf.get_choice(FILTER0_NAME, [4, 8, 16, 32]) filter_1 = hpf.get_choice(FILTER1_NAME, [32, 48, 64]) filter_2 = hpf.get_choice(FILTER2_NAME, [64, 96, 128]) dense = hpf.get_int(DENSE_NAME, 32, 128, 8) lr = hpf.get_choice(LEARNING_RATE_NAME, [1e-2, 1e-3, 1e-4]) model = Sequential([ Input(name='MapView_Input', shape=(43, 39, 7)), MaxPooling2D(max_pool0, name='MapView_MaxPool_0'), Conv2D(filter_0, 2, strides=1, activation=tf.nn.relu, name='MapView_Conv2D_1'), MaxPooling2D(max_pool1, name='MapView_MaxPool_1'), Conv2D(filter_1, 3, strides=1, activation=tf.nn.relu, name='MapView_Conv2D_2'), MaxPooling2D(max_pool2, name='MapView_MaxPool_2'), Conv2D(filter_2, 2, strides=1, activation=tf.nn.relu, name='MapView_Conv2D_3'), Flatten(name='MapView_Flatten'), Dropout(0.1, name='MapView_Dropout'), Dense(dense, activation=tf.nn.relu, name='MapView_Dense'), Dense(5, activation=tf.nn.softmax, name='MapView_Output'), ]) loss_fn = tf.keras.losses.CategoricalCrossentropy() opt = tf.keras.optimizers.Adam(learning_rate=lr) model.compile(optimizer=opt, loss=loss_fn, metrics=[tf.keras.metrics.categorical_accuracy]) return model
def __init__(self, hidden_layer_sizes=(100, ), activation="relu", solver='adam', alpha=0.0001, batch_size='auto', learning_rate="constant", learning_rate_init=0.001, power_t=0.5, max_iter=200, shuffle=True, random_state=None, tol=1e-4, verbose=False, warm_start=False, momentum=0.9, nesterovs_momentum=True, early_stopping=False, validation_fraction=0.1, beta_1=0.9, beta_2=0.999, epsilon=1e-8, n_iter_no_change=10, max_fun=15000, conf=None): super().__init__(hidden_layer_sizes=hidden_layer_sizes, activation=activation, solver=solver, alpha=alpha, batch_size=batch_size, learning_rate=learning_rate, learning_rate_init=learning_rate_init, power_t=power_t, max_iter=max_iter, loss='log_loss', shuffle=shuffle, random_state=random_state, tol=tol, verbose=verbose, warm_start=warm_start, momentum=momentum, nesterovs_momentum=nesterovs_momentum, early_stopping=early_stopping, validation_fraction=validation_fraction, beta_1=beta_1, beta_2=beta_2, epsilon=epsilon, n_iter_no_change=n_iter_no_change, max_fun=max_fun) # Load model self.conf = conf self.logger = loggerElk(__name__, True) # Building the model self.classifier = Sequential() # Creating the method for model # Step 1- Convolution self.classifier.add( Convolution2D(128, (5, 5), input_shape=(self.conf.nn_image_size, self.conf.nn_image_size, 1), activation='relu')) # adding another layer self.classifier.add(Convolution2D(64, (4, 4), activation='relu')) # Pooling it self.classifier.add(MaxPooling2D(pool_size=(2, 2))) # Adding another layer self.classifier.add(Convolution2D(32, (3, 3), activation='relu')) # Pooling self.classifier.add(MaxPooling2D(pool_size=(2, 2))) # Adding another layer self.classifier.add(Convolution2D(32, (3, 3), activation='relu')) # Pooling self.classifier.add(MaxPooling2D(pool_size=(2, 2))) # Step 2- Flattening self.classifier.add(Flatten()) # Step 3- Full connection self.classifier.add(Dense(units=128, activation='relu')) # For the output step self.classifier.add( Dense(units=self.conf.nn_class_size, activation='softmax')) self.classifier.add(Dropout(0.02)) # Add reularizers # classifier.add(Dense(128, # input_dim = 128, # kernel_regularizer = regularizers.l1(0.001), # activity_regularizer = regularizers.l1(0.001), # activation = 'relu')) self.classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.add( Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_SHAPE, IMG_SHAPE, 3))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(32, 3, padding='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, 3, padding='same', activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dropout(0.2)) model.add(Dense(512, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(5, activation='softmax')) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) print("Training model...") history = model.fit_generator( train_data_gen, steps_per_epoch=int(np.ceil(train_data_gen.n / float(batch_size))), epochs=EPOCHS, validation_data=val_data_gen,
Y_train = to_categorical(Y_train, 10, dtype='float16') Y_test = to_categorical(Y_test, 10, dtype='float16') print(f'X_train: {X_train.shape}, Y_train: {Y_train.shape}') print(f'X_test: {X_test.shape}, Y_test: {Y_test.shape}') model = Sequential() model.add( Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu')) model.add(MaxPool2D(pool_size=2)) model.add(Dropout(rate=0.25)) model.add(Flatten()) model.add(Dense(units=128, activation='relu')) model.add(Dropout(rate=0.5)) model.add(Dense(units=10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) early_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=0) history = model.fit(X_train, Y_train, batch_size=200, epochs=50,
def construct_keras_api_model(embedding_weights): # input_no_time_no_repeat = Input(shape=max_len, dtype='int32') # embedded_no_time_no_repeat = Embedding( # creative_id_window,embedding_size,weights=[embedding_weights],trainable=False # )(input_no_time_no_repeat) # ================================================================================== Input_fix_creative_id = Input(shape=(math.ceil(time_id_max / period_days) * period_length), dtype='int32', name='input_fix_creative_id') Embedded_fix_creative_id = Embedding( creative_id_window, embedding_size, weights=[embedding_weights], trainable=False)(Input_fix_creative_id) # ================================================================================== # input_no_time_with_repeat = Input(shape=max_len, dtype='int32') # embedded_no_time_with_repeat = Embedding(creative_id_window,embedding_size,weights=[embedding_weights],trainable=False)(input_no_time_with_repeat) # ---------------------------------------------------------------------- GM_x = keras.layers.GlobalMaxPooling1D()(Embedded_fix_creative_id) GM_x = Dropout(0.5)(GM_x) GM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(GM_x) GM_x = BatchNormalization()(GM_x) GM_x = Activation('relu')(GM_x) GM_x = Dropout(0.5)(GM_x) GM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(GM_x) GM_x = BatchNormalization()(GM_x) GM_x = Activation('relu')(GM_x) GM_x = Dense(1, 'sigmoid')(GM_x) # ---------------------------------------------------------------------- GA_x = GlobalAveragePooling1D()(Embedded_fix_creative_id) GA_x = Dropout(0.5)(GA_x) GA_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(GA_x) GA_x = BatchNormalization()(GA_x) GA_x = Activation('relu')(GA_x) GA_x = Dropout(0.5)(GA_x) GA_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(GA_x) GA_x = BatchNormalization()(GA_x) GA_x = Activation('relu')(GA_x) GA_x = Dense(1, 'sigmoid')(GA_x) # ================================================================================== Conv_creative_id = Conv1D(embedding_size, 15, 5, activation='relu')(Embedded_fix_creative_id) # ---------------------------------------------------------------------- Conv_GM_x = MaxPooling1D(7)(Conv_creative_id) Conv_GM_x = Conv1D(embedding_size, 2, 1, activation='relu')(Conv_GM_x) Conv_GM_x = GlobalMaxPooling1D()(Conv_GM_x) Conv_GM_x = Dropout(0.5)(Conv_GM_x) Conv_GM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(Conv_GM_x) Conv_GM_x = BatchNormalization()(Conv_GM_x) Conv_GM_x = Activation('relu')(Conv_GM_x) Conv_GM_x = Dropout(0.5)(Conv_GM_x) Conv_GM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(Conv_GM_x) Conv_GM_x = BatchNormalization()(Conv_GM_x) Conv_GM_x = Activation('relu')(Conv_GM_x) Conv_GM_x = Dense(1, 'sigmoid')(Conv_GM_x) # ---------------------------------------------------------------------- Conv_GA_x = AveragePooling1D(7)(Conv_creative_id) Conv_GA_x = Conv1D(embedding_size, 2, 1, activation='relu')(Conv_GA_x) Conv_GA_x = GlobalAveragePooling1D()(Conv_GA_x) Conv_GA_x = Dropout(0.5)(Conv_GA_x) Conv_GA_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(Conv_GA_x) Conv_GA_x = BatchNormalization()(Conv_GA_x) Conv_GA_x = Activation('relu')(Conv_GA_x) Conv_GA_x = Dropout(0.5)(Conv_GA_x) Conv_GA_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(Conv_GA_x) Conv_GA_x = BatchNormalization()(Conv_GA_x) Conv_GA_x = Activation('relu')(Conv_GA_x) Conv_GA_x = Dense(1, 'sigmoid')(Conv_GA_x) # ---------------------------------------------------------------------- LSTM_x = Conv1D(embedding_size, 14, 7, activation='relu')(Conv_creative_id) LSTM_x = LSTM(embedding_size, return_sequences=True)(LSTM_x) LSTM_x = LSTM(embedding_size, return_sequences=True)(LSTM_x) LSTM_x = LSTM(embedding_size)(LSTM_x) LSTM_x = Dropout(0.5)(LSTM_x) LSTM_x = Dense(embedding_size // 2, kernel_regularizer=l2(0.001))(LSTM_x) LSTM_x = BatchNormalization()(LSTM_x) LSTM_x = Activation('relu')(LSTM_x) LSTM_x = Dropout(0.5)(LSTM_x) LSTM_x = Dense(embedding_size // 4, kernel_regularizer=l2(0.001))(LSTM_x) LSTM_x = BatchNormalization()(LSTM_x) LSTM_x = Activation('relu')(LSTM_x) LSTM_x = Dense(1, 'sigmoid')(LSTM_x) # ---------------------------------------------------------------------- concatenated = concatenate([ GM_x, GA_x, Conv_GM_x, Conv_GA_x, LSTM_x, ], axis=-1) output_tensor = Dense(1, 'sigmoid')(concatenated) keras_api_model = Model( [ # input_no_time_no_repeat, Input_fix_creative_id, # input_no_time_with_repeat, ], output_tensor) keras_api_model.summary() plot_model(keras_api_model, to_file='model/keras_api_word2vec_model.png') print('-' * 5 + ' ' * 3 + "编译模型" + ' ' * 3 + '-' * 5) keras_api_model.compile(optimizer=optimizers.RMSprop(lr=RMSProp_lr), loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy]) return keras_api_model
# Create CNN Model NUM_EPOCHS = 25 LR = 0.001 BATCH_SIZE = 32 model = tf.keras.Sequential() model.add( Conv2D(28, (3, 3), strides=2, padding='same', activation='relu', input_shape=(28, 28, 1))) model.add(BatchNormalization()) model.add(MaxPooling2D((2, 2))) model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D((2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dense(256, activation='relu')) model.add(Dense(64, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax'))