Skip to content

Instantly share code, notes, and snippets.

@SuyogSoti
Last active January 12, 2020 17:09
Show Gist options
  • Save SuyogSoti/2843ebeb89f02ca68e2f0438aa8217b9 to your computer and use it in GitHub Desktop.
Save SuyogSoti/2843ebeb89f02ca68e2f0438aa8217b9 to your computer and use it in GitHub Desktop.
Brainiac Install Doc

For Data Scientist

  1. Create an account at Libnosis.com

  2. Set the following environment variables so that you can call the package function on your trained/partially trained/untrained model:

    export BRAINIAC_CLIENT_USERNAME="username"
    export BRAINIAC_CLIENT_PASSWORD="password"
  3. Install brainiac_client for python with pip install --user brainiac_client --upgrade

  4. Pass the model into the package function, with a project name. An example with a model to predict the housing prices in Boston is shown below.

    # #############################################################################
    # Import necessary libraries
    import numpy as np
    
    from sklearn import ensemble
    from sklearn import datasets
    from sklearn.model_selection import train_test_split
    from sklearn.utils import shuffle
    from typing import Dict
    
    from brainiac_client.scientist import package
    
    # #############################################################################
    # Load data
    boston = datasets.load_boston()
    X, y = shuffle(boston.data, boston.target, random_state=13)
    X = X.astype(np.float32)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
    print("train => ({},{})".format(np.shape(X_train), np.shape(y_train)))
    print("test => ({},{})".format(np.shape(X_test), np.shape(y_test)))
    
    # #############################################################################
    # Fit regression model
    params = {
        'n_estimators': 500,
        'max_depth': 4,
        'min_samples_split': 2,
        'learning_rate': 0.01,
        'loss': 'ls'
    }
    clf = ensemble.GradientBoostingRegressor(**params)
    
    clf.fit(X_train, y_train)
    
    # #############################################################################
    # Create a wrapper object to control inputs
    
    class HousingModel:
        def train(self, body: Dict):
            clf.fit(body["x"], body["y"])
    
        def score(self, body: Dict):
            return clf.score(body["x"], body["y"])
    
        def predict(self, body: Dict):
            return clf.predict(body["x"])
    
    # #############################################################################
    # Upload Model to Brainiac
    
    model = HousingModel()
    response = package(model, "housing")
    print("Response Status Code:", response.status_code) # Check that the upload succeeded
  5. Please note that that housing model can have any function the data scientist wants. The only requirement is that it takes in a dictionary called body. This dictionary's only requirement is that it must be protobuf serializable. To be more precise, the body is a google.protobuf.Struct. In a future version, it will just be a regular dictionary.

  6. The data scientist can access events related to his models at libnosis.com/api/model/event. More types of events will be collected in the future and the data scientists will be given a way to decide what analytics to collect later. If the analytics are an important issue to you and you would like to have a chat about it, please let us know. 😄

For Clients

  1. Create an account at Libnosis.com

  2. Set the following environment variables so that you can call the package function on your trained/partially trained/untrained model:

    export BRAINIAC_CLIENT_USERNAME="username"
    export BRAINIAC_CLIENT_PASSWORD="password"
  3. Install brainiac_client for python with pip install --user brainiac_client --upgrade

  4. Create a brainiac client for the project you would like to use. Then you can call whatever method is defined by the data scientist on that client. An example for how to use the housing model created by the data scientist above is shown below.

    # #############################################################################
    # Import libraries
    from brainiac_client.client import BrainiacClient
    import numpy as np
    
    # #############################################################################
    # Load Data
    testData = np.load("./test_data.npz")
    x_test = testData.get("X_test")
    y_test = testData.get("y_test")
    
    # #############################################################################
    # Use Brainiac
    brainiac = BrainiacClient()
    try:
        model = brainiac.get_model_client("suyogsoti", "housing")
        response = model.score({"x": x_test.tolist(), "y": y_test.tolist()})
    except Exception as e:
        print("EXCEPTION:", e)
  5. Note: get_model_client does make a network request in an attempt to cache the model.

  6. The response for the function actually called, score in this case, is defined with the following protobuf structure.

    message Status {
      int32 status_code = 1;
      string status_reason = 2;
    }
    
    message BrainiacResponse {
      Status status = 1;
      google.protobuf.Struct body = 2;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment