omoindrot / tensorflow-triplet-loss

Implementation of triplet loss in TensorFlow
https://omoindrot.github.io/triplet-loss
MIT License
1.12k stars 284 forks source link

Using batch_all_triplet_loss function #49

Closed aknakshay closed 4 years ago

aknakshay commented 4 years ago

batch_all_triplet_loss is giving this issue however hard loss is working fine.

AttributeError Traceback (most recent call last)

in 4 #return batch_hard_triplet_loss(y_true, y_pred,margin=0.1) 5 ----> 6 model.compile(optimizer='sgd', loss=func, metrics=["accuracy"]) 7 8 model.fit_generator( c:\users\akshay\anaconda3\envs\pythongpu\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs) 340 with K.name_scope(self.output_names[i] + '_loss'): 341 output_loss = weighted_loss(y_true, y_pred, --> 342 sample_weight, mask) 343 if len(self.outputs) > 1: 344 self.metrics_tensors.append(output_loss) c:\users\akshay\anaconda3\envs\pythongpu\lib\site-packages\keras\engine\training_utils.py in weighted(y_true, y_pred, weights, mask) 415 if weights is not None: 416 # reduce score_array to same ndim as weight array --> 417 ndim = K.ndim(score_array) 418 weight_ndim = K.ndim(weights) 419 score_array = K.mean(score_array, c:\users\akshay\anaconda3\envs\pythongpu\lib\site-packages\keras\backend\tensorflow_backend.py in ndim(x) 617 ``` 618 """ --> 619 dims = x.get_shape()._dims 620 if dims is not None: 621 return len(dims) AttributeError: 'tuple' object has no attribute 'get_shape'
aknakshay commented 4 years ago

Also, I have included metric as accuracy. I was wondering if you could explain how would keras calculate accuracy with embeddings and labels.

aknakshay commented 4 years ago

It was since the function was returning two outputs instead of one. Solved now.