[Python] How do I print out the tensor values in between layers in Keras 3?

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Keras 3 with the PyTorch backend (I couldn't get the TF backend to work for some reason, and I've never used JAX).

    I'm trying to port a model written by someone else to another runtime, and I want to dump summary statistics about the tensor after each layer so I can figure out which operation I implemented incorrectly in my port (probably attention, lol).

    How do I insert print statements into a Keras 3 model?? All of the other answers I can find are related to tf.keras, which seems to be completely different from what I'm using. There is no method keras.backend.print_tensor() either.

    I also tried creating an intermediate model like this (for context, the model I am picking apart is Moonshine):

    encoder = model.encoder.encoder
    encoder_intermediate_model = Model(
    inputs=encoder.inputs, outputs=[layer.output for layer in encoder.layers]
    )


    But attempting to run this crashes with a vague error:

    Traceback (most recent call last):
    File "C:\Users\ibiyemi\projects\wellington-ml\moonshine.py", line 764, in <module>
    encoder_outputs = encoder_intermediate_model(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\ibiyemi\projects\wellington-ml\.venv\Lib\site-packages\keras\src\utils\traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
    File "C:\Users\ibiyemi\projects\wellington-ml\.venv\Lib\site-packages\torch\nn\modules\module.py", line 1736, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\ibiyemi\projects\wellington-ml\.venv\Lib\site-packages\torch\nn\modules\module.py", line 1747, in _call_impl
    return forward_call(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    KeyError: "Exception encountered when calling Functional.call().\n\n\x1b[1m2365371176512\x1b[0m\n\nArguments received by Functional.call():\n • inputs=['torch.Tensor(shape=torch.Size([1, 1248, 416]), dtype=float32)', 'torch.Tensor(shape=torch.Size([1]), dtype=int32)']\n • training=None\n • mask=['None', 'None']"

    Continue reading...

Compartilhe esta Página