This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7 | |
# System-related | |
WORKDIR /app | |
COPY . . | |
# Install package | |
RUN pip3 install --upgrade pip && \ | |
python3 setup.py install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fastapi import FastAPI | |
from fastapi_sentiment import sentiment_analysis | |
from fastapi_sentiment.models import ( | |
SentimentDocument, | |
SentimentScoring, | |
Message | |
) | |
app = FastAPI() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get predictions from the models for the same data | |
y_pred = clf.predict(X_test) | |
y_pred_cpu = clf_cpu.predict(X_test) | |
y_pred_gpu = clf_gpu.predict(X_test) | |
def plot_distributions(data_label_pairs, title='Score Distribution(s)'): | |
""" | |
Plot the distribution of a list of prediction vectors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%timeit | |
clf_gpu.predict(X_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%timeit | |
clf_cpu.predict(X_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%timeit | |
clf.predict(X_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('Creating CPU Hummingbird model\n\t', end='') | |
clf_cpu = compile_pytorch(clf, force_cpu=True) | |
print('Creating GPU Hummingbird model\n\t', end='') | |
clf_gpu = compile_pytorch(clf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compile_pytorch(model, force_cpu=False): | |
""" | |
Use Hummingbird to compile a PyTorch model, moving it to a GPU if available. | |
@param Model model: trained ML model which has a predict() method for inference. | |
@param bool force_cpu: whether to keep the model on CPU, regardless of the presence of a GPU. | |
@return Model: augmented ML model, which uses tensor computations to perform inference | |
""" | |
model = convert(clf, 'pytorch', extra_config={"tree_implementation":"gemm"}) | |
if torch.cuda.is_available() and not force_cpu: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X_train, y_train = make_data(n_samples=1e3) | |
X_test, y_test = make_data(n_samples=1e4) | |
clf = RandomForestRegressor(n_estimators=250, max_depth=10) | |
clf.fit(X_train, y_train) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_data(n_samples=1000): | |
""" | |
Return some synthetic classification data from sklearn.datasets. | |
@param int n_samples: number of samples the data set should have. | |
@param int n_features: number of features the data set should have. | |
@return tuple: | |
[0]: numpy.ndarray data set of shape (n_samples, n_features) | |
[1]: numpu.ndarray labels of shape (n_samples, ) | |
""" |