Exemplo n.º 1
0
def train_new():
    df = pd.DataFrame(request.json)

    # We use a global here so we can modify the model and list of column names
    global model_columns, model
    model_columns, model = model_utils.train(df)
    joblib.dump(model_columns, model_utils.MODEL_COLUMNS_FILE_NAME)
    joblib.dump(model, model_utils.MODEL_FILE_NAME)

    return 'Success'
Exemplo n.º 2
0
def train():
    df = pd.read_csv(TRAINING_FILE_PATH)

    # We use a global here so we can modify the model and list of column names
    global model_columns, model
    model_columns, model = model_utils.train(df)
    joblib.dump(model_columns, model_utils.MODEL_COLUMNS_FILE_NAME)
    joblib.dump(model, model_utils.MODEL_FILE_NAME)

    return 'Success'
Exemplo n.º 3
0
def train_model_without_file():
    print('training model...wait')
    df = pd.read_csv(TRAINING_FILE_PATH)
    global model_columns, model
    model_columns, model = model_utils.train(df)
    #joblib.dump(model_columns, model_utils.MODEL_COLUMNS_FILE_NAME)
    #joblib.dump(model, model_utils.MODEL_FILE_NAME)
    pickle.dump(model, open(model_utils.MODEL_FILE_NAME, 'wb'))
    pickle.dump(model_columns, open(model_utils.MODEL_COLUMNS_FILE_NAME, 'wb'))
    return 'success: from train_model_without_file'
Exemplo n.º 4
0
def train_model():
    print('training model...waiting for data')
    try:
        df= pd.DataFrame(request.json)
    except Exception as e:
        return jsonify({'error': str(e), 'trace': traceback.format_exc()})
    
    global model_columns, model
    model_columns, model = model_utils.train(df)
    pickle.dump(model, open(model_utils.MODEL_FILE_NAME, 'wb'))
    pickle.dump(model_columns, open(model_utils.MODEL_COLUMNS_FILE_NAME, 'wb'))
    
    return 'success:from train_model'
Exemplo n.º 5
0
def train():
    df = pd.read_csv(TRAINING_FILE_PATH)

    # We use a global here so we can modify the model and list of column names
    global model_columns, model
    model_columns, model = model_utils.train(df)
    
    # save the model

    # serialize model to JSON
    model_json = model.to_json()
    with open(model_utils.MODEL_FILE_NAME + ".json", "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(model_utils.MODEL_FILE_NAME + ".h5")
    print("Saved model to disk")

    return jsonify({'success': 'Model training successful'})
]

# Define optimizer
optimizer = AdamW(grouped_model_parameters)

# Place model & loss function on GPU
model, criterion = model.to(DEVICE), criterion.to(DEVICE)

# Start actual training, check test loss after each epoch
best_test_loss = float('inf')
for epoch in range(NUM_EPOCHS):
    print("EPOCH NO: %d" % (epoch + 1))

    train_loss, train_acc = train(model=model,
                                  iterator=train_loader,
                                  criterion=criterion,
                                  optimizer=optimizer,
                                  device=DEVICE,
                                  include_bert_masks=True)

    test_loss, test_acc = test(model=model,
                               iterator=test_loader,
                               criterion=criterion,
                               device=DEVICE,
                               include_bert_masks=True)

    if test_loss < best_test_loss:
        best_test_loss = test_loss
        torch.save(model.state_dict(), 'saved_models/finetuned-bert-model.pt')

    print(f'\tTrain Loss: {train_loss:.3f} | Train Accuracy: {train_acc * 100:.2f}%')
    print(f'\tTest Loss:  {test_loss:.3f} | Test Accuracy:  {test_acc * 100:.2f}%')