示例#1
0
def train_model(model, training_gen, test_gen, loss, epochs, vec_output, save_period=1):
    print(model.summary())
    model.compile(optimizer=SGD(), loss=loss, metrics=['accuracy', custom_true_positive_metric(vec_output),
                                                       custom_false_positive_metric(vec_output)])
    fname = '../models/model.{epoch:03d}--ACC_{val_acc:.4f}--FP_{val_false_positive_metric:.4f}--' \
            'TP_{val_true_positive_metric:.4f}.hdf5'
    model_checkpoint_tp = ModelCheckpoint(fname, monitor='true_positive_metric', save_best_only=True, mode='max',
                                          period=save_period)
    model_checkpoint_fn = ModelCheckpoint(fname, monitor='val_false_positive_metric', save_best_only=True, mode='min',
                                          period=save_period)
    model.fit_generator(generator=training_gen, epochs=epochs, class_weight={0:1, 1:6.5}, validation_data=test_gen,
                        max_queue_size=10, callbacks=[model_checkpoint_tp, model_checkpoint_fn])
示例#2
0
def get_model(final_activation='softmax'):
    model = Sequential()
    model.add(
        Bidirectional(LSTM(units=no_lstm_units),
                      input_shape=(token_per_gadget, embedding_dim)))
    model.add(Dropout(dropout_proba))
    model.add(Dense(2, activation=final_activation))
    model.compile(optimizer='adam',
                  loss=loss,
                  metrics=[
                      'accuracy',
                      custom_true_positive_metric(vec_output),
                      custom_false_positive_metric(vec_output)
                  ])
    return model
示例#3
0
def train_network(batch_size, epochs, loss, optimizer, vec_output,
                  final_nonlinearity, random_seed):
    if not os.path.isdir('models'):
        os.makedirs('models')
    x_train, x_test, y_train, y_test, filenames_test = get_train_data_test_data(
        random_seed)
    # np.save('data_mimicus/test_data/float_encoded/test_data.npy', x_test)
    # np.save('data_mimicus/test_data/float_encoded/test_labels.npy', y_test)
    no_features = x_train.shape[1]
    model = get_network(no_features, final_nonlinearity, vec_output)
    fname = 'models/model.{epoch:03d}--ACC_{val_accuracy:.4f}--FP_{val_false_positive_metric:.4f}--' \
            'TP_{val_true_positive_metric:.4f}.hdf5'
    model_checkpoint_fp = ModelCheckpoint(fname,
                                          monitor='val_false_positive_metric',
                                          save_best_only=True,
                                          mode='min')
    model_checkpoint_tp = ModelCheckpoint(fname,
                                          monitor='val_true_positive_metric',
                                          save_best_only=True,
                                          mode='max')
    model.compile(
        optimizer,
        loss,
        metrics=[
            'accuracy',
            custom_true_positive_metric(vec_output),
            custom_false_positive_metric(vec_output),
        ],
    )
    print(model.summary())
    model.fit(x_train,
              y_train,
              batch_size,
              epochs,
              validation_data=(x_test, y_test),
              verbose=2,
              callbacks=[model_checkpoint_tp, model_checkpoint_fp])
    get_statistics(model, x_test, y_test)
示例#4
0
def train_network_batchwise(data_path, network, no_epochs, batch_size, testset_size, random_state=42):
    if not os.path.isdir('models'):
        os.makedirs('models')
    network.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy',
                                                                              custom_true_positive_metric(vec_output),
                                                                              custom_false_positive_metric(vec_output)
                                                                              ])
    filenames_sorted = get_sorted_list_of_filenames(data_path)
    names_train, names_test = train_test_split(filenames_sorted, test_size=testset_size, random_state=random_state)
    for j in range(no_epochs):
        acc_train, acc_test = [], []
        print('Training epoch {}'.format(j+1))
        for i in tqdm(range(0, len(names_train), batch_size)):
            x, y = filename_list_to_numpy_arrays(names_train[i:i+batch_size], data_path)
            res = network.train_on_batch(pad_sequences(x, len(x[-1]), dtype='uint8'), y)
            acc_train.append(res[1])
        print('Train accuracy after {} epochs: {}:'.format(j+1, np.mean(acc_train)))
        for k in range(0, len(names_test), batch_size):
            x, y = filename_list_to_numpy_arrays(names_test[k:k+batch_size], data_path)
            res = network.test_on_batch(pad_sequences(x, len(x[-1]), dtype='uint8'), y)
            acc_test.append(res[1])
        print('Test accuracy after {} epochs: {}:'.format(j+1, np.mean(acc_test)))
        network.save('models/damd_model_%d' % j)