Пользовательская функция парных расстояний в кератах - PullRequest
0 голосов
/ 12 марта 2020

Я пытаюсь запустить эту пользовательскую функцию потерь в Керасе, и я всегда сталкиваюсь с этой ошибкой ниже. Это параплановое ограничение потерь

def loss(y_true, y_pred):
    pw = pairwise_distances(y_true, squared=False)
    n, d = y_pred.get_shape()
    # generate constraint data points
    c1 = y_pred[pw[:, 0], :]
    c2 = y_pred[pw[:, 1], :]
    loss = np.zeros(dtype=np.float32, shape=(pw.shape[0], d * 2))
    loss[:, :d]  = np.abs(c1 - c2)
    loss[:, d:] = (c1 + c2) / 2
    return loss

Bellow is the error i get when i try to implement this loss function  


  File "C:\Users\Benji\Anaconda2\envs\ben\lib\site-packages\keras\engine\training.py", line 692, in _prepare_total_loss
    y_true, y_pred, sample_weight=sample_weight)
  File "C:\Users\Benji\Anaconda2\envs\ben\lib\site-packages\keras\losses.py", line 71, in __call__
    losses = self.call(y_true, y_pred)
  File "C:\Users\Benji\Anaconda2\envs\ben\lib\site-packages\keras\losses.py", line 132, in call
    return self.fn(y_true, y_pred, **self._fn_kwargs)
  File "C:/Users/Benji/PycharmProjects/Code/NEWWORK6.py", line 73, in loss
    c1 = y_pred[pw[:, 0], :]
  File "C:\Users\Benji\Anaconda2\envs\ben\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 766, in _slice_helper
    _check_index(s)
  File "C:\Users\Benji\Anaconda2\envs\ben\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 655, in _check_index
    raise TypeError(_SLICE_TYPE_ERROR + ", got {!r}".format(idx))
TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got <tf.Tensor 'loss/activation_6_loss/loss/strided_slice:0' shape=(?,) dtype=float32>

Process finished with exit code 1


...