chanelcolgate / hydroelectric-project

0 stars 0 forks source link

Model Analysis and Validation #16

Open chanelcolgate opened 2 years ago

chanelcolgate commented 2 years ago

Description

Predicted 1 Predicted 0
True value 1 True positives False negatives
True value 0 False positives True negatives

import tensorflow_model_analysis as tfma import tensorflow as tf

eval_shared_model = tfma.default_eval_shared_model( eval_saved_model_path=_MODEL_DIR, tags=[tf.saved_model.SERVING] )

- Next, we provide an EvalConfig. In this step, we tell TFMA what our label is, provide any specifications for slicing the model by one of the features, and stipulate all the metrics we want TFMA to calculate and display:
```python
eval_config = tfma.EvalConfig(
    model_specs=[tfma.ModelSpec(label_key='consumer_disputed')],
    slicing_specs=[tfma.SlicingSpec()],
    metrics_specs=[
      tfma.MetricsSpec(metrics=[
        tfma.MetricConfig(class_name="BinaryAccuracy"),
        tfma.MetricConfig(class_name="ExampleCount"),
        tfma.MetricConfig(class_name="FalsePositives"),
        tfma.MetricConfig(class_name="TruePositives"),
        tfma.MetricConfig(class_name="FalseNegatives"),
        tfma.MetricConfig(class_name="TrueNegatives")
      ])
    ]
)

eval_result_2 = tfma.run_model_analysis( eval_shared_model=eval_shared_model_2, eval_config=eval_config, data_location=_EVAL_DATA_FILE, output_path=_EVAL_RESULT_LOCATION_2, file_format='tfrecords' )

- Then, we load them using the following code:
```python
eval_results_from_disk = tfma.load_eval_results(
    [_EVAL_RESULT_LOCATION, _EVAL_RESULT_LOCATION_2]
)

writer = tf.summary.create_file_writer('./fairness_indicator_logs') with writer.as_default(): summary_v2.FairnessIndicators('./eval_result_fairness', step=1) writer.close()

- And load the result in TensorBoard to a Jupyter Notebook:

%load_ext tensorboard %tensorboard --logdir=./fairness_indicator_logs

- Going Deeper with the What-If tool
- We can install the WIT with:

$ pip install witwidget

- Next, we create a TFRecordDataset to load the data file. We sample 1,000 training examples and convert it to a list of TFExamples. The visualizations in the What-If-Tool work well with this number of training examples, but they get harder to understand with a larger sample:
```python
eval_data = tf.data.TFRecordDataset(_EVAL_DATA_FILE, compression_type="GZIP")
subset = eval_data.take(1000)
eval_examples = [tf.train.Example.FromString(d.numpy()) for d in subset]

def predict(test_examples): test_examples = tf.constant([example.SerializeToString() for example in test_examples]) preds = predict_fn(examples=test_examples) return preds['outputs'].numpy()

- Then we configure the WIT using:
```python
from witwidget.notebook.visualization import WitConfigBuilder
config_builder = WitConfigBuilder(eval_examples).set_custom_predict_fn(predict)