()
模型训练，将训练集分为9份，9折交叉验证，分别按照loss和f1保存模型。
    :return:
模型训练，将训练集分为9份，9折交叉验证，分别按照loss和f1保存模型。
    :return:
模型训练，将训练集分为9份，9折交叉验证，分别按照loss和f1保存模型。 : return :
def train():
    '''
    模型训练，将训练集分为9份，9折交叉验证，分别按照loss和f1保存模型。
    :return:
    '''
    dataset = get_input('data/ER_input_train_match.pkl')
    train=dataset
    kfold = KFold(n_splits=9, shuffle=False)
    for i, (tra_index, val_index) in enumerate(kfold.split(train[0])):
        K.clear_session()
        input_train = [tra[tra_index] for tra in train]
        input_val = [tra[val_index] for tra in train]
        filepath_loss = "model/ER_match_bert_loss.h5_"+str(i)
        filepath_f1 = "model/ER_match_bert_f1.h5_"+str(i)
        evaluate = Evaluate((input_val[:-1], input_val[-1]), filepath_f1)
        checkpoint = ModelCheckpoint(filepath_loss, monitor='val_loss', verbose=2, save_best_only=True,save_weights_only=True, mode='min')
        earlystopping = EarlyStopping(monitor='val_loss', min_delta=0.000001, patience=2, verbose=3, mode='auto')
        lrate = LearningRateScheduler(step_decay, verbose=2)
        callbacks = [checkpoint,evaluate,lrate,earlystopping]
        model = link_model()
        print(model.summary())
        logging.debug(str(i))
        logging.debug(str(i)*30)
        model = model.fit(input_train[:-1], input_train[-1], batch_size=arg.batch_size, epochs=arg.num_epochs,
                          validation_data=(input_val[:-1], input_val[-1]), verbose=1, callbacks=callbacks)
        logging.debug(arg.__str__())
        logging.debug(model.history)
        logging.debug(np.min(model.history['val_loss']))
def train ( ) : dataset = get_input ( 'data/ER_input_train_match.pkl' ) train = dataset kfold = KFold ( n_splits = 9 , shuffle = False ) for i , ( tra_index , val_index ) in enumerate ( kfold . split ( train [ 0 ] ) ) : K . clear_session ( ) input_train = [ tra [ tra_index ] for tra in train ] input_val = [ tra [ val_index ] for tra in train ] filepath_loss = "model/ER_match_bert_loss.h5_" + str ( i ) filepath_f1 = "model/ER_match_bert_f1.h5_" + str ( i ) evaluate = Evaluate ( ( input_val [ : - 1 ] , input_val [ - 1 ] ) , filepath_f1 ) checkpoint = ModelCheckpoint ( filepath_loss , monitor = 'val_loss' , verbose = 2 , save_best_only = True , save_weights_only = True , mode = 'min' ) earlystopping = EarlyStopping ( monitor = 'val_loss' , min_delta = 0.000001 , patience = 2 , verbose = 3 , mode = 'auto' ) lrate = LearningRateScheduler ( step_decay , verbose = 2 ) callbacks = [ checkpoint , evaluate , lrate , earlystopping ] model = link_model ( ) print ( model . summary ( ) ) logging . debug ( str ( i ) ) logging . debug ( str ( i ) * 30 ) model = model . fit ( input_train [ : - 1 ] , input_train [ - 1 ] , batch_size = arg . batch_size , epochs = arg . num_epochs , validation_data = ( input_val [ : - 1 ] , input_val [ - 1 ] ) , verbose = 1 , callbacks = callbacks ) logging . debug ( arg . __str__ ( ) ) logging . debug ( model . history ) logging . debug ( np . min ( model . history [ 'val_loss' ] ) )