Need to pass numpy array to function during forward computation in model.fit, but tf.Tensor has no `.numpy()` attribute

  eager-execution, keras, numpy, python, tensorflow

I have built a custom keras model and during its forward pass, it uses the output of a function from another library. However, the parameter to this function must be a numpy array. During model.compile() I can set the run_eagerly parameter to True, then I can convert the output from forward pass to numpy by using .numpy() method of EagerTensor, but this doesn’t seem computationally efficient since .numpy() is only needed once in my network. How can I convert the tensor to an eager tensor for just the one computation? Is this even possible? I have tried to use K.get_session() and use hidden_layer_outputs = sess.run(hidden_layer_outputs), but this raises an ‘Cannot get session inside TensorFlow graph function’ error. Below is an example illustrating my problem.

def third_party_library_fxn(np_arr):
    """Uses len to get the first dimension of the array.

    :param np_arr: <class 'numpy.ndarray'> 
    """

    # First part of function is to get the 1st dimension of the array
    first_dim = len(np_arr)
    
    # This function does more comptuations on n
    # ....
    # ....
    # return results

Example custom model and use of the function:

import tensorflow as tf
from tensorflow.python.framework.ops import EagerTensor

from third_party_library import third_party_library_fxn

class CustomModel(tf.keras.Model):
    def __init__(self, units, **kwargs):
        self.dense = tf.keras.layers.Dense(units=units)

    def call(self, inputs):
        hidden_layer_outputs = self.dense(inputs)

        # Raises Error: This block executes if the model is NOT running eagerly (i.e., during model.fit)
        if not(isinstance(hidden_layer_outputs, EagerTensor)):
            # You cannot calculate `len()` of `tf.Tensor`
            outputs_from_third_party_library = third_party_library_fxn(hidden_layer_outputs)

        # No Error: This block is executed if the model IS running eagerly
        else:
            outputs_from_third_party_library = third_party_library_fxn(hidden_layer_outputs.numpy())

Compilation and fitting that raises error:

model = CustomModel(units=arbitrary_number)

# Compilation is NOT eager by default
model.compile(loss=arbitrary_loss, optimizer=arbitrary_optimizer, run_eagerly=False) 

# Raises error
model.fit(arbitrary_tf_batch_data, epochs=arbitrary_epochs) 

Best,

Jared

Source: Python Questions

LEAVE A COMMENT