Training thermodynamic computers by gradient descent