Eager execution inside lambda layer in Tensorflow

Viewed 28

I have Tensorflow 2.9.1 installed at my laptop, and according to the documentation the eager execution should be enabled by default. I have a problem while trying to convert Tensor object to Numpy Array inside a model. I keep getting 'Tensor' object has no attribute 'numpy'

I wanted to have some Lambda Layers inside my model and do some operations using Numpy, but the eager execution seems to be disabled inside the model. I tried to run tf.executing_eagerly() inside model and it returned false. On the otherhand, when I tried to run tf.executing_eagerly() outside the mode, I got true.

Could someone clear my confusion here?

import keras
import tensorflow as tf
from keras import layers, models
import numpy as np
import matplotlib.pyplot as plt

tf.config.run_functions_eagerly(True)

def do_something(input_tensor):
    a = add_one(input_tensor.numpy)
    b = minus_one(a)
    c = tf.convert_to_tensor(b, dtype=tf.float32)
    return c

def add_one(input):
    return input + 1.0

def minus_one(input):
    return input - 1.0


encoding_dim = 32

input_img = layers.Input(shape=(784,))
encoded = layers.Dense(encoding_dim, activation='relu')(input_img)

simulation_layer = layers.Lambda(do_something, name="channel_simulation")(encoded)

decoded = layers.Dense(784, activation='sigmoid')(encoded)

autoencoder = models.Model(input_img, decoded)

encoder = models.Model(input_img, encoded)

encoded_input = layers.Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = models.Model(encoded_input, decoder_layer(encoded_input))

autoencoder.compile(optimizer='adam', loss='binary_crossentropy', run_eagerly=True)
0 Answers
Related