Convert Figma logo to code with AI

tensorflow logomodel-analysis

Model analysis tools for TensorFlow

1,254
276
1,254
35

Top Related Projects

29,761

Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more

82,049

Tensors and Dynamic neural networks in Python with strong GPU acceleration

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

scikit-learn: machine learning in Python

18,287

Open source platform for the machine learning lifecycle

Quick Overview

TensorFlow Model Analysis (TFMA) is an open-source library for evaluating TensorFlow models. It allows users to perform in-depth model analysis, compute evaluation metrics over slices of data, and visualize model performance. TFMA is particularly useful for understanding model behavior across different subgroups and identifying potential biases.

Pros

  • Seamless integration with TensorFlow and TFX (TensorFlow Extended) ecosystem
  • Supports large-scale, distributed evaluation of models
  • Provides a wide range of customizable metrics and slicing options
  • Offers interactive visualization tools for easy interpretation of results

Cons

  • Steep learning curve for users not familiar with TensorFlow ecosystem
  • Limited documentation and examples for advanced use cases
  • Primarily focused on TensorFlow models, which may not be suitable for users of other frameworks
  • Can be resource-intensive for very large datasets or complex models

Code Examples

  1. Basic model evaluation:
import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(name='my_model')],
    metrics_specs=[tfma.MetricsSpec(metrics=[
        tfma.MetricConfig(class_name='BinaryAccuracy'),
        tfma.MetricConfig(class_name='AUC')
    ])]
)

eval_result = tfma.run_model_analysis(
    eval_shared_model=eval_shared_model,
    eval_config=eval_config,
    data_location=data_location,
    output_path=output_path
)
  1. Sliced evaluation:
slicing_specs = [
    tfma.SlicingSpec(feature_keys=['gender']),
    tfma.SlicingSpec(feature_keys=['age_group', 'gender'])
]

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(name='my_model')],
    slicing_specs=slicing_specs,
    metrics_specs=[tfma.MetricsSpec(metrics=[
        tfma.MetricConfig(class_name='BinaryAccuracy'),
        tfma.MetricConfig(class_name='AUC')
    ])]
)
  1. Visualizing results:
from tensorflow_model_analysis.addons.fairness.view import widget_view

widget_view.render_fairness_indicator(eval_result)

Getting Started

To get started with TensorFlow Model Analysis:

  1. Install TFMA:
pip install tensorflow-model-analysis
  1. Import the library and create an evaluation configuration:
import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(name='my_model')],
    metrics_specs=[tfma.MetricsSpec(metrics=[
        tfma.MetricConfig(class_name='BinaryAccuracy')
    ])]
)
  1. Run the analysis and view results:
eval_result = tfma.run_model_analysis(
    eval_shared_model=eval_shared_model,
    eval_config=eval_config,
    data_location=data_location,
    output_path=output_path
)

tfma.view.render_slicing_metrics(eval_result)

Competitor Comparisons

29,761

Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more

Pros of JAX

  • More flexible and composable, allowing for easier customization of models and algorithms
  • Better performance, especially for large-scale machine learning tasks and GPU acceleration
  • Supports automatic differentiation and vectorization, enabling efficient gradient computations

Cons of JAX

  • Steeper learning curve, especially for those familiar with TensorFlow ecosystem
  • Smaller ecosystem and fewer pre-built models compared to TensorFlow Model Analysis
  • Less focus on production-ready model analysis and evaluation tools

Code Comparison

JAX example:

import jax.numpy as jnp
from jax import grad, jit

def loss(params, x, y):
    return jnp.mean((params[0] * x + params[1] - y) ** 2)

grad_loss = jit(grad(loss))

TensorFlow Model Analysis example:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(name='my_model')],
    metrics_specs=[tfma.MetricsSpec(metrics=[tfma.MetricConfig(class_name='AUC')])]
)

JAX focuses on low-level numerical computing and automatic differentiation, while TensorFlow Model Analysis provides high-level tools for model evaluation and analysis in production environments.

82,049

Tensors and Dynamic neural networks in Python with strong GPU acceleration

Pros of PyTorch

  • More intuitive and Pythonic API, easier for beginners to learn
  • Dynamic computational graphs allow for more flexible model architectures
  • Stronger community support for research and experimentation

Cons of PyTorch

  • Slower deployment in production environments compared to TensorFlow
  • Fewer built-in tools for model analysis and evaluation
  • Less extensive support for mobile and embedded devices

Code Comparison

PyTorch:

import torch

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
z = torch.matmul(x, y)

TensorFlow Model Analysis:

import tensorflow as tf
import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(model_specs=[tfma.ModelSpec()])
eval_results = tfma.run_model_analysis(eval_config)

While PyTorch focuses on creating and training models with a more flexible approach, TensorFlow Model Analysis provides specialized tools for evaluating and analyzing model performance. PyTorch's code is more straightforward for basic operations, while TensorFlow Model Analysis offers more comprehensive evaluation capabilities out of the box.

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

Pros of ONNX Runtime

  • Supports multiple frameworks (TensorFlow, PyTorch, etc.) and hardware accelerators
  • Optimized for production deployment with focus on performance and efficiency
  • Provides a unified API for inference across different platforms and devices

Cons of ONNX Runtime

  • Primarily focused on inference, not model analysis or evaluation
  • May require additional tools for comprehensive model analysis and metrics
  • Less integrated with TensorFlow ecosystem for end-to-end ML workflows

Code Comparison

ONNX Runtime:

import onnxruntime as ort

session = ort.InferenceSession("model.onnx")
input_name = session.get_inputs()[0].name
output = session.run(None, {input_name: input_data})

TensorFlow Model Analysis:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(model_specs=[tfma.ModelSpec()])
eval_results = tfma.run_model_analysis(eval_config=eval_config,
                                       data_location=data_location,
                                       output_path=output_path)

Summary

ONNX Runtime excels in cross-framework inference and deployment optimization, while TensorFlow Model Analysis specializes in comprehensive model evaluation within the TensorFlow ecosystem. ONNX Runtime is better suited for production inference across various platforms, whereas TensorFlow Model Analysis offers more in-depth analysis and metrics for TensorFlow models.

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

Pros of Transformers

  • Broader support for various NLP tasks and models
  • More active community and frequent updates
  • Easier integration with PyTorch and TensorFlow

Cons of Transformers

  • Less focused on model analysis and evaluation
  • May require more setup for specific analysis tasks
  • Potentially steeper learning curve for beginners

Code Comparison

Transformers:

from transformers import AutoModelForSequenceClassification, AutoTokenizer

model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
inputs = tokenizer("Hello, world!", return_tensors="pt")
outputs = model(**inputs)

Model Analysis:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(name='my_model')],
    metrics_specs=[tfma.MetricsSpec(metrics=[tfma.MetricConfig(class_name='AUC')])]
)
eval_result = tfma.run_model_analysis(eval_config=eval_config, ...)

The Transformers library is more versatile for NLP tasks, while Model Analysis focuses on model evaluation and analysis. Transformers offers easier model loading and inference, whereas Model Analysis provides specialized tools for assessing model performance and fairness.

scikit-learn: machine learning in Python

Pros of scikit-learn

  • Broader range of machine learning algorithms and tools
  • Simpler API and easier to use for beginners
  • More extensive documentation and community support

Cons of scikit-learn

  • Less optimized for large-scale distributed computing
  • Limited support for deep learning models
  • Fewer built-in model evaluation and analysis tools

Code Comparison

scikit-learn:

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)

TensorFlow Model Analysis:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(model_specs=[tfma.ModelSpec(label_key='label')])
eval_result = tfma.run_model_analysis(eval_shared_model=eval_shared_model,
                                      eval_config=eval_config,
                                      data_location=data_location)

scikit-learn offers a more straightforward approach for basic machine learning tasks, while TensorFlow Model Analysis provides more advanced evaluation capabilities, especially for TensorFlow models and large-scale datasets.

18,287

Open source platform for the machine learning lifecycle

Pros of MLflow

  • More versatile, supporting multiple ML frameworks (not just TensorFlow)
  • Offers experiment tracking, model packaging, and deployment in one platform
  • Active community with frequent updates and contributions

Cons of MLflow

  • Less specialized for deep learning model analysis compared to TensorFlow Model Analysis
  • May require more setup and configuration for specific use cases
  • Potentially steeper learning curve for users familiar with TensorFlow ecosystem

Code Comparison

MLflow:

import mlflow

mlflow.start_run()
mlflow.log_param("param1", 5)
mlflow.log_metric("accuracy", 0.85)
mlflow.end_run()

TensorFlow Model Analysis:

import tensorflow_model_analysis as tfma

eval_config = tfma.EvalConfig(model_specs=[tfma.ModelSpec()])
eval_result = tfma.run_model_analysis(eval_config=eval_config)
tfma.view.render_slicing_metrics(eval_result)

MLflow provides a more general-purpose approach to experiment tracking and model management, while TensorFlow Model Analysis offers specialized tools for analyzing TensorFlow models. The choice between them depends on the specific requirements of your ML project and the frameworks you're using.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

TensorFlow Model Analysis

Python PyPI Documentation

TensorFlow Model Analysis (TFMA) is a library for evaluating TensorFlow models. It allows users to evaluate their models on large amounts of data in a distributed manner, using the same metrics defined in their trainer. These metrics can be computed over different slices of data and visualized in Jupyter notebooks.

TFMA Slicing Metrics Browser

Caution: TFMA may introduce backwards incompatible changes before version 1.0.

Installation

The recommended way to install TFMA is using the PyPI package:

pip install tensorflow-model-analysis

pip install from https://pypi-nightly.tensorflow.org

pip install -i https://pypi-nightly.tensorflow.org/simple tensorflow-model-analysis

pip install from the HEAD of the git:

pip install git+https://github.com/tensorflow/model-analysis.git#egg=tensorflow_model_analysis

pip install from a released version directly from git:

pip install git+https://github.com/tensorflow/model-analysis.git@v0.21.3#egg=tensorflow_model_analysis

If you have cloned the repository locally, and want to test your local change, pip install from a local folder.

pip install -e $FOLDER_OF_THE_LOCAL_LOCATION

Note that protobuf must be installed correctly for the above option since it is building TFMA from source and it requires protoc and all of its includes reference-able. Please see protobuf install instruction for see the latest install instructions.

Currently, TFMA requires that TensorFlow is installed but does not have an explicit dependency on the TensorFlow PyPI package. See the TensorFlow install guides for instructions.

Build TFMA from source

To build from source follow the following steps:

Install the protoc as per the link mentioned: protoc

Create a virtual environment by running the commands

python3 -m venv <virtualenv_name>
source <virtualenv_name>/bin/activate
pip3 install setuptools wheel
git clone https://github.com/tensorflow/model-analysis.git
cd model-analysis
python3 setup.py bdist_wheel

This will build the TFMA wheel in the dist directory. To install the wheel from dist directory run the commands

cd dist
pip3 install tensorflow_model_analysis-<version>-py3-none-any.whl

Jupyter Lab

As of writing, because of https://github.com/pypa/pip/issues/9187, pip install might never finish. In that case, you should revert pip to version 19 instead of 20: pip install "pip<20".

Using a JupyterLab extension requires installing dependencies on the command line. You can do this within the console in the JupyterLab UI or on the command line. This includes separately installing any pip package dependencies and JupyterLab labextension plugin dependencies, and the version numbers must be compatible. JupyterLab labextension packages refer to npm packages (eg, tensorflow_model_analysis.

The examples below use 0.32.0. Check available versions below to use the latest.

Jupyter Lab 3.0.x

pip install tensorflow_model_analysis==0.32.0
jupyter labextension install tensorflow_model_analysis@0.32.0
pip install jupyterlab_widgets==1.0.0

Jupyter Lab 2.2.x

pip install tensorflow_model_analysis==0.32.0
jupyter labextension install tensorflow_model_analysis@0.32.0
jupyter labextension install @jupyter-widgets/jupyterlab-manager@2

Jupyter Lab 1.2.x

pip install tensorflow_model_analysis==0.32.0
jupyter labextension install tensorflow_model_analysis@0.32.0
jupyter labextension install @jupyter-widgets/jupyterlab-manager@1.1

Classic Jupyter Notebook

To enable TFMA visualization in the classic Jupyter Notebook (either through jupyter notebook or through the JupyterLab UI), you'll also need to run:

jupyter nbextension enable --py widgetsnbextension
jupyter nbextension enable --py tensorflow_model_analysis

Note: If Jupyter notebook is already installed in your home directory, add --user to these commands. If Jupyter is installed as root, or using a virtual environment, the parameter --sys-prefix might be required.

Building TFMA from source

If you want to build TFMA from source and use the UI in JupyterLab, you'll need to make sure that the source contains valid version numbers. Check that the Python package version number and npm package version number are exactly the same, and that both valid version numbers (eg, remove the -dev suffix).

Troubleshooting

Check pip packages:

pip list

Check JupyterLab extensions:

jupyter labextension list  # for JupyterLab
jupyter nbextension list  # for classic Jupyter Notebook

Standalone HTML page with embed_minimal_html

TFMA notebook extension can be built into a standalone HTML file that also bundles data into the HTML file. See the Jupyter Widgets docs on embed_minimal_html.

Kubeflow Pipelines

Kubeflow Pipelines includes integrations that embed the TFMA notebook extension (code). This integration relies on network access at runtime to load a variant of the JavaScript build published on unpkg.com (see config and loader code).

Notable Dependencies

TensorFlow is required.

Apache Beam is required; it's the way that efficient distributed computation is supported. By default, Apache Beam runs in local mode but can also run in distributed mode using Google Cloud Dataflow and other Apache Beam runners.

Apache Arrow is also required. TFMA uses Arrow to represent data internally in order to make use of vectorized numpy functions.

Getting Started

For instructions on using TFMA, see the get started guide.

Compatible Versions

The following table is the TFMA package versions that are compatible with each other. This is determined by our testing framework, but other untested combinations may also work.

tensorflow-model-analysisapache-beam[gcp]pyarrowtensorflowtensorflow-metadatatfx-bsl
GitHub master2.47.010.0.0nightly (2.x)1.15.01.15.1
0.46.02.47.010.0.02.151.15.01.15.1
0.45.02.47.010.0.02.131.14.01.14.0
0.44.02.40.06.0.02.121.13.11.13.0
0.43.02.40.06.0.02.111.12.01.12.0
0.42.02.40.06.0.01.15.5 / 2.101.11.01.11.1
0.41.02.40.06.0.01.15.5 / 2.91.10.01.10.1
0.40.02.38.05.0.01.15.5 / 2.91.9.01.9.0
0.39.02.38.05.0.01.15.5 / 2.81.8.01.8.0
0.38.02.36.05.0.01.15.5 / 2.81.7.01.7.0
0.37.02.35.05.0.01.15.5 / 2.71.6.01.6.0
0.36.02.34.05.0.01.15.5 / 2.71.5.01.5.0
0.35.02.33.05.0.01.15 / 2.61.4.01.4.0
0.34.12.32.02.0.01.15 / 2.61.2.01.3.0
0.34.02.31.02.0.01.15 / 2.61.2.01.3.1
0.33.02.31.02.0.01.15 / 2.51.2.01.2.0
0.32.12.29.02.0.01.15 / 2.51.1.01.1.1
0.32.02.29.02.0.01.15 / 2.51.1.01.1.0
0.31.02.29.02.0.01.15 / 2.51.0.01.0.0
0.30.02.28.02.0.01.15 / 2.40.30.00.30.0
0.29.02.28.02.0.01.15 / 2.40.29.00.29.0
0.28.02.28.02.0.01.15 / 2.40.28.00.28.0
0.27.02.27.02.0.01.15 / 2.40.27.00.27.0
0.26.12.28.00.17.01.15 / 2.30.26.00.26.0
0.26.02.25.00.17.01.15 / 2.30.26.00.26.0
0.25.02.25.00.17.01.15 / 2.30.25.00.25.0
0.24.32.24.00.17.01.15 / 2.30.24.00.24.1
0.24.22.23.00.17.01.15 / 2.30.24.00.24.0
0.24.12.23.00.17.01.15 / 2.30.24.00.24.0
0.24.02.23.00.17.01.15 / 2.30.24.00.24.0
0.23.02.23.00.17.01.15 / 2.30.23.00.23.0
0.22.22.20.00.16.01.15 / 2.20.22.20.22.0
0.22.12.20.00.16.01.15 / 2.20.22.20.22.0
0.22.02.20.00.16.01.15 / 2.20.22.00.22.0
0.21.62.19.00.15.01.15 / 2.10.21.00.21.3
0.21.52.19.00.15.01.15 / 2.10.21.00.21.3
0.21.42.19.00.15.01.15 / 2.10.21.00.21.3
0.21.32.17.00.15.01.15 / 2.10.21.00.21.0
0.21.22.17.00.15.01.15 / 2.10.21.00.21.0
0.21.12.17.00.15.01.15 / 2.10.21.00.21.0
0.21.02.17.00.15.01.15 / 2.10.21.00.21.0
0.15.42.16.00.15.01.15 / 2.0n/a0.15.1
0.15.32.16.00.15.01.15 / 2.0n/a0.15.1
0.15.22.16.00.15.01.15 / 2.0n/a0.15.1
0.15.12.16.00.15.01.15 / 2.0n/a0.15.0
0.15.02.16.00.15.01.15n/an/a
0.14.02.14.0n/a1.14n/an/a
0.13.12.11.0n/a1.13n/an/a
0.13.02.11.0n/a1.13n/an/a
0.12.12.10.0n/a1.12n/an/a
0.12.02.10.0n/a1.12n/an/a
0.11.02.8.0n/a1.11n/an/a
0.9.22.6.0n/a1.9n/an/a
0.9.12.6.0n/a1.10n/an/a
0.9.02.5.0n/a1.9n/an/a
0.6.02.4.0n/a1.6n/an/a

Questions

Please direct any questions about working with TFMA to Stack Overflow using the tensorflow-model-analysis tag.