Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
linenumberstrue
def train_model(model, train_data, validation_data, max_epoches, batch_size, environment):
    # train model (e.g. until max_epoches are reached or validation loss increases)
    model.train(train_data, validation_data, max_epoches, batch_size)
    # store model parameters within database
    post_data = {
        "algorithm_run_id": environment['runtime']['run_id'],
        "config_data": model.get_parameters(),
        "description": ""
    }
    response = requests.post('http://localhost:8004/algoconfig/%s' % environment['algorithm']['cfg_name'], json=post_data).json()
    if response['has_error'] == True:
        raise ValueError('An error occurred while storing the algorithm\'s configuration:\n%s' % response['error'])
    return response['data']
 
def test_model(model, test_data, environment):
    # test model (e.g. predict the test_data)
    model.run(test_data)
    # store predictions within database
    prediction_data = []
    for prediction in model.get_predictions():
        prediction_data.append({
            "time_start": prediction.['time_start'],
            "time_duration": prediction.['time_duration'],
            "probability": prediction.['probability'],
            "intensity_min": prediction.['intensity_min'],
            "intensity_max": prediction.['intensity_max'],
            "meta": {
                "harp": prediction.['harp'],
                "nar": prediction.['nar']
            },
            "data": prediction.['data']
        })
    post_data = {
        "algorithm_config": environment['algorithm']['cfg_name'],
        "algorithm_run_id": environment['runtime']['run_id'],
        "prediction_data": prediction_data,
        "source_data": [get_fc_id(row) for row in test_data]
    }

    response = requests.post('http://localhost:8004/predictionset', json=post_data).json()
    if response['has_error'] == True:
        raise ValueError('An error occurred while storing the algorithm\'s prediction set:\n%s' % response['error'])
    return response['data']

The post_data structure is equivalent to the algorithm_config_data or prediction_data definitions as given by the routes /algoconfig/{name} and /prediction/bulk:

Image Modified

Image Modified

  • Every algorithm_configuration_data needs at least an algorithm_run_id and
    config_data attribute.
  • Every specific configuration value has to be added to the config_data attribute and has
    to be a key-value pair.
  • Every prediction_set needs at least an algorithm_configuration,
    algorithm_run_id,
    source_data and prediction_data attribute.
  • The prediction_data represents a list of predictions, where each prediction needs
    at
    least a time_start, time_duration, probability, intensity_min, intensity_max
    and data
    attribute.
  • Every specific prediction value has to be added to the data attribute and has to be a
    key-value pair.

Source Code

...