def trainCNN(obj, wordVec_model, dataset_headLines, dataset_body): embedding_dim = 300 LSTM_neurons = 50 dense_neuron = 16 lamda = 0.0 nb_filter = 100 filter_length = 4 batch_size = 50 epochs = 5 ntn_out = 16 ntn_in = nb_filter state = False train_head, train_body, embedding_matrix = obj.process_data( sent_Q=dataset_headLines, sent_A=dataset_body, dimx=dimx, dimy=dimy, wordVec_model=wordVec_model) inpx = Input(shape=(dimx, ), dtype='int32', name='inpx') #x = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimx)(inpx) x = word2vec_embedding_layer(embedding_matrix)(inpx) inpy = Input(shape=(dimy, ), dtype='int32', name='inpy') #y = Embedding(output_dim=embedding_dim, input_dim=vocab_size, input_length=dimy)(inpy) y = word2vec_embedding_layer(embedding_matrix)(inpy) ques = Convolution1D(nb_filter=nb_filter, filter_length=filter_length, border_mode='valid', activation='relu', subsample_length=1)(x) ans = Convolution1D(nb_filter=nb_filter, filter_length=filter_length, border_mode='valid', activation='relu', subsample_length=1)(y) hx = Lambda(max_1d, output_shape=(nb_filter, ))(ques) hy = Lambda(max_1d, output_shape=(nb_filter, ))(ans) #hx = GlobalMaxPooling1D()(ques) #hy = GlobalMaxPooling1D()(ans) #hx = Flatten() #hy = Flatten() hx1 = Multiply()([hx, hy]) hy1 = Abs()([hx, hy]) h = Merge(mode="concat", name='h')([hx1, hy1]) ''' shared_lstm = Bidirectional(LSTM(LSTM_neurons,return_sequences=True),merge_mode='sum') #shared_lstm = LSTM(LSTM_neurons,return_sequences=True) hx = shared_lstm(x) #hx = Dropout(0.2)(hx) hy = shared_lstm(y) #hy = Dropout(0.2)(hy) #corr = CorrelationRegularization(-lamda)([hx,hy]) h1 = Flatten()(hx) h2 = Flatten()(hy) hx1 = Multiply()([h1,h2]) hx2 = Abs()([h1,h2]) h = Merge(mode="concat",name='h')([hx1,hx2]) ''' #h1 = Multiply()([hx,hy]) #h2 = Abs()([hx,hy]) #h = Merge(mode="concat",name='h')([h1,h2]) #h = Merge(mode="concat",name='h')([hx,hy]) #h = NeuralTensorLayer(output_dim=1,input_dim=ntn_in)([hx,hy]) #h = ntn_layer(ntn_in,ntn_out,activation=None)([hx,hy]) #score = h wrap = Dense(dense_neuron, activation='relu', name='wrap')(h) #score = Dense(1,activation='sigmoid',name='score')(h) #wrap = Dense(dense_neuron,activation='relu',name='wrap')(h) score = Dense(4, activation='softmax', name='score')(wrap) #score=K.clip(score,1e-7,1.0-1e-7) #corr = CorrelationRegularization(-lamda)([hx,hy]) #model = Model( [inpx,inpy],[score,corr]) model = Model([inpx, inpy], score) model.compile(loss='categorical_crossentropy', optimizer="adadelta", metrics=['accuracy']) return model, train_head, train_body
# Convolutional model submodels = [] for kw in size_filters: submodel = Sequential() submodel.add( Conv1D(num_filters, kw, padding='valid', kernel_initializer=initializers.RandomNormal(np.sqrt(2 / kw)), input_shape=(tam_fijo, embedding_vecor_length))) submodel.add(advanced_activations.PReLU(initializers.Constant(value=0.25))) submodel.add(GlobalMaxPooling1D()) submodels.append(submodel) model = Sequential() model.add(Merge(submodels, mode="concat")) model.add(Dropout(dropout)) model.add(Dense(2, activation='softmax')) # Log to tensorboard tensorBoardCallback = TensorBoard(log_dir='./logs22', write_graph=True) adadelta = optimizers.Adadelta(lr=alpha) model.compile(loss='categorical_crossentropy', optimizer=adadelta, metrics=['accuracy', 'mse']) model.fit([X_train] * len(size_filters), y_train, epochs=n_iter, callbacks=[tensorBoardCallback], batch_size=size_batch,
output = Merge(mode='concat', concat_axis=-1)([output, output_reverse]) #embedding_input = TimeDistributed(Dense(dense_dim, activation='sigmoid'))(input) #output = Merge(mode=func, output_shape=lambda x: x[0])([embedding_input, input_mask]) #output = MaxPooling1D(pool_length=max_sequence_length)(output) #output = Flatten()(output) model = Model(input=[input, input_mask], output=output) return model gru_m = gru_model() gru_sum_embedding_sen_1 = gru_m([embedding_sen_1, sen_1_mask]) gru_sum_embedding_sen_2 = gru_m([embedding_sen_2, sen_2_mask]) abs_merge = lambda x: tf.abs(x[0] - x[1]) mul_merge = lambda x: tf.mul(x[0], x[1]) abs_feature = Merge(mode=abs_merge, output_shape=lambda x: x[0])( [sum_embedding_sen_1, sum_embedding_sen_2]) mul_feature = Merge(mode=mul_merge, output_shape=lambda x: x[0])( [sum_embedding_sen_1, sum_embedding_sen_2]) gru_abs_feature = Merge(mode=abs_merge, output_shape=lambda x: x[0])( [gru_sum_embedding_sen_1, gru_sum_embedding_sen_2]) gru_mul_feature = Merge(mode=mul_merge, output_shape=lambda x: x[0])( [gru_sum_embedding_sen_1, gru_sum_embedding_sen_2]) leaks_input = Input(shape=(3, ), dtype='float32') leaks_dense = Dense(50, activation='relu')(leaks_input) feature = Merge(mode='concat', concat_axis=-1)( [abs_feature, mul_feature, gru_abs_feature, gru_mul_feature, leaks_dense]) feature = Dropout(dropout_rate)(feature) feature = Dense(1, activation='sigmoid')(feature) final_model = Model(input=[sen_1, sen_1_mask, sen_2, sen_2_mask, leaks_input],
word_vec_dim=300 hidden_dim_feed=1000 no_classes=1000 ######################################################################### # ''' ##will load the image model here image_model=Sequential() # image_model.add(Reshape(input_shape = (img_dim,), dims=(img_dim,))) image_model.add(Reshape((img_dim,), input_shape=(img_dim,))) ##will import the language model here questions_model=Sequential() questions_model.add(GRU(output_dim=quest_dim,input_shape=(SEQ_LENGTH,word_vec_dim),return_sequences=True)) questions_model.add(TimeDistributed(Dense(100,activation='relu',init='uniform'))) questions_model.add(Reshape((23*100,))) ################################################################################ model=Sequential() ##merging image_dim and quest_dim model.add(Merge([image_model,questions_model],mode='concat')) model.add(Dense(output_dim=hidden_dim_feed,activation='relu',init='uniform')) model.add(Dense(output_dim=500,activation='relu',init='uniform')) model.add(Dense(output_dim=no_classes,activation='softmax')) # ##saving the model to json name_model_mlp=model.to_json() open('../model/1_gru_timedistributed_2hidden.json','w').write(name_model_mlp) print("Model is saved") import gc; gc.collect()
def _build_model(self): # Neural Net for Deep-Q learning Model # input image dimensions img_rows, img_cols = 224, 320 # number of convolutional filters to use nb_filters = 32 # size of pooling area for max pooling nb_pool = 2 # convolution kernel size nb_conv = 3 # model = Sequential() img_input = Input(shape=(224,320,3)) x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input) x = Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) # Block 2 x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) # Block 3 x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block3_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) # Block 4 x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block4_conv4')(x) x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) x = Flatten(name='flatten')(x) # x = Dense(64, activation='relu', name='fc1')(x) x = Dense(64, activation='relu', name='fc2')(x) left_branch = Model(img_input, x, name='vgg19') map_input = Input(shape=(188, 20, 1)) x2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='m2block1_conv1')(map_input) x2 = Conv2D(32, (3, 3), activation='relu', padding='same', name='m2block1_conv2')(x2) x2 = MaxPooling2D((2, 2), strides=(2, 2), name='m2block1_pool')(x2) # Block 2 x2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='m2block2_conv1')(x2) x2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='m2block2_conv2')(x2) x2 = MaxPooling2D((2, 2), strides=(2, 2), name='m2block2_pool')(x2) # Block 3 x2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='m2block3_conv1')(x2) x2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='m2block3_conv2')(x2) x2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='m2block3_conv3')(x2) x2 = Conv2D(128, (3, 3), activation='relu', padding='same', name='m2block3_conv4')(x2) x2 = MaxPooling2D((2, 2), strides=(2, 2), name='m2block3_pool')(x2) x2 = Flatten(name='m2flatten')(x2) x2 = Dense(64, activation='relu', name='m2fc2')(x2) right_branch = Model(map_input, x2, name='m2_vgg19') merged = Merge([left_branch, right_branch], mode='concat') final_model = Sequential() final_model.add(merged) final_model.add(Dense(24, activation='relu', name='mfc1')) final_model.add(Dense(self.q_lenght, activation='relu', name='mfc2')) final_model.compile(loss='mse', optimizer='adam', metrics=['accuracy']) # print(final_model.inputs) # exit() return final_model
def merge_wide3_layer_Models(loss='categorical_crossentropy', optimizer='adadelta', img_rows=28, img_cols=28, depth=1, classes=10, init='adam'): left = Sequential() left.add(GaussianNoise(sigma=0.01, input_shape=(depth, img_rows, img_cols))) left.add(Convolution2D(classes * 4, 3, 3, border_mode='valid')) left.add(Activation('relu')) left.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) left.add(Convolution2D(nb_filters * 2, 3, 3)) left.add(Activation('relu')) left.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) left.add(Dropout(0.25)) left.add(Flatten()) left.add(Dense(classes * 10)) left.add(Activation('relu')) right = Sequential() right.add(GaussianNoise(sigma=0.2, input_shape=(depth, img_rows, img_cols))) right.add(Convolution2D(classes * 4, 3, 3, border_mode='valid')) right.add(Activation('relu')) right.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) right.add(Convolution2D(nb_filters, 3, 3)) right.add(Activation('relu')) right.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) right.add(Dropout(0.25)) right.add(Flatten()) right.add(Dense(classes * 10)) right.add(Activation('relu')) middle = Sequential() middle.add( Convolution2D(classes * 4, 3, 3, border_mode='valid', input_shape=(depth, img_rows, img_cols))) middle.add(Activation('relu')) middle.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) middle.add(Convolution2D(nb_filters, 3, 3)) middle.add(Activation('relu')) middle.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) middle.add(Dropout(0.25)) middle.add(Flatten()) middle.add(Dense(classes * 10)) middle.add(Activation('relu')) merged = Merge([left, middle, right], mode='concat') final = Sequential() final.add(merged) final.add(Dropout(0.5)) final.add(Dense(classes)) final.add(Activation('softmax')) final.compile(loss=loss, optimizer=optimizer, metrics=['accuracy']) return final
model_dayofweek = Sequential() model_dayofweek.add(Embedding(len_uniq_dayofweek, 3, input_length=1)) model_dayofweek.add(Reshape(target_shape=(3, ))) models.append(model_dayofweek) model_isweekend = Sequential() model_isweekend.add(Embedding(len_uniq_isweekend, 1, input_length=1)) model_isweekend.add(Reshape(target_shape=(1, ))) models.append(model_isweekend) model_others = Sequential() model_others.add(Dense(1, input_dim=train_x[-1].shape[1])) models.append(model_others) model = Sequential() model.add(Merge(models, mode='concat')) model.add(Dense(512, init='normal', activation='relu')) model.add(Dropout(0.1)) model.add(Dense(512, init='normal', activation='relu')) model.add(Dropout(0.1)) model.add(Dense(1, init='normal', activation='sigmoid')) model.compile(loss='mae', optimizer='adam') print(model.summary()) print('Training is about to start...') starttime = time.time() history = model.fit(train_x, train_y, nb_epoch=EPOCHNO, batch_size=BATCHSIZE, callbacks=[early_stopping, checkpointer],
model_z.add(Convolution2D(nb_filters, kernel_size_2d[0], kernel_size_2d[1])) model_z.add(Activation('relu')) model_z.add(MaxPooling2D(pool_size=pool_size_2d)) #model_z.add(Dropout(0.25)) model_z.add(Convolution2D(nb_filters, kernel_size_2d[0], kernel_size_2d[1])) model_z.add(Activation('relu')) model_z.add(MaxPooling2D(pool_size=pool_size_2d)) #model_z.add(Dropout(0.25)) model_z.add(Flatten()) merged = Merge([model_y, model_z], mode='concat') final_model = Sequential() final_model.add(merged) # print("Output shape after merge:", final_model.output_shape) # final_model.add(Dense(1024)) # final_model.add(Activation('relu')) # final_model.add(Dropout(0.5)) print("Output shape after fully connected(dropout0.5):", final_model.output_shape) final_model.add(Dense(128)) final_model.add(Activation('relu')) final_model.add(Dropout(0.5)) print("Output shape after dully connected(dropout0.5):", final_model.output_shape) final_model.add(Dense(nb_classes))
left = Sequential() left.add( LSTM(output_dim=layers[1], return_sequences=False, input_shape=(SEQ_LEN, len(english_sorted)))) right = Sequential() right.add( LSTM(output_dim=layers[1], return_sequences=False, input_shape=(SEQ_LEN, len(english_sorted)), go_backwards=True)) model = Sequential() model.add(Merge([left, right], mode='sum')) model.add(Dense(layers[2], activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # In[162]: model.load_weights(weight_fil) y = model.predict([X, X], batch_size=BATCH_SIZE, verbose=0) out = '' for arr in y: out += meter_sorted[np.argmax(arr)]
model.add(RNN(HIDDEN_SIZE, input_shape=(None, len(chars)), return_sequences=True)) else: model.add(RNN(HIDDEN_SIZE, input_shape=(None, len(chars)), return_sequences=False)) model.add(Dense(help_nn)) if LAYERS>2: for _ in xrange(LAYERS-2): model.add(RNN(HIDDEN_SIZE, return_sequences=True)) # #model.add(Dropout(0.5)) if LAYERS>1: model.add(RNN(HIDDEN_SIZE, return_sequences=False)) #model.add(Dense(len(classes))) #model.add(Activation('softmax')) #model.compile(loss=loss_function0, optimizer='adam') merged = Merge([model_fixed, model], mode='concat') final_model = Sequential() final_model.add(merged) final_model.add(Dense(help_nn)) final_model.add(Activation('tanh')) final_model.add(Dense(len(classes))) final_model.add(Activation('softmax')) final_model.compile(loss=loss_function0, optimizer="adam") model = final_model #save the model #json_string = model.to_json() #open(path_save+file_name0+'_model.json', 'w').write(json_string)
def cnn02(input_shape): # left model_left = Sequential() model_left.add( Conv2D(filters=64, kernel_size=3, strides=1, padding="same", input_shape=input_shape)) model_left.add(MaxPool2D(pool_size=2, strides=2)) model_left.add(Activation('relu')) model_left.add(Flatten()) model_left.add(Dense(64)) model_left.add(Activation('relu')) # middle model_middle = Sequential() model_middle.add( Conv2D(filters=128, kernel_size=3, strides=1, padding="same", input_shape=input_shape)) model_middle.add(MaxPool2D(pool_size=2, strides=2)) model_middle.add(Activation('relu')) model_middle.add( Conv2D(filters=256, kernel_size=3, strides=1, padding="same")) model_middle.add(MaxPool2D(pool_size=2, strides=2)) model_middle.add(Activation('relu')) model_middle.add(Flatten()) model_middle.add(Dense(128)) model_middle.add(Activation('relu')) # right model_right = Sequential() model_right.add( Conv2D(filters=128, kernel_size=3, strides=1, padding="same", input_shape=input_shape)) model_right.add(MaxPool2D(pool_size=2, strides=2)) model_right.add(Activation('relu')) model_right.add( Conv2D(filters=256, kernel_size=3, strides=1, padding="same")) model_right.add(MaxPool2D(pool_size=2, strides=2)) model_right.add(Activation('relu')) model_right.add(Flatten()) model_right.add(Dense(128)) model_right.add(Activation('relu')) merged_model = Merge([model_left, model_middle, model_right], mode='concat') # merged_model = Merge([model_left, model_middle], mode='concat') # merge final_model = Sequential() final_model.add(merged_model) # output final_model.add(Dense(1)) final_model.add(Activation('sigmoid')) final_model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) return final_model
input_length=story_maxlen)) story_encoder_m.add(Dropout(0.3)) # question encoder. Output dim: (None, query_maxlen, EMBED_HIDDEN_SIZE) question_encoder = Sequential() question_encoder.add( Embedding(input_dim=vocab_size, output_dim=EMBED_HIDDEN_SIZE, input_length=question_maxlen)) question_encoder.add(Dropout(0.3)) # compute match between story and question. # Output dim: (None, story_maxlen, question_maxlen) match = Sequential() match.add( Merge([story_encoder_m, question_encoder], mode="dot", dot_axes=[2, 2])) # encode story into vector space of question # output dim: (None, story_maxlen, query_maxlen) story_encoder_c = Sequential() story_encoder_c.add( Embedding(input_dim=vocab_size, output_dim=question_maxlen, input_length=story_maxlen)) story_encoder_c.add(Dropout(0.3)) # combine match and story vectors. # Output dim: (None, query_maxlen, story_maxlen) response = Sequential() response.add(Merge([match, story_encoder_c], mode="sum")) response.add(Permute((2, 1)))
def modelMultiFilters(input_shape): arc_params = {} # pre-def params arc_params['n_frames'] = 43 arc_params['n_mel'] = 96 arc_params['class_count'] = 15 # number of filters in 1L arc_params['n_filters_l1'] = [32, 32, 48] # dfine different shapes in the 1L arc_params['kernel_size_l1'] = [(3, 60), (3, 30), (3, 10)] # always the same # max pooling shapes in 1L, in tuple () arc_params['pool_size_l1'] = (5, 5) # number of filters in 2L arc_params['n_filters_l2'] = 256 # dfine shape in the 2L arc_params['kernel_size_l2'] = (5, 7) arc_params['pool_size_l2'] = (4, 4) # -- # input_shape = (43, 96, 1) channel_axis = 3 # for BN # this model is valid for any number of different shapes, which is hardcoded in the yaml and defined previously # instantiate input layer tf_input = Input(shape=input_shape) convs_1L = [] # empty list to append the diferent filter shapes' layers for i, ksz in enumerate(arc_params['kernel_size_l1']): print(i) print(ksz) conv = Conv2D( arc_params['n_filters_l1'][i], arc_params['kernel_size_l1'][i], padding= 'same', # IMPORTANT; easy option for now, else we have different sizes data_format='channels_last', kernel_initializer='uniform') x = conv(tf_input) convs_1L.append(x) # out_1L = keras.layers.concatenate(convs_1L, axis=channel_axis) error global name # so merge1 = Merge(mode='concat', concat_axis=channel_axis) out_1L = merge1(convs_1L) # ***got here the output of the several convolution layers with different filters, in parallel # create a model that takes the T-F representation as input and applies the distribution of filters as designed # This creates a model that includes the Input layer and the convLayers defined distrib_1L = Model(inputs=tf_input, outputs=out_1L) # create seq model, where to start adding the orevious layer and others model = Sequential() model.add(distrib_1L) model.add(BatchNormalization(axis=channel_axis)) model.add(Activation('relu')) model.add( MaxPooling2D(pool_size=arc_params['pool_size_l1'], data_format="channels_last")) # allows defining padding model.add( Conv2D(arc_params['n_filters_l2'], arc_params['kernel_size_l2'], padding='valid', data_format="channels_last", kernel_initializer='uniform')) model.add(BatchNormalization(axis=channel_axis)) model.add(Activation('relu')) model.add( MaxPooling2D(pool_size=arc_params['pool_size_l2'], data_format="channels_last")) # allows defining padding model.add(Flatten()) model.add( Dense(arc_params['class_count'], activation='softmax', kernel_initializer="uniform")) optimizer = Adam(lr=0.001) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) return model
def main(argv): def f1_score(y_true, y_pred): # Count positive samples. c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) c2 = K.sum(K.round(K.clip(y_pred, 0, 1))) c3 = K.sum(K.round(K.clip(y_true, 0, 1))) # If there are no true samples, fix the F1 score at 0. if c3 == 0 or c2 == 0: return 0 # How many selected items are relevant? precision = c1 / c2 # How many relevant items are selected? recall = c1 / c3 # Calculate f1_score f1_score = 2 * (precision * recall) / (precision + recall) return f1_score def precision(y_true, y_pred): # Count positive samples. c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) c2 = K.sum(K.round(K.clip(y_pred, 0, 1))) if c2 == 0: return 0 # How many selected items are relevant? precision = c1 / c2 return precision def recall(y_true, y_pred): # Count positive samples. c1 = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) c3 = K.sum(K.round(K.clip(y_true, 0, 1))) # If there are no true samples, fix the F1 score at 0. if c3 == 0: return 0 # How many relevant items are selected? recall = c1 / c3 return recall try: opts, args = getopt.getopt(argv,"hi:o:e:n:w:",["ifile=", "ofile=", "embeddingfile", "nbfile", "weightFile"]) except getopt.GetoptError: print ('test_FFN_noHCF.py -i <QUESTION_PAIRS_FILE> -o <RESULT_FILE> -e <WORD_EMBEDDING_MATRIX_FILE> -n <NB_WORDS_DATA_FILE> -w <MODEL_WEIGHTS_FILE>') sys.exit(2) for opt, arg in opts: if opt == '-h': print ('test_FFN_noHCF.py -i <QUESTION_PAIRS_FILE> -o <RESULT_FILE> -e <WORD_EMBEDDING_MATRIX_FILE> -n <NB_WORDS_DATA_FILE> -w <MODEL_WEIGHTS_FILE>') sys.exit() elif opt in ("-i", "--ifile"): QUESTION_PAIRS_FILE = arg elif opt in ("-o", "--ofile"): RESULT_FILE = arg elif opt in ("-e", "--embeddingfile"): WORD_EMBEDDING_MATRIX_FILE = arg elif opt in ("-n", "--nbfile"): NB_WORDS_DATA_FILE = arg elif opt in ("-w", "--weightFile"): MODEL_WEIGHTS_FILE = arg ## Initialize global variables EMBEDDING_DIM = 300 MAX_SEQUENCE_LENGTH = 25 MAX_NB_WORDS = 200000 Q1_TESTING_DATA_FILE = 'q1_test.npy' Q2_TESTING_DATA_FILE = 'q2_test.npy' ## Load word embedding matrix word_embedding_matrix = np.load(open(WORD_EMBEDDING_MATRIX_FILE, 'rb')) ## Load nb words data with open(NB_WORDS_DATA_FILE, 'r') as f: nb_words = json.load(f)['nb_words'] ## Load testing question pairs if exists(Q1_TESTING_DATA_FILE) and exists(Q2_TESTING_DATA_FILE) and exists(WORD_EMBEDDING_MATRIX_FILE) and exists(NB_WORDS_DATA_FILE): q1_data = np.load(open(Q1_TESTING_DATA_FILE, 'rb')) q2_data = np.load(open(Q2_TESTING_DATA_FILE, 'rb')) else: print("Processing", QUESTION_PAIRS_FILE) question1 = [] question2 = [] with open(QUESTION_PAIRS_FILE, encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile, delimiter=',') for row in reader: question1.append(row['question1']) question2.append(row['question2']) print('Question pairs: %d' % len(question1)) questions = question1 + question2 tokenizer = Tokenizer(nb_words=MAX_NB_WORDS) tokenizer.fit_on_texts(questions) question1_word_sequences = tokenizer.texts_to_sequences(question1) question2_word_sequences = tokenizer.texts_to_sequences(question2) word_index = tokenizer.word_index print("Words in index: %d" % len(word_index)) q1_data = pad_sequences(question1_word_sequences, maxlen=MAX_SEQUENCE_LENGTH) q2_data = pad_sequences(question2_word_sequences, maxlen=MAX_SEQUENCE_LENGTH) print('Shape of question1 data tensor:', q1_data.shape) print('Shape of question2 data tensor:', q2_data.shape) np.save(open(Q1_TESTING_DATA_FILE, 'wb'), q1_data) np.save(open(Q2_TESTING_DATA_FILE, 'wb'), q2_data) X = np.stack((q1_data, q2_data), axis=1) Q1_test = X[:,0] Q2_test = X[:,1] Q1 = Sequential() Q1.add(Embedding(nb_words + 1, EMBEDDING_DIM, weights=[word_embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False)) Q1.add(TimeDistributed(Dense(EMBEDDING_DIM, activation='relu'))) Q1.add(Lambda(lambda x: K.sum(x, axis=1), output_shape=(EMBEDDING_DIM, ))) Q2 = Sequential() Q2.add(Embedding(nb_words + 1, EMBEDDING_DIM, weights=[word_embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False)) Q2.add(TimeDistributed(Dense(EMBEDDING_DIM, activation='relu'))) Q2.add(Lambda(lambda x: K.sum(x, axis=1), output_shape=(EMBEDDING_DIM, ))) ## Build the model print("Build Model") model = Sequential() model.add(Merge([Q1, Q2], mode='concat')) model.add(BatchNormalization()) model.add(Dense(300, activation='relu'))
branch1.add(Dense(30, activation='tanh', kernel_initializer='glorot_normal', kernel_regularizer=regularizers.l2(0.00005), use_bias=True, bias_initializer='glorot_normal')) branch2 = Sequential() branch2.add(Dense(1, input_shape=(1,), activation='tanh', kernel_initializer='glorot_normal', kernel_regularizer=regularizers.l2(0.00005), use_bias=True, bias_initializer='glorot_normal')) model = Sequential() model.add(Merge([branch1, branch2], mode = 'concat')) # merged = concatenate([branch1, branch2]) model.add(Dense(1, activation='tanh', # 'LateFusionActivation', kernel_initializer='glorot_normal', kernel_regularizer=regularizers.l2(0.00005), use_bias=True, bias_initializer='glorot_normal')) model.add(Dense(1, activation='linear')) model.summary() model.compile(loss='mean_squared_error', #categorical_crossentropy optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999),
print('Build model...') sentrnn = Sequential() sentrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=story_maxlen)) sentrnn.add(Dropout(0.3)) qrnn = Sequential() qrnn.add(Embedding(vocab_size, EMBED_HIDDEN_SIZE, input_length=query_maxlen)) qrnn.add(Dropout(0.3)) qrnn.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False)) qrnn.add(RepeatVector(story_maxlen)) model = Sequential() model.add(Merge([sentrnn, qrnn], mode='sum')) model.add(RNN(EMBED_HIDDEN_SIZE, return_sequences=False)) model.add(Dropout(0.3)) model.add(Dense(vocab_size, activation='softmax')) #model.add(Dense(vocab_size, activation='tanh')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) print('Training') model.fit([X, Xq], Y, batch_size=BATCH_SIZE, nb_epoch=EPOCHS, validation_split=0.05)
def nn_model(): models = [] x0 = Sequential() x0.add(Embedding(184, 30, input_length=1)) x0.add(Reshape(target_shape=(30,))) models.append(x0) x1 = Sequential() x1.add(Embedding(40, 6, input_length=1)) x1.add(Reshape(target_shape=(6,))) models.append(x1) x2 = Sequential() x2.add(Embedding(26, 4, input_length=1)) x2.add(Reshape(target_shape=(4,))) models.append(x2) x3 = Sequential() x3.add(Embedding(13, 2, input_length=1)) x3.add(Reshape(target_shape=(2,))) models.append(x3) x4 = Sequential() x4.add(Embedding(6, 1, input_length=1)) x4.add(Reshape(target_shape=(1,))) models.append(x4) x5 = Sequential() x5.add(Embedding(37, 6, input_length=1)) x5.add(Reshape(target_shape=(6,))) models.append(x5) x6 = Sequential() x6.add(Embedding(8, 1, input_length=1)) x6.add(Reshape(target_shape=(1,))) models.append(x6) x7 = Sequential() x7.add(Embedding(4, 1, input_length=1)) x7.add(Reshape(target_shape=(1,))) models.append(x7) x8 = Sequential() x8.add(Embedding(10, 2, input_length=1)) x8.add(Reshape(target_shape=(2,))) models.append(x8) x9 = Sequential() x9.add(Embedding(23, 4, input_length=1)) x9.add(Reshape(target_shape=(4,))) models.append(x9) x10 = Sequential() x10.add(Embedding(25, 5, input_length=1)) x10.add(Reshape(target_shape=(5,))) models.append(x10) x11 = Sequential() x11.add(Embedding(15, 3, input_length=1)) x11.add(Reshape(target_shape=(3,))) models.append(x11) x12 = Sequential() x12.add(Embedding(13, 2, input_length=1)) x12.add(Reshape(target_shape=(2,))) models.append(x12) x13 = Sequential() x13.add(Embedding(2, 1, input_length=1)) x13.add(Reshape(target_shape=(1,))) models.append(x13) x14 = Sequential() x14.add(Embedding(235, 50, input_length=1)) x14.add(Reshape(target_shape=(50,))) models.append(x14) x15 = Sequential() x15.add(Embedding(17, 3, input_length=1)) x15.add(Reshape(target_shape=(3,))) models.append(x15) x16 = Sequential() x16.add(Embedding(5652, 50, input_length=1)) x16.add(Reshape(target_shape=(50,))) models.append(x16) x17 = Sequential() x17.add(Embedding(472, 50, input_length=1)) x17.add(Reshape(target_shape=(50,))) models.append(x17) x18 = Sequential() x18.add(Embedding(458, 50, input_length=1)) x18.add(Reshape(target_shape=(50,))) models.append(x18) x19 = Sequential() x19.add(Embedding(4, 1, input_length=1)) x19.add(Reshape(target_shape=(1,))) models.append(x19) x20 = Sequential() x20.add(Embedding(187, 30, input_length=1)) x20.add(Reshape(target_shape=(30,))) models.append(x20) x21 = Sequential() x21.add(Embedding(404, 50, input_length=1)) x21.add(Reshape(target_shape=(50,))) models.append(x21) x22 = Sequential() x22.add(Embedding(530, 50, input_length=1)) x22.add(Reshape(target_shape=(50,))) models.append(x22) x23 = Sequential() x23.add(Embedding(6, 1, input_length=1)) x23.add(Reshape(target_shape=(1,))) models.append(x23) x24 = Sequential() x24.add(Embedding(155, 30, input_length=1)) x24.add(Reshape(target_shape=(30,))) models.append(x24) x25 = Sequential() x25.add(Embedding(1665, 50, input_length=1)) x25.add(Reshape(target_shape=(50,))) models.append(x25) x26 = Sequential() x26.add(Embedding(595, 50, input_length=1)) x26.add(Reshape(target_shape=(50,))) models.append(x26) x27 = Sequential() x27.add(Embedding(15, 3, input_length=1)) x27.add(Reshape(target_shape=(3,))) models.append(x27) x28 = Sequential() x28.add(Embedding(32, 6, input_length=1)) x28.add(Reshape(target_shape=(6,))) models.append(x28) x29 = Sequential() x29.add(Embedding(37, 6, input_length=1)) x29.add(Reshape(target_shape=(6,))) models.append(x29) x30 = Sequential() x30.add(Embedding(149, 30, input_length=1)) x30.add(Reshape(target_shape=(30,))) models.append(x30) x31 = Sequential() x31.add(Embedding(190, 30, input_length=1)) x31.add(Reshape(target_shape=(30,))) models.append(x31) x32 = Sequential() x32.add(Embedding(8, 2, input_length=1)) x32.add(Reshape(target_shape=(2,))) models.append(x32) x33 = Sequential() x33.add(Embedding(9, 2, input_length=1)) x33.add(Reshape(target_shape=(2,))) models.append(x33) x34 = Sequential() x34.add(Embedding(2, 1, input_length=1)) x34.add(Reshape(target_shape=(1,))) models.append(x34) x35 = Sequential() x35.add(Embedding(12, 2, input_length=1)) x35.add(Reshape(target_shape=(2,))) models.append(x35) x36 = Sequential() x36.add(Embedding(4, 1, input_length=1)) x36.add(Reshape(target_shape=(1,))) models.append(x36) x37 = Sequential() x37.add(Dense(1, input_dim=1)) models.append(x37) x38 = Sequential() x38.add(Dense(1, input_dim=1)) models.append(x38) x39 = Sequential() x39.add(Dense(1, input_dim=1)) models.append(x39) x40 = Sequential() x40.add(Dense(1, input_dim=1)) models.append(x40) x41 = Sequential() x41.add(Dense(1, input_dim=1)) models.append(x41) x42 = Sequential() x42.add(Dense(1, input_dim=1)) models.append(x42) x43 = Sequential() x43.add(Dense(1, input_dim=1)) models.append(x43) x44 = Sequential() x44.add(Dense(1, input_dim=1)) models.append(x44) x45 = Sequential() x45.add(Dense(1, input_dim=1)) models.append(x45) x46 = Sequential() x46.add(Dense(1, input_dim=1)) models.append(x46) x47 = Sequential() x47.add(Dense(1, input_dim=1)) models.append(x47) x48 = Sequential() x48.add(Dense(1, input_dim=1)) models.append(x48) x49 = Sequential() x49.add(Dense(1, input_dim=1)) models.append(x49) x50 = Sequential() x50.add(Dense(1, input_dim=1)) models.append(x50) x51 = Sequential() x51.add(Dense(1, input_dim=1)) models.append(x51) x52 = Sequential() x52.add(Dense(1, input_dim=1)) models.append(x52) x53 = Sequential() x53.add(Dense(1, input_dim=1)) models.append(x53) x54 = Sequential() x54.add(Dense(1, input_dim=1)) models.append(x54) x55 = Sequential() x55.add(Dense(1, input_dim=1)) models.append(x55) x56 = Sequential() x56.add(Dense(1, input_dim=1)) models.append(x56) x57 = Sequential() x57.add(Dense(1, input_dim=1)) models.append(x57) x58 = Sequential() x58.add(Dense(1, input_dim=1)) models.append(x58) x59 = Sequential() x59.add(Dense(1, input_dim=1)) models.append(x59) x60 = Sequential() x60.add(Dense(1, input_dim=1)) models.append(x60) x61 = Sequential() x61.add(Dense(1, input_dim=1)) models.append(x61) x62 = Sequential() x62.add(Dense(1, input_dim=1)) models.append(x62) x63 = Sequential() x63.add(Dense(1, input_dim=1)) models.append(x63) x64 = Sequential() x64.add(Dense(1, input_dim=1)) models.append(x64) x65 = Sequential() x65.add(Dense(1, input_dim=1)) models.append(x65) x66 = Sequential() x66.add(Dense(1, input_dim=1)) models.append(x66) x67 = Sequential() x67.add(Dense(1, input_dim=1)) models.append(x67) x68 = Sequential() x68.add(Dense(1, input_dim=1)) models.append(x68) x69 = Sequential() x69.add(Dense(1, input_dim=1)) models.append(x69) x70 = Sequential() x70.add(Dense(1, input_dim=1)) models.append(x70) x71 = Sequential() x71.add(Dense(1, input_dim=1)) models.append(x71) x72 = Sequential() x72.add(Dense(1, input_dim=1)) models.append(x72) x73 = Sequential() x73.add(Dense(1, input_dim=1)) models.append(x73) x74 = Sequential() x74.add(Dense(1, input_dim=1)) models.append(x74) x75 = Sequential() x75.add(Dense(1, input_dim=1)) models.append(x75) x76 = Sequential() x76.add(Dense(1, input_dim=1)) models.append(x76) x77 = Sequential() x77.add(Dense(1, input_dim=1)) models.append(x77) x78 = Sequential() x78.add(Dense(1, input_dim=1)) models.append(x78) x79 = Sequential() x79.add(Dense(1, input_dim=1)) models.append(x79) x80 = Sequential() x80.add(Dense(1, input_dim=1)) models.append(x80) x81 = Sequential() x81.add(Dense(1, input_dim=1)) models.append(x81) x82 = Sequential() x82.add(Dense(1, input_dim=1)) models.append(x82) x83 = Sequential() x83.add(Dense(1, input_dim=1)) models.append(x83) model = Sequential() model.add(Merge(models, mode='concat')) model.add(Dropout(0.2)) model.add(Dense(800, init = 'uniform'))#500 model.add(PReLU()) model.add(BatchNormalization()) model.add(Dropout(0.3)) model.add(Dense(800, init = 'uniform'))#400 model.add(PReLU()) model.add(BatchNormalization()) model.add(Dropout(0.3)) model.add(Dense(500, init = 'uniform'))#400 model.add(PReLU()) model.add(BatchNormalization()) model.add(Dropout(0.2)) model.add(Dense(500, init = 'uniform'))#400 model.add(PReLU()) model.add(BatchNormalization()) model.add(Dropout(0.1)) model.add(Dense(1, init='zero')) model.compile(loss = 'mean_absolute_error', optimizer = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.)) return(model)
def build_pre_model(self, config, weights): cnn_i_model = Sequential() cnn_i_model.add( Embedding( config['max_features'], config['embedding_dims'], input_length=config['input_length'], weights=[weights['Wemb']] if 'Wemb' in weights else None)) #dropout = 0.2)) cnn_i_model.add( Convolution1D(nb_filter=config['nb_filter_1'], filter_length=config['filter_length_1'], border_mode='valid', activation='relu', subsample_length=1)) cnn_i_model.add( MaxPooling1D(pool_length=6, stride=2, border_mode='valid')) cnn_i_model.add( Convolution1D(nb_filter=config['nb_filter_2'], filter_length=config['filter_length_2'], border_mode='valid', activation='relu', subsample_length=1)) cnn_i_model.add(GlobalMaxPooling1D()) cnn_ii_model = Sequential() cnn_ii_model.add( Embedding( config['max_features'], config['embedding_dims'], input_length=config['input_length'], weights=[weights['Wemb']] if 'Wemb' in weights else None)) #dropout = 0.2)) cnn_ii_model.add( Convolution1D(nb_filter=config['nb_filter_2'], filter_length=config['filter_length_2'], border_mode='valid', activation='relu', subsample_length=1)) cnn_ii_model.add(GlobalMaxPooling1D()) # merged model merged_model = Sequential() merged_model.add( Merge([cnn_i_model, cnn_ii_model], mode='concat', concat_axis=1)) merged_model.add(Dropout(self.config['dropout'])) merged_model.add( Dense(self.config['nb_classes'], activation='softmax', name="dense_pretrain")) print '<dense output dimension>:', self.config['nb_classes'] merged_model.compile(loss="categorical_crossentropy", optimizer=self.get_optimizer(config['optimizer']), metrics=['accuracy']) return merged_model
# print('Images shape: ', img_dataset.shape) model_cnn = Sequential() model_cnn.add(Conv2D(32, kernel_size=(3, 3), input_shape=[256, 256, 3])) model_cnn.add(Conv2D(64, kernel_size=(3, 3))) model_cnn.add(MaxPooling2D()) model_cnn.add(Conv2D(128, kernel_size=(3, 3))) model_cnn.add(Flatten()) model_cnn.add(Dense(1024, name='cnn_dense_1024')) model_cnn.add(Dense(128, activation='relu', name='cnn_dense_128')) model_cnn.add(RepeatVector(MAX_LEN)) model_rnn_1 = Sequential() model_rnn_1.add( LSTM(128, input_shape=(MAX_LEN, VOC_SIZE), return_sequences=True)) model_rnn_1.add(TimeDistributed(Dense(128, name='rnn_dense_128'))) model = Sequential() merged = Merge([model_cnn, model_rnn_1], mode='concat') model.add(merged) model.add(LSTM(512, return_sequences=True)) model.add(LSTM(512, return_sequences=False)) model.add(Dense(VOC_SIZE, activation='softmax', name='final_dense_16')) model.compile(optimizer='adam', loss='categorical_crossentropy') y = np.zeros((seq_dataset.shape[0], seq_dataset.shape[2])) y[:, 8] = 1 # ohe for 9 (</body) model.fit([img_dataset, seq_dataset], y, batch_size=1, epochs=5)
# graph subnet with one input and one output, # convolutional layers concateneted in parallel graph_in = Input(shape=(sequence_length, embedding_dim)) convs = [] for fsz in filter_sizes: conv = Convolution1D(nb_filter=num_filters, filter_length=fsz, border_mode='valid', activation='relu', subsample_length=1)(graph_in) pool = MaxPooling1D(pool_length=2)(conv) flatten = Flatten()(pool) convs.append(flatten) if len(filter_sizes) > 1: out = Merge(mode='concat')(convs) else: out = convs[0] graph = Model(input=graph_in, output=out) # main sequential model model = Sequential() if not model_variation == 'CNN-static': model.add( Embedding(len(vocabulary), embedding_dim, input_length=sequence_length, weights=embedding_weights)) model.add( Dropout(dropout_prob[0], input_shape=(sequence_length, embedding_dim)))
def simple_SSD(input_shape, num_classes, min_size, num_priors, max_size, aspect_ratios, variances): input_tensor = Input(shape=input_shape) body = Convolution2D(16, 7, 7)(input_tensor) body = Activation('relu')(body) body = MaxPooling2D(2, 2, border_mode='valid')(body) body = Convolution2D(32, 5, 5)(body) body = Activation('relu')(body) branch_1 = MaxPooling2D(2, 2, border_mode='valid')(body) body = Convolution2D(64, 3, 3)(branch_1) body = Activation('relu')(body) branch_2 = MaxPooling2D(2, 2, border_mode='valid')(body) # first branch norm_1 = Normalize(20)(branch_1) localization_1 = Convolution2D(num_priors * 4, 3, 3, border_mode='same')(norm_1) localization_1 = Flatten(localization_1) classification_1 = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same')(norm_1) classification_1 = Flatten()(classification_1) prior_boxes_1 = PriorBox(input_shape[0:2], min_size, max_size, aspect_ratios) # second branch norm_2 = Normalize(20)(branch_2) localization_2 = Convolution2D(num_priors * 4, 3, 3, border_mode='same')(norm_2) localization_2 = Flatten(localization_2) classification_2 = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same')(norm_2) classification_2 = Flatten()(classification_2) prior_boxes_2 = PriorBox(input_shape[0:2], min_size, max_size, aspect_ratios) localization_head = Merge([localization_1, localization_2], mode='concat', concat_axis=1) classification_head = Merge([classification_1, classification_2], mode='concat', concat_axis=1) prior_boxes_head = Merge([prior_boxes_1, prior_boxes_2], mode='concat', concat_axis=1) if hasattr(localization_head, '_keras_shape'): num_boxes = localization_head._keras_shape[-1] // 4 elif hasattr(localization_head, 'int_shape'): num_boxes = K.int_shape(localization_head)[-1] // 4 localization_head = Reshape((num_boxes, 4))(localization_head) classification_head = Reshape( (num_boxes, num_classes))(classification_head) classification_head = Activation('softmax')(classification_head) predictions = Merge(localization_head, classification_head, prior_boxes_head, mode='concat', concat_axis=2) model = Model(input_tensor, predictions) return model
def recompileModel(classes, embedding_matrix, EMBEDDING_DIM = 200, chunk_size = 1000, CONVOLUTION_FEATURE = 256, BORDER_MODE = 'valid', LSTM_FEATURE = 256, DENSE_FEATURE = 256, DROP_OUT = 0.5, LEARNING_RATE = 0.01, MOMENTUM = 0.9): global sgd ngram_filters = [3, 4] # Define ngrams list, 3-gram, 4-gram, 5-gram convs = [] graph_in = Input(shape=(chunk_size, EMBEDDING_DIM)) for n_gram in ngram_filters: conv = Convolution1D( # Layer X, Features: 256, Kernel Size: ngram nb_filter=CONVOLUTION_FEATURE, # Number of kernels or number of filters to generate filter_length=n_gram, # Size of kernels, ngram activation='relu')(graph_in) # Activation function to use pool = MaxPooling1D( # Layer X a, Max Pooling: 3 pool_length=3)(conv) # Size of kernels lstm = LSTM( # Layer X b, Output Size: 256 output_dim=LSTM_FEATURE)(pool) # Features: 256 convs.append(lstm) model = Sequential() model.add(Embedding( # Layer 0, Start input_dim=nb_words + 1, # Size to dictionary, has to be input + 1 output_dim=EMBEDDING_DIM, # Dimensions to generate weights=[embedding_matrix], # Initialize word weights input_length=chunk_size, # Define length to input sequences in the first layer trainable=False)) # Disable weight changes during training model.add(Dropout(0.25)) # Dropout 25% out = Merge(mode='concat')(convs) # Layer 1, Output Size: Concatted ngrams feature maps graph = Model(input=graph_in, output=out) # Concat the ngram convolutions model.add(graph) # Concat the ngram convolutions model.add(Dropout(DROP_OUT)) # Dropout 50% model.add(Dense( # Layer 3, Output Size: 256 output_dim=DENSE_FEATURE, # Output dimension activation='relu')) # Activation function to use model.add(Dense( # Layer 4, Output Size: Size Unique Labels, Final output_dim=classes, # Output dimension activation='softmax')) # Activation function to use sgd = SGD(lr=LEARNING_RATE, momentum=MOMENTUM, nesterov=True) filepath="author-cnn-ngrams-lstm-word.hdf5" model.load_weights(filepath) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) print("Done compiling.") return model
GRU(256, recurrent_dropout=0.3, dropout=0.3, return_sequences=False)) model_q2 = Sequential() model_q2.add( Embedding(len(word_index) + 1, EMBEDDING_DIM, weights=[embedding_matrix], input_length=SEQ_MAX_LEN, trainable=False, dropout=0.2)) model_q2.add( GRU(256, recurrent_dropout=0.3, dropout=0.3, return_sequences=False)) model_GRU = Sequential() model_GRU.add(Merge([model_q1, model_q2], mode='concat')) model_GRU.add(BatchNormalization()) model_GRU.add(Dense(512)) model_GRU.add(BatchNormalization()) model_GRU.add(Activation(elu)) model_GRU.add(Dropout(0.5)) #model_GRU.add(Dense(1, activation='sigmoid')) # In[19]: model_sum1 = Sequential() model_sum1.add( Embedding(len(word_index) + 1, EMBEDDING_DIM,
# left_branch.add(LSTM(250,return_sequences=False)) left_branch.add(IndRNN(250, recurrent_clip_min=-1, recurrent_clip_max=-1, dropout=0.0, recurrent_dropout=0.0, return_sequences=True)) left_branch.add(IndRNN(250, recurrent_clip_min=-1, recurrent_clip_max=-1, dropout=0.0, recurrent_dropout=0.0, return_sequences=False)) merged = Merge([left_branch,right_branch], mode='dot',output_shape=lambda x: x[0]) final_model = Sequential() final_model.add(merged) final_model.add(Dense(1)) final_model.add(Activation('sigmoid')) print('compile...') final_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print('fit...') final_model.fit([x_train,x_train], y_train, batch_size=1, nb_epoch=nb_epoch, validation_data=([x_test,x_test], y_test) )
def create_model(): models = [] model_ps_ind_02_cat = Sequential() model_ps_ind_02_cat.add(Embedding(5, 3, input_length=1)) model_ps_ind_02_cat.add(Reshape(target_shape=(3, ))) models.append(model_ps_ind_02_cat) model_ps_ind_04_cat = Sequential() model_ps_ind_04_cat.add(Embedding(3, 2, input_length=1)) model_ps_ind_04_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_ind_04_cat) model_ps_ind_05_cat = Sequential() model_ps_ind_05_cat.add(Embedding(8, 5, input_length=1)) model_ps_ind_05_cat.add(Reshape(target_shape=(5, ))) models.append(model_ps_ind_05_cat) model_ps_car_01_cat = Sequential() model_ps_car_01_cat.add(Embedding(13, 7, input_length=1)) model_ps_car_01_cat.add(Reshape(target_shape=(7, ))) models.append(model_ps_car_01_cat) model_ps_car_02_cat = Sequential() model_ps_car_02_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_02_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_02_cat) model_ps_car_03_cat = Sequential() model_ps_car_03_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_03_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_03_cat) model_ps_car_04_cat = Sequential() model_ps_car_04_cat.add(Embedding(10, 5, input_length=1)) model_ps_car_04_cat.add(Reshape(target_shape=(5, ))) models.append(model_ps_car_04_cat) model_ps_car_05_cat = Sequential() model_ps_car_05_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_05_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_05_cat) model_ps_car_06_cat = Sequential() model_ps_car_06_cat.add(Embedding(18, 8, input_length=1)) model_ps_car_06_cat.add(Reshape(target_shape=(8, ))) models.append(model_ps_car_06_cat) model_ps_car_07_cat = Sequential() model_ps_car_07_cat.add(Embedding(3, 2, input_length=1)) model_ps_car_07_cat.add(Reshape(target_shape=(2, ))) models.append(model_ps_car_07_cat) model_ps_car_09_cat = Sequential() model_ps_car_09_cat.add(Embedding(6, 3, input_length=1)) model_ps_car_09_cat.add(Reshape(target_shape=(3, ))) models.append(model_ps_car_09_cat) # model_ps_car_10_cat = Sequential() # model_ps_car_10_cat.add(Embedding(3, 2, input_length=1)) # model_ps_car_10_cat.add(Reshape(target_shape=(2,))) # models.append(model_ps_car_10_cat) # model_ps_car_11_cat = Sequential() # model_ps_car_11_cat.add(Embedding(104, 10, input_length=1)) # model_ps_car_11_cat.add(Reshape(target_shape=(10,))) # models.append(model_ps_car_11_cat) model_rest = Sequential() model_rest.add(Dense(16, input_dim=28)) models.append(model_rest) model = Sequential() model.add(Merge(models, mode='concat')) model.add(Dense(80)) model.add(Activation('relu')) model.add(Dropout(.35)) model.add(Dense(20)) model.add(Activation('relu')) model.add(Dropout(.15)) model.add(Dense(10)) model.add(Activation('relu')) model.add(Dropout(.15)) model.add(Dense(1)) model.add(Activation('sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam') return model
def attention_imp_merge_exp(): """ https://richliao.github.io/supervised/classification/2016/12/26/textclassifier-HATN/ :return: """ global X_DIM, Y_DIM # Load Embeddings matrix embedding_weights = joblib.load(config.DUMPED_VECTOR_DIR + 'mb_voc_embeddings.pkl') # model cnn model_atn = Sequential() model_atn.add(Embedding(max_features, embedding_dims, input_length=max_len, weights=[embedding_weights], trainable=True)) model_atn.add(Bidirectional(GRU(200, return_sequences=True), name='bidirectional')) model_atn.add(TimeDistributed(Dense(200), name='time_dist')) model_atn.add(AttLayer(name='att')) model_feature_vec = Sequential() model_feature_vec.add(Dense(200, input_dim=N_Features, init='normal', activation='relu')) model_feature_vec.add(Dense(100, init='normal', activation='relu')) model_feature_vec.add(Dropout(0.2)) model_feature_vec.add(Dense(50, init='normal', activation='relu')) model_feature_vec.add(Dense(10, init='normal', activation='relu')) # functional API sentence = Input(shape=(max_len,), dtype='float32', name='w1') embedding_layer = Embedding(input_dim=max_features, output_dim=embedding_dims, weights=[embedding_weights], ) sentence = Input(shape=(max_len,), dtype='float32', name='w1') embedding_layer = Embedding(input_dim=max_features, output_dim=embedding_dims, weights=[embedding_weights], ) sentence_emb = embedding_layer(sentence) # dropout_1 = Dropout(0.2, name='emb_dropout') # sentence_drop = dropout_1(sentence_emb) cnn_layers = [Convolution1D(filter_length=filter_length, nb_filter=512, activation='relu', border_mode='same') for filter_length in [1, 2, 3, 5]] merged_cnn = concatenate([cnn(sentence_emb) for cnn in cnn_layers], axis=-1) # pooling_layer = MaxPooling1D(2, name='maxpool')(merged_cnn) attention = AttLayer(name='att')(merged_cnn) # flatten_layer = Flatten()(attention) cnn_model = Dense(128, init='normal', activation='relu')(attention) model_cnn = Model(input=[sentence], output=[cnn_model], name='cnn_model') # model_cnn = Sequential() # model_cnn.add(Embedding(max_features, # embedding_dims, # input_length=max_len, # weights=[embedding_weights], # trainable=True)) # model_cnn.add(Conv1D(512, 3, activation='relu', name='cnn1')) # model_cnn.add(Conv1D(512, 4, activation='relu', name='cnn2')) # model_cnn.add(Conv1D(512, 5, activation='relu', name='cnn3')) # model_cnn.add(MaxPooling1D(2, name='maxpool')) # model_cnn.add(Flatten()) # model_cnn.add(Dense(128, activation='relu')) merged_layer = Sequential() merged_layer.add(Merge([model_atn, model_feature_vec, model_cnn], mode='concat', concat_axis=1, name='merge_layer')) merged_layer.add(Reshape((1, 338))) merged_layer.add(Bidirectional(GRU(200, return_sequences=True), name='bidirectional_2')) merged_layer.add(TimeDistributed(Dense(50), name='time_dist')) merged_layer.add(AttLayer(name='att')) merged_layer.add(Dense(1, init='normal', name='combined_dense', activation='tanh')) # # Compile model merged_layer.compile(loss='mae', optimizer='adam') print(merged_layer.summary()) return merged_layer
# passage_net.add(Dropout(0.5)) # question_net.add(Dense(1, activation='sigmoid')) question_net = Sequential() question_net.add(q_embedding_layer) question_net.add(Activation('tanh')) question_net.add(Dropout(0.1)) # question_net.add(Bidirectional(LSTM(MAX_PASSAGE_LENGTH, return_sequences=True))) # question_net.add(GRU(EMBEDDING_DIM, return_sequences=True)) # question_net.add(LSTM(EMBEDDING_DIM, return_sequences=True)) print("question layer shape:", question_net.layers[-1].output_shape) # question_net.add(Dense(1, activation='sigmoid')) plot(question_net, to_file='question_net.png', show_shapes=True) merged = Merge([passage_net, question_net], mode='dot') # merged = Merge([passage_net, question_net], mode='cos') print("merged layer shape:", question_net.layers[-1].output_shape) model = Sequential() model.add(merged) # model.add(MyLayer(400)) # model.add(Reshape((1, 400, 400))) # model.add(Permute((0, 2, 1))) # model.add(MaxPooling2D(pool_size=(1, 25), border_mode='valid')) # model.add(AveragePooling2D(pool_size=(1, 4), border_mode='valid')) # model.add(Permute((0, 2, 1))) model.add(Permute((2, 1))) model.add(MaxPooling1D(pool_length=25, stride=None, border_mode='valid'))
branch_4.add(MaxPooling1D(pool_size=2)) branch_4.add(Dropout(0.2)) branch_4.add(BatchNormalization()) branch_4.add(LSTM(100)) branch_5 = Sequential() branch_5.add(input_layer) branch_5.add(Conv1D(filters=32, kernel_size=5, padding='same')) branch_5.add(Activation('relu')) branch_5.add(MaxPooling1D(pool_size=2)) branch_5.add(Dropout(0.2)) branch_5.add(BatchNormalization()) branch_5.add(LSTM(100)) model = Sequential() model.add(Merge([branch_3, branch_4, branch_5], mode='concat')) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print("") # -+-+-+-+-+-+-+- TRAINING MODEL -+-+-+-+-+-+-+- print("RUNNING MODEL") extra_hist = ExtraHistory() start_time = time.time() hist = model.fit(np.vstack((X_train, X_test)), np.hstack((y_train, y_test)), validation_split=0.5, epochs=inputs.num_epochs,
def trainCNN(x_train, y_train, x_val, y_val, word_index, embeddings_index): nb_words = min(MAX_NB_WORDS, len(word_index)) embedding_matrix = np.zeros((nb_words + 1, EMBEDDING_DIM)) for word, i in word_index.items(): if i > MAX_NB_WORDS: continue embedding_vector = embeddings_index.get(word) if embedding_vector is not None: # words not found in embedding index will be all-zeros. embedding_matrix[ i] = embedding_vector # word_index to word_embedding_vector ,<20000(nb_words) # load pre-trained word embeddings into an Embedding layer # 神经网路的第一层,词向量层,本文使用了预训练glove词向量,可以把trainable那里设为False embedding_layer = Embedding(nb_words + 1, EMBEDDING_DIM, input_length=MAX_SEQUENCE_LENGTH, weights=[embedding_matrix], trainable=False) print('Training model.') # train a 1D convnet with global maxpoolinnb_wordsg # left model 第一块神经网络,卷积窗口是5*dim(dim是词向量维度) model_left = Sequential() # model.add(Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')) model_left.add(embedding_layer) model_left.add(Conv1D(128, 5, padding="SAME", activation='relu')) model_left.add(MaxPooling1D(2)) model_left.add(Conv1D(128, 5, padding="SAME", activation='relu')) model_left.add(MaxPooling1D(2)) model_left.add(Conv1D(128, 5, padding="SAME", activation='relu')) model_left.add(MaxPooling1D(10)) model_left.add(Flatten()) print(model_left.summary()) # right model <span style="font-family: Arial, Helvetica, sans-serif;">第二块神经网络,卷积窗口是4*50</span> model_right = Sequential() model_right.add(embedding_layer) model_right.add(Conv1D(128, 4, padding="SAME", activation='relu')) model_right.add(MaxPooling1D(2)) model_right.add(Conv1D(128, 4, padding="SAME", activation='relu')) model_right.add(MaxPooling1D(2)) model_right.add(Conv1D(128, 4, padding="SAME", activation='relu')) model_right.add(MaxPooling1D(10)) model_right.add(Flatten()) # third model <span style="font-family: Arial, Helvetica, sans-serif;">第三块神经网络,卷积窗口是6*50</span> model_3 = Sequential() model_3.add(embedding_layer) model_3.add(Conv1D(128, 3, padding="SAME", activation='relu')) model_3.add(MaxPooling1D(2)) model_3.add(Conv1D(128, 3, padding="SAME", activation='relu')) model_3.add(MaxPooling1D(2)) model_3.add(Conv1D(128, 3, padding="SAME", activation='relu')) model_3.add(MaxPooling1D(10)) model_3.add(Flatten()) merged = Merge( [model_left, model_right, model_3], mode='concat' ) # 将三种不同卷积窗口的卷积层组合 连接在一起,当然也可以只是用三个model中的一个,一样可以得到不错的效果,只是本文采用论文中的结构设计 model = Sequential() model.add(merged) # add merge model.add(Dropout(0.5)) #model.add(Dense(128, activation='tanh')) # 全连接层 model.add(Dense(5, activation='softmax')) # softmax,输出文本属于20种类别中每个类别的概率 opt = keras.optimizers.Adadelta(lr=0.5, rho=0.95, epsilon=1e-06) # 优化器我这里用了adadelta,也可以使用其他方法 model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) print(model.summary()) # =下面开始训练,nb_epoch是迭代次数,可以高一些,训练效果会更好,但是训练会变慢 model.fit(x_train, y_train, batch_size=48, nb_epoch=15, validation_data=(x_val, y_val)) score = model.evaluate(x_train, y_train, verbose=0) # 评估模型在训练集中的效果,准确率约99% print('train score:', score[0]) print('train accuracy:', score[1]) score = model.evaluate(x_val, y_val, verbose=0) # 评估模型在测试集中的效果,准确率约为97%,迭代次数多了,会进一步提升 print('Test score:', score[0]) print('Test accuracy:', score[1])
def lstm_model(sequences_length_for_training, embedding_dim, embedding_matrix, vocab_size): which_model = 2 # Look back is equal to -INF # This model creates a Stateful LSTM with lookback of the whole document # Input should be of the format (TOTAL_DOCUMENTS, TOTAL_SEQUENCES, SEQUENCE_DIM) # Also train using the custom trainer # Convolution layers print "Building Convolution layers" ngram_filters = [1, 2, 3, 4, 5] conv_hidden_units = [300, 300, 300, 300, 300] print 'Build MAIN model...' # pdb.set_trace() main_input = Input(shape=(SEQUENCES_LENGTH_FOR_TRAINING, embedding_dim), dtype='float32', name='main_input') embedded_input = TimeDistributed( Embedding(vocab_size + 1, GLOVE_EMBEDDING_DIM, weights=[embedding_matrix], input_length=embedding_dim, init='uniform'))(main_input) convs = [] for n_gram, hidden_units in zip(ngram_filters, conv_hidden_units): conv = TimeDistributed( Convolution1D(nb_filter=hidden_units, filter_length=n_gram, border_mode='same', activation='relu'))(embedded_input) flattened = TimeDistributed(Flatten())(conv) # pool = GlobalMaxPooling1D()(conv) convs.append(flattened) convoluted_input = Merge(mode='concat')(convs) CONV_DIM = sum(conv_hidden_units) # Dropouts for LSTMs can be merged # ForLSTM_stateful = LSTM(300, batch_input_shape=(1, embedding_dim, CONV_DIM), return_sequences=False, stateful=True)(convoluted_input) # RevLSTM_stateful = LSTM(300, batch_input_shape=(1, embedding_dim, CONV_DIM), return_sequences=False, stateful=True, go_backwards=True)(convoluted_input) # BLSTM_stateful = merge([ForLSTM_stateful, RevLSTM_stateful], mode='concat') # BLSTM_stateful = merge([Dropout(0.3)(ForLSTM_stateful), Dropout(0.3)(RevLSTM_stateful)], mode='concat') # BLSTM_prior = Dense(1)(BLSTM_stateful) # encoded = Bidirectional(LSTM(512, input_shape=(SEQUENCES_LENGTH_FOR_TRAINING, CONV_DIM), return_sequences=True, stateful=False), merge_mode='concat')(convoluted_input) # decoded = Bidirectional(LSTM(512, input_shape=(SEQUENCES_LENGTH_FOR_TRAINING, CONV_DIM), return_sequences=True, stateful=False), merge_mode='concat')(encoded) encoded = Bidirectional(LSTM(512, input_shape=(SEQUENCES_LENGTH_FOR_TRAINING, CONV_DIM), return_sequences=True, stateful=False), merge_mode='concat')(convoluted_input) encoded_drop = Dropout(0.3)(encoded) more_encoded = Bidirectional(LSTM(512), merge_mode='concat')(encoded_drop) more_encoded_drop = Dropout(0.3)(more_encoded) encoded_input = RepeatVector(SEQUENCES_LENGTH_FOR_TRAINING)(more_encoded) decoded = Bidirectional(LSTM(512, input_shape=(SEQUENCES_LENGTH_FOR_TRAINING, CONV_DIM), return_sequences=True, stateful=False), merge_mode='concat')(encoded_input) decoded_drop = Dropout(0.3)(decoded) dense_out = Dense(300)(decoded_drop) dense_out_drop = Dropout(0.3)(dense_out) output = TimeDistributed(Dense(2, activation='sigmoid'))(dense_out_drop) model = Model(input=[main_input], output=output) model.layers[1].trainable = False model.compile(loss=w_binary_crossentropy, optimizer='rmsprop', metrics=['accuracy', 'recall']) print model.summary() return model