A Cost Function for Internal Representations