Skip to content

Commit 5747439

Browse files
gustavocidornelaswhoseoyster
authored andcommitted
added tensorflow example
1 parent 2feeeeb commit 5747439

File tree

1 file changed

+158
-5
lines changed

1 file changed

+158
-5
lines changed

unboxapi/models.py

Lines changed: 158 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def fasttext(self) -> str:
5757
... description='this is my fasttext model',
5858
... )
5959
>>> model.to_dict()
60-
6160
"""
6261
return "FasttextModelArtifact"
6362

@@ -235,12 +234,168 @@ def sklearn(self) -> str:
235234

236235
@property
237236
def pytorch(self) -> str:
238-
"""For models built with `PyTorch <https://pytorch.org/>`_."""
237+
"""For models built with `PyTorch <https://pytorch.org/>`_.
238+
239+
Examples
240+
--------
241+
.. seealso::
242+
Our `sample notebooks <https://github.com/unboxai/unboxapi-python-client/tree/cid/api-docs-improvements/examples/text-classification/tensorflow>`_ and
243+
`tutorials <https://unbox.readme.io/docs/overview-of-tutorial-tracks>`_.
244+
245+
Let's say you have trained a ``torch`` model that performs text classification. Your training pipeline might look like this:
246+
247+
>>> import tensorflow as tf
248+
>>> from tensorflow import keras
249+
>>>
250+
>>> model.compile(optimizer='adam',
251+
... loss='binary_crossentropy',
252+
... metrics=['accuracy'])
253+
>>>
254+
>>> model.fit(X_train, y_train, epochs=30, batch_size=512)
255+
256+
You must next define a ``predict_proba`` function that adheres to the signature defined below.
257+
258+
**If your task type is text classification...**
259+
260+
>>> def predict_proba(model, text_list: List[str], **kwargs):
261+
... # Optional pre-processing of text_list
262+
... preds = model(text_list)
263+
... # Optional re-weighting of preds
264+
... return preds
265+
266+
The ``model`` arg must be the actual trained model object, and the ``text_list`` arg must be a list of
267+
strings.
268+
269+
**If your task type is tabular classification...**
270+
271+
>>> def predict_proba(model, input_features: np.ndarray, **kwargs):
272+
... # Optional pre-processing of input_features
273+
... preds = model(input_features)
274+
... # Optional re-weighting of preds
275+
... return preds
276+
277+
The ``model`` arg must be the actual trained model object, and the ``input_features`` arg must be a 2D numpy array
278+
containing a batch of features that will be passed to the model as inputs.
279+
280+
On both cases, you can optionally include other kwargs in the function, including tokenizers, variables, encoders etc.
281+
You simply pass those kwargs to the :meth:`unboxapi.UnboxClient.add_model` function call when you upload the model.
282+
283+
To upload the model to Unbox, first instantiate the client
284+
285+
>>> import unboxapi
286+
>>> client = unboxapi.UnboxClient('YOUR_API_KEY_HERE')
287+
288+
Now, you can use the ``client.add_model()`` method:
289+
290+
**If your task type is text classification...**
291+
292+
>>> model = client.add_model(
293+
... function=predict_proba,
294+
... model=model,
295+
... model_type=ModelType.tensorflow,
296+
... task_type=TaskType.TextClassification,
297+
... class_names=['Negative', 'Positive'],
298+
... name='My Tensorflow model',
299+
... description='this is my tensorflow model',
300+
... )
301+
>>> model.to_dict()
302+
303+
**If your task type is tabular classification...**
304+
305+
>>> model = client.add_model(
306+
... function=predict_proba,
307+
... model=model,
308+
... model_type=ModelType.tensorflow,
309+
... task_type=TaskType.TabularClassification,
310+
... class_names=['Exited', 'Retained'],
311+
... name='My Tensorflow model',
312+
... description='this is my tensorflow model',
313+
... )
314+
>>> model.to_dict()
315+
"""
239316
return "PytorchModelArtifact"
240317

241318
@property
242319
def tensorflow(self) -> str:
243-
"""For models built with `TensorFlow <https://www.tensorflow.org/>`_."""
320+
"""For models built with `TensorFlow <https://www.tensorflow.org/>`_.
321+
322+
Examples
323+
--------
324+
.. seealso::
325+
Our `sample notebooks <https://github.com/unboxai/unboxapi-python-client/tree/cid/api-docs-improvements/examples/text-classification/tensorflow>`_ and
326+
`tutorials <https://unbox.readme.io/docs/overview-of-tutorial-tracks>`_.
327+
328+
Let's say you have trained a ``tensorflow`` binary classifier. Your training pipeline might look like this:
329+
330+
>>> import tensorflow as tf
331+
>>> from tensorflow import keras
332+
>>>
333+
>>> model.compile(optimizer='adam',
334+
... loss='binary_crossentropy',
335+
... metrics=['accuracy'])
336+
>>>
337+
>>> model.fit(X_train, y_train, epochs=30, batch_size=512)
338+
339+
You must next define a ``predict_proba`` function that adheres to the signature defined below.
340+
341+
**If your task type is text classification...**
342+
343+
>>> def predict_proba(model, text_list: List[str], **kwargs):
344+
... # Optional pre-processing of text_list
345+
... preds = model(text_list)
346+
... # Optional re-weighting of preds
347+
... return preds
348+
349+
The ``model`` arg must be the actual trained model object, and the ``text_list`` arg must be a list of
350+
strings.
351+
352+
**If your task type is tabular classification...**
353+
354+
>>> def predict_proba(model, input_features: np.ndarray, **kwargs):
355+
... # Optional pre-processing of input_features
356+
... preds = model(input_features)
357+
... # Optional re-weighting of preds
358+
... return preds
359+
360+
The ``model`` arg must be the actual trained model object, and the ``input_features`` arg must be a 2D numpy array
361+
containing a batch of features that will be passed to the model as inputs.
362+
363+
On both cases, you can optionally include other kwargs in the function, including tokenizers, variables, encoders etc.
364+
You simply pass those kwargs to the :meth:`unboxapi.UnboxClient.add_model` function call when you upload the model.
365+
366+
To upload the model to Unbox, first instantiate the client
367+
368+
>>> import unboxapi
369+
>>> client = unboxapi.UnboxClient('YOUR_API_KEY_HERE')
370+
371+
Now, you can use the ``client.add_model()`` method:
372+
373+
**If your task type is text classification...**
374+
375+
>>> model = client.add_model(
376+
... function=predict_proba,
377+
... model=model,
378+
... model_type=ModelType.tensorflow,
379+
... task_type=TaskType.TextClassification,
380+
... class_names=['Negative', 'Positive'],
381+
... name='My Tensorflow model',
382+
... description='this is my tensorflow model',
383+
... )
384+
>>> model.to_dict()
385+
386+
**If your task type is tabular classification...**
387+
388+
>>> model = client.add_model(
389+
... function=predict_proba,
390+
... model=model,
391+
... model_type=ModelType.tensorflow,
392+
... task_type=TaskType.TabularClassification,
393+
... class_names=['Exited', 'Retained'],
394+
... name='My Tensorflow model',
395+
... description='this is my tensorflow model',
396+
... )
397+
>>> model.to_dict()
398+
"""
244399
return "TensorflowSavedModelArtifact"
245400

246401
@property
@@ -308,7 +463,6 @@ def transformers(self) -> str:
308463
... requirements_txt_file='./requirements.txt'
309464
... )
310465
>>> model.to_dict()
311-
312466
"""
313467
return "TransformersModelArtifact"
314468

@@ -386,7 +540,6 @@ def rasa(self) -> str:
386540
... description='this is my rasa model',
387541
... )
388542
>>> model.to_dict()
389-
390543
"""
391544
return "Rasa"
392545

0 commit comments

Comments
 (0)