Efficient Gradient Computation for Structured Output Learning with Rational and Tropical Losses