Exemplo n.º 1
0
def fit():
    # X_file = request.files['X']
    # y_file = request.files['y']
    if not request.is_json:
        return "NO JSON!"
    data = request.json
    X = np.array(data['X'])
    y = np.array(data['y'])
    # X = np.array(json.loads(X_file.read().decode('utf-8')))
    # y = np.array(json.loads(y_file.read().decode('utf-8')))

    model = RegressionModel()
    model.fit(X, y)
    model_bytes = pickle.dumps(model)

    model_ddb.add_model_to_db(dynamodb,
                              models_table_name,
                              model_bytes)

    data_ddb.add_fit_data_to_db(dynamodb,
                                data_table_name,
                                pickle.dumps(X),
                                pickle.dumps(y))

    request_url = request.url
    request_ddb.add_request_to_db(dynamodb,
                                  requests_table_name,
                                  request_url)

    return 'ok'
Exemplo n.º 2
0
def main():

    num_inducing = args.num_inducing
    data_name = args.fname
    prop = args.prop
    n_layers = args.layers

    with open("settings-uci.pkl", "rb") as file:
        settings = pickle.load(file)

    data = pd.read_csv('./datasets/{}.csv'.format(data_name),
                       header=None).values
    key = data_name + "-{}".format(n_layers)
    try:
        adam_lr = [settings[key][0], settings[key][1], settings[key][2]]
        max_iter = settings[key][3]
    except:
        adam_lr = [0.005, 0.0001, 0.0025]
        max_iter = 20000

    if data_name == "energy":
        X_full = data[:, :-2]
        Y_full = data[:, -2:-1]
    else:
        X_full = data[:, :-1]
        Y_full = data[:, -1:]

    N = X_full.shape[0]
    n = int(N * prop)

    np.random.seed(0)
    ind = np.arange(N)

    np.random.shuffle(ind)
    train_ind = ind[:n]
    test_ind = ind[n:]

    X = X_full[train_ind]
    Xs = X_full[test_ind]
    Y = Y_full[train_ind]
    Ys = Y_full[test_ind]

    X_mean = np.mean(X, 0)
    X_std = np.std(X, 0)
    Y_std = np.std(Y, 0)
    X = (X - X_mean) / X_std
    Xs = (Xs - X_mean) / X_std
    Y_mean = np.mean(Y, 0)
    Y = (Y - Y_mean) / Y_std
    Ys = (Ys - Y_mean) / Y_std

    model = RegressionModel(adam_lr, max_iter, n_layers, num_inducing)
    model.fit(X, Y, Xs, Ys, Y_std)
Exemplo n.º 3
0
def main(X_filename='data/X_train.json',
         y_filename='data/y_train.json',
         model_filename='regression_model.pkl'):
    """
    Init RegressionModel object and call `fit` method with corresponding arguments and save it as `pickle`.

    **Parameters**:

    - `X_filename`: filename where object-feature matrix is saved
    - `y_filename`: filename of `y` values form `f(X) = y`
    - `model_filename`: name of file to save model
    """

    X = np.array(json.load(codecs.open(X_filename, 'r', encoding='utf-8')))
    y = np.array(json.load(codecs.open(y_filename, 'r', encoding='utf-8')))

    reg_model = RegressionModel()
    reg_model.fit(X, y)

    with open(model_filename, 'wb') as f:
        pickle.dump(reg_model, f)