Tensor Objects Are Not Iterable When Eager Execution Is Not Enabled. To Iterate Over This Tensor Use Tf.map_fn
I am trying to create my own loss function: def custom_mse(y_true, y_pred): tmp = 10000000000 a = list(itertools.permutations(y_pred)) for i in range(0, len(a)):
Solution 1:
The error is happening because y_pred
is a tensor (non iterable without eager execution), and itertools.permutations expects an iterable to create the permutations from. In addition, the part where you compute the minimum loss would not work either, because the values of tensor t
are unknown at graph creation time.
Instead of permuting the tensor, I would create permutations of the indices (this is something you can do at graph creation time), and then gather the permuted indices from the tensor. Assuming that your Keras backend is TensorFlow and that y_true
/y_pred
are 2-dimensional, your loss function could be implemented as follows:
defcustom_mse(y_true, y_pred):
batch_size, n_elems = y_pred.get_shape()
idxs = list(itertools.permutations(range(n_elems)))
permutations = tf.gather(y_pred, idxs, axis=-1) # Shape=(batch_size, n_permutations, n_elems)
mse = K.square(permutations - y_true[:, None, :]) # Shape=(batch_size, n_permutations, n_elems)
mean_mse = K.mean(mse, axis=-1) # Shape=(batch_size, n_permutations)
min_mse = K.min(mean_mse, axis=-1) # Shape=(batch_size,)return min_mse
Post a Comment for "Tensor Objects Are Not Iterable When Eager Execution Is Not Enabled. To Iterate Over This Tensor Use Tf.map_fn"