Learning with Similarity Functions: a Tensor-Based Framework