Skip to content

Instantly share code, notes, and snippets.

View aagnone3's full-sized avatar
😸
so make it

Anthony Agnone aagnone3

😸
so make it
View GitHub Profile
@aagnone3
aagnone3 / fast-api-sentiment-dockerfile
Created August 22, 2020 21:45
fast-api-sentiment-dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
# System-related
WORKDIR /app
COPY . .
# Install package
RUN pip3 install --upgrade pip && \
python3 setup.py install
@aagnone3
aagnone3 / fastapi-sentiment-routes.py
Last active August 22, 2020 21:42
fastapi-sentiment-routes
from fastapi import FastAPI
from fastapi_sentiment import sentiment_analysis
from fastapi_sentiment.models import (
SentimentDocument,
SentimentScoring,
Message
)
app = FastAPI()
@aagnone3
aagnone3 / hummingbird-deepnote_11.py
Created July 7, 2020 17:04
hummingbird-deepnote_11.py
# 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.
@aagnone3
aagnone3 / hummingbird-deepnote_10.py
Created July 7, 2020 17:04
hummingbird-deepnote_10.py
%%timeit
clf_gpu.predict(X_test)
@aagnone3
aagnone3 / hummingbird-deepnote_9.py
Created July 7, 2020 17:04
hummingbird-deepnote_9.py
%%timeit
clf_cpu.predict(X_test)
@aagnone3
aagnone3 / hummingbird-deepnote_8.py
Created July 7, 2020 17:04
hummingbird-deepnote_8.py
%%timeit
clf.predict(X_test)
@aagnone3
aagnone3 / hummingbird-deepnote_7.py
Created July 7, 2020 17:04
hummingbird-deepnote_7.py
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)
@aagnone3
aagnone3 / hummingbird-deepnote_6.py
Created July 7, 2020 17:04
hummingbird-deepnote_6.py
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:
@aagnone3
aagnone3 / hummingbird-deepnote_5.py
Created July 7, 2020 17:04
hummingbird-deepnote_5.py
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)
@aagnone3
aagnone3 / hummingbird-deepnote_4.py
Created July 7, 2020 17:04
hummingbird-deepnote_4.py
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, )
"""