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:
def custom_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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…