keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
59 stars 28 forks source link

[Contributors wanted!] A model debugging utility #101

Open fchollet opened 1 year ago

fchollet commented 1 year ago

I'd like to see a utility like this:

>>> model = ...
>>> debug_forward_pass(model, input_data=x)

As well as:

>>> model = 
>>> model.compile(...)
>>> debug_train_step(model, input_data=(x, y))

The utility would do the following:

It might also wrap the __call__ method of every layer in the model to keep track of what was passed, the shape of input arguments, etc.

It might even give you code you can use to write a reusable unit test for your layer.

Who wants to implement this? Could just start from a simple Colab notebook implementation.

irvineAlgotrading commented 1 year ago
import tensorflow as tf
import openai
from functools import wraps

# Set your OpenAI API key
openai.api_key = "your_openai_api_key"

def debug_forward_pass(model, input_data):
    print_model_info(model)
    print_input_data_info(input_data)
    model.run_eagerly = True

    try:
        output = model(input_data)
    except Exception as e:
        print("Error during forward pass:", e)
        prompt_gpt3_for_debugging_tips(model, input_data, e)
    else:
        print("Forward pass successful.")
        return output

def debug_train_step(model, input_data):
    x, y = input_data
    print_model_info(model)
    print_input_data_info(x)
    model.run_eagerly = True

    try:
        with tf.GradientTape() as tape:
            predictions = model(x)
            loss = model.compiled_loss(y, predictions)
        gradients = tape.gradient(loss, model.trainable_variables)
        model.optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    except Exception as e:
        print("Error during training step:", e)
        prompt_gpt3_for_debugging_tips(model, x, e)
    else:
        print("Training step successful.")

def print_model_info(model):
    print(f"Model Summary:")
    model.summary()

def print_input_data_info(input_data):
    print(f"Input data shape: {input_data.shape}")
    print(f"Input data dtype: {input_data.dtype}")

def prompt_gpt3_for_debugging_tips(model, input_data, error):
    code = generate_code_from_model(model)
    prompt = (
        f"I encountered the following error while running a TensorFlow model:\n\n"
        f"Error: {error}\n\n"
        f"Here is the code for the model:\n\n{code}\n\n"
        f"Here is the input data shape and dtype: {input_data.shape}, {input_data.dtype}\n\n"
        f"Can you please help me debug this issue and provide any tips?"
    )

    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=200,
        n=1,
        stop=None,
        temperature=0.5,
    )

    message = response.choices[0].text.strip()
    print(f"GPT-3 debugging tips:\n{message}")

def generate_code_from_model(model):
    # This function should convert the model to a string representation of its code
    # You can either implement this yourself or use an existing library to generate the code
    pass
irvineAlgotrading commented 1 year ago

sorry for the broken formatting I'm mobile at the moment, this is a great starting point

radi-cho commented 1 year ago

@fchollet I made an overly simplistic starting point in Colab: https://colab.research.google.com/drive/11C9GZWE-NTPgPcl75irMZ19EUblzm5sH?usp=sharing.

Some takeaways:

Would love to hear some feedback. I can work on extending this further to a standalone Python library.

rajatmohan22 commented 1 year ago

I would love to work on this!

I could create a utility by wrapping layers with a custom function that tracks input/output shapes. Then, I'll use try/except blocks for error handling and use GPT-3 API for debugging tips. Here's a simplified code outline:

layer_wrapper(layer) modify_layers(model) try: model(input_data) # or model.fit(x, y) except Exception as e: get_gpt3_debugging_tips(e)

Could you assign it to me?

rajatmohan22 commented 1 year ago

Can i contribute? is this method ok?

I would love to work on this!

I could create a utility by wrapping layers with a custom function that tracks input/output shapes. Then, I'll use try/except blocks for error handling and use GPT-3 API for debugging tips. Here's a simplified code outline:

layer_wrapper(layer) modify_layers(model) try: model(input_data) # or model.fit(x, y) except Exception as e: get_gpt3_debugging_tips(e)

Could you assign it to me?